~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
/**
* Direct runtime contracts for PDF generation and browser downloads.
* jsPDF and html2canvas are boundary fakes; all branching, DOM walking,
* sanitization requirements, download ownership, and cleanup are production
* code from services/pdf.js.
*/
import '@js/services/pdf.js';
const { downloadPdf, generatePdf } = window.pdfService;
let pdfInstances;
let createdBlob;
let pageWidth;
let pageHeight;
class FakePdf {
constructor() {
this.internal = {
pageSize: {
getWidth: () => pageWidth,
getHeight: () => pageHeight,
},
};
this.setFontSize = vi.fn();
this.setTextColor = vi.fn();
this.text = vi.fn();
this.setFont = vi.fn();
this.splitTextToSize = vi.fn((text) => String(text).split('\n'));
this.getTextWidth = vi.fn((text) => String(text).length * 5);
this.addPage = vi.fn();
this.setFillColor = vi.fn();
this.rect = vi.fn();
this.textWithLink = vi.fn();
this.setDrawColor = vi.fn();
this.setLineWidth = vi.fn();
this.line = vi.fn();
this.addImage = vi.fn();
this.output = vi.fn(() => createdBlob);
pdfInstances.push(this);
}
}
function canvas(width = 200, height = 100) {
return {
width,
height,
toDataURL: vi.fn(() => 'data:image/png;base64,rendered'),
};
}
beforeEach(() => {
document.body.replaceChildren();
pdfInstances = [];
createdBlob = new Blob(['pdf'], { type: 'application/pdf' });
pageWidth = 612;
pageHeight = 792;
globalThis.jsPDF = FakePdf;
globalThis.html2canvas = vi.fn().mockResolvedValue(canvas());
// marked/DOMPurify are only presence checks here: the body is rendered
// by ui.renderMarkdown (the hardened pipeline, covered end to end in
// pdf-sanitization.test.js), which is a boundary fake in this file.
window.marked = { parse: vi.fn((markdown) => String(markdown)) };
window.DOMPurify = { isSupported: true, sanitize: vi.fn((html) => html) };
window.XSSProtection = { sanitizeStable: vi.fn((html) => html) };
window.ui = {
renderMarkdown: vi.fn(
(markdown) => `<div class="ldr-markdown-content">${markdown}</div>`,
),
};
globalThis.URLValidator = {
safeAssign: vi.fn((element, property, value) => {
element[property] = value;
}),
};
Object.defineProperty(URL, 'createObjectURL', {
configurable: true,
value: vi.fn(() => 'blob:pdf-download'),
});
Object.defineProperty(URL, 'revokeObjectURL', {
configurable: true,
value: vi.fn(),
});
window.alert = vi.fn();
vi.spyOn(SafeLogger, 'error').mockImplementation(() => {});
});
afterEach(() => {
vi.restoreAllMocks();
document.body.replaceChildren();
delete globalThis.jsPDF;
delete globalThis.html2canvas;
delete globalThis.URLValidator;
delete window.marked;
delete window.DOMPurify;
delete window.XSSProtection;
delete window.ui;
delete window.alert;
});
function renderReturns(html) {
window.ui.renderMarkdown.mockReturnValue(
`<div class="ldr-markdown-content">${html}</div>`,
);
}
it('fails before rendering when jsPDF is unavailable', async () => {
delete globalThis.jsPDF;
await expect(generatePdf('Title', 'Report')).rejects.toThrow(
'PDF generation libraries not loaded (jsPDF missing)',
);
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
});
it('fails before rendering when html2canvas is unavailable', async () => {
delete globalThis.html2canvas;
await expect(generatePdf('Title', 'Report')).rejects.toThrow(
'PDF generation libraries not loaded (html2canvas missing)',
);
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
});
it('requires the markdown parser and cleans its temporary DOM', async () => {
delete window.marked;
await expect(generatePdf('Title', '# Report')).rejects.toThrow(
'Markdown parser (marked.js) not loaded. Cannot generate PDF.',
);
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
});
it('requires the sanitizer and cleans its temporary DOM', async () => {
renderReturns('<p>Report</p>');
delete window.DOMPurify;
await expect(generatePdf('Title', '# Report')).rejects.toThrow(
'DOMPurify not loaded. Cannot generate PDF safely.',
);
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
});
it.each([
['ui.renderMarkdown', () => { delete window.ui; }],
['XSSProtection.sanitizeStable', () => { delete window.XSSProtection; }],
])('fails closed when %s is unavailable', async (_name, remove) => {
remove();
await expect(generatePdf('Title', '# Report')).rejects.toThrow(
'Markdown sanitizer not loaded. Cannot generate PDF safely.',
);
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
expect(pdfInstances).toHaveLength(0);
});
it('fails closed when renderMarkdown degrades to its fallback output', async () => {
window.ui.renderMarkdown.mockReturnValue(
'<div class="alert alert-danger">Error rendering content: boom</div>',
);
await expect(generatePdf('Title', '# Report')).rejects.toThrow(
'Markdown rendering failed. Cannot generate PDF safely.',
);
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
expect(pdfInstances).toHaveLength(0);
});
it('sanitizes and walks structured report content into selectable PDF operations', async () => {
const html = `
<h1>Migration report</h1>
<h2>FastAPI contracts</h2>
<h3>Details</h3>
<p>Read <a href="https://example.test/source">the source</a> now</p>
<p>Legacy citation [[7]](https://example.test/citation)</p>
<ul><li>Bullet <a href="https://example.test/list">link</a></li></ul>
<ol><li>Numbered item</li></ol>
<table>
<tr><th>Route</th><th>Status</th></tr>
<tr><td>/api/report</td><td>migrated</td></tr>
</table>
<pre>const route = '/api/report';</pre>
<img src="data:image/jpeg;base64,image">
<section>Fallback text remains selectable</section>
<section><svg></svg></section>
`;
renderReturns(html);
const result = await generatePdf('Migration', '# ignored');
expect(result).toBe(createdBlob);
expect(window.ui.renderMarkdown).toHaveBeenCalledWith('# ignored');
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
expect(pdfInstances).toHaveLength(1);
const pdf = pdfInstances[0];
expect(pdf.textWithLink).toHaveBeenCalledWith(
'the source ',
expect.any(Number),
expect.any(Number),
{ url: 'https://example.test/source' },
);
expect(pdf.textWithLink).toHaveBeenCalledWith(
'[7]',
expect.any(Number),
expect.any(Number),
{ url: 'https://example.test/citation' },
);
expect(pdf.rect).toHaveBeenCalled();
expect(pdf.addImage).toHaveBeenCalledWith(
'data:image/png;base64,rendered',
'PNG',
expect.any(Number),
expect.any(Number),
expect.any(Number),
expect.any(Number),
);
expect(pdf.output).toHaveBeenCalledWith('blob');
expect(globalThis.html2canvas).toHaveBeenCalledWith(
expect.any(Element),
{
scale: 2,
useCORS: true,
logging: false,
backgroundColor: '#FFFFFF',
},
);
});
it('contains one malformed element and keeps producing the remaining PDF', async () => {
renderReturns('<p>Broken</p><p>Still rendered</p>');
class PartiallyFailingPdf extends FakePdf {
constructor() {
super();
this.splitTextToSize
.mockImplementationOnce(() => {
throw new Error('cannot measure first paragraph');
})
.mockImplementation((text) => [String(text)]);
}
}
globalThis.jsPDF = PartiallyFailingPdf;
await expect(generatePdf('Title', 'Report')).resolves.toBe(createdBlob);
expect(SafeLogger.error).toHaveBeenCalledWith(
'Error processing element:',
expect.objectContaining({ message: 'cannot measure first paragraph' }),
);
expect(pdfInstances[0].text).toHaveBeenCalledWith(
'[Error rendering content]',
40,
expect.any(Number),
);
expect(pdfInstances[0].text).toHaveBeenCalledWith(
['Still rendered'],
40,
expect.any(Number),
);
});
it('paginates long structured reports and redraws table ownership headers', async () => {
pageHeight = 115;
renderReturns(`
<h1>Long migration report</h1>
<h2>Next section</h2>
<p>Paragraph crossing the remaining page</p>
<ul><li>First item</li><li>Second item</li></ul>
<table>
<tr><th>Route</th><th>Owner</th><th>Method</th><th>Status</th><th>Notes</th></tr>
<tr><td>/api/report</td><td>results</td><td>GET</td><td>ready</td><td>one</td></tr>
<tr><td>/api/start</td><td>research</td><td>POST</td><td>ready</td><td>two</td></tr>
</table>
<pre>line one\nline two\nline three</pre>
<section>Fallback text</section>
<section><svg></svg></section>
`);
await expect(generatePdf('Long report', 'ignored'))
.resolves.toBe(createdBlob);
const pdf = pdfInstances[0];
expect(pdf.addPage.mock.calls.length).toBeGreaterThanOrEqual(6);
const headerDraws = pdf.text.mock.calls.filter(([text]) =>
Array.isArray(text) && text[0] === 'Route');
expect(headerDraws.length).toBeGreaterThanOrEqual(2);
expect(pdf.output).toHaveBeenCalledWith('blob');
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
});
// Wraps like jsPDF (5pt per character, see getTextWidth) and records the
// page and fill colour of every draw, which the stock fake does not.
class PagedPdf extends FakePdf {
constructor() {
super();
this.page = 1;
this.fill = null;
this.ops = [];
this.splitTextToSize.mockImplementation((text, maxWidth) => {
const lines = [];
let line = '';
for (const word of String(text).split(' ')) {
const next = line ? `${line} ${word}` : word;
if (line && next.length * 5 > maxWidth) {
lines.push(line);
line = word;
} else {
line = next;
}
}
lines.push(line);
return lines;
});
this.addPage.mockImplementation(() => { this.page++; });
this.setFillColor.mockImplementation((...rgb) => { this.fill = rgb.join(','); });
this.rect.mockImplementation((x, y, w, h) => {
this.ops.push({ op: 'rect', page: this.page, y, h, fill: this.fill });
});
this.line.mockImplementation((x1, y1, x2, y2) => {
this.ops.push({ op: 'line', page: this.page, x1, y1, x2, y2 });
});
this.text.mockImplementation((text, x, y) => {
this.ops.push({ op: 'text', page: this.page, text, y });
});
}
headerBands() {
return this.ops.filter((o) => o.op === 'rect' && o.fill === '0,51,102');
}
columnRules(page) {
return this.ops.filter((o) =>
o.op === 'line' && o.x1 === o.x2 && o.page === page);
}
}
const WRAPPING_HEADER =
'<tr><th>Aspect of the claim being compared across both of the reports</th><th>Note</th></tr>';
it('sizes the table header band to its wrapped header text', async () => {
globalThis.jsPDF = PagedPdf;
renderReturns(`<table>${WRAPPING_HEADER}
<tr><td>first</td><td>one</td></tr></table>`);
await generatePdf('Title', 'ignored');
const pdf = pdfInstances[0];
const [band] = pdf.headerBands();
const headerText = pdf.ops.find((o) =>
o.op === 'text' && o.text[0] === 'Aspect of the claim being compared across both of');
// Two header lines at fontSize 10: 2 * (10 + 2) + 10.
expect(headerText.text).toHaveLength(2);
expect(band.h).toBe(34);
const separator = pdf.ops.find((o) =>
o.op === 'line' && o.y1 === o.y2 && o.y1 === band.y + band.h);
expect(separator).toBeDefined();
const firstRow = pdf.ops.find((o) =>
o.op === 'text' && o.text[0] === 'first');
expect(firstRow.y).toBe(band.y + 34 + 12);
});
function longTable(rowCount) {
const rows = Array.from({ length: rowCount }, (_, i) =>
`<tr><td>row ${i}</td><td>value</td></tr>`).join('');
return `<table>${WRAPPING_HEADER}${rows}</table>`;
}
it('redraws the same wrapped header on every page a table spans', async () => {
pageHeight = 300;
globalThis.jsPDF = PagedPdf;
renderReturns(longTable(15));
await generatePdf('Title', 'ignored');
const pdf = pdfInstances[0];
const bands = pdf.headerBands();
expect(bands.map((b) => b.page)).toEqual([1, 2]);
for (const band of bands) {
expect(band.h).toBe(34);
const firstRowOnPage = pdf.ops.find((o) =>
o.op === 'text' && o.page === band.page && o.text[0].startsWith('row'));
expect(firstRowOnPage.y).toBe(band.y + 34 + 12);
}
});
it('draws column rules on every page a table spans, within that page', async () => {
pageHeight = 300;
globalThis.jsPDF = PagedPdf;
renderReturns(longTable(15));
await generatePdf('Title', 'ignored');
const pdf = pdfInstances[0];
const bands = pdf.headerBands();
expect(bands).toHaveLength(2);
for (const band of bands) {
const rules = pdf.columnRules(band.page);
// Three rules for two columns, from this page's header to the
// last row drawn on this page.
expect(rules).toHaveLength(3);
const lastRowLine = pdf.ops.filter((o) =>
o.op === 'line' && o.y1 === o.y2 && o.page === band.page).at(-1);
for (const rule of rules) {
expect(rule.y1).toBe(band.y);
expect(rule.y2).toBe(lastRowLine.y1);
}
}
});
it('moves the table to a new page when its wrapped header does not fit', async () => {
// Three paragraphs put the table at y=100; 165 - 40 leaves 25pt,
// room for a one-line header but not for this two-line one.
pageHeight = 165;
globalThis.jsPDF = PagedPdf;
renderReturns(`<p>one</p><p>two</p><p>three</p><table>${WRAPPING_HEADER}
<tr><td>first</td><td>one</td></tr></table>`);
await generatePdf('Title', 'ignored');
const pdf = pdfInstances[0];
const [band] = pdf.headerBands();
expect(band.page).toBe(2);
expect(band.y).toBe(40);
for (const rule of pdf.columnRules(1)) {
expect(rule.y1).not.toBe(100);
}
expect(pdf.columnRules(2).every((rule) => rule.y1 === 40)).toBe(true);
});
it('caps a header taller than half the page so rows still share a page', async () => {
// 120 twelve-character words wrap four to a line: 30 header lines,
// taller than the 220pt between the margins of a 300pt page.
pageHeight = 300;
globalThis.jsPDF = PagedPdf;
const longHeader = Array.from({ length: 120 }, (_, i) =>
`header${String(i).padStart(6, '0')}`).join(' ');
const rows = Array.from({ length: 10 }, (_, i) =>
`<tr><td>row ${i}</td><td>value</td></tr>`).join('');
renderReturns(`<table><tr><th>${longHeader}</th><th>Note</th></tr>${rows}</table>`);
await generatePdf('Title', 'ignored');
const pdf = pdfInstances[0];
const bands = pdf.headerBands();
// Half of 220pt allows 8 lines: 8 * (10 + 2) + 10.
expect(bands.map((b) => [b.page, b.h])).toEqual([[1, 106], [2, 106]]);
const headerText = pdf.ops.find((o) =>
o.op === 'text' && o.text[0].startsWith('header000000'));
expect(headerText.text).toHaveLength(8);
// The eighth line was full, so it loses characters to fit the marker.
expect(headerText.text[7]).toBe('header000028 header000029 header000030 header000...');
const rowPages = pdf.ops.filter((o) =>
o.op === 'text' && o.text[0].startsWith('row')).map((o) => o.page);
expect(rowPages).toEqual([1, 1, 1, 1, 1, 2, 2, 2, 2, 2]);
});
it('keeps one header line when half the page is shorter than a line', async () => {
// 110 - 80 leaves 30pt between the margins; half of it minus the
// band padding is under one 12pt line, so the cap floors at 1.
pageHeight = 110;
globalThis.jsPDF = PagedPdf;
renderReturns(`<table>${WRAPPING_HEADER}
<tr><td>first</td><td>one</td></tr></table>`);
await generatePdf('Title', 'ignored');
const pdf = pdfInstances[0];
const bands = pdf.headerBands();
expect(bands.length).toBeGreaterThan(0);
// One line is 1 * (10 + 2) + 10 = 22, under the 25pt minimum band.
expect(bands.every((b) => b.h === 25)).toBe(true);
const headerText = pdf.ops.find((o) =>
o.op === 'text' && o.text[0].startsWith('Aspect'));
expect(headerText.text).toHaveLength(1);
expect(headerText.text[0].endsWith('...')).toBe(true);
expect(pdf.ops.some((o) => o.op === 'text' && o.text[0] === 'first')).toBe(true);
});
it('caps a tall header in a column other than the first', async () => {
pageHeight = 300;
globalThis.jsPDF = PagedPdf;
const longHeader = Array.from({ length: 120 }, (_, i) =>
`header${String(i).padStart(6, '0')}`).join(' ');
const rows = Array.from({ length: 10 }, (_, i) =>
`<tr><td>row ${i}</td><td>value</td></tr>`).join('');
renderReturns(`<table><tr><th>Note</th><th>${longHeader}</th></tr>${rows}</table>`);
await generatePdf('Title', 'ignored');
const pdf = pdfInstances[0];
expect(pdf.headerBands().map((b) => [b.page, b.h])).toEqual([[1, 106], [2, 106]]);
const firstColumn = pdf.ops.find((o) => o.op === 'text' && o.text[0] === 'Note');
expect(firstColumn.text).toHaveLength(1);
const secondColumn = pdf.ops.find((o) =>
o.op === 'text' && o.text[0].startsWith('header000000'));
expect(secondColumn.text).toHaveLength(8);
expect(secondColumn.text[7]).toBe('header000028 header000029 header000030 header000...');
const rowPages = pdf.ops.filter((o) =>
o.op === 'text' && o.text[0].startsWith('row')).map((o) => o.page);
expect(rowPages).toEqual([1, 1, 1, 1, 1, 2, 2, 2, 2, 2]);
});
function cappedHeaderTable(firstRowLines) {
const words = (prefix, count) => Array.from({ length: count }, (_, i) =>
`${prefix}${String(i).padStart(6, '0')}`).join(' ');
// Four twelve-character words fill one line of a 256pt column.
return `<table><tr><th>${words('header', 120)}</th><th>Note</th></tr>
<tr><td>${words('firstr', 4 * firstRowLines)}</td><td>one</td></tr>
<tr><td>row 1</td><td>two</td></tr></table>`;
}
it('moves a capped header to the next page when its first row does not fit under it', async () => {
// The table starts at y=100 and the band ends at 206. The five-line
// first row needs 70pt, which passes the 260pt bottom margin.
pageHeight = 300;
globalThis.jsPDF = PagedPdf;
renderReturns(`<p>one</p><p>two</p><p>three</p>${cappedHeaderTable(5)}`);
await generatePdf('Title', 'ignored');
const pdf = pdfInstances[0];
expect(pdf.headerBands().map((b) => [b.page, b.y, b.h])).toEqual([[2, 40, 106]]);
expect(pdf.columnRules(1)).toHaveLength(0);
const firstRow = pdf.ops.find((o) =>
o.op === 'text' && o.text[0].startsWith('firstr'));
expect([firstRow.page, firstRow.y]).toEqual([2, 40 + 106 + 12]);
});
it('keeps the header-only check when the first row cannot fit under the header on any page', async () => {
// 106 + 12 * 12 + 10 = 260pt, more than the 220pt between the margins,
// so a new page would not keep them together either.
pageHeight = 300;
globalThis.jsPDF = PagedPdf;
renderReturns(`<p>one</p><p>two</p><p>three</p>${cappedHeaderTable(12)}`);
await generatePdf('Title', 'ignored');
const pdf = pdfInstances[0];
// The header stays on page 1; the row moves and takes a repeated header.
expect(pdf.headerBands().map((b) => [b.page, b.y, b.h])).toEqual([[1, 100, 106], [2, 40, 106], [3, 40, 106]]);
const firstRow = pdf.ops.find((o) =>
o.op === 'text' && o.text[0].startsWith('firstr'));
expect([firstRow.page, firstRow.y]).toEqual([2, 40 + 106 + 12]);
});
it('moves the header with its first row when the two exactly fill a page', async () => {
// 106 + 8 * 12 + 10 = 212pt, exactly the space between the margins.
pageHeight = 292;
globalThis.jsPDF = PagedPdf;
renderReturns(`<p>one</p><p>two</p><p>three</p>${cappedHeaderTable(8)}`);
await generatePdf('Title', 'ignored');
const pdf = pdfInstances[0];
// No band on page 1; the second row, not the first, opens page 3.
expect(pdf.headerBands().map((b) => [b.page, b.y, b.h])).toEqual([[2, 40, 106], [3, 40, 106]]);
const firstRow = pdf.ops.find((o) =>
o.op === 'text' && o.text[0].startsWith('firstr'));
expect([firstRow.page, firstRow.y]).toEqual([2, 40 + 106 + 12]);
});
it('checks only the header of a table with no data rows', async () => {
pageHeight = 300;
globalThis.jsPDF = PagedPdf;
const longHeader = Array.from({ length: 120 }, (_, i) =>
`header${String(i).padStart(6, '0')}`).join(' ');
renderReturns(`<p>one</p><p>two</p><p>three</p>
<table><tr><th>${longHeader}</th><th>Note</th></tr></table>`);
await generatePdf('Title', 'ignored');
const pdf = pdfInstances[0];
expect(pdf.headerBands().map((b) => [b.page, b.y, b.h])).toEqual([[1, 100, 106]]);
});
it('wraps long links and citations across pages without losing their targets', async () => {
pageWidth = 140;
pageHeight = 90;
renderReturns(`
<p>alpha beta gamma <a href="https://example.test/report-source">report source</a> omega psi</p>
<p>alpha beta [[99]](https://example.test/citation) omega psi</p>
<ul>
<li>alpha beta <a href="https://example.test/list-source">list source</a> omega</li>
</ul>
`);
await expect(generatePdf('Linked report', 'ignored'))
.resolves.toBe(createdBlob);
const pdf = pdfInstances[0];
expect(pdf.addPage.mock.calls.length).toBeGreaterThanOrEqual(4);
expect(pdf.textWithLink).toHaveBeenCalledWith(
'report source ',
expect.any(Number),
expect.any(Number),
{ url: 'https://example.test/report-source' },
);
expect(pdf.textWithLink).toHaveBeenCalledWith(
'[99]',
expect.any(Number),
expect.any(Number),
{ url: 'https://example.test/citation' },
);
expect(pdf.textWithLink).toHaveBeenCalledWith(
'list source ',
expect.any(Number),
expect.any(Number),
{ url: 'https://example.test/list-source' },
);
});
it('falls back to a readable placeholder when an image URL is rejected', async () => {
renderReturns(
'<img src="https://untrusted.example/image.jpg">',
);
globalThis.URLValidator.safeAssign.mockImplementation(() => {
throw new Error('unsafe image URL');
});
await expect(generatePdf('Report', 'ignored')).resolves.toBe(createdBlob);
expect(SafeLogger.error).toHaveBeenCalledWith(
'Error adding image:',
expect.objectContaining({ message: 'unsafe image URL' }),
);
expect(pdfInstances[0].text).toHaveBeenCalledWith(
'[Image could not be rendered]',
40,
expect.any(Number),
);
});
it('contains an html2canvas failure to the unsupported element', async () => {
renderReturns('<section><svg></svg></section>');
globalThis.html2canvas.mockRejectedValue(new Error('canvas tainted'));
await expect(generatePdf('Report', 'ignored')).resolves.toBe(createdBlob);
expect(pdfInstances[0].text).toHaveBeenCalledWith(
'[Content could not be rendered]',
40,
expect.any(Number),
);
expect(pdfInstances[0].output).toHaveBeenCalledWith('blob');
});
it('keeps the loading state until rendering completes and revokes the download URL', async () => {
renderReturns('<section><svg></svg></section>');
let finishCanvas;
globalThis.html2canvas.mockImplementation(() => new Promise((resolve) => {
finishCanvas = resolve;
}));
let clickedDownload;
let clickedHref;
vi.spyOn(window.HTMLAnchorElement.prototype, 'click')
.mockImplementation(function click() {
clickedDownload = this.download;
clickedHref = this.href;
});
const pending = downloadPdf('Migration: #3299', 'Report');
await vi.waitFor(() => {
expect(document.querySelector('.ldr-loading-indicator')).not.toBeNull();
});
finishCanvas(canvas());
await expect(pending).resolves.toBe(true);
expect(clickedDownload).toBe('migration___3299_research.pdf');
expect(clickedHref).toBe('blob:pdf-download');
expect(URL.createObjectURL).toHaveBeenCalledWith(createdBlob);
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:pdf-download');
expect(globalThis.URLValidator.safeAssign).toHaveBeenCalledWith(
expect.any(window.HTMLAnchorElement),
'href',
'blob:pdf-download',
);
expect(document.querySelector('.ldr-loading-indicator')).toBeNull();
expect(document.querySelector('a[download]')).toBeNull();
});
it('safely reduces legacy HTML research data to text before PDF rendering', async () => {
const clickSpy = vi.spyOn(window.HTMLAnchorElement.prototype, 'click')
.mockImplementation(() => {});
window.pwned = undefined;
await downloadPdf({
query: 'Legacy report',
html: '<h1>Hello</h1><p><img src=x onerror="window.pwned=true">Safe & sound</p>',
created_at: '2026-09-01T10:00:00Z',
}, 'research-3299');
const markdownInput = window.ui.renderMarkdown.mock.calls[0][0];
expect(markdownInput).toContain('# Hello');
expect(markdownInput).toContain('Safe & sound');
expect(markdownInput).not.toContain('<img');
expect(markdownInput).not.toContain('onerror');
expect(window.pwned).toBeUndefined();
expect(clickSpy).toHaveBeenCalledOnce();
});
it('reports generation failures and always removes transient browser state', async () => {
globalThis.jsPDF = class BrokenPdf {
constructor() {
throw new Error('printer unavailable');
}
};
await expect(downloadPdf('Report', 'Body'))
.rejects.toThrow('printer unavailable');
expect(window.alert)
.toHaveBeenCalledWith('Error generating PDF: printer unavailable');
expect(SafeLogger.error).toHaveBeenCalledWith(
'Error generating PDF:',
expect.objectContaining({ message: 'printer unavailable' }),
);
expect(document.querySelector('.ldr-loading-indicator')).toBeNull();
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
});
it('removes the offscreen container when moving the parsed report throws', async () => {
renderReturns('<p>Huge report</p>');
const original = Node.prototype.appendChild;
vi.spyOn(Node.prototype, 'appendChild').mockImplementation(function (child) {
if (this.id === 'pdf-body-content' && this.isConnected) {
throw new Error('body move failed');
}
return original.call(this, child);
});
await expect(generatePdf('Huge report', '# ignored')).rejects.toThrow(
'body move failed',
);
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
});
it('rejects a missing PDF body container and removes the offscreen node', async () => {
const original = Element.prototype.querySelector;
vi.spyOn(Element.prototype, 'querySelector').mockImplementation(function (selector) {
if (selector === '#pdf-body-content') {
return null;
}
return original.call(this, selector);
});
await expect(generatePdf('Title', '# Report')).rejects.toThrow(
'PDF body container not found in template',
);
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
expect(pdfInstances).toHaveLength(0);
});
it('removes the offscreen container when wrapping top-level text throws', async () => {
renderReturns('bare text that the PDF walker would otherwise drop');
const original = Node.prototype.insertBefore;
vi.spyOn(Node.prototype, 'insertBefore').mockImplementation(function (node, child) {
if (this.id === 'pdf-body-content' && this.isConnected
&& child?.nodeType === Node.TEXT_NODE) {
throw new Error('text wrap failed');
}
return original.call(this, node, child);
});
await expect(generatePdf('Report', 'bare text')).rejects.toThrow(
'text wrap failed',
);
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
});
it('removes the offscreen container when rewriting KaTeX throws', async () => {
renderReturns(
'<span class="katex-display"><annotation encoding="application/x-tex">x</annotation></span>',
);
const original = Element.prototype.querySelectorAll;
vi.spyOn(Element.prototype, 'querySelectorAll').mockImplementation(
function (selector) {
if (this.id === 'pdf-body-content' && selector === '.katex-display') {
throw new Error('katex walk failed');
}
return original.call(this, selector);
},
);
await expect(generatePdf('Report', '$$x$$')).rejects.toThrow(
'katex walk failed',
);
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
expect(pdfInstances).toHaveLength(0);
});