zip download of single pdfs
This commit is contained in:
86
src/pdf/pdfZipService.test.ts
Normal file
86
src/pdf/pdfZipService.test.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { unzipSync } from 'fflate';
|
||||
import { createSplitResultsZip, createSplitZipFilename } from './pdfZipService';
|
||||
import type { SplitResult } from './pdfTypes';
|
||||
|
||||
async function unzipBlob(blob: Blob): Promise<Record<string, Uint8Array>> {
|
||||
const arrayBuffer = await blob.arrayBuffer();
|
||||
return unzipSync(new Uint8Array(arrayBuffer));
|
||||
}
|
||||
|
||||
describe('pdfZipService', () => {
|
||||
it('creates a ZIP archive from split PDF blobs', async () => {
|
||||
const results: SplitResult[] = [
|
||||
{
|
||||
pageIndex: 0,
|
||||
filename: 'document_page_001.pdf',
|
||||
blob: new Blob([new Uint8Array([1, 2, 3])], {
|
||||
type: 'application/pdf',
|
||||
}),
|
||||
},
|
||||
{
|
||||
pageIndex: 1,
|
||||
filename: 'document_page_002.pdf',
|
||||
blob: new Blob([new Uint8Array([4, 5, 6])], {
|
||||
type: 'application/pdf',
|
||||
}),
|
||||
},
|
||||
];
|
||||
|
||||
const zipBlob = await createSplitResultsZip(results);
|
||||
const entries = await unzipBlob(zipBlob);
|
||||
|
||||
expect(zipBlob.type).toBe('application/zip');
|
||||
expect(Object.keys(entries)).toEqual([
|
||||
'document_page_001.pdf',
|
||||
'document_page_002.pdf',
|
||||
]);
|
||||
expect(Array.from(entries['document_page_001.pdf'])).toEqual([1, 2, 3]);
|
||||
expect(Array.from(entries['document_page_002.pdf'])).toEqual([4, 5, 6]);
|
||||
});
|
||||
|
||||
it('sanitizes and deduplicates ZIP entry names', async () => {
|
||||
const results: SplitResult[] = [
|
||||
{
|
||||
pageIndex: 0,
|
||||
filename: '../page.pdf',
|
||||
blob: new Blob([new Uint8Array([1])], { type: 'application/pdf' }),
|
||||
},
|
||||
{
|
||||
pageIndex: 1,
|
||||
filename: '../page.pdf',
|
||||
blob: new Blob([new Uint8Array([2])], { type: 'application/pdf' }),
|
||||
},
|
||||
{
|
||||
pageIndex: 2,
|
||||
filename: '',
|
||||
blob: new Blob([new Uint8Array([3])], { type: 'application/pdf' }),
|
||||
},
|
||||
];
|
||||
|
||||
const zipBlob = await createSplitResultsZip(results);
|
||||
const entries = await unzipBlob(zipBlob);
|
||||
|
||||
expect(Object.keys(entries)).toEqual([
|
||||
'.._page.pdf',
|
||||
'.._page_2.pdf',
|
||||
'page_003.pdf',
|
||||
]);
|
||||
});
|
||||
|
||||
it('creates a readable ZIP filename from the source PDF name', () => {
|
||||
expect(createSplitZipFilename('contract.pdf')).toBe(
|
||||
'contract_split_pages.zip'
|
||||
);
|
||||
expect(createSplitZipFilename('contract.final.PDF')).toBe(
|
||||
'contract.final_split_pages.zip'
|
||||
);
|
||||
expect(createSplitZipFilename('')).toBe('document_split_pages.zip');
|
||||
});
|
||||
|
||||
it('rejects empty split results', async () => {
|
||||
await expect(createSplitResultsZip([])).rejects.toThrow(
|
||||
'Cannot create a ZIP archive without split results.'
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user