import { describe, expect, it } from 'vitest'; import { DEFAULT_VIEW_TAGS, type ViewNode } from '@moxxy/sdk'; import { parseView, validateDoc } from './parse.js'; const parse = (src: string) => parseView(src, DEFAULT_VIEW_TAGS); function find(node: ViewNode, tag: string): Extract | null { if (node.kind === 'element') { if (node.tag === tag) return node; for (const c of node.children) { const hit = find(c, tag); if (hit) return hit; } } return null; } function findAll(node: ViewNode, tag: string): Array> { const out: Array> = []; const walk = (n: ViewNode) => { if (n.kind !== 'element') return; if (n.tag === tag) out.push(n); n.children.forEach(walk); }; walk(node); return out; } describe('parseView — valid', () => { it('parses a form, extracts the title and the form action + fields', () => { const res = parse(`
`); expect(res.ok).toBe(true); if (!res.ok) return; expect(res.doc.title).toBe('Find flights'); const form = find(res.doc.root, 'form')!; expect(form.action?.name).toBe('search_flights'); expect(form.action?.fields).toEqual(['from', 'to', 'depart', 'cabin']); // submit is a recognized attribute, coerced onto props expect(form.props.submit).toBe('Search'); }); it('expands results into a card stack with open buttons', () => { const res = parse(` `); expect(res.ok).toBe(true); if (!res.ok) return; // No component tags survive expansion. expect(find(res.doc.root, 'results')).toBeNull(); expect(find(res.doc.root, 'result')).toBeNull(); const cards = findAll(res.doc.root, 'card'); expect(cards).toHaveLength(2); const buttons = findAll(res.doc.root, 'button'); // No explicit action/to → defaults to open:. expect(buttons.map((b) => b.action?.name)).toEqual(['open:UA42', 'open:B6-11']); }); it('coerces number, boolean and enum attributes', () => { const res = parse(``); expect(res.ok).toBe(true); if (!res.ok) return; const grid = find(res.doc.root, 'grid')!; expect(grid.props.cols).toBe(3); expect(grid.props.gap).toBe('lg'); const input = find(res.doc.root, 'input')!; expect(input.props.required).toBe(true); }); it('u50-2: decodes HTML entities in attribute string values (consistent with text)', () => { const res = parse(`Tom & Jerry <3`); expect(res.ok).toBe(true); if (!res.ok) return; const img = find(res.doc.root, 'image')!; expect(img.props.alt).toBe('Tom & Jerry <3'); }); it('u50-2: entity-decoded href is still scheme-checked on the decoded value', () => { // A plain disallowed scheme stays rejected (unchanged). expect(parse(`x`).ok).toBe(false); // An href whose entities decode to a still-safe value is accepted, with // the decoded form on props. const res = parse(`x`); expect(res.ok).toBe(true); if (!res.ok) return; expect(find(res.doc.root, 'link')!.props.href).toBe('https://x.test/?a=1&b=2'); }); it('carries explicit button fields from a csv', () => { const res = parse(`