import { expect, test } from 'vitest'; import { connectedDirtyForms, markPreparationDirty, usesGenericPreparationTracking, } from '@/lib/preparation-forms'; function form(connected = true): HTMLFormElement { const attributes = new Map(); return { isConnected: connected, setAttribute: (key: string, value: string) => attributes.set(key, value), getAttribute: (key: string) => attributes.get(key) ?? null, } as unknown as HTMLFormElement; } function root(forms: HTMLFormElement[]): Pick { return { querySelectorAll: () => forms.filter( (node) => node.getAttribute('data-preparation-dirty') === 'true', ), } as unknown as Pick; } test('consultation filters remain outside unsaved business preparations after deferred navigation while other edits stay protected', async () => { const query = form(); query.setAttribute('data-query-form', 'true'); markPreparationDirty(query, true); const tracked = new Set([query]); await Promise.resolve(); expect(connectedDirtyForms(tracked, root([query]))).toEqual([]); const business = form(); markPreparationDirty(business, true); tracked.add(business); expect(connectedDirtyForms(tracked, root([query, business]))).toEqual([ business, ]); markPreparationDirty(business, false); expect(connectedDirtyForms(tracked, root([query, business]))).toEqual([]); }); test('generic input tracking cannot re-dirty a managed form after its own state clears during event propagation', () => { const managed = form(); const legacy = form(); expect(usesGenericPreparationTracking(legacy)).toBe(true); markPreparationDirty(managed, true, true); expect(connectedDirtyForms([managed], root([managed]))).toEqual([managed]); markPreparationDirty(managed, false, true); if (usesGenericPreparationTracking(managed)) markPreparationDirty(managed, true); expect(connectedDirtyForms([managed], root([managed]))).toEqual([]); if (usesGenericPreparationTracking(legacy)) markPreparationDirty(legacy, true); expect( connectedDirtyForms([managed, legacy], root([managed, legacy])), ).toEqual([legacy]); const query = form(); query.setAttribute('data-query-form', 'true'); expect(usesGenericPreparationTracking(query)).toBe(false); }); test('a restored dirty preparation is protected even before any input event or listener registration', () => { const restored = form(); markPreparationDirty(restored, true); expect(connectedDirtyForms([], root([restored]))).toEqual([restored]); expect(connectedDirtyForms(new Set([restored]), root([restored]))).toEqual([ restored, ]); }); test('cleaned unmounted and explicitly discarded preparations do not leave false guards', () => { const cleaned = form(); const unmounted = form(false); const legacy = form(); markPreparationDirty(cleaned, false); markPreparationDirty(unmounted, true); expect( connectedDirtyForms( [cleaned, unmounted, legacy], root([cleaned, unmounted]), ), ).toEqual([legacy]); markPreparationDirty(legacy, false); expect( connectedDirtyForms( [cleaned, unmounted, legacy], root([cleaned, unmounted, legacy]), ), ).toEqual([]); }); test('one submitted form does not remove another dirty preparation from the departure check', () => { const submitted = form(); const other = form(); markPreparationDirty(submitted, true); markPreparationDirty(other, true); expect( connectedDirtyForms([], root([submitted, other])).some( (node) => node !== submitted, ), ).toBe(true); markPreparationDirty(other, false); expect( connectedDirtyForms([], root([submitted, other])).some( (node) => node !== submitted, ), ).toBe(false); });