import { readFile } from 'node:fs/promises'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import { createKernel, type Kernel } from '@embedpdf/core'; import { createLocalEngine } from '@embedpdf/engine'; import { toPageRef, type AnnotationRef, type Engine, type PdfActionTree, } from '@embedpdf/engine-core/runtime'; import { actionsPlugin } from '../src/actions.plugin'; import { ActionsToken } from '../src/host-contract'; import type { ActionsHostCapability } from '../src/host-contract'; const here = dirname(fileURLToPath(import.meta.url)); const fixturePath = resolve( here, '..', '..', '..', 'engine', 'main', 'test', 'fixtures', 'action_payloads.pdf', ); /** * The dispatcher against a real engine and the real payload fixture: trees * come from `annotations.list()`, hide names resolve through `doc.forms`, * and the executor/sink/adapter seams carry recording fakes. */ describe('plugin-actions integration (real engine)', () => { let engine: Engine; let kernel: Kernel; let actions: ActionsHostCapability; let firstPage = 0; let treeOf: (nm: string) => PdfActionTree; let refOf: (nm: string) => AnnotationRef; /** One flat recording of every seam crossing, in call order. */ const calls: Array<{ seam: string; detail: unknown }> = []; beforeAll(async () => { engine = await createLocalEngine({ runtime: { prefer: 'wasm' } }); kernel = createKernel({ engine, plugins: [actionsPlugin()] }); const bytes = new Uint8Array(await readFile(fixturePath)); await kernel.documents.open({ kind: 'bytes', id: 'action-payloads', bytes }); actions = kernel.capability(ActionsToken) as ActionsHostCapability; actions.registerExecutor('goto', (node) => { calls.push({ seam: 'goto', detail: node.type === 'goto' ? node.destination : null }); return { status: 'executed' }; }); actions.registerExecutor('named', (node) => { calls.push({ seam: 'named', detail: node.type === 'named' ? node.name : null }); return { status: 'executed' }; }); actions.registerExecutor('javascript', (node) => { calls.push({ seam: 'js', detail: node.type === 'javascript' ? node.script : null }); return { status: 'executed' }; }); actions.registerExecutor('reset-form', (node) => { calls.push({ seam: 'reset', detail: node.type === 'reset-form' ? { fields: node.fields, exclude: node.exclude } : null, }); return { status: 'executed' }; }); actions.registerFormCommitSink(async (effects) => { calls.push({ seam: 'hideForm', detail: effects.map((effect) => effect.kind === 'setDisplay' ? { display: effect.display } : { kind: effect.kind }, ), }); return { status: 'applied' }; }); actions.registerAnnotCommitSink(async (entries) => { calls.push({ seam: 'hide', detail: entries.map((entry) => ({ annotObjectNumber: entry.annotObjectNumber, hidden: entry.patch.flags?.hidden ?? false, })), }); return { results: entries.map((entry) => ({ annotObjectNumber: entry.annotObjectNumber, status: 'applied' as const, })), }; }); actions.setUiAdapter({ openUri: (uri, options) => calls.push({ seam: 'uri', detail: { uri, ...options } }), print: () => calls.push({ seam: 'print', detail: null }), }); // Trees for execute() come straight off the annotation DTOs — read them // through a side handle so the tests stay independent of dispatch(). const opened = await engine.open( { kind: 'bytes', id: 'action-payloads-probe', bytes }, { scope: ['*'] }, ); const { pages } = await opened.pages.list(); const page = pages[0]; firstPage = page.ref.objectNumber; const { annotations } = await opened.page(toPageRef(firstPage)).annotations.list(); const byNm = new Map(annotations.map((annotation) => [annotation.nm, annotation])); treeOf = (nm: string) => { const tree = byNm.get(nm)?.actions?.activate; if (!tree) throw new Error(`no activate tree on '${nm}'`); return tree; }; refOf = (nm: string) => { const ref = byNm.get(nm)?.ref; if (!ref) throw new Error(`no annotation named '${nm}'`); return ref; }; await opened.close(); }); afterAll(async () => { await kernel?.destroy(); // closes documents; the engine is caller-owned await engine?.destroy(); }); const user = { origin: 'user' as const, source: { kind: 'api' as const }, event: { type: 'activate' as const }, }; const lastCallsSince = (mark: number) => calls.slice(mark).map((call) => call.seam); it('routes a GoTo /FitR tree to the navigation executor with its payload', async () => { const result = await actions.execute(treeOf('goto-fitr'), user); expect(result.status).toBe('executed'); expect(calls.at(-1)).toMatchObject({ seam: 'goto', // The file's [10 20 300 400] on a letter page, from the page's top-left. detail: { kind: 'fitR', x: 10, y: 392, width: 290, height: 380 }, }); }); it('routes a Named verb to the executor', async () => { const result = await actions.execute(treeOf('named-next'), user); expect(result.status).toBe('executed'); expect(calls.at(-1)).toMatchObject({ seam: 'named', detail: 'NextPage' }); }); it('routes hide-mixed by plane: the NAME → form setDisplay, the objectNumber → annot flags', async () => { const mark = calls.length; const result = await actions.execute(treeOf('hide-mixed'), user); expect(result.status).toBe('executed'); // Declared order: the forms plane commits first, then the annot plane. expect(lastCallsSince(mark)).toEqual(['hideForm', 'hide']); expect(calls.at(-2)!.detail).toEqual([{ display: 'visible' }]); // field note1 const entries = calls.at(-1)!.detail as Array<{ annotObjectNumber: number; hidden: boolean }>; expect(entries).toEqual([{ annotObjectNumber: 4, hidden: false }]); // the square expect(result.diagnostics).toEqual([]); }); it('reports an unresolved hide name as a diagnostic, still executing the rest', async () => { const result = await actions.execute(treeOf('hide-scalar'), user); // (fieldB) — no such field expect(result.nodes[0]?.status).toBe('executed'); expect(result.diagnostics).toContainEqual( expect.objectContaining({ code: 'unresolved-target' }), ); }); it('carries the ResetForm three-state payload to the executor untouched', async () => { await actions.execute(treeOf('reset-include'), user); expect(calls.at(-1)).toMatchObject({ seam: 'reset', detail: { fields: [{ kind: 'name', name: 'calc1' }], exclude: true }, }); await actions.execute(treeOf('reset-absent'), user); expect(calls.at(-1)).toMatchObject({ seam: 'reset', detail: { fields: null, exclude: true } }); await actions.execute(treeOf('reset-empty'), user); expect(calls.at(-1)).toMatchObject({ seam: 'reset', detail: { fields: [], exclude: false } }); }); it('routes a user-origin URI through the adapter with /IsMap', async () => { const result = await actions.execute(treeOf('uri-map'), user); expect(result.status).toBe('executed'); expect(calls.at(-1)).toMatchObject({ seam: 'uri', detail: { uri: 'https://example.test/map', isMap: true, origin: 'user' }, }); }); it('refuses launch and goto-remote as never-executable', async () => { for (const nm of ['launch-app', 'gotor-file']) { const result = await actions.execute(treeOf(nm), user); expect(result.nodes[0]?.status).toBe('blocked'); expect(result.diagnostics).toContainEqual(expect.objectContaining({ code: 'blocked' })); } }); it('runs the JS→GoTo→Hide chain with document work first, navigation deferred last', async () => { const mark = calls.length; const result = await actions.execute(treeOf('chain-js-goto-hide'), user); expect(result.status).toBe('executed'); // Walk order executes JS then Hide inline (the note1 name rides the // forms plane); the GoTo navigation thunk fires last. expect(lastCallsSince(mark)).toEqual(['js', 'hideForm', 'goto']); expect(result.nodes.map((node) => [node.path.join('.'), node.type, node.status])).toEqual([ ['', 'javascript', 'executed'], ['0', 'goto', 'executed'], ['0.0', 'hide', 'executed'], ]); expect((calls[mark]!.detail as string).includes('chain')).toBe(true); }); it('degraded payload-dropped nodes arrive as unknown and land no-executor', async () => { for (const nm of ['goto-malformed', 'hide-partial']) { const mark = calls.length; const result = await actions.execute(treeOf(nm), user); expect(result.nodes[0]?.type).toBe('unknown'); expect(result.nodes[0]?.status).toBe('no-executor'); expect(lastCallsSince(mark)).toEqual([]); // nothing executed, nothing leaked } }); it('dispatches by activate trigger: annotation ref → tree → executors', async () => { const mark = calls.length; const result = await actions.dispatch({ type: 'activate', // The ref a trigger source really passes: the annotation's own (objectNumber). annotation: refOf('named-next'), page: toPageRef(firstPage), }); expect(result.status).toBe('executed'); expect(lastCallsSince(mark)).toEqual(['named']); }); });