pdf-assets.client.spec.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /** PDF binary resources are exact-name, local data with independent transferable buffers. */
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { createPdfBinaryDataFactory, type PdfAssetMap } from '../src/client/pdf/assets.ts'
  4. afterEach(() => { vi.unstubAllGlobals() })
  5. describe('PDF binary assets', () => {
  6. const assets: PdfAssetMap = {
  7. cMapUrl: { 'sample.bcmap': 'AQID' },
  8. standardFontDataUrl: { 'font.pfb': 'BAU=' },
  9. wasmUrl: { 'decoder.wasm': 'BgcI' },
  10. }
  11. it('reads the ambient build payload only when the factory is created', async () => {
  12. vi.stubGlobal('__DSH_PDFJS_ASSETS__', assets)
  13. const Factory = createPdfBinaryDataFactory()
  14. const factory = new Factory()
  15. expect(Array.from(await factory.fetch({ kind: 'cMapUrl', filename: 'sample.bcmap' }))).toEqual([1, 2, 3])
  16. expect(Array.from(await factory.fetch({ kind: 'standardFontDataUrl', filename: 'font.pfb' }))).toEqual([4, 5])
  17. expect(Array.from(await factory.fetch({ kind: 'wasmUrl', filename: 'decoder.wasm' }))).toEqual([6, 7, 8])
  18. })
  19. it('never shares a buffer that PDF.js may transfer away', async () => {
  20. const Factory = createPdfBinaryDataFactory(assets)
  21. const factory = new Factory()
  22. const first = await factory.fetch({ kind: 'cMapUrl', filename: 'sample.bcmap' })
  23. structuredClone(first, { transfer: [first.buffer] })
  24. expect(first.byteLength).toBe(0)
  25. expect(Array.from(await factory.fetch({ kind: 'cMapUrl', filename: 'sample.bcmap' }))).toEqual([1, 2, 3])
  26. })
  27. it.each(['../font.pfb', 'missing.bcmap', 'toString'])('rejects unbundled filename %s without fetching', async (filename) => {
  28. const fetch = vi.fn()
  29. vi.stubGlobal('fetch', fetch)
  30. const Factory = createPdfBinaryDataFactory(assets)
  31. await expect(new Factory().fetch({ kind: 'cMapUrl', filename })).rejects.toThrow('not bundled')
  32. expect(fetch).not.toHaveBeenCalled()
  33. })
  34. })