import { describe, expect, test } from 'vitest'; import type { AnnotationDraft } from '../../src/annotation/kinds'; import type { AnnotationRef } from '../../src/identity/AnnotationRef'; import { pdfAnnotationDraftOf } from '../../src/pageSpace/annotations'; import { annotationOfDraft, resolveAnnotationDraft } from '../../src/pageSpace/helpers'; import { pdfResolveAnnotationDraft } from '../../src/validation'; const page = { kind: 'objectNumber', objectNumber: 3 } as const; const ref: AnnotationRef = { kind: 'objectNumber', page, objectNumber: 41 }; const box = { x: 72, y: 72, width: 120, height: 80 }; describe('annotationOfDraft', () => { test('a pending square: its draft, its kind’s defaults, and what the context says', () => { const drawn = { x: 71.5, y: 71.5, width: 121, height: 81 }; const square = annotationOfDraft( { subtype: 'square', box, color: '#123456' }, { ref, attribution: { author: 'Ada', userId: 'u-1' }, rect: drawn }, ); expect(square).toMatchObject({ subtype: 'square', ref, page, nm: null, box, rect: drawn, color: '#123456', opacity: 1, strokeWidth: 1, borderStyle: 'solid', interiorColor: null, rotation: null, print: true, author: 'Ada', userId: 'u-1', createdAt: null, popup: null, actions: null, }); }); test("a pending free text: its rich text carries its text style; color is the border's", () => { const text = annotationOfDraft( { subtype: 'free-text', box, intent: 'free-text', fontFamily: 'times-bold', fontSize: 20, textAlign: 'center', color: '#0000FF', contents: 'One\nTwo', }, { ref }, ); expect(text.subtype === 'free-text' && text.contents).toBe('One\nTwo'); expect(text.subtype === 'free-text' && text.richText).toEqual({ body: expect.objectContaining({ family: 'Times', weight: 700, size: 20, color: '#000000', align: 'center', }), paragraphs: [{ runs: [{ text: 'One' }] }, { runs: [{ text: 'Two' }] }], }); expect(text).toMatchObject({ color: '#0000FF', fontColor: '#000000', intent: 'free-text' }); }); test('a dashed border with its pattern left out states the default, 3 on and 3 off; null is none', () => { const dashed = annotationOfDraft({ subtype: 'square', box, borderStyle: 'dashed' }, { ref }); expect(dashed).toMatchObject({ borderStyle: 'dashed', dashArray: [3, 3] }); const cleared = annotationOfDraft( { subtype: 'ink', inkList: [[box, box]], borderStyle: 'dashed', dashArray: null }, { ref }, ); expect(cleared).toMatchObject({ borderStyle: 'dashed', dashArray: null }); // Its own pattern stays; a solid border states none. expect( annotationOfDraft( { subtype: 'line', linePoints: { start: box, end: box }, borderStyle: 'dashed', dashArray: [6, 2], }, { ref }, ), ).toMatchObject({ dashArray: [6, 2] }); expect(annotationOfDraft({ subtype: 'circle', box }, { ref })).toMatchObject({ borderStyle: 'solid', dashArray: null, }); }); test('a note’s standard review state brings its model; a custom one without is refused', () => { const note = resolveAnnotationDraft({ subtype: 'text', rect: { x: 0, y: 0, width: 20, height: 20 }, state: 'accepted', }); expect(note).toMatchObject({ stateModel: 'review' }); expect(() => resolveAnnotationDraft({ subtype: 'text', rect: { x: 0, y: 0, width: 20, height: 20 }, state: 'escalated', }), ).toThrow(expect.objectContaining({ code: 'InvalidArg' })); }); test('an empty dash pattern is refused: a solid line is borderStyle solid', () => { const page = { left: 0, bottom: 0, right: 600, top: 800 }; for (const draft of [ { subtype: 'square', box, borderStyle: 'dashed', dashArray: [] }, { subtype: 'ink', inkList: [[box, box]], dashArray: [] }, { subtype: 'free-text', box, contents: 'Hi', borderStyle: 'dashed', dashArray: [] }, ] as unknown as AnnotationDraft[]) { expect(() => pdfResolveAnnotationDraft(pdfAnnotationDraftOf(draft, page, () => page)), ).toThrow(expect.objectContaining({ code: 'InvalidArg', details: { field: 'dashArray' } })); } }); test('predicts a draft whose values the engine refuses, which the engine refuses at its door', () => { const draft = { subtype: 'square', box, color: 'red', opacity: 2, } as unknown as AnnotationDraft; // The page predicts without the schemas: the create shows, and leaves when refused. expect(annotationOfDraft(draft, { ref })).toMatchObject({ color: 'red', opacity: 2 }); const page = { left: 0, bottom: 0, right: 600, top: 800 }; expect(() => pdfResolveAnnotationDraft(pdfAnnotationDraftOf(draft, page, () => page))).toThrow( expect.objectContaining({ code: 'InvalidArg', details: { field: 'color' } }), ); }); });