/** * T9 (STEP6 §15): the WinAnsi canary. One text ingress, so a glyph the * standard fonts cannot encode never reaches pdf-lib; `formatDate` never * throws on user data. */ import { beforeAll, describe, expect, it } from 'vitest' import { PDFDocument, StandardFonts } from 'pdf-lib' import { buildSheetSnapshot } from '@faunosteo/domain' import { defaultPdfLabels, exportReportArchive } from '../src' import { formatDate } from '../src/render/dates' import { wrapLines } from '../src/render/text' import { safeText } from '../src/text' import { FIXED_EXPORTED_AT, nologosBranding, rasterizerOf, recordDrawing, } from './helpers/goldens' import { loadGoldenReports, type GoldenReports } from './helpers/reports' let golden: GoldenReports beforeAll(() => { golden = loadGoldenReports() }) describe('safeText', () => { it('keeps the WinAnsi glyphs the pages use', () => { const kept = 'Fixation (C2–C7) — bilan • zone · Sévérité à l’écu €' expect(safeText(kept)).toBe(kept) }) it('replaces unencodable glyphs by ? and strips control characters', () => { expect(safeText('⚠ alerte')).toBe('? alerte') expect(safeText('a → b')).toBe('a ? b') expect(safeText('ok 😀')).toBe('ok ?') expect(safeText('a\tb')).toBe('ab') expect(safeText('a
b')).toBe('ab') expect(safeText('a\x1bb')).toBe('ab') }) it('wrapLines splits on any whitespace first, then makes each word safe', async () => { const doc = await PDFDocument.create() const font = await doc.embedFont(StandardFonts.Helvetica) expect(wrapLines('un\tdeux\ntrois ⚠', font, 10, 1000)).toEqual([ 'un deux trois ?', ]) }) }) describe('export with hostile text', () => { it('a report with ⚠ in a comment exports; the glyph prints as ?', async () => { const demo = golden.reports.demo const report = { ...demo, observations: [ { ...demo.observations[0]!, comment: 'Attention ⚠ tab\tici → fin' }, ], conclusion: 'Conclusion ⚠', } const recording = recordDrawing() try { const { warnings } = await exportReportArchive({ report, sheet: buildSheetSnapshot(golden.draft, report), branding: nologosBranding(), labels: defaultPdfLabels, locale: 'fr', rasterizer: rasterizerOf('nologos'), exportedAt: FIXED_EXPORTED_AT, }) expect(warnings).toEqual([]) const drawn = recording.texts.map((t) => t.text) expect(drawn).toContain('Attention ? tab ici ? fin') expect(drawn).toContain('Conclusion ?') expect(drawn.every((t) => safeText(t) === t)).toBe(true) } finally { recording.stop() } }) }) describe('formatDate', () => { it('formats an ISO date per locale and returns anything else unchanged', () => { expect(formatDate('2026-11-20', 'fr')).toBe('20 novembre 2026') expect(formatDate('2026-11-20', 'en')).toBe('November 20, 2026') expect(formatDate('n/a', 'fr')).toBe('n/a') expect(formatDate('2026-13-45', 'en')).toBe('2026-13-45') expect(formatDate('2026-02-30', 'en')).toBe('2026-02-30') }) })