import type { PredictedOp } from '@embedpdf/core'; import { formWidget, toPageRef, type FormField, type FormSnapshot, type WidgetAnnotation, } from '@embedpdf/engine-core/runtime'; import { describe, expect, test } from 'vitest'; import { fieldByRef, indexFields, widgetRowOf, type FieldIndex } from '../src/model'; import { fieldsNamedBy, predictFormOp, undoLookOf, withFieldsFrom } from '../src/sync/predict'; const PAGE = toPageRef(3); const base = { origin: 'acroform', readOnly: false, required: false, noExport: false, alternateName: null, mappingName: null, groupId: null, assignment: { kind: 'unassigned' }, lockedBy: null, purpose: null, placeholder: null, createdBy: null, createdAt: null, filledBy: null, filledByName: null, filledAt: null, importedBy: null, } as const; const name: FormField = { ...base, ref: { kind: 'objectNumber', objectNumber: 4 }, name: 'name', kind: 'text', valueEntry: { kind: 'scalar', value: 'abc' }, defaultValueEntry: { kind: 'scalar', value: 'start' }, widgets: [formWidget(5, PAGE)], value: 'abc', defaultValue: 'start', maxLength: 5, multiline: false, password: false, comb: false, format: null, }; const toggle = (objectNumber: number, onState: string) => ({ ...formWidget(objectNumber, PAGE), onState, exportValue: onState, checked: false, }); /** One checkbox shown twice: both widgets check together. */ const agree: FormField = { ...base, ref: { kind: 'objectNumber', objectNumber: 10 }, name: 'agree', kind: 'checkbox', valueEntry: { kind: 'scalar', value: 'Off' }, defaultValueEntry: { kind: 'none' }, checked: false, exportValue: 'Yes', hasValuesList: false, widgets: [toggle(11, 'Yes'), toggle(12, 'Yes')], }; const size: FormField = { ...base, ref: { kind: 'objectNumber', objectNumber: 20 }, name: 'size', kind: 'radio', valueEntry: { kind: 'scalar', value: 'Off' }, defaultValueEntry: { kind: 'scalar', value: 'L' }, value: 'Off', radiosInUnison: false, noToggleToOff: false, hasValuesList: false, widgets: [toggle(21, 'S'), toggle(22, 'L')], }; const send: FormField = { ...base, ref: { kind: 'objectNumber', objectNumber: 30 }, name: 'send', kind: 'pushbutton', hasImage: false, takesImage: false, valueEntry: { kind: 'none' }, defaultValueEntry: { kind: 'none' }, widgets: [formWidget(31, PAGE)], }; const row = (objectNumber: number, over: Partial = {}): WidgetAnnotation => ({ subtype: 'widget', ref: { kind: 'objectNumber', page: PAGE, objectNumber }, page: PAGE, rect: { x: objectNumber, y: 0, width: 10, height: 10 }, box: { x: objectNumber, y: 0, width: 10, height: 10 }, rotation: null, fieldKind: 'text', hasAppearance: true, appearanceState: null, hidden: false, invisible: false, noView: false, print: true, ...over, }) as unknown as WidgetAnnotation; const form = (): FieldIndex => indexFields({ formKind: 'acroform', needsAppearances: false, fields: [name, agree, size, send], widgets: [ row(5, { hasAppearance: false }), row(11, { appearanceState: 'Off' }), row(12, { appearanceState: 'Off' }), row(21, { appearanceState: 'Off' }), row(22, { appearanceState: 'Off' }), row(31), ], calculationOrder: [], } as unknown as FormSnapshot); const field = (index: FieldIndex, objectNumber: number) => fieldByRef(index, { kind: 'objectNumber', objectNumber }) as FormField & Record; describe('predictFormOp', () => { test('a value shows on its field and draws its widgets', () => { const next = predictFormOp(form(), { type: 'forms.setValue', field: { kind: 'fqn', name: 'name' }, value: { value: 'hello world' }, }); // Cut to the field's maximum length, as the engine does. expect(field(next, 4).value).toBe('hello'); expect(widgetRowOf(next, 5)?.hasAppearance).toBe(true); }); test('the same op over the same form gives the same form, every time the view replays it', () => { const index = form(); const op = { type: 'forms.setValue', field: name.ref, value: { value: 'x' } } as const; const once = predictFormOp(index, op); expect(predictFormOp(index, op)).toBe(once); expect(fieldByRef(predictFormOp(index, op), name.ref)).toBe(fieldByRef(once, name.ref)); }); test('a checkbox checks every widget it shows in', () => { const next = predictFormOp(form(), { type: 'forms.setValue', field: agree.ref, value: { checked: true }, }); expect(field(next, 10).checked).toBe(true); expect(widgetRowOf(next, 11)?.appearanceState).toBe('Yes'); expect(widgetRowOf(next, 12)?.appearanceState).toBe('Yes'); }); test('a reset puts back the fields it names, or every field that holds a value', () => { const filled = predictFormOp( predictFormOp(form(), { type: 'forms.setValue', field: agree.ref, value: { checked: true } }), { type: 'forms.setValue', field: name.ref, value: { value: 'x' } }, ); const named = predictFormOp(filled, { type: 'forms.reset', fields: [name.ref] }); expect(field(named, 4).value).toBe('start'); expect(field(named, 10).checked).toBe(true); const all = predictFormOp(filled, { type: 'forms.reset' }); expect(field(all, 4).value).toBe('start'); expect(field(all, 10).checked).toBe(false); expect(field(all, 20).value).toBe('L'); expect(widgetRowOf(all, 22)?.appearanceState).toBe('L'); }); test('what the engine would refuse shows nothing', () => { const index = form(); // A checkbox doesn't take text it has no widget for, and a push button has no default. expect( predictFormOp(index, { type: 'forms.setValue', field: agree.ref, value: { value: 'No' } }), ).toBe(index); expect(predictFormOp(index, { type: 'forms.reset', fields: [name.ref, send.ref] })).toBe(index); expect( predictFormOp(index, { type: 'forms.setValue', field: { kind: 'fqn', name: 'missing' }, value: { value: 'x' }, }), ).toBe(index); }); test('display shows on the widget flags; other planes are left alone', () => { const index = form(); const hidden = predictFormOp(index, { type: 'forms.setDisplay', field: agree.ref, display: 'hidden', }); expect(widgetRowOf(hidden, 11)?.hidden).toBe(true); expect(widgetRowOf(hidden, 12)?.hidden).toBe(true); expect(predictFormOp(index, { type: 'metadata.update', patch: {} } as never)).toBe(index); }); test('an undo look puts the field and its widgets back as they were', () => { const before = form(); const ops = [ { type: 'forms.setValue', field: agree.ref, value: { checked: true } }, { type: 'forms.setValue', field: name.ref, value: { value: 'x' } }, { type: 'forms.setDisplay', field: agree.ref, display: 'hidden' }, ] as const; const after = ops.reduce(predictFormOp, before); const undo = undoLookOf(before, ops); // One restore per op, the last op's first. expect(undo.map((op) => op.type === 'forms.restore' && op.field.name)).toEqual([ 'agree', 'name', 'agree', ]); const undone = undo.reduce(predictFormOp, after); expect(field(undone, 10).checked).toBe(false); expect(field(undone, 4).value).toBe('abc'); expect(widgetRowOf(undone, 11)).toEqual(widgetRowOf(before, 11)); expect(widgetRowOf(undone, 5)).toEqual(widgetRowOf(before, 5)); }); test('a restored widget comes back with its field', () => { const index = form(); const gone = indexFields({ ...index.snapshot!, widgets: index.snapshot!.widgets.filter((candidate) => candidate !== widgetRowOf(index, 12)), }); const back = predictFormOp(gone, { type: 'forms.restoreWidget', widget: widgetRowOf(index, 12)!, field: agree, }); expect(widgetRowOf(back, 12)).toEqual(widgetRowOf(index, 12)); }); }); describe('the fields a fill names', () => { test('a whole-form reset names every field that holds a value', () => { expect(fieldsNamedBy(form(), [{ type: 'forms.reset' }]).map((each) => each.name)).toEqual([ 'name', 'agree', 'size', ]); }); test('withFieldsFrom takes fields and their widgets from another form', () => { const before = form(); const after = predictFormOp(before, { type: 'forms.setValue', field: agree.ref, value: { checked: true }, }); const back = withFieldsFrom(after, before, [agree]); expect(field(back, 10).checked).toBe(false); expect(widgetRowOf(back, 11)?.appearanceState).toBe('Off'); }); }); /** * The fields and widget rows of a form, keyed, with its calculation order and the widgets' * paint order. A restore puts a row back at the end, as the engine's restore event does, so * undo looks compare the content alone (`withoutOrder`). */ const contentOf = (index: FieldIndex) => ({ fields: Object.fromEntries( (index.snapshot?.fields ?? []).map((each) => [JSON.stringify(each.ref), each]), ), widgets: Object.fromEntries( (index.snapshot?.widgets ?? []).map((each) => [JSON.stringify(each.ref), each]), ), widgetOrder: (index.snapshot?.widgets ?? []).map((each) => JSON.stringify(each.ref)), calculationOrder: index.snapshot?.calculationOrder ?? [], }); const withoutOrder = ({ widgetOrder: _order, ...content }: ReturnType) => content; describe('design ops', () => { const widgetRef = (objectNumber: number) => ({ kind: 'objectNumber', page: PAGE, objectNumber }) as const; test('a create shows the field and its widgets under the numbers it took', () => { const next = predictFormOp(form(), { type: 'forms.create', draft: { kind: 'text', name: 'total', widgets: [{ page: PAGE, rect: { x: 0, y: 40, width: 80, height: 20 } }], actions: { calculate: { type: 'javascript', script: 'event.value = 1;' } }, }, objectNumber: 90, widgetObjectNumbers: [91], }); expect(field(next, 90)).toMatchObject({ name: 'total', kind: 'text', value: '' }); expect(widgetRowOf(next, 91)?.rect).toMatchObject({ x: 0, y: 40, width: 80, height: 20 }); expect(next.snapshot?.calculationOrder).toEqual([{ kind: 'objectNumber', objectNumber: 90 }]); // Without its numbers, it shows once the engine answers. const unnamed = form(); expect( predictFormOp(unnamed, { type: 'forms.create', draft: { kind: 'text', name: 'later' } }), ).toBe(unnamed); }); test('a field shows in the group its draft names, and in none when it names none', () => { const op = { type: 'forms.create' as const, draft: { kind: 'text' as const, name: 'clause', widgets: [{ page: PAGE, rect: { x: 0, y: 40, width: 80, height: 20 } }], }, objectNumber: 90, widgetObjectNumbers: [91], }; // Where the engine puts it: nobody's, whatever the session's own group is. expect(field(predictFormOp(form(), op), 90).groupId).toBeNull(); const none = { ...op, draft: { ...op.draft, groupId: null } }; expect(field(predictFormOp(form(), none), 90).groupId).toBeNull(); const named = { ...op, draft: { ...op.draft, groupId: 'buyer' } }; expect(field(predictFormOp(form(), named), 90).groupId).toBe('buyer'); }); test('a field taken out of its group shows in none', () => { const created = predictFormOp(form(), { type: 'forms.create', draft: { kind: 'text', name: 'clause', groupId: 'buyer', widgets: [{ page: PAGE, rect: { x: 0, y: 40, width: 80, height: 20 } }], }, objectNumber: 90, widgetObjectNumbers: [91], }); const removed = predictFormOp(created, { type: 'forms.update', field: { kind: 'objectNumber', objectNumber: 90 }, patch: { groupId: null }, }); expect(field(removed, 90).groupId).toBeNull(); }); test('a widget moves, and its field keeps its value', () => { const next = predictFormOp(form(), { type: 'forms.updateWidget', widget: widgetRef(5), patch: { rect: { x: 100, y: 100, width: 10, height: 10 } } as never, }); expect(widgetRowOf(next, 5)?.rect).toMatchObject({ x: 100, y: 100 }); expect(field(next, 4).value).toBe('abc'); }); test('deleting one of two widgets leaves the field with the other; deleting the field takes both', () => { const one = predictFormOp(form(), { type: 'forms.deleteWidget', widget: widgetRef(11) }); expect(widgetRowOf(one, 11)).toBeNull(); expect(field(one, 10).widgets.map((widget) => widget.objectNumber)).toEqual([12]); const all = predictFormOp(form(), { type: 'forms.delete', field: agree.ref }); expect(field(all, 10)).toBeNull(); expect(widgetRowOf(all, 11)).toBeNull(); expect(widgetRowOf(all, 12)).toBeNull(); }); test('a setting and the widget order change as the engine changes them', () => { const renamed = predictFormOp(form(), { type: 'forms.update', field: name.ref, patch: { name: 'full_name', required: true }, }); expect(field(renamed, 4)).toMatchObject({ name: 'full_name', required: true }); const raised = predictFormOp(form(), { type: 'forms.reorderWidgets', page: PAGE, widgets: [widgetRef(5)], position: 'end', }); expect(raised.snapshot?.widgets.map((each) => widgetRowOf(raised, 5) === each).at(-1)).toBe( true, ); }); test.each([ [ 'create', { type: 'forms.create', draft: { kind: 'checkbox', name: 'new', widgets: [{ page: PAGE, rect: { x: 0, y: 0, width: 10, height: 10 } }], }, objectNumber: 90, widgetObjectNumbers: [91], }, ], [ 'update', { type: 'forms.update', field: name.ref, patch: { name: 'full_name', maxLength: null } }, ], ['delete', { type: 'forms.delete', field: agree.ref }], [ 'updateWidget', { type: 'forms.updateWidget', widget: widgetRef(21), patch: { rect: { x: 50, y: 50, width: 10, height: 10 } }, }, ], ['deleteWidget', { type: 'forms.deleteWidget', widget: widgetRef(12) }], [ 'reorderWidgets', { type: 'forms.reorderWidgets', page: PAGE, widgets: [widgetRef(5), widgetRef(31)], position: 'end', }, ], ] as [string, PredictedOp][])('undoing %s shows the form as it was', (_name, op) => { const before = form(); const after = predictFormOp(before, op); expect(contentOf(after)).not.toEqual(contentOf(before)); const undone = undoLookOf(before, [op]).reduce(predictFormOp, after); expect(withoutOrder(contentOf(undone))).toEqual(withoutOrder(contentOf(before))); if (op.type === 'forms.reorderWidgets') { expect(contentOf(undone).widgetOrder).toEqual(contentOf(before).widgetOrder); } }); }); describe('whether a push button takes an image, before the engine answers', () => { const widgetRef = (objectNumber: number) => ({ kind: 'objectNumber', page: PAGE, objectNumber }) as const; /** Acrobat's image field press, as a read gives it and as a write names it. */ const IMPORTS = { root: { type: 'javascript', subtype: 'JavaScript', script: 'event.target.buttonImportIcon();', next: [], }, incomplete: false, warnings: [], }; const IMPORT_WRITE = { type: 'javascript', script: 'event.target.buttonImportIcon();' } as const; const PRINTS = { type: 'named', name: 'Print' } as const; const button = (objectNumber: number, name: string, takesImage: boolean, boxes: number[]) => ({ ...send, ref: { kind: 'objectNumber', objectNumber }, name, takesImage, widgets: boxes.map((box) => formWidget(box, PAGE)), }) as FormField; const box = (objectNumber: number, actions: object | null) => row(objectNumber, { fieldKind: 'pushbutton', layout: 'image', actions } as never); /** An image field with two boxes, a button with the script on one box of two, and a logo. */ const buttons = (): FieldIndex => indexFields({ formKind: 'acroform', needsAppearances: false, fields: [ button(40, 'initials', true, [41, 42]), button(50, 'mixed', false, [51, 52]), button(60, 'logo', false, [61]), ], widgets: [ box(41, { activate: IMPORTS }), box(42, { mouseUp: IMPORTS, focus: IMPORTS }), box(51, { activate: IMPORTS }), box(52, null), box(61, null), ], calculationOrder: [], } as unknown as FormSnapshot); const takesImage = (index: FieldIndex, objectNumber: number) => (field(index, objectNumber) as { takesImage?: boolean }).takesImage; const patched = (index: FieldIndex, objectNumber: number, actions: unknown) => predictFormOp(index, { type: 'forms.updateWidget', widget: widgetRef(objectNumber), patch: { actions } as never, }); test('a box whose press goes, or does something else, makes the button a button', () => { expect(takesImage(patched(buttons(), 41, { activate: null }), 40)).toBe(false); expect(takesImage(patched(buttons(), 42, { mouseUp: PRINTS }), 40)).toBe(false); // Every action of the box taken away. expect(takesImage(patched(buttons(), 41, null), 40)).toBe(false); // The script on both events of a press is not the script alone. expect(takesImage(patched(buttons(), 41, { mouseUp: IMPORT_WRITE }), 40)).toBe(false); // The script with something after it. expect( takesImage(patched(buttons(), 41, { activate: { ...IMPORT_WRITE, next: [PRINTS] } }), 40), ).toBe(false); }); test('the script on the box that lacked it makes the button an image field', () => { const next = patched(buttons(), 52, { activate: IMPORT_WRITE }); expect(takesImage(next, 50)).toBe(true); // The box states its press as a read gives it: one script, nothing after it. expect(widgetRowOf(next, 52)?.actions?.activate).toMatchObject({ root: { type: 'javascript', script: IMPORT_WRITE.script, next: [] }, incomplete: false, }); // It moved from one press event to the other: still the script alone. expect(takesImage(patched(buttons(), 41, { activate: null, mouseUp: IMPORT_WRITE }), 40)).toBe( true, ); }); test('a write that names no press leaves the button what it was', () => { // An event that is no press: the box keeps its press. const hovered = patched(buttons(), 41, { cursorEnter: PRINTS }); expect(takesImage(hovered, 40)).toBe(true); expect(widgetRowOf(hovered, 41)?.actions?.activate).toEqual(IMPORTS); // The actions a read gave, sent back as they were. expect(takesImage(patched(buttons(), 42, { mouseUp: IMPORTS, focus: IMPORTS }), 40)).toBe(true); // A move: the field is the same object, so nothing that reads it runs again. const before = buttons(); const moved = predictFormOp(before, { type: 'forms.updateWidget', widget: widgetRef(41), patch: { rect: { x: 50, y: 50, width: 10, height: 10 } }, }); expect(field(moved, 40)).toBe(field(before, 40)); expect(takesImage(moved, 40)).toBe(true); }); test('a box that goes changes it, and undoing shows the button as it was', () => { const before = buttons(); const op: PredictedOp = { type: 'forms.deleteWidget', widget: widgetRef(52) }; const after = predictFormOp(before, op); expect(takesImage(after, 50)).toBe(true); const undone = undoLookOf(before, [op]).reduce(predictFormOp, after); expect(takesImage(undone, 50)).toBe(false); // A press taken away, and put back by the undo. const pressGone: PredictedOp = { type: 'forms.updateWidget', widget: widgetRef(41), patch: { actions: { activate: null } }, }; const button = predictFormOp(before, pressGone); expect(takesImage(button, 40)).toBe(false); expect(takesImage(undoLookOf(before, [pressGone]).reduce(predictFormOp, button), 40)).toBe( true, ); }); test('a new box of an image field gets its press; one with a press of its own makes it a button', () => { const placement = { page: PAGE, rect: { x: 0, y: 40, width: 30, height: 10 } }; const added = predictFormOp(buttons(), { type: 'forms.addWidget', field: { kind: 'objectNumber', objectNumber: 40 }, placement, objectNumber: 43, }); expect(field(added, 40).widgets.map((widget) => widget.objectNumber)).toEqual([41, 42, 43]); expect(takesImage(added, 40)).toBe(true); expect(widgetRowOf(added, 43)?.actions?.activate).toMatchObject({ root: { type: 'javascript', script: IMPORT_WRITE.script, next: [] }, }); const own = predictFormOp(buttons(), { type: 'forms.addWidget', field: { kind: 'objectNumber', objectNumber: 40 }, placement: { ...placement, actions: { activate: PRINTS } }, objectNumber: 43, }); expect(takesImage(own, 40)).toBe(false); // A button's new box gets no script, and the button stays one. const plain = predictFormOp(buttons(), { type: 'forms.addWidget', field: { kind: 'objectNumber', objectNumber: 60 }, placement, objectNumber: 62, }); expect(takesImage(plain, 60)).toBe(false); expect(widgetRowOf(plain, 62)?.actions ?? null).toBeNull(); }); test('a created button is an image field by the fact, or by the press its boxes are placed with', () => { const rect = { x: 0, y: 40, width: 30, height: 10 }; const created = (draft: object, numbers: number[]) => predictFormOp(buttons(), { type: 'forms.create', draft: { kind: 'pushbutton', name: 'new', ...draft } as never, objectNumber: 90, widgetObjectNumbers: numbers, }); expect( takesImage(created({ takesImage: true, widgets: [{ page: PAGE, rect }] }, [91]), 90), ).toBe(true); // As a copy of an image field carries it: the press on every box, and no fact. const press = { actions: { activate: IMPORT_WRITE } }; expect( takesImage( created( { widgets: [ { page: PAGE, rect, ...press }, { page: PAGE, rect, ...press }, ], }, [91, 92], ), 90, ), ).toBe(true); expect( takesImage( created( { widgets: [ { page: PAGE, rect, ...press }, { page: PAGE, rect }, ], }, [91, 92], ), 90, ), ).toBe(false); expect(takesImage(created({ widgets: [{ page: PAGE, rect }] }, [91]), 90)).toBe(false); }); test('a copy of an image field is an image field at once, and a copy of a button a button', () => { const copied = (objectNumber: number, numbers: number[]) => predictFormOp(buttons(), { type: 'forms.copy', fields: [{ kind: 'objectNumber', objectNumber }], offset: { x: 20, y: 20 }, objectNumbers: numbers, }); const image = copied(40, [90, 91, 92]); expect(field(image, 90)).toMatchObject({ kind: 'pushbutton', name: 'initials_2' }); expect(takesImage(image, 90)).toBe(true); expect(takesImage(copied(50, [90, 91, 92]), 90)).toBe(false); expect(takesImage(copied(60, [90, 91]), 90)).toBe(false); }); });