// @vitest-environment node import { describe, expect, it, vi } from 'vitest'; import { checkFieldValue, coerceValue, computeFormLogic, createFormController, createMemoryDrafts, createWebStorageDrafts, FORM_FORMATS, FORM_LIMITS, type FormFormat, type FormTimers, formatMessage, matchesFormat, submittedValues, validateFormSchema, validateFormValues, } from '../src/model/index.ts'; import { CONTACT, compiled } from './fixtures.ts'; const field = (name: string, extra: Record = {}) => ({ name, type: 'text', label: name, ...extra, }); const schemaWith = (...fields: unknown[]) => ({ title: 'T', fields }); /** Fake timers: run() fires everything scheduled. */ function fakeTimers(): FormTimers & { run(): void; size(): number } { const pending = new Map void>(); let next = 0; return { set(callback) { next += 1; pending.set(next, callback); return next; }, clear(handle) { pending.delete(handle as number); }, run() { const callbacks = [...pending.values()]; pending.clear(); for (const callback of callbacks) callback(); }, size: () => pending.size, }; } const settle = () => new Promise((resolve) => setTimeout(resolve, 0)); describe('the schema validator', () => { it('compiles a multi-step form with sections, conditions and rules', () => { const form = compiled(); expect(form.multiStep).toBe(true); expect(form.steps.map((s) => s.id)).toEqual(['about', 'attendance', 'extras']); expect(form.sections.map((s) => s.id)).toEqual(['when', 'what']); expect(form.fields).toHaveLength(17); expect(form.byName.get('guardian')?.show?.fields).toEqual(['age']); expect(form.rules[0]?.test.fields).toEqual(['arrival', 'departure']); expect(Object.isFrozen(form.schema)).toBe(true); const contact = compiled(CONTACT); expect(contact.multiStep).toBe(false); expect(contact.steps).toHaveLength(1); expect(contact.steps[0]?.title).toBe('Contact us'); }); it('refuses each invalid schema with its code and path', () => { const cases: [unknown, string, string][] = [ [[], 'shape', '$'], [{ title: 'T' }, 'layout', '$'], [{ title: 'T', fields: [], sections: [] }, 'layout', '$'], [{ title: '', fields: [field('a')] }, 'missing', '$.title'], [{ ...schemaWith(field('a')), script: 'x' }, 'unknown-member', '$.script'], [schemaWith(field('a', { pattern: '^(a+)+$' })), 'unknown-member', '$.fields[0].pattern'], [schemaWith(field('a', { format: '^\\d+$' })), 'type', '$.fields[0].format'], [schemaWith(field('a', { type: 'html' })), 'type', '$.fields[0].type'], // A credential type is known, but agent output may not ask for one (G117). [schemaWith(field('a', { type: 'password' })), 'credential', '$.fields[0].type'], [schemaWith(field('A b')), 'name', '$.fields[0].name'], [schemaWith(field('a'), field('a')), 'duplicate', '$.fields[1].name'], [schemaWith(field('a', { min: 3 })), 'not-applicable', '$.fields[0].min'], [schemaWith(field('a', { type: 'number', min: 5, max: 1 })), 'range', '$.fields[0].min'], [schemaWith(field('a', { type: 'select' })), 'shape', '$.fields[0].options'], [ schemaWith( field('a', { type: 'radio', options: [ { value: 'x', label: 'X' }, { value: 'x', label: 'Y' }, ], }) ), 'duplicate', '$.fields[0].options[1].value', ], [ schemaWith( field('a', { type: 'select', options: [{ value: 'x', label: 'X' }], default: 'y' }) ), 'default', '$.fields[0].default', ], [ schemaWith(field('a', { type: 'date', default: '2026-02-30' })), 'default', '$.fields[0].default', ], [schemaWith(field('a', { label: 'bad\u0000label' })), 'text', '$.fields[0].label'], [ schemaWith(field('a', { link: { label: 'Help', href: 'javascript:alert(1)' } })), 'url', '$.fields[0].link.href', ], [ schemaWith(field('a', { link: { label: 'Help', href: 'http://example.com' } })), 'url', '$.fields[0].link.href', ], [ schemaWith(field('a', { type: 'file', accept: ['