import { describe, expect, test } from 'vitest'; import type { WidgetAnnotation, WidgetPatch } from '../../src/annotation/kinds/widget'; import { actionReadOf, actionWriteOf, decodeSubmitFormFlags, widgetActionsAfter, widgetActionsOf, type PdfActionWrite, type PdfAnnotationActions, } from '../../src/dto/PdfAction'; import { PdfActionTreeSchema, PdfAnnotationActionsSchema } from '../../src/dto/PdfAction.schema'; import { actionTreeOf } from '../../src/dto/PdfLinkTarget'; import { scriptTreeOf } from '../../src/forms/format'; import { formFieldOfDraft } from '../../src/forms/predict'; import { IMPORT_ICON_SCRIPT, takesImageOf } from '../../src/forms/takesImage'; import { widgetAfterPatch } from '../../src/forms/widgets'; import { annotationOfDraft } from '../../src/pageSpace/helpers'; const PAGE = { kind: 'objectNumber', objectNumber: 3 } as const; const script = (source: string): PdfActionWrite => ({ type: 'javascript', script: source }); const NEXT_PAGE: PdfActionWrite = { type: 'named', name: 'NextPage' }; const RESET: PdfActionWrite = { type: 'reset-form', fields: null, exclude: false }; const SPOT = { kind: 'xyz', page: PAGE, x: 10, y: 20, zoom: null } as const; const NO_FLAGS = decodeSubmitFormFlags(0); /** One write of every type a write makes, with the root a read gives for it. */ const WRITES: readonly { write: PdfActionWrite; root: Record }[] = [ { write: script('run();'), root: { type: 'javascript', subtype: 'JavaScript', script: 'run();', next: [] }, }, { write: { type: 'goto', destination: SPOT }, root: { type: 'goto', subtype: 'GoTo', destination: SPOT, next: [] }, }, { write: { type: 'uri', uri: 'https://example.com/terms' }, root: { type: 'uri', subtype: 'URI', uri: 'https://example.com/terms', isMap: false, next: [], }, }, { write: NEXT_PAGE, root: { type: 'named', subtype: 'Named', name: 'NextPage', next: [] } }, { write: { type: 'hide', targets: [ { kind: 'name', name: 'other' }, { kind: 'objectNumber', objectNumber: 42 }, ], hide: false, }, root: { type: 'hide', subtype: 'Hide', targets: [ { kind: 'name', name: 'other' }, { kind: 'objectNumber', objectNumber: 42 }, ], hide: false, next: [], }, }, { write: { type: 'reset-form', fields: [{ kind: 'name', name: 'kept' }], exclude: true }, root: { type: 'reset-form', subtype: 'ResetForm', fields: [{ kind: 'name', name: 'kept' }], exclude: true, next: [], }, }, { write: { type: 'submit-form', url: 'https://example.com/in', fields: null }, root: { type: 'submit-form', subtype: 'SubmitForm', payload: { ...NO_FLAGS, url: 'https://example.com/in', fields: null }, next: [], }, }, ]; describe('the read of a written action', () => { test('every type a write makes, with the subtype the engine writes', () => { for (const { write, root } of WRITES) { expect(actionReadOf(write)).toEqual({ root, incomplete: false, warnings: [] }); } expect(WRITES.map(({ write }) => write.type)).toEqual([ 'javascript', 'goto', 'uri', 'named', 'hide', 'reset-form', 'submit-form', ]); }); test('is a tree a read can be: it passes the schema a read is checked with', () => { for (const { write } of WRITES) { const tree = actionReadOf(write); expect(PdfActionTreeSchema.parse(tree)).toEqual(tree); } }); test("a submit's payload holds every flag, the ones left out as their defaults", () => { const partial = actionReadOf({ type: 'submit-form', url: 'https://example.com/in', fields: [{ kind: 'name', name: 'secret' }], exclude: true, format: 'xfdf', includeNoValueFields: true, }); expect(partial.root).toMatchObject({ payload: { ...NO_FLAGS, url: 'https://example.com/in', fields: [{ kind: 'name', name: 'secret' }], exclude: true, format: 'xfdf', includeNoValueFields: true, }, }); // A flag only some formats have reads as the file's bits say: `get` goes with HTML and PDF. const fdfByGet = actionReadOf({ type: 'submit-form', url: 'https://example.com/in', fields: null, method: 'get', }); expect(fdfByGet.root).toMatchObject({ payload: { format: 'fdf', method: 'post' } }); const htmlByGet = actionReadOf({ type: 'submit-form', url: 'https://example.com/in', fields: null, format: 'html', method: 'get', }); expect(htmlByGet.root).toMatchObject({ payload: { format: 'html', method: 'get' } }); }); test('what runs after an action is read after it, in order and to any depth', () => { const tree = actionReadOf({ ...script('first();'), next: [NEXT_PAGE, { ...script('third();'), next: [RESET] }], }); expect(tree.root).toEqual({ type: 'javascript', subtype: 'JavaScript', script: 'first();', next: [ { type: 'named', subtype: 'Named', name: 'NextPage', next: [] }, { type: 'javascript', subtype: 'JavaScript', script: 'third();', next: [ { type: 'reset-form', subtype: 'ResetForm', fields: null, exclude: false, next: [] }, ], }, ], }); // An empty list of next actions is none. expect(actionReadOf({ ...NEXT_PAGE, next: [] })).toEqual(actionReadOf(NEXT_PAGE)); }); test('is the inverse of the write of a read', () => { for (const { write } of WRITES) { const back = actionWriteOf(actionReadOf(write)); // A submit comes back with every flag stated. expect(back).toMatchObject(write); expect(actionReadOf(back!)).toEqual(actionReadOf(write)); } }); test("a link's target and a format's script are read by the same rule", () => { expect(actionTreeOf({ kind: 'uri', uri: 'https://example.com/terms' })).toEqual( actionReadOf({ type: 'uri', uri: 'https://example.com/terms' }), ); expect(actionTreeOf({ kind: 'goto', destination: SPOT })).toEqual( actionReadOf({ type: 'goto', destination: SPOT }), ); expect(actionTreeOf({ kind: 'named', name: 'NextPage' })).toEqual(actionReadOf(NEXT_PAGE)); expect(scriptTreeOf('run();')).toEqual(actionReadOf(script('run();'))); }); }); describe("a widget's actions after a patch", () => { const READ: PdfAnnotationActions = { activate: actionReadOf(NEXT_PAGE), focus: actionReadOf(script('focused();')), }; test('a write sets its event, and an event left out keeps what it has', () => { const after = widgetActionsAfter(READ, { focus: RESET, blur: script('left();') }); expect(after).toEqual({ activate: READ.activate, focus: actionReadOf(RESET), blur: actionReadOf(script('left();')), }); // The event it left alone keeps its very tree. expect(after!.activate).toBe(READ.activate); expect(widgetActionsAfter(null, { activate: NEXT_PAGE })).toEqual({ activate: READ.activate }); expect(widgetActionsAfter(undefined, { activate: NEXT_PAGE })).toEqual({ activate: READ.activate, }); }); test('null removes an event, and a null patch removes them all', () => { expect(widgetActionsAfter(READ, { activate: null })).toEqual({ focus: READ.focus }); expect(widgetActionsAfter(READ, { activate: null, focus: null })).toBe(null); expect(widgetActionsAfter(READ, null)).toBe(null); expect(widgetActionsAfter(null, null)).toBe(null); }); test('no event left reads as a widget without actions', () => { expect(widgetActionsAfter(null, {})).toBe(null); expect(widgetActionsAfter({}, {})).toBe(null); expect(widgetActionsAfter(null, { blur: null })).toBe(null); }); test('a patch that changes nothing gives the very actions it had', () => { expect(widgetActionsAfter(READ, {})).toBe(READ); expect(widgetActionsAfter(READ, { blur: null })).toBe(READ); }); test("a read's own tree is never written: its event keeps what the widget has", () => { // The whole read sent back. expect(widgetActionsAfter(READ, READ)).toBe(READ); // A tree beside a write: the write is read, the tree changes nothing. const other = actionReadOf(script('other();')); // The types refuse a patch of both: untyped code can still send one. const mixed = { activate: other, blur: RESET } as never; expect(widgetActionsAfter(READ, mixed)).toEqual({ ...READ, blur: actionReadOf(RESET), }); expect(widgetActionsAfter(null, { activate: other })).toBe(null); }); test('what it gives is what a read is checked with, and what a patch writes back', () => { const after = widgetActionsAfter(READ, { mouseUp: { type: 'goto', destination: SPOT } })!; expect(PdfAnnotationActionsSchema.parse(after)).toEqual(after); expect(widgetActionsOf(after)).toMatchObject({ activate: NEXT_PAGE, focus: script('focused();'), mouseUp: { type: 'goto', destination: SPOT }, blur: null, }); }); }); describe('a predicted widget holds its actions as a read does', () => { /** A push button's widget row as a read gives it, with `actions`. */ const row = (actions: PdfAnnotationActions | null): WidgetAnnotation => ({ ...(annotationOfDraft( { subtype: 'widget', box: { x: 10, y: 10, width: 90, height: 30 } } as never, { ref: { kind: 'objectNumber', page: PAGE, objectNumber: 21 } }, ) as object), field: { kind: 'objectNumber', objectNumber: 20 }, fieldKind: 'pushbutton', caption: null, layout: 'image', actions, }) as unknown as WidgetAnnotation; const imports = actionReadOf(script(IMPORT_ICON_SCRIPT)); test('a widget patch: written events read, the rest kept, null and all removed', () => { const before = row({ activate: imports, focus: actionReadOf(script('focused();')) }); const written = widgetAfterPatch(before, { actions: { blur: NEXT_PAGE } }); expect(written.actions).toEqual({ ...before.actions, blur: actionReadOf(NEXT_PAGE) }); expect(takesImageOf([written.actions])).toBe(true); const pressed = widgetAfterPatch(before, { actions: { activate: NEXT_PAGE } }); expect(pressed.actions).toEqual({ ...before.actions, activate: actionReadOf(NEXT_PAGE) }); expect(takesImageOf([pressed.actions])).toBe(false); const emptied = widgetAfterPatch(before, { actions: { activate: null } }); expect(emptied.actions).toEqual({ focus: before.actions!.focus }); expect(widgetAfterPatch(before, { actions: null }).actions).toBe(null); expect(widgetAfterPatch(row(null), { actions: { activate: null } }).actions).toBe(null); }); test('a button given the import script by hand reads as an image field', () => { const made = widgetAfterPatch(row(null), { actions: { activate: script(IMPORT_ICON_SCRIPT) } }); expect(made.actions).toEqual({ activate: imports }); expect(takesImageOf([made.actions])).toBe(true); }); test('a read sent back whole, and a patch without actions, keep the very actions', () => { const before = row({ activate: imports }); expect(widgetAfterPatch(before, { actions: before.actions } as WidgetPatch).actions).toBe( before.actions, ); expect(widgetAfterPatch(before, { interiorColor: '#eeeeee' }).actions).toBe(before.actions); }); test("a created field's widgets, and the fact a push button's rows give it", () => { const context = { objectNumber: 20, widgets: [ { kind: 'objectNumber', page: PAGE, objectNumber: 21 }, { kind: 'objectNumber', page: PAGE, objectNumber: 22 }, ], } as const; const rect = { x: 10, y: 10, width: 90, height: 30 }; const made = formFieldOfDraft( { kind: 'pushbutton', name: 'photo', widgets: [ { page: PAGE, rect, actions: { activate: script(IMPORT_ICON_SCRIPT), focus: NEXT_PAGE } }, { page: PAGE, rect, actions: { mouseUp: script(IMPORT_ICON_SCRIPT) } }, ], }, context, ); expect(made.widgets.map((widget) => widget.actions)).toEqual([ { activate: imports, focus: actionReadOf(NEXT_PAGE) }, { mouseUp: imports }, ]); // No `takesImage` in the draft: the press scripts make it one, as a read says. expect(made.field).toMatchObject({ kind: 'pushbutton', takesImage: true }); const plain = formFieldOfDraft( { kind: 'pushbutton', name: 'go', widgets: [ { page: PAGE, rect, actions: { activate: NEXT_PAGE } }, { page: PAGE, rect }, ], }, context, ); expect(plain.widgets.map((widget) => widget.actions)).toEqual([ { activate: actionReadOf(NEXT_PAGE) }, null, ]); expect(plain.field).toMatchObject({ takesImage: false }); }); });