import { describe, expect, it } from 'vitest'; import { DEFAULT_VIEW_TAGS, type ViewNode } from '@moxxy/sdk'; import { countNodes, parseView } from './parse.js'; const parse = (src: string) => parseView(src, DEFAULT_VIEW_TAGS); const ok = (src: string) => { const r = parse(src); if (!r.ok) throw new Error(`expected ok, got errors: ${r.errors.map((e) => e.message).join('; ')}`); return r.doc; }; const errs = (src: string): string[] => { const r = parse(src); if (r.ok) throw new Error('expected errors'); return r.errors.map((e) => e.message); }; const hasErr = (src: string, re: RegExp) => expect(errs(src).some((m) => re.test(m))).toBe(true); 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; } function firstText(node: ViewNode): string | null { if (node.kind === 'text') return node.value; for (const c of node.children) { const t = firstText(c); if (t != null) return t; } return null; } describe('attribute coercion', () => { it('coerces booleans: bare, ="true", ="false"', () => { const doc = ok(''); const inputs = findAll(doc.root, 'input'); expect(inputs[0]!.props.required).toBe(true); expect(inputs[1]!.props.required).toBe(true); expect(inputs[2]!.props.required).toBe(false); }); it('coerces numbers including negatives and floats are range-checked', () => { expect(find(ok('').root, 'grid')!.props.cols).toBe(4); hasErr('', /must be ≥ 1/); hasErr('', /must be a number/); hasErr('x', /must be ≤ 3/); }); it('rejects JS numeric-literal syntax (hex/binary/octal/exponent) — only plain decimals', () => { // `Number()` would silently turn these into surprising values: `0x4`→4 (a // human reads hex 16), `0b100`→4, `1e1`→10. The agent must get a clear error // instead of a guessed value that doesn't match what it typed. hasErr('', /must be a number/); hasErr('', /must be a number/); hasErr('', /must be a number/); hasErr('', /must be a number/); hasErr('', /must be a number/); // Plain decimals (incl. whitespace-padded, signed, fractional) still coerce. expect(find(ok('').root, 'grid')!.props.cols).toBe(3); expect(find(ok('').root, 'image')!.props.w).toBe(1.5); expect(find(ok('').root, 'image')!.props.w).toBe(-2); }); it('validates each enum attribute', () => { ok(''); ok(''); ok('x'); ok('