import type { ConformanceTestRunner, ConformanceOptions } from './runMetadataConformance'; import type { SquareDraft } from '../annotation/kinds'; import type { DocumentHandle } from '../engine/DocumentHandle'; import type { Engine } from '../engine/Engine'; import { EngineError } from '../errors/EngineError'; import { EngineErrorCode } from '../errors/EngineErrorCode'; import type { DocumentEvent } from '../events/DocumentEvent'; import type { AnnotationRef } from '../identity/AnnotationRef'; import { toPageRef } from '../identity/PageRef'; import { AnnotationFlattenResultSchema } from '../wire/schemas'; import { EVERY_PERMISSION } from './fixtureScope'; const square = (x: number, y: number): SquareDraft => ({ subtype: 'square', box: { x, y, width: 40, height: 40 }, color: '#1428dc', strokeWidth: 2, }); /** * Selective annotation flatten: `page(pon).annotations.flatten(refs)` is * `pages.flatten` for a chosen set. Locks, on every engine: * 1. only the given refs are painted and removed; the rest of the page's * annotations are untouched; * 2. per-ref status (`applied` / `skipped`), one `annotations.flattened` * event, a `meta` with that page's new pins, layout unchanged; * 3. a hidden ref is `skipped`; a ref on another page rejects `InvalidArg` * with nothing mutated. * Skips cleanly when the engine does not implement the optional member. */ export function runAnnotationFlattenConformance( runner: ConformanceTestRunner, options: ConformanceOptions, ): void { const { describe, test, beforeAll, afterAll, expect } = runner; describe(`annotation flatten conformance: ${options.label}`, () => { let engine: Engine; beforeAll(async () => { engine = await options.makeEngine(); }); afterAll(async () => { if (engine) await engine.destroy(); }); test('flattens exactly the given refs and reports per-ref status', async () => { const doc = await openFixture(engine, options); try { const layoutBefore = await doc.pages.list(); const pageObjectNumber = layoutBefore.pages[0].ref.objectNumber; const page = doc.page(toPageRef(pageObjectNumber)); if (!page.annotations.flatten) return; const a = (await page.annotations.create(square(20, 20))).annotation.ref; const b = (await page.annotations.create(square(80, 20))).annotation.ref; const c = (await page.annotations.create(square(140, 20))).annotation.ref; // Hide `c` — ineligible for display flatten, so it must be skipped. await page.annotations.update(c, { subtype: 'square', hidden: true }); const before = await page.annotations.list(); const events: DocumentEvent[] = []; const unsubscribe = doc.events.subscribe((event) => { if (event.type === 'annotations.flattened') events.push(event); }); const result = await page.annotations.flatten([a, c], { usage: 'display' }); unsubscribe(); expect(AnnotationFlattenResultSchema.safeParse(result).success).toBe(true); expect(result.page.objectNumber).toBe(pageObjectNumber); expect(result.usage).toBe('display'); expect(result.results.map((item) => item.status)).toEqual(['applied', 'unchanged']); expect(result.meta.affectedPages).toEqual([page.ref]); expect(events).toHaveLength(1); // Layout untouched; `a` gone, `b` and `c` still there. expect(await doc.pages.list()).toEqual(layoutBefore); const after = await page.annotations.list(); expect(after.annotations.length).toBe(before.annotations.length - 1); expect(after.annotations.some((dto) => sameRef(dto.ref, b))).toBe(true); expect(after.annotations.some((dto) => sameRef(dto.ref, c))).toBe(true); expect(after.annotations.some((dto) => sameRef(dto.ref, a))).toBe(false); } finally { await doc.close(); } }); test('a ref on another page rejects InvalidArg and mutates nothing', async () => { const doc = await openFixture(engine, options); try { const layout = await doc.pages.list(); if (layout.pages.length < 2) return; const page0 = doc.page(layout.pages[0].ref); const page1 = doc.page(layout.pages[1].ref); if (!page0.annotations.flatten) return; const own = (await page0.annotations.create(square(20, 100))).annotation.ref; const foreign = (await page1.annotations.create(square(20, 100))).annotation.ref; const countBefore = (await page0.annotations.list()).annotations.length; await expect(page0.annotations.flatten([own, foreign])).rejects.toMatchObject({ code: EngineErrorCode.InvalidArg, }); expect((await page0.annotations.list()).annotations.length).toBe(countBefore); } finally { await doc.close(); } }); test('an empty ref list rejects InvalidArg; an unknown ref rejects NotFound', async () => { const doc = await openFixture(engine, options); try { const pageObjectNumber = (await doc.pages.list()).pages[0].ref.objectNumber; const page = doc.page(toPageRef(pageObjectNumber)); if (!page.annotations.flatten) return; await expect(page.annotations.flatten([])).rejects.toMatchObject({ code: EngineErrorCode.InvalidArg, }); let unknown: unknown = null; try { await page.annotations.flatten([ { kind: 'objectNumber', page: toPageRef(pageObjectNumber), objectNumber: 987654321, }, ]); } catch (error) { unknown = error; } expect(EngineError.is(unknown, EngineErrorCode.NotFound)).toBe(true); } finally { await doc.close(); } }); }); } function sameRef(left: AnnotationRef, right: AnnotationRef): boolean { if (left.kind === 'objectNumber' && right.kind === 'objectNumber') { return left.objectNumber === right.objectNumber; } return JSON.stringify(left) === JSON.stringify(right); } async function openFixture(engine: Engine, opts: ConformanceOptions): Promise { if (opts.openKind === 'bytes') { return engine.open( { kind: 'bytes', id: opts.fixture.id, bytes: await opts.fixture.bytes() }, EVERY_PERMISSION, ); } return engine.open({ kind: 'id', id: opts.fixture.cloudId ?? opts.fixture.id }); }