import type { DocumentEvent } from '@embedpdf/core'; import { quadFromRect } from '@embedpdf/core-geometry'; import type { AnnotationFlags, AnnotationRef, PdfQuad } from '@embedpdf/engine-core/runtime'; import { annotationKey, toPageRef } from '@embedpdf/engine-core/runtime'; import { afterEach, describe, expect, it, vi } from 'vitest'; import type { CommentsCapability } from '../src/contract'; import { annotationHarness, type FileAnnotation } from './harness'; import { groupOf, irtOf, styleOf } from '@embedpdf/core-annotation'; const PON = 1; const PON2 = 2; const PAGE = toPageRef(PON); const PAGE2 = toPageRef(PON2); const CROP = { left: 0, bottom: 0, right: 600, top: 800 }; const PAGE_BOX = { x: 0, y: 0, width: 600, height: 800 }; const NO_FLAGS: AnnotationFlags = { invisible: false, hidden: false, print: true, noZoom: false, noRotate: false, noView: false, readOnly: false, locked: false, toggleNoView: false, lockedContents: false, }; const ref = (annotObjectNumber: number): AnnotationRef => ({ kind: 'objectNumber', page: PAGE, objectNumber: annotObjectNumber, }); const base = (annotObjectNumber: number) => ({ ref: ref(annotObjectNumber), page: PAGE, hasAppearance: true, appearanceState: null, structurePosition: null, nm: null, ...NO_FLAGS, contents: null, subject: null, author: null, createdAt: null, modifiedAt: null, blendMode: 'normal' as const, }); const caretDTO = (): FileAnnotation => ({ ...base(10), subtype: 'caret', intent: 'replace', rect: { left: 85, bottom: 745, right: 95, top: 755 }, box: { left: 85, bottom: 745, right: 95, top: 755 }, rotation: null, color: '#ef4444', opacity: 1, reply: null, popup: null, groupId: null, userId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, }) as FileAnnotation; const strikeoutDTO = (): FileAnnotation => { const quad: PdfQuad = { upperLeft: { x: 10, y: 780 }, upperRight: { x: 90, y: 780 }, lowerLeft: { x: 10, y: 765 }, lowerRight: { x: 90, y: 765 }, }; return { ...base(11), subtype: 'strikeout', intent: 'strikeout-text-edit', rect: { left: 10, bottom: 765, right: 90, top: 780 }, color: '#ef4444', opacity: 1, quadPoints: [quad], richText: null, reply: { to: ref(10), type: 'group' }, popup: null, groupId: null, userId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, }; }; const createHarness = annotationHarness; afterEach(() => vi.restoreAllMocks()); /** Every comment check for one comment, read at once. */ const commentChecks = (comments: CommentsCapability, target: AnnotationRef) => ({ canReply: comments.canReply(target), canEditText: comments.canSetText(target), canSetStatus: comments.canSetStatus(target), canSetMarked: comments.canSetMarked(target), canDelete: comments.canDelete(target), canDeleteThread: comments.canDeleteThread(target), }); describe('Replace Text grouped persistence', () => { it('is one change: the Caret, then the StrikeOut answering it by its number (/IRT + /RT /Group)', async () => { const harness = createHarness(); harness.create .mockResolvedValueOnce({ annotation: caretDTO() }) .mockResolvedValueOnce({ annotation: strikeoutDTO() }); const rect = { x: 10, y: 20, width: 80, height: 15 }; harness.capability.createReplaceText( PAGE, [quadFromRect(rect)], { glyphQuad: quadFromRect(rect), advance: 1 }, 'replace-text', ); // Both show at once, keyed for life, the pair selected. const [caretId, strikeoutId] = harness.model().order; const caretRef = harness.model().byId[caretId!]!.annotation.ref; const strikeout = harness.model().byId[strikeoutId!]!.annotation; expect(irtOf(strikeout)).toBe(caretId); expect(groupOf(strikeout)).toBe(caretId); expect(harness.model().selected).toEqual([caretId, strikeoutId]); await vi.waitFor(() => expect(harness.create).toHaveBeenCalledTimes(2)); expect(harness.applied).toHaveLength(1); expect(harness.create.mock.calls[0]![0]).toMatchObject({ subtype: 'caret', intent: 'replace', print: true, }); expect(harness.create.mock.calls[1]![0]).toMatchObject({ subtype: 'strikeout', intent: 'strikeout-text-edit', reply: { to: caretRef, type: 'group' }, print: true, }); expect(harness.model().order).toEqual([caretId, strikeoutId]); }); it('a refused StrikeOut refuses the pair: both parts go, and nothing needs deleting', async () => { const harness = createHarness(); vi.spyOn(console, 'error').mockImplementation(() => {}); harness.create .mockResolvedValueOnce({ annotation: caretDTO() }) .mockRejectedValueOnce(new Error('strikeout failed')); const rect = { x: 10, y: 20, width: 80, height: 15 }; harness.capability.createReplaceText( PAGE, [quadFromRect(rect)], { glyphQuad: quadFromRect(rect), advance: 1 }, 'replace-text', ); expect(harness.model().order).toHaveLength(2); await vi.waitFor(() => expect(harness.model().order).toHaveLength(0)); expect(harness.remove).not.toHaveBeenCalled(); expect(harness.model().selected).toEqual([]); }); }); describe('annotation flags', () => { const squareDTO = (objectNumber: number, flags: Partial = {}): FileAnnotation => ({ ...base(objectNumber), ...NO_FLAGS, ...flags, subtype: 'square', rect: { left: 100, bottom: 700, right: 180, top: 760 }, box: { left: 100, bottom: 700, right: 180, top: 760 }, color: '#000000', opacity: 1, strokeWidth: 2, reply: null, popup: null, groupId: null, userId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, }) as FileAnnotation; /** Load these records as the document's annotations. */ const loadPage = async (harness: ReturnType, dtos: FileAnnotation[]) => { await harness.load(dtos); expect(harness.model().order.length).toBe(dtos.length); }; it('updateSelectionFlags writes a flags-only engine patch and keeps the render source', async () => { const harness = createHarness(); await loadPage(harness, [squareDTO(20)]); const id = harness.model().order[0]; harness.capability.selection.select([ref(20)]); harness.update.mockResolvedValueOnce({ annotation: squareDTO(20, { locked: true }) }); harness.capability.selection.update({ locked: true }); // optimistic: the model flips immediately, source untouched (still baked) expect(harness.model().byId[id].annotation.locked).toBe(true); expect(harness.model().byId[id].source).toBe('baked'); await vi.waitFor(() => expect(harness.update).toHaveBeenCalledTimes(1)); const [wref, patch] = harness.update.mock.calls[0]!; expect(wref).toEqual(ref(20)); // a flags-only patch, of the one flag that changed: nothing re-bakes expect(patch).toEqual({ subtype: 'square', locked: true }); // the re-sync preserves 'baked' await vi.waitFor(() => expect(harness.model().byId[id].source).toBe('baked')); }); it('selection.getProperties reports the flags: uniform values, and mixed ones', async () => { const harness = createHarness(); await loadPage(harness, [squareDTO(21, { locked: true }), squareDTO(22)]); expect(harness.capability.selection.getProperties().properties).toEqual([]); // nothing selected harness.capability.selection.select([ref(21)]); harness.capability.selection.add([ref(22)]); const { properties, values, mixed } = harness.capability.selection.getProperties(); expect(properties.find((property) => property.key === 'locked')?.control).toBe('flag'); expect(values.print).toBe(true); // uniform expect(mixed).toContain('locked'); expect(mixed).not.toContain('print'); expect(values.hidden).toBe(false); }); it('unlocking works on a locked annotation (setFlags bypasses the locked gate)', async () => { const harness = createHarness(); await loadPage(harness, [squareDTO(23, { locked: true })]); const id = harness.model().order[0]; harness.capability.selection.select([ref(23)]); harness.update.mockResolvedValueOnce({ annotation: squareDTO(23) }); harness.capability.selection.update({ locked: false }); expect(harness.model().byId[id].annotation.locked).toBe(false); await vi.waitFor(() => expect(harness.update).toHaveBeenCalledTimes(1)); }); it('update writes each flag it names as its own engine field', async () => { const harness = createHarness(); await loadPage(harness, [squareDTO(25)]); harness.update.mockResolvedValueOnce({ annotation: squareDTO(25, { hidden: true }) }); await harness.capability.update(ref(25), { subtype: 'square', hidden: true }); expect(harness.update.mock.calls[0]![1]).toEqual({ subtype: 'square', hidden: true }); }); it('create writes the draft as given: printing is the engine’s default, not added', async () => { const harness = createHarness(); await harness.load([]); harness.create.mockResolvedValueOnce({ annotation: squareDTO(24) }); await harness.capability.create(PAGE, { subtype: 'square', box: { x: 0, y: 0, width: 10, height: 10 }, }); expect(harness.create.mock.calls[0]![0]).not.toHaveProperty('print'); }); }); describe('claimsTouchAt (touch consent)', () => { it('a SELECTED text markup does not claim — selectable, not movable', async () => { const harness = createHarness(); harness.create .mockResolvedValueOnce({ annotation: caretDTO() }) .mockResolvedValueOnce({ annotation: strikeoutDTO() }); const rect = { x: 10, y: 20, width: 80, height: 15 }; harness.capability.createReplaceText( PAGE, [quadFromRect(rect)], { glyphQuad: quadFromRect(rect), advance: 1 }, 'replace-text', ); await vi.waitFor(() => expect(harness.create).toHaveBeenCalledTimes(2)); expect(harness.model().selected.length).toBe(2); // the strikeout's body is under the point (the hit-test finds it)… expect(harness.capability.getHitKind(PAGE, { x: 50, y: 27 })).toBe('annot'); // …but the claim must refuse: the selection cannot move, so a drag here // would be a dead zone — it has to keep scrolling instead. expect(harness.capability.claimsTouchAt(PAGE, { x: 50, y: 27 })).toBe(false); }); it('empty space never claims', () => { const harness = createHarness(); expect(harness.capability.claimsTouchAt(PAGE, { x: 300, y: 400 })).toBe(false); }); }); // ── whole-document hydration + remote delivery ────────────────────────── const hydrationSquare = (objectNumber: number): FileAnnotation => ({ ...base(objectNumber), subtype: 'square', rect: { left: 100, bottom: 700, right: 180, top: 760 }, box: { left: 100, bottom: 700, right: 180, top: 760 }, color: '#000000', opacity: 1, strokeWidth: 2, reply: null, popup: null, groupId: null, userId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, }) as FileAnnotation; /** The mutation meta every annotation event carries (a remote event replays the writer's result). */ const META = { affectedPages: [], cacheDelta: null, changed: [], weakRefsInvalidated: false, shouldRefetch: null, }; const remoteOrigin = (serverId: number) => ({ kind: 'remote' as const, sessionId: 'cloud:other', subject: 'u-2', at: new Date(0).toISOString(), serverSequence: serverId, }); const createdEvent = (dto: FileAnnotation, serverId: number): DocumentEvent => ({ type: 'annotations.created', page: PAGE, origin: remoteOrigin(serverId), annotation: dto, meta: META, }) as unknown as DocumentEvent; const updatedEvent = (dto: FileAnnotation, serverId: number, changed: boolean): DocumentEvent => ({ type: 'annotations.updated', page: PAGE, origin: remoteOrigin(serverId), annotation: dto, appearance: { changed }, meta: META, }) as unknown as DocumentEvent; const deletedEvent = (annotObjectNumber: number, serverId: number): DocumentEvent => ({ type: 'annotations.deleted', page: PAGE, origin: remoteOrigin(serverId), deleted: [{ kind: 'objectNumber', objectNumber: annotObjectNumber }], meta: META, }) as unknown as DocumentEvent; const snapshot = (dtos: FileAnnotation[], auditHead?: number) => ({ annotations: dtos, pages: [{ page: PAGE }], ...(auditHead !== undefined ? { auditHead } : {}), }); describe('the records mirror', () => { it('loads the whole document once, after connect', async () => { const harness = createHarness(); harness.listAll.mockResolvedValue(snapshot([hydrationSquare(20), hydrationSquare(21)], 40)); expect(harness.capability.getStatus()).toBe('idle'); harness.startSync(); expect(harness.capability.getStatus()).toBe('loading'); await harness.capability.whenSynced(); expect(harness.capability.getStatus()).toBe('ready'); expect(harness.listAll).toHaveBeenCalledTimes(1); expect(harness.model().order).toHaveLength(2); }); it('queues events during the load and replays them past the audit cursor', async () => { const harness = createHarness(); let resolveSnap!: (value: unknown) => void; harness.listAll.mockReturnValueOnce(new Promise((resolve) => (resolveSnap = resolve))); harness.startSync(); // A delete newer than the snapshot arrives during the load: the snapshot // still contains obj:30, and must not bring it back. harness.emit(deletedEvent(30, 45)); // An update the snapshot already contains (serverId ≤ auditHead) is // skipped: replaying it would regress obj:31 to the event's record. harness.emit(updatedEvent(hydrationSquare(31), 44, true)); resolveSnap(snapshot([hydrationSquare(30), hydrationSquare(31)], 44)); await harness.capability.whenSynced(); expect(harness.model().byId['obj:30']).toBeUndefined(); expect(harness.model().order).toEqual(['obj:31']); expect(harness.model().byId['obj:31']!.pictureVersion ?? 0).toBe(0); }); it('applies events live when the load fails', async () => { const harness = createHarness(); let rejectSnap!: (reason: unknown) => void; harness.listAll.mockReturnValueOnce(new Promise((_resolve, reject) => (rejectSnap = reject))); harness.startSync(); harness.emit(createdEvent(hydrationSquare(50), 45)); rejectSnap(new Error('network down')); await harness.capability.whenSynced(); expect(harness.capability.getStatus()).toBe('error'); // The queued event applied to the previous (empty) view. expect(harness.model().byId['obj:50']).toBeDefined(); harness.emit(deletedEvent(50, 46)); expect(harness.model().byId['obj:50']).toBeUndefined(); }); it('a reload reaps confirmed records missing from the snapshot and keeps optimistic ones', async () => { const harness = createHarness(); await harness.load([hydrationSquare(40), hydrationSquare(41)], 40); expect(harness.model().order).toHaveLength(2); // A create the engine hasn't answered: its records (caret and strikeout) // still show over the reloaded records. harness.create.mockReturnValue(new Promise(() => {})); const rect = { x: 10, y: 20, width: 80, height: 15 }; harness.capability.createReplaceText( PAGE, [quadFromRect(rect)], { glyphQuad: quadFromRect(rect), advance: 1 }, 'replace-text', ); const newIds = harness.model().order.filter((id) => id !== 'obj:40' && id !== 'obj:41'); expect(newIds).toHaveLength(2); // obj:41 was deleted while the stream could not be trusted. harness.listAll.mockResolvedValueOnce(snapshot([hydrationSquare(40)], 60)); harness.emit({ type: 'stream.desynced', reason: 'backlog-overflow', at: new Date(0).toISOString(), } as DocumentEvent); await harness.capability.whenSynced(); expect(harness.model().byId['obj:41']).toBeUndefined(); expect(harness.model().byId['obj:40']).toBeDefined(); // A reload re-fetches rasters once: changes during the gap were invisible. expect(harness.model().byId['obj:40']!.pictureVersion).toBe(1); for (const id of newIds) expect(harness.model().byId[id]).toBeDefined(); }); it("keys this session's create by the number it takes, and the engine's record lands under it", async () => { const harness = createHarness(); await harness.load([]); harness.create.mockResolvedValueOnce({ annotation: hydrationSquare(60) }); const creating = harness.capability.create( PAGE, { subtype: 'square', box: { x: 10, y: 10, width: 50, height: 40 } }, undefined, { select: true }, ); const [id] = harness.model().order; expect(harness.model().selected).toEqual([id]); const created = await creating; expect(annotationKey(created.annotation.ref)).toBe(id); expect(harness.model().order).toEqual([id]); expect(harness.model().selected).toEqual([id]); }); it('announces its own changes with the engine session as origin', async () => { const harness = createHarness(); await harness.load([hydrationSquare(70)]); const updated = vi.fn(); harness.capability.onUpdated(updated); harness.update.mockResolvedValueOnce({ annotation: hydrationSquare(70), appearance: { changed: true }, }); await harness.capability.update(ref(70), { subtype: 'square', contents: 'x' } as never); expect(updated).toHaveBeenCalledTimes(1); expect(updated.mock.calls[0]![0].origin).toEqual({ kind: 'local', sessionId: 'me', subject: null, at: new Date(0).toISOString(), serverSequence: null, // The change it belongs to: one event of one. transaction: { id: harness.applied[0]!.opId, index: 0, count: 1 }, }); }); }); describe('links lens — substrate children, no ledger', () => { const TARGET = { kind: 'uri', uri: 'https://www.embedpdf.com/' } as const; const childDTO = (objectNumber: number, parent: number): FileAnnotation => ({ ...hydrationSquare(objectNumber), subtype: 'link', target: TARGET, reply: { to: ref(parent), type: 'group' }, popup: null, groupId: null, userId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, }) as unknown as FileAnnotation; it('links.of derives from the committed child; a remote child delete clears it (no sweep)', async () => { const harness = createHarness(); await harness.load([hydrationSquare(20), childDTO(21, 20)]); expect(harness.capability.links.get(ref(20))).toEqual(TARGET); // The child is substrate: never painted, never hit as itself. // A remote session deletes the child → ordinary remove, lens re-derives. harness.emit({ type: 'annotations.deleted', page: PAGE, deleted: [{ kind: 'objectNumber', objectNumber: 21 }], meta: META, origin: { kind: 'remote', subject: 'alice' }, ts: Date.now(), } as unknown as DocumentEvent); expect(harness.capability.links.get(ref(20))).toBe(null); }); it('a linked annotation selects as a SINGLE unit: no ungroup, full selection', async () => { const harness = createHarness(); await harness.load([hydrationSquare(20), childDTO(21, 20)]); harness.capability.selection.select([ref(20)]); // One selected id — the child never joins the selection… expect(harness.capability.selection.getSelected().map((annotation) => annotation.ref)).toEqual([ ref(20), ]); // …and the group verbs stay hidden: ungroup on this "group" would strip // the child's /IRT and orphan it into an unmanaged standalone link. expect(harness.capability.selection.canUngroup()).toBe(false); expect(harness.capability.selection.canGroup()).toBe(false); }); it('links.set creates the grouped child and resolves when committed; clear deletes it', async () => { const harness = createHarness(); await harness.load([hydrationSquare(20)]); harness.create.mockResolvedValueOnce({ annotation: childDTO(30, 20) }); await harness.capability.links.set(ref(20), TARGET); // The engine write is the grouped child create… expect(harness.create).toHaveBeenCalledWith( expect.objectContaining({ subtype: 'link', target: TARGET, reply: { to: ref(20), type: 'group' }, }), ); // …and the lens reads the new value the moment the promise settles. expect(harness.capability.links.get(ref(20))).toEqual(TARGET); const child = harness.capability.list().find((annotation) => annotation.subtype === 'link')!; await harness.capability.links.clear(ref(20)); expect(harness.remove).toHaveBeenCalledWith(child.ref); expect(harness.capability.links.get(ref(20))).toBe(null); }); }); describe('link nav items — attached vs standalone', () => { it('labels attached children so the nav layer can defer to editing', async () => { const harness = createHarness(); await harness.load([ hydrationSquare(20), // The square's attached link child (`/RT /Group` → parent): folds into // parent.link and surfaces as an attached nav item over the parent. { ...hydrationSquare(21), subtype: 'link', target: { kind: 'uri', uri: 'https://example.com' }, reply: { to: ref(20), type: 'group' }, popup: null, groupId: null, userId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, } as unknown as FileAnnotation, // A standalone document link (no group): navigates under any link-nav // tool — never stands down. { ...hydrationSquare(22), subtype: 'link', target: { kind: 'uri', uri: 'https://docs.example.com' }, } as unknown as FileAnnotation, ]); const items = harness.capability.listLinkItems(PAGE); const byAttached = new Map(items.map((i) => [i.attached, i])); expect(items).toHaveLength(2); expect(byAttached.get(true)?.target).toEqual({ kind: 'uri', uri: 'https://example.com' }); expect(byAttached.get(false)?.target).toEqual({ kind: 'uri', uri: 'https://docs.example.com' }); }); }); describe('conversation plane at the capability boundary', () => { it('a remote review-status annotation joins the model but never paints or churns the epoch', async () => { const harness = createHarness(); await harness.load([hydrationSquare(80)]); const epochBefore = harness.capability.getAppearanceEpoch(PAGE); const statusDto = { ...base(81), subtype: 'text', rect: { left: 100, bottom: 700, right: 120, top: 720 }, color: '#ffff00', opacity: 1, icon: 'note', state: 'accepted', stateModel: 'review', reply: { to: ref(80), type: 'reply' }, popup: null, groupId: null, userId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, } as unknown as FileAnnotation; harness.emit(createdEvent(statusDto, 45)); // In the model (the conversation plane will read it)… expect(harness.model().byId['obj:81']).toBeDefined(); // …but invisible to the page: not painted, and the raster cache key of // the page is untouched despite the created-event's bake-fetch default. expect(harness.capability.listPageItems(PAGE).map((i) => i.id)).toEqual(['obj:80']); expect(harness.capability.getAppearanceEpoch(PAGE)).toBe(epochBefore); }); }); describe('the comments lens', () => { const NOTE_RECT = { left: 100, bottom: 700, right: 120, top: 720 }; const textDto = (objectNumber: number, over: Record): FileAnnotation => ({ ...base(objectNumber), subtype: 'text', rect: NOTE_RECT, color: '#ffff00', opacity: 1, icon: 'note', state: null, stateModel: null, reply: null, popup: null, groupId: null, userId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, ...over, }) as unknown as FileAnnotation; const rootAt = (objectNumber: number, top: number): FileAnnotation => ({ ...hydrationSquare(objectNumber), rect: { left: 100, bottom: top - 60, right: 180, top }, }) as FileAnnotation; const page2Root = (objectNumber: number): FileAnnotation => ({ ...hydrationSquare(objectNumber), ref: { kind: 'objectNumber', page: PAGE2, objectNumber: objectNumber }, page: PAGE2, }) as FileAnnotation; /** Seed: two threads on page 1 (root 20 high, root 25 lower — 20's thread * has a reply and alice's accepted status), one thread on page 2. */ const seed = async (harness: ReturnType) => { await harness.load([ rootAt(25, 500), rootAt(20, 760), textDto(21, { reply: { to: ref(20), type: 'reply' }, contents: 'a reply' }), textDto(22, { reply: { to: ref(20), type: 'reply' }, popup: null, groupId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, state: 'accepted', stateModel: 'review', userId: 'alice', modifiedAt: '2026-08-29T10:00:00Z', }), page2Root(30), ]); }; it('composes display-ordered threads and resolves any member to its thread', async () => { const harness = createHarness(); await seed(harness); const threads = harness.capability.comments.listThreads(); // Page 1 first (top-of-page before lower), then page 2. expect( threads.map((thread) => thread.root.ref.kind === 'objectNumber' ? thread.root.ref.objectNumber : -1, ), ).toEqual([20, 25, 30]); const t20 = threads[0]!; expect(t20.replies).toHaveLength(1); expect(t20.review.byReviewer['alice']?.state).toBe('accepted'); expect(t20.review.mine).toBe(null); // session user 'me' has no status // Any member resolves: the reply and the state annotation. expect(harness.capability.comments.getThread(ref(21))).toBe(t20); expect(harness.capability.comments.getThread(ref(22))).toBe(t20); // Memoized: same inputs, same array. expect(harness.capability.comments.listThreads()).toBe(threads); }); it('reply writes FLAT to the root, whatever member was passed', async () => { const harness = createHarness(); await seed(harness); harness.create.mockResolvedValueOnce({ annotation: textDto(40, { reply: { to: ref(20), type: 'reply' }, contents: 'agreed' }), }); const created = await harness.capability.comments.reply(ref(21), 'agreed'); // via the reply expect(harness.create).toHaveBeenCalledWith( expect.objectContaining({ subtype: 'text', contents: 'agreed', icon: 'comment', reply: { to: ref(20) }, // the root, not the reply noZoom: true, noRotate: true, }), ); expect(harness.model().byId[annotationKey(created.annotation.ref)]).toBeDefined(); }); it('setStatus chains: first to the root, the next to my previous status', async () => { const harness = createHarness(); await seed(harness); harness.create.mockResolvedValueOnce({ annotation: textDto(41, { reply: { to: ref(20), type: 'reply' }, popup: null, groupId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, state: 'accepted', stateModel: 'review', userId: 'me', modifiedAt: '2026-08-29T11:00:00Z', }), }); await harness.capability.comments.setStatus(ref(20), 'accepted'); expect(harness.create).toHaveBeenLastCalledWith( expect.objectContaining({ state: 'accepted', stateModel: 'review', reply: { to: ref(20) }, hidden: true, noZoom: true, noRotate: true, }), ); const first = harness.capability.comments.getThread(ref(20))!.review.mine!; expect(first.state).toBe('accepted'); harness.create.mockResolvedValueOnce({ annotation: textDto(42, { reply: { to: first.ref, type: 'reply' }, popup: null, groupId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, state: 'rejected', stateModel: 'review', userId: 'me', modifiedAt: '2026-08-29T12:00:00Z', }), }); await harness.capability.comments.setStatus(ref(20), 'rejected'); expect(harness.create).toHaveBeenLastCalledWith( expect.objectContaining({ state: 'rejected', reply: { to: first.ref } }), // the ISO chain ); expect(harness.capability.comments.getThread(ref(20))!.review.mine?.state).toBe('rejected'); }); it('edit patches contents with the wire subtype', async () => { const harness = createHarness(); await seed(harness); harness.update.mockResolvedValueOnce({ annotation: rootAt(20, 760) }); await harness.capability.comments.setText(ref(20), 'new text'); expect(harness.update).toHaveBeenCalledWith( ref(20), expect.objectContaining({ subtype: 'square', contents: 'new text' }), ); }); it('removeThread deletes the thread with one delete of its root', async () => { const harness = createHarness(); await seed(harness); const result = await harness.capability.comments.deleteThread(ref(20)); expect(result.failed).toEqual([]); expect(result.deleted).toEqual([ref(21), ref(22), ref(20)]); // The engine deletes the replies with the root, in one change. expect(harness.remove.mock.calls.map((call) => call[0])).toEqual([ref(20)]); expect(harness.capability.comments.getThread(ref(20))).toBe(null); }); it('removeThread preflight: one locked member blocks the whole cascade', async () => { const harness = createHarness(); await harness.load([ rootAt(20, 760), { ...textDto(21, { reply: { to: ref(20), type: 'reply' } }), ...NO_FLAGS, locked: true, } as unknown as FileAnnotation, ]); const result = await harness.capability.comments.deleteThread(ref(20)); expect(result.deleted).toEqual([]); expect(result.failed.map((failure) => failure.ref)).toEqual([ref(21)]); expect(harness.remove).not.toHaveBeenCalled(); }); it('removeThread takes a hidden review state along: hidden is how it draws, not whether it may go', async () => { const harness = createHarness(); await harness.load([ rootAt(20, 760), textDto(21, { reply: { to: ref(20), type: 'reply' }, contents: 'a reply' }), // As `setStatus` writes one: hidden, so no viewer draws an icon for it. { ...textDto(22, { reply: { to: ref(20), type: 'reply' }, state: 'completed', stateModel: 'review', }), ...NO_FLAGS, hidden: true, } as unknown as FileAnnotation, ]); expect(harness.capability.canDelete(ref(20))).toBe(true); // the page's delete expect(commentChecks(harness.capability.comments, ref(20)).canDeleteThread).toBe(true); const result = await harness.capability.comments.deleteThread(ref(20)); expect(result.failed).toEqual([]); expect(result.deleted).toEqual([ref(21), ref(22), ref(20)]); expect(harness.remove.mock.calls.map((call) => call[0])).toEqual([ref(20)]); }); it('permissionsFor splits the two lock flags and aggregates the thread gate', async () => { const harness = createHarness(); await harness.load([ { ...rootAt(20, 760), ...NO_FLAGS, lockedContents: true, } as unknown as FileAnnotation, textDto(21, { reply: { to: ref(20), type: 'reply' } }), ]); const perms = commentChecks(harness.capability.comments, ref(20)); expect(perms.canEditText).toBe(false); // lockedContents gates text expect(perms.canDelete).toBe(true); // …but not deletion expect(perms.canReply).toBe(true); expect(perms.canSetStatus).toBe(true); expect(perms.canDeleteThread).toBe(true); // Lock the reply: deleting the root would delete it too, so neither the // root nor the thread may go, from the panel or from the page. await harness.load([ rootAt(20, 760), { ...textDto(21, { reply: { to: ref(20), type: 'reply' } }), ...NO_FLAGS, locked: true, } as unknown as FileAnnotation, ]); const perms2 = commentChecks(harness.capability.comments, ref(20)); expect(perms2.canDelete).toBe(false); expect(perms2.canDeleteThread).toBe(false); expect(harness.capability.canDelete(ref(20))).toBe(false); // the page's delete }); it('a foreign status keeps a self-only deleter from deleting the root and its thread', async () => { const harness = createHarness(); // `annotations:delete:self`-shaped narrowing: only records stamped // with the session's own userId pass. harness.allowsAnnotationMutation.mockImplementation( (_action, target) => target.userId === 'me', ); await harness.load([ { ...rootAt(20, 760), userId: 'me' } as unknown as FileAnnotation, textDto(21, { reply: { to: ref(20), type: 'reply' }, userId: 'me' }), textDto(22, { reply: { to: ref(20), type: 'reply' }, popup: null, groupId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, state: 'accepted', stateModel: 'review', userId: 'alice', }), ]); // Deleting my root would delete alice's status with it, which the engine // refuses: the root, its thread and the page's delete all say no… const perms = commentChecks(harness.capability.comments, ref(20)); expect(perms.canDelete).toBe(false); expect(perms.canDeleteThread).toBe(false); expect(harness.capability.canDelete(ref(20))).toBe(false); // …while my own reply, which takes nothing of hers along, may go. expect(harness.capability.comments.canDelete(ref(21))).toBe(true); const result = await harness.capability.comments.deleteThread(ref(20)); expect(result.failed.map((failure) => failure.ref)).toEqual([ref(22)]); expect(harness.remove).not.toHaveBeenCalled(); }); it('canReply/canSetStatus gate on the CREATE mirror, not the target owner', async () => { const harness = createHarness(); harness.allowsAnnotationCreate.mockReturnValue(false); await seed(harness); const perms = commentChecks(harness.capability.comments, ref(20)); expect(perms.canReply).toBe(false); expect(perms.canSetStatus).toBe(false); // Mutation authority is unaffected — separate questions. expect(perms.canDelete).toBe(true); expect(harness.capability.canCreate()).toBe(false); }); }); describe('the twin law — authority fused into presentation and gestures', () => { const selfOnly = (harness: ReturnType) => harness.allowsAnnotationMutation.mockImplementation( (_action, target) => target.userId === 'me', ); const stamped = (objectNumber: number, userId?: string): FileAnnotation => ({ ...hydrationSquare(objectNumber), ...(userId ? { userId } : {}) }) as FileAnnotation; it('a foreign record renders the LOCKED treatment: selectable, zero handles', async () => { const harness = createHarness(); selfOnly(harness); await harness.load([stamped(20, 'me'), stamped(21, 'alice')]); // Own record: full selection chrome. harness.capability.selection.select([ref(20)]); expect( harness.capability.listChromeNodes(PAGE, 1, 0, 1).filter((node) => node.kind === 'handle') .length, ).toBeGreaterThan(0); // Alice's record under `:self`: selectable, but the same fused predicate // that answers canEdit(false) strips every handle — pixels can't lie. harness.capability.selection.select([ref(21)]); expect( harness.capability.listChromeNodes(PAGE, 1, 0, 1).filter((node) => node.kind === 'handle'), ).toHaveLength(0); expect(harness.capability.canUpdate(ref(21))).toBe(false); expect(harness.capability.canDelete(ref(21))).toBe(false); expect(harness.capability.canUpdate(ref(20))).toBe(true); }); it('no create authority → creation gestures are inert (no ghost, no draft, no 403)', async () => { const harness = createHarness(); harness.allowsAnnotationCreate.mockReturnValue(false); harness.capability.createPointer('square', 'down', PAGE, { x: 10, y: 10 }); harness.capability.createPointer('square', 'move', PAGE, { x: 80, y: 60 }); harness.capability.createPointer('square', 'up', PAGE, { x: 80, y: 60 }, true); expect(harness.model().order).toHaveLength(0); expect(harness.create).not.toHaveBeenCalled(); expect(harness.capability.canCreate()).toBe(false); }); it('EVERY optimistic create door self-refuses, not just the pointer', async () => { const harness = createHarness(); harness.allowsAnnotationCreate.mockReturnValue(false); const rect = { x: 10, y: 20, width: 80, height: 15 }; harness.capability.createMarkup('highlight', PAGE, [quadFromRect(rect)], 'highlight'); harness.capability.createCaret(PAGE, { glyphQuad: quadFromRect(rect), advance: 1 }); harness.capability.createReplaceText( PAGE, [quadFromRect(rect)], { glyphQuad: quadFromRect(rect), advance: 1 }, 'replace-text', ); await expect(harness.capability.createFromSelection('highlight')).rejects.toMatchObject({ code: 'permission-denied', permission: 'annotations:create', }); expect(harness.model().order).toHaveLength(0); expect(harness.create).not.toHaveBeenCalled(); }); it('without doc.annotate.read the records are never requested', async () => { const harness = createHarness(); harness.allows.mockImplementation((capability: string) => capability !== 'doc.annotate.read'); await harness.capability.refresh(); expect(harness.capability.getStatus()).toBe('forbidden'); expect(harness.listAll).not.toHaveBeenCalled(); expect(harness.capability.canRead()).toBe(false); }); it('a refused patch rolls the optimistic change back', async () => { const harness = createHarness(); vi.spyOn(console, 'error').mockImplementation(() => {}); await harness.load([stamped(20, 'me')]); harness.capability.selection.select([ref(20)]); const id = harness.model().order[0]!; const before = styleOf(harness.model().byId[id]!.annotation).color; harness.update.mockRejectedValueOnce(new Error('Forbidden')); harness.capability.selection.update({ color: '#00ff00' }); // optimistic first… expect(styleOf(harness.model().byId[id]!.annotation).color).toBe('#00ff00'); // …then the refusal restores the pre-patch annotation. await vi.waitFor(() => expect(styleOf(harness.model().byId[id]!.annotation).color).toBe(before), ); }); }); describe('per-record authorization (collab-resolver mirrors)', () => { const stamped = (objectNumber: number, userId?: string): FileAnnotation => ({ ...hydrationSquare(objectNumber), ...(userId ? { userId } : {}) }) as FileAnnotation; /** `annotations:*:self`-shaped narrowing installed on the harness mirror. */ const selfOnly = (harness: ReturnType) => harness.allowsAnnotationMutation.mockImplementation( (_action, target) => target.userId === 'me', ); it('canEdit/canDelete answer per annotation from the stamped owner', async () => { const harness = createHarness(); selfOnly(harness); await harness.load([stamped(20, 'me'), stamped(21, 'alice'), stamped(22)]); expect(harness.capability.canUpdate(ref(20))).toBe(true); expect(harness.capability.canUpdate(ref(21))).toBe(false); expect(harness.capability.canDelete(ref(20))).toBe(true); // Unstamped record → `{}` target: denied under narrowing, same as the engine. expect(harness.capability.canDelete(ref(22))).toBe(false); // The mirror received the target's own stamp, not the caller's. expect(harness.allowsAnnotationMutation).toHaveBeenCalledWith('update', { userId: 'alice' }); expect(harness.allowsAnnotationMutation).toHaveBeenCalledWith('delete', {}); }); it('canGroup requires the per-record update check on EVERY member', async () => { const harness = createHarness(); selfOnly(harness); await harness.load([stamped(20, 'me'), stamped(21, 'alice')]); harness.capability.selection.select([ref(20)]); harness.capability.selection.add([ref(21)]); expect(harness.capability.selection.canGroup()).toBe(false); const h2 = createHarness(); selfOnly(h2); await h2.load([stamped(20, 'me'), stamped(21, 'me')]); h2.capability.selection.select([ref(20)]); h2.capability.selection.add([ref(21)]); expect(h2.capability.selection.canGroup()).toBe(true); }); }); describe('remote delivery — echo-driven appearance invalidation', () => { const seed = async (harness: ReturnType, dto: FileAnnotation) => { await harness.load([dto]); }; it('a PRESERVED remote update re-syncs the model without an appearance re-fetch', async () => { const harness = createHarness(); await seed(harness, hydrationSquare(70)); harness.emit(updatedEvent(hydrationSquare(70), 45, false)); expect(harness.model().byId['obj:70']!.pictureVersion ?? 0).toBe(0); expect(harness.model().byId['obj:70']!.source).toBe('baked'); }); it('a REGENERATED remote update advances pictureVersion exactly once', async () => { const harness = createHarness(); await seed(harness, hydrationSquare(70)); harness.emit(updatedEvent(hydrationSquare(70), 45, true)); expect(harness.model().byId['obj:70']!.pictureVersion).toBe(1); }); it('a remote kind change replaces the record in place, as its new kind', async () => { const harness = createHarness(); await seed(harness, hydrationSquare(70)); const circle = { ...hydrationSquare(70), subtype: 'circle' } as FileAnnotation; harness.emit(updatedEvent(circle, 45, true)); expect(harness.model().order).toEqual(['obj:70']); expect(harness.model().byId['obj:70']!.annotation.subtype).toBe('circle'); expect(harness.model().byId['obj:70']!.pictureVersion).toBe(1); }); it('a remote z-order move never re-fetches appearances', async () => { const harness = createHarness(); await seed(harness, hydrationSquare(70)); harness.emit({ type: 'annotations.reordered', page: PAGE, origin: remoteOrigin(45), order: [hydrationSquare(70).ref], meta: META, } as unknown as DocumentEvent); expect(harness.model().byId['obj:70']!.pictureVersion ?? 0).toBe(0); }); }); describe.each([ { name: 'line', subtype: 'line' as const, intent: undefined }, { name: 'distance', subtype: 'line' as const, intent: 'line-dimension' as const }, { name: 'perimeter', subtype: 'polyline' as const, intent: 'polyline-dimension' as const }, { name: 'area', subtype: 'polygon' as const, intent: 'polygon-dimension' as const }, ])('$name rendering after local edits', ({ intent, subtype }) => { const dto = { ...base(72), subtype, vertices: [ { x: 100, y: 700 }, { x: 300, y: 700 }, { x: 300, y: 600 }, ], intent, rect: { left: 100, bottom: 680, right: 300, top: 700 }, linePoints: { start: { x: 100, y: 700 }, end: { x: 300, y: 700 } }, color: '#000000', strokeWidth: 1, interiorColor: null, borderStyle: 'solid', opacity: 1, captionEnabled: true, leader: { length: -20 }, lineEndings: { start: 'none', end: 'none' }, contents: '200 pt', reply: null, popup: null, groupId: null, userId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, } as unknown as FileAnnotation; it('a programmatic restyle draws live, like the same edit made by hand, and stays live', async () => { const harness = createHarness(); await harness.load([dto]); expect(harness.capability.listPageItems(PAGE)[0].source).toBe('baked'); const updated = { ...dto, strokeWidth: 2 }; harness.update.mockResolvedValueOnce({ annotation: updated, appearance: { changed: true } }); await harness.capability.update(dto.ref, { subtype, strokeWidth: 2 }); expect(harness.capability.get(dto.ref)).toEqual(harness.read(updated)); expect(harness.capability.listPageItems(PAGE)[0].source).toBe('vector'); // A record drawn live is no part of the page's raster. expect(harness.capability.getAppearanceEpoch(PAGE)).toBe(''); }); it('preserves vector rendering through consecutive local edits and engine responses', async () => { const harness = createHarness(); await harness.load([dto]); harness.capability.selection.select([dto.ref]); for (const strokeWidth of [2, 3]) { const updated = { ...dto, strokeWidth }; let finishWrite!: (result: unknown) => void; harness.update.mockReturnValueOnce( new Promise((resolve) => { finishWrite = resolve; }), ); harness.capability.selection.update({ strokeWidth }); expect(harness.capability.listPageItems(PAGE)[0].source).toBe('vector'); const epoch = harness.capability.getAppearanceEpoch(PAGE); expect(epoch).toBe(''); finishWrite({ annotation: updated, appearance: { changed: true } }); await vi.waitFor(() => expect(harness.capability.get(dto.ref)).toEqual(harness.read(updated)), ); expect(harness.capability.listPageItems(PAGE)[0].source).toBe('vector'); expect(harness.capability.getAppearanceEpoch(PAGE)).toBe(epoch); } }); }); describe('distance authoring and recalibration', () => { it('uses the viewport at the first point, retaining that snapshot throughout the drag', async () => { const harness = createHarness(); harness.seedToolDefaults(); const { measureFromKnownLength } = await import('@embedpdf/engine-core/runtime'); const fallback = measureFromKnownLength(100, { value: 1, unit: 'm' }); const region = measureFromKnownLength(100, { value: 10, unit: 'ft' }); harness.capability.setPageViewports( PAGE, [ { name: null, owned: false, bbox: { x: 0, y: 0, width: 60, height: 100 }, measure: region, }, ], fallback, ); const dto = { ...base(71), color: '#000000', strokeWidth: 1, opacity: 1, borderStyle: 'solid', subtype: 'line', intent: 'line-dimension', rect: CROP, linePoints: { start: { x: 20, y: 780 }, end: { x: 220, y: 780 } }, measure: region, captionEnabled: true, } as FileAnnotation; harness.create.mockResolvedValue({ annotation: dto }); harness.capability.createPointer('distance', 'down', PAGE, { x: 20, y: 20 }); harness.capability.setPageViewports(PAGE, [], fallback); harness.capability.createPointer('distance', 'move', PAGE, { x: 220, y: 20 }); harness.capability.createPointer('distance', 'up', PAGE, { x: 220, y: 20 }); expect(harness.capability.getDistanceCreationPage()).toEqual(PAGE); expect(harness.capability.hasCreationAwaitingClick()).toBe(true); harness.capability.createPointer('distance', 'down', PAGE, { x: 220, y: 8 }); expect(harness.capability.hasCreationAwaitingClick()).toBe(false); expect(harness.create).toHaveBeenCalledWith( expect.objectContaining({ intent: 'line-dimension', measure: region, captionEnabled: true, captionPosition: 'inline', leader: { length: 12, extension: 5, offset: 0 }, }), ); // The label is the engine's: it works it out from the points and the scale. expect(harness.create.mock.calls[0]![0]).not.toHaveProperty('contents'); const created = harness.capability.list()[0]!.ref; await vi.waitFor(() => expect(harness.capability.isPending(created)).toBe(false)); expect(harness.capability.listPageItems(PAGE)[0].source).toBe('vector'); expect(harness.capability.getAppearanceEpoch(PAGE)).toBe(''); expect(commentChecks(harness.capability.comments, created).canEditText).toBe(false); await expect(harness.capability.comments.setText(created, 'fake value')).rejects.toThrow( 'worked out', ); }); it('does not create over a winning foreign viewport or before viewport hydration', async () => { const harness = createHarness(); harness.seedToolDefaults(); const { measureFromRatio } = await import('@embedpdf/engine-core/runtime'); harness.capability.createPointer('distance', 'down', PAGE, { x: 20, y: 20 }); expect(harness.model().draft).toBeNull(); harness.capability.setPageViewports( PAGE, [ { name: null, owned: true, bbox: PAGE_BOX, measure: measureFromRatio(1, 1, 'm') }, { name: null, owned: false, bbox: PAGE_BOX, measure: { subtype: 'geospatial' } }, ], measureFromRatio(1, 1, 'm'), ); harness.capability.createPointer('distance', 'down', PAGE, { x: 20, y: 20 }); expect(harness.model().draft).toBeNull(); }); it('captures calibration with modify authority even without create authority', () => { const harness = createHarness(), captured = vi.fn(); harness.allowsAnnotationCreate.mockReturnValue(false); harness.capability.onDraftCaptured(captured); harness.capability.createPointer('calibrate', 'down', PAGE, { x: 20, y: 20 }); harness.capability.createPointer('calibrate', 'move', PAGE, { x: 120, y: 20 }); harness.capability.createPointer('calibrate', 'up', PAGE, { x: 120, y: 20 }); expect(captured).toHaveBeenCalledWith({ tool: 'calibrate', page: PAGE, from: { x: 20, y: 20 }, to: { x: 120, y: 20 }, }); expect(harness.create).not.toHaveBeenCalled(); harness.allows.mockReturnValue(false); harness.capability.createPointer('calibrate', 'down', PAGE, { x: 20, y: 20 }); expect(harness.model().draft).toBeNull(); }); it('stages the updates it can make and reports what it leaves alone, and why', async () => { const harness = createHarness(); const { measureFromKnownLength } = await import('@embedpdf/engine-core/runtime'); const scale = measureFromKnownLength(100, { value: 2, unit: 'm' }); const dtos = [80, 81, 82, 83, 84].map( (objectNumber) => ({ ...base(objectNumber), color: '#000000', strokeWidth: 1, opacity: 1, borderStyle: 'solid', subtype: 'line', rect: CROP, intent: 'line-dimension', measure: objectNumber === 82 ? { subtype: 'geospatial' } : scale, captionEnabled: true, linePoints: { start: { x: 0, y: 0 }, end: { x: 100, y: 0 } }, ...NO_FLAGS, locked: objectNumber === 81, userId: objectNumber === 83 ? 'other' : 'me', }) as unknown as FileAnnotation, ); harness.listAll.mockResolvedValue(snapshot(dtos)); harness.allowsAnnotationMutation.mockImplementation( (_action, target) => target.userId !== 'other', ); await harness.capability.whenRemeasurable(); // Staged at once, so a calibration's group takes the updates into its one change. const report = harness.capability.stageRemeasure(PAGE, scale); expect(report.updated).toEqual([ref(80), ref(84)]); expect(report.skipped).toEqual([ { ref: ref(81), reason: 'locked' }, { ref: ref(82), reason: 'foreign-measure' }, { ref: ref(83), reason: 'no-authority' }, ]); expect(report.failed).toEqual([]); }); }); describe.each(['area', 'perimeter'])('%s scale resolution', (tool) => { it('freezes the first viewport through multiple vertices and writes shape caption defaults', async () => { const harness = createHarness(); harness.seedToolDefaults(); const { measureFromKnownLength } = await import('@embedpdf/engine-core/runtime'); const region = measureFromKnownLength(100, { value: 10, unit: 'm' }); const fallback = measureFromKnownLength(100, { value: 1, unit: 'm' }); harness.capability.setPageViewports( PAGE, [ { name: null, owned: false, bbox: { x: 0, y: 0, width: 60, height: 100 }, measure: region, }, ], fallback, ); const subtype = tool === 'area' ? 'polygon' : 'polyline'; harness.create.mockResolvedValue({ annotation: { ...base(75), subtype, rect: CROP, vertices: [ { x: 20, y: 780 }, { x: 220, y: 780 }, { x: 220, y: 680 }, ], intent: tool === 'area' ? 'polygon-dimension' : 'polyline-dimension', measure: region, captionEnabled: true, color: '#ef4444', strokeWidth: 1, opacity: 1, }, }); harness.capability.createPointer(tool, 'down', PAGE, { x: 20, y: 20 }); harness.capability.setPageViewports(PAGE, [], fallback); harness.capability.createPointer(tool, 'down', PAGE, { x: 220, y: 20 }); harness.capability.createPointer(tool, 'down', PAGE, { x: 220, y: 120 }); harness.capability.draft.finish(); expect(harness.create).toHaveBeenCalledWith( expect.objectContaining({ subtype, measure: region, captionEnabled: true }), ); // The label is the engine's: it works it out from the points and the scale. expect(harness.create.mock.calls[0]![0]).not.toHaveProperty('contents'); const created = harness.capability.list()[0]!.ref; await vi.waitFor(() => expect(harness.capability.isPending(created)).toBe(false)); expect(harness.capability.listPageItems(PAGE)[0].source).toBe('vector'); expect(harness.capability.getAppearanceEpoch(PAGE)).toBe(''); }); it('rejects unhydrated, foreign and unauthorized creation without starting a draft', async () => { const harness = createHarness(); harness.seedToolDefaults(); const { measureFromRatio } = await import('@embedpdf/engine-core/runtime'); harness.capability.createPointer(tool, 'down', PAGE, { x: 20, y: 20 }); expect(harness.model().draft).toBeNull(); harness.capability.setPageViewports( PAGE, [{ name: null, owned: false, bbox: PAGE_BOX, measure: { subtype: 'geospatial' } }], measureFromRatio(1, 1, 'm'), ); harness.capability.createPointer(tool, 'down', PAGE, { x: 20, y: 20 }); expect(harness.model().draft).toBeNull(); harness.capability.setPageViewports(PAGE, [], measureFromRatio(1, 1, 'm')); harness.allowsAnnotationCreate.mockReturnValue(false); harness.capability.createPointer(tool, 'down', PAGE, { x: 20, y: 20 }); expect(harness.model().draft).toBeNull(); }); }); describe('where UI attaches to an annotation', () => { const note = (objectNumber: number): FileAnnotation => ({ ...base(objectNumber), subtype: 'text', rect: { left: 100, bottom: 700, right: 120, top: 720 }, color: '#ffff00', opacity: 1, icon: 'note', state: null, stateModel: null, reply: null, popup: null, groupId: null, userId: null, createdBy: null, modifiedBy: null, importedBy: null, actions: null, }) as unknown as FileAnnotation; it('a note that keeps its size on screen says where it is in any view; a square needs no view', async () => { const harness = createHarness(); await harness.load([note(70), hydrationSquare(80)]); const anchor = harness.capability.getAnnotationAnchor(ref(70))!; // At twice the zoom it takes half the page, so it shows at the same size. const atTwice = anchor.boundsIn!({ zoom: 2, rotation: 0 })!; expect(atTwice.width).toBeCloseTo(anchor.bounds.width / 2); expect(atTwice).toMatchObject({ x: anchor.bounds.x, y: anchor.bounds.y }); expect(harness.capability.getAnnotationAnchor(ref(80))!.boundsIn).toBeUndefined(); }); it('is the same object until the annotation itself moves, so UI on it doesn’t render again', async () => { const harness = createHarness(); await harness.load([note(70), hydrationSquare(80)]); const first = harness.capability.getAnnotationAnchor(ref(70)); harness.capability.selection.select([ref(80)]); // the model changes, the note doesn't expect(harness.capability.getAnnotationAnchor(ref(70))).toBe(first); }); });