import { readFile } from 'node:fs/promises' import { existsSync, readdirSync } from 'node:fs' import { tmpdir } from 'node:os' import { dirname } from 'node:path' import { describe, expect, it, vi } from 'vitest' import { buildPrintableHtml, printHtmlToPdf, sanitizePrintableBody, type PrintWindow, } from '../src/print-html-pdf' describe('buildPrintableHtml', () => { it('escapes the title, sanitizes the body, and installs a restrictive CSP', () => { const html = buildPrintableHtml('', '

Title

Body

') expect(html).toContain('<A & B>') expect(html).toContain('

Title

Body

') expect(html).toContain(`default-src 'none'`) expect(html).toContain(`form-action 'none'`) expect(html).toContain('', '', '', '', '

Report

', '

source

', ].join(''), ) expect(html).not.toContain('attacker.invalid') expect(html).not.toContain('127.0.0.1') expect(html).not.toContain('file:///') expect(html).not.toContain('Report') expect(html).toContain('source') }) }) describe('sanitizePrintableBody', () => { it('keeps the restricted document structure and unwraps unknown containers', () => { expect( sanitizePrintableBody( '

Heading

Text boldx^2

', ), ).toBe('

Heading

Text boldx^2

') }) }) function fakeWindow(over: Partial = {}) { const seen: { html?: string; dir?: string } = {} const win = { loadFile: vi.fn(async (path: string) => { seen.dir = dirname(path) seen.html = await readFile(path, 'utf8') }), destroy: vi.fn(), webContents: { printToPDF: vi.fn(async () => Buffer.from('%PDF-fake')), ...over, }, } as unknown as PrintWindow return { win, seen } } describe('printHtmlToPdf', () => { it('loads the HTML from a temp file, prints, and cleans up', async () => { const { win, seen } = fakeWindow() const html = buildPrintableHtml('Test', '

hi

') const bytes = await printHtmlToPdf(html, () => win) expect(bytes.toString()).toBe('%PDF-fake') expect(seen.html).toBe(html) expect(win.destroy).toHaveBeenCalledOnce() expect(seen.dir && existsSync(seen.dir)).toBe(false) }) it('destroys the window and removes the temp dir when printing fails', async () => { const { win, seen } = fakeWindow({ printToPDF: vi.fn(async () => Promise.reject(new Error('print failed'))), }) await expect(printHtmlToPdf(buildPrintableHtml('Test', ''), () => win)).rejects.toThrow( 'print failed', ) expect(win.destroy).toHaveBeenCalledOnce() expect(seen.dir && existsSync(seen.dir)).toBe(false) }) it('removes the temp dir when the window cannot be created', async () => { const leftovers = () => readdirSync(tmpdir()).filter((d) => d.startsWith('genoffice-ai-doc-')) const before = leftovers().length await expect( printHtmlToPdf(buildPrintableHtml('Test', ''), () => { throw new Error('no window') }), ).rejects.toThrow('no window') expect(leftovers().length).toBe(before) }) })