import type { DocumentHandle } from '../engine/DocumentHandle'; import type { Engine } from '../engine/Engine'; import type { PdfActionNode } from '../dto/PdfAction'; import { DocumentActionsSnapshotSchema } from '../dto/PdfAction.schema'; import { toPageRef } from '../identity/PageRef'; import type { ConformanceFixture, ConformanceTestRunner } from './runMetadataConformance'; import { EVERY_PERMISSION } from './fixtureScope'; /** The script payload rides only the javascript/rendition arms. */ function scriptOf(node: PdfActionNode | null | undefined): string { if (!node) return ''; if (node.type === 'javascript') return node.script; if (node.type === 'rendition') return node.script ?? ''; return ''; } export interface ActionsConformanceFixtures { document: ConformanceFixture; page: ConformanceFixture; annotation: ConformanceFixture; field: ConformanceFixture; /** `action_payloads.pdf` — every executable payload shape on one page of * /NM-keyed links. Required on both flavours: payload parity is a gate. */ payloads: ConformanceFixture; /** `open_action_dest.pdf` — a destination-form catalog `/OpenAction`. */ openDestination: ConformanceFixture; /** A page whose only annotation is a Link with a `/S /JavaScript` action. * Optional: the javascript-link classification test skips without it. */ javascriptLink?: ConformanceFixture; } export interface ActionsConformanceOptions { label: string; makeEngine: () => Promise | Engine; fixtures: ActionsConformanceFixtures; openKind: 'bytes' | 'id'; } /** Action extraction parity. These tests assert data only; no script executes. */ export function runActionsConformance( runner: ConformanceTestRunner, options: ActionsConformanceOptions, ): void { const { describe, test, beforeAll, afterAll, expect } = runner; let engine: Engine; describe(`actions conformance: ${options.label}`, () => { beforeAll(async () => { engine = await options.makeEngine(); }); afterAll(async () => { if (engine) await engine.destroy(); }); test('reads catalog additional actions and preserves /CO identity', async () => { const doc = await open(engine, options, options.fixtures.document); try { expect(Boolean(doc.actions)).toBe(true); const snapshot = await doc.actions!.get(); expect(DocumentActionsSnapshotSchema.safeParse(snapshot).success).toBe(true); expect(snapshot.openAction).toBeNull(); expect(snapshot.willSave?.root?.type).toBe('javascript'); expect(scriptOf(snapshot.willSave?.root)).toMatch(/Will Save/); expect(scriptOf(snapshot.didSave?.root)).toMatch(/Did Save/); expect(scriptOf(snapshot.willPrint?.root)).toMatch(/Will Print/); expect(scriptOf(snapshot.didPrint?.root)).toMatch(/Did Print/); const form = await doc.forms.list(); expect(form.calculationOrder).toEqual([{ kind: 'objectNumber', objectNumber: 9 }]); } finally { await doc.close(); } }); test('attaches page /AA to PageLayout', async () => { const doc = await open(engine, options, options.fixtures.page); try { const pages = await doc.pages.list(); expect(pages.pages[0].actions?.open?.root?.type).toBe('goto-embedded'); expect(pages.pages[0].actions?.close).toBe(undefined); expect(pages.pages[1].actions?.open?.root?.type).toBe('goto-embedded'); expect(pages.pages[1].actions?.close?.root?.type).toBe('goto-embedded'); } finally { await doc.close(); } }); test('attaches activation actions to AnnotationBase, including links', async () => { const doc = await open(engine, options, options.fixtures.annotation); try { const firstPage = (await doc.pages.list()).pages[0]; const snapshot = await doc.page(firstPage.ref).annotations.list(); // The button is a widget: its row comes with the form. const button = (await doc.forms.list()).widgets.find( (widget) => widget.ref.kind === 'objectNumber' && widget.ref.objectNumber === 7, ); const link = snapshot.annotations.find( (annotation) => annotation.ref.kind === 'objectNumber' && annotation.ref.objectNumber === 8, ); expect(button?.actions?.activate?.root?.type).toBe('uri'); // A link carries both planes: the base-level scripting action model // (chain shape, for the orchestrator) and its own normalized target // (payload, for navigation). They must agree on the action type. expect(link?.subtype).toBe('link'); expect(link?.actions?.activate?.root?.type).toBe('uri'); if (link?.subtype === 'link') { expect(link.target?.kind).toBe('uri'); } } finally { await doc.close(); } }); test('classifies a /S /JavaScript link target, script staying on base.actions', async () => { const fixture = options.fixtures.javascriptLink; if (!fixture) return; // fixture not provided for this engine flavour const doc = await open(engine, options, fixture); try { const firstPage = (await doc.pages.list()).pages[0]; const snapshot = await doc.page(firstPage.ref).annotations.list(); const link = snapshot.annotations.find((annotation) => annotation.subtype === 'link'); expect(link?.subtype).toBe('link'); if (link?.subtype === 'link') { // The navigation vocabulary classifies (FPDFAction_GetType alone // cannot — it has no JavaScript code)… expect(link.target).toEqual({ kind: 'javascript' }); } // …while the script text lives only on the scripting plane's model. expect(link?.actions?.activate?.root?.type).toBe('javascript'); expect(scriptOf(link?.actions?.activate?.root)).toMatch(/app\.alert/); } finally { await doc.close(); } }); test('keeps field actions separate from merged widget events', async () => { const doc = await open(engine, options, options.fixtures.field); try { const form = await doc.forms.list(); expect(form.fields).toHaveLength(1); expect(form.fields[0].actions?.format?.root?.type).toBe('javascript'); expect(scriptOf(form.fields[0].actions?.format?.root)).toMatch(/AFDate_FormatEx/); expect(form.widgets).toHaveLength(1); expect(form.widgets[0]!.actions).toBe(null); } finally { await doc.close(); } }); test('carries interpreter payloads on every executable node', async () => { const doc = await open(engine, options, options.fixtures.payloads); try { const page = (await doc.pages.list()).pages[0]; const snapshot = await doc.page(page.ref).annotations.list(); const pageObjectNumber = page.ref.objectNumber; const rootOf = (nm: string) => { const annotation = snapshot.annotations.find((candidate) => candidate.nm === nm); expect(Boolean(annotation)).toBe(true); return annotation?.actions?.activate?.root ?? null; }; const targetOf = (nm: string) => { const annotation = snapshot.annotations.find((candidate) => candidate.nm === nm); return annotation?.subtype === 'link' ? annotation.target : null; }; // The file's /FitR 10 20 300 400, from the page's top-left. const visible = page.pdfCropBox; expect(rootOf('goto-fitr')).toMatchObject({ type: 'goto', destination: { kind: 'fitR', page: toPageRef(pageObjectNumber), x: 10 - visible.left, y: visible.top - 400, width: 290, height: 380, }, }); // Dual planes agree by construction: the target is the tree's projection. expect(targetOf('goto-fitr')).toMatchObject({ kind: 'goto', destination: { kind: 'fitR' }, }); expect(rootOf('uri-map')).toMatchObject({ type: 'uri', uri: 'https://example.test/map', isMap: true, }); expect(rootOf('named-next')).toMatchObject({ type: 'named', name: 'NextPage' }); const mixed = rootOf('hide-mixed'); expect(mixed).toMatchObject({ type: 'hide', hide: false }); const mixedTargets = mixed?.type === 'hide' ? mixed.targets : []; expect(mixedTargets).toHaveLength(2); expect(mixedTargets[0]).toEqual({ kind: 'name', name: 'note1' }); expect(mixedTargets[1]?.kind === 'objectNumber' && mixedTargets[1].objectNumber > 0).toBe( true, ); expect(rootOf('hide-scalar')).toMatchObject({ type: 'hide', targets: [{ kind: 'name', name: 'fieldB' }], hide: true, }); // ResetForm's three states survive the wire distinctly. expect(rootOf('reset-include')).toMatchObject({ type: 'reset-form', fields: [{ kind: 'name', name: 'calc1' }], exclude: true, }); expect(rootOf('reset-absent')).toMatchObject({ type: 'reset-form', fields: null, exclude: true, }); expect(rootOf('reset-empty')).toMatchObject({ type: 'reset-form', fields: [], exclude: false, }); // SubmitForm's atomic payload. /UF beats /F in a // conforming << /FS /URL >> spec; a bare-string /F is the // producer-compat extension; bit 9 dominates format with GetMethod // kept alive (ISO 32000-2 Table 240). expect(rootOf('submit-urlspec')).toMatchObject({ type: 'submit-form', payload: { url: 'https://uf.example.test/submit', fields: [{ kind: 'name', name: 'parent' }], exclude: false, format: 'fdf', method: 'post', }, }); expect(rootOf('submit-compat-xfdf')).toMatchObject({ type: 'submit-form', payload: { url: 'https://example.test/xfdf', fields: [ { kind: 'name', name: 'noexport' }, { kind: 'name', name: 'plain' }, ], exclude: true, includeNoValueFields: true, format: 'xfdf', method: 'post', charSet: 'utf-8', }, }); expect(rootOf('submit-pdf-get')).toMatchObject({ type: 'submit-form', payload: { url: 'https://example.test/pdf', fields: null, format: 'pdf', method: 'get', }, }); expect(rootOf('launch-app')).toMatchObject({ type: 'launch', filePath: 'app.exe' }); expect(rootOf('gotor-file')).toMatchObject({ type: 'goto-remote', filePath: 'other.pdf' }); // A mixed /Next chain carries every payload in PDF order. const chain = rootOf('chain-js-goto-hide'); expect(chain).toMatchObject({ type: 'javascript', script: "app.alert('chain');" }); // The file's /XYZ 5 10 1.25, from the page's top-left. expect(chain?.next[0]).toMatchObject({ type: 'goto', destination: { kind: 'xyz', page: toPageRef(pageObjectNumber), x: 5 - visible.left, y: visible.top - 10, zoom: 1.25, }, }); expect(chain?.next[0]?.next[0]).toMatchObject({ type: 'hide', targets: [{ kind: 'name', name: 'note1' }], hide: true, }); } finally { await doc.close(); } }); test('degrades unreadable payloads to unknown with payload-dropped', async () => { const doc = await open(engine, options, options.fixtures.payloads); try { const page = (await doc.pages.list()).pages[0]; const snapshot = await doc.page(page.ref).annotations.list(); for (const [nm, subtype] of [ ['goto-malformed', 'GoTo'], ['hide-partial', 'Hide'], // a partial target list must never half-execute // The atomic-payload law: a submit whose required /F is not a URL // (or is absent) degrades whole — never a half payload. ['submit-not-url', 'SubmitForm'], ['submit-no-f', 'SubmitForm'], ] as const) { const annotation = snapshot.annotations.find((candidate) => candidate.nm === nm); const tree = annotation?.actions?.activate; expect(tree?.root).toMatchObject({ type: 'unknown', subtype }); expect(tree?.warnings ?? []).toContain('payload-dropped'); // /A precedence, pinned: a broken action is a dead link — never a // silent /Dest fallback. if (annotation?.subtype === 'link') { expect(annotation.target).toEqual({ kind: 'unsupported' }); } } } finally { await doc.close(); } }); test('reads a destination-form /OpenAction as openDestination', async () => { const doc = await open(engine, options, options.fixtures.openDestination); try { expect(Boolean(doc.actions)).toBe(true); const snapshot = await doc.actions!.get(); expect(DocumentActionsSnapshotSchema.safeParse(snapshot).success).toBe(true); expect(snapshot.openAction).toBeNull(); const pageObjectNumber = (await doc.pages.list()).pages[0].ref.objectNumber; // The file's [/XYZ 10 700 1.5] on a letter page, from the page's top-left. expect(snapshot.openDestination).toEqual({ kind: 'xyz', page: toPageRef(pageObjectNumber), x: 10, y: 92, zoom: 1.5, }); } finally { await doc.close(); } }); }); } async function open( engine: Engine, opts: ActionsConformanceOptions, fixture: ConformanceFixture, ): Promise { if (opts.openKind === 'bytes') { return engine.open( { kind: 'bytes', id: fixture.id, bytes: await fixture.bytes() }, EVERY_PERMISSION, ); } return engine.open({ kind: 'id', id: fixture.cloudId ?? fixture.id }); }