Something went wrong. Try again.
Synapse is a study workspace. Organize courses read and annotate the material, train with generated exercises and watch your progress forked from denishovart.com/synapse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136import { describe, expect, it } from 'vitest';import { PDFDocument, PDFName } from 'pdf-lib';import type { Annotation } from '../types';import { exportAnnotatedPdf } from './annotatedExport';
function ann(partial: Partial<Annotation> & Pick<Annotation, 'type' | 'data'>): Annotation { return { id: 'a1', document_id: 'd1', page: 1, color: '#e2665a', created_at: '2026-07-01 09:00:00', ...partial, } as Annotation;}
async function twoPagePdf(): Promise<Uint8Array> { const pdf = await PDFDocument.create(); pdf.addPage([600, 800]); pdf.addPage([600, 800]); return pdf.save();}
const ANNOTATIONS: Annotation[] = [ ann({ type: 'highlight', data: JSON.stringify({ x: 0.2, y: 0.2, width: 0.3, height: 0.1, rot: 0.3 }), }), ann({ id: 'a2', type: 'drawing', color: '#4caf82', data: JSON.stringify({ points: [ { x: 0.1, y: 0.5 }, { x: 0.3, y: 0.55 }, { x: 0.5, y: 0.5 }, ], }), }), ann({ id: 'a3', type: 'note', color: '#6c8cff', data: JSON.stringify({ x: 0.7, y: 0.3, html: '<p>Une note</p>' }), }),];
interface AnnotDict { get(n: PDFName): { toString(): string; decodeText?(): string } | undefined;}
function subtypesOn(pdf: PDFDocument, pageIndex = 0): string[] { const annots = pdf.getPages()[pageIndex].node.Annots(); if (!annots) return []; const out: string[] = []; for (let i = 0; i < annots.size(); i++) { const dict = annots.lookup(i) as unknown as AnnotDict; out.push(dict.get(PDFName.of('Subtype'))?.toString() ?? ''); } return out;}
describe('annotated PDF export (on-device)', () => { it('produces a valid PDF', async () => { const bytes = await exportAnnotatedPdf(await twoPagePdf(), ANNOTATIONS); expect(new TextDecoder().decode(bytes.slice(0, 5))).toBe('%PDF-'); });
it('writes each mark as its own markup annotation, not baked ink', async () => { const bytes = await exportAnnotatedPdf(await twoPagePdf(), ANNOTATIONS); const pdf = await PDFDocument.load(bytes); const subtypes = subtypesOn(pdf); // Highlights, drawings and notes are real, selectable/editable annotations // in any reader (Acrobat, PDF Expert, Preview) rather than flattened marks. expect(subtypes).toContain('/Highlight'); expect(subtypes).toContain('/Ink'); expect(subtypes).toContain('/Text'); });
it('gives every mark an appearance stream so it renders without a synthesiser', async () => { const bytes = await exportAnnotatedPdf(await twoPagePdf(), ANNOTATIONS); const pdf = await PDFDocument.load(bytes); const annots = pdf.getPages()[0].node.Annots()!; for (let i = 0; i < annots.size(); i++) { const dict = annots.lookup(i) as unknown as AnnotDict; const subtype = dict.get(PDFName.of('Subtype'))?.toString(); // /Text uses the reader's note glyph; the painted marks carry an /AP. if (subtype === '/Highlight' || subtype === '/Ink') { expect(dict.get(PDFName.of('AP'))).toBeDefined(); } } });
it('gives the note a readable text body and a valid modification date', async () => { const bytes = await exportAnnotatedPdf(await twoPagePdf(), ANNOTATIONS); const pdf = await PDFDocument.load(bytes); const annots = pdf.getPages()[0].node.Annots()!; let checked = false; for (let i = 0; i < annots.size(); i++) { const dict = annots.lookup(i) as unknown as AnnotDict; if (dict.get(PDFName.of('Subtype'))?.toString() !== '/Text') continue; expect(dict.get(PDFName.of('Contents'))?.decodeText?.()).toBe('Une note'); expect(dict.get(PDFName.of('M'))?.decodeText?.()).toMatch(/^D:\d{14}/); checked = true; } expect(checked).toBe(true); });
it('carries the highlight quad points so the tint sits over the text', async () => { const bytes = await exportAnnotatedPdf(await twoPagePdf(), ANNOTATIONS); const pdf = await PDFDocument.load(bytes); const annots = pdf.getPages()[0].node.Annots()!; let quadCount = 0; for (let i = 0; i < annots.size(); i++) { const dict = annots.lookup(i) as unknown as AnnotDict & { get(n: PDFName): { size?(): number } | undefined; }; if (dict.get(PDFName.of('Subtype'))?.toString() !== '/Highlight') continue; const quads = dict.get(PDFName.of('QuadPoints')) as { size(): number } | undefined; expect(quads?.size()).toBe(8); // one quad = 4 points = 8 numbers quadCount++; } expect(quadCount).toBe(1); });
it('skips page notes and out-of-range pages', async () => { const bytes = await exportAnnotatedPdf(await twoPagePdf(), [ ann({ type: 'page_note', data: JSON.stringify({ html: 'ignored' }) }), ann({ id: 'a9', type: 'note', page: 99, data: JSON.stringify({ x: 0.5, y: 0.5 }) }), ]); const pdf = await PDFDocument.load(bytes); expect(pdf.getPages()[0].node.Annots()).toBeUndefined(); });});