pdfjsTests.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const fs = require('node:fs')
  2. const Path = require('node:path')
  3. const { expect } = require('chai')
  4. const { parseXrefTable } = require('../../../app/js/XrefParser')
  5. const { NoXrefTableError } = require('../../../app/js/Errors')
  6. const PATH_EXAMPLES = 'test/acceptance/fixtures/examples/'
  7. const PATH_SNAPSHOTS = 'test/unit/js/snapshots/pdfjs/'
  8. const EXAMPLES = fs.readdirSync(PATH_EXAMPLES)
  9. function snapshotPath(example) {
  10. return Path.join(PATH_SNAPSHOTS, example, 'XrefTable.json')
  11. }
  12. function pdfPath(example) {
  13. return Path.join(PATH_EXAMPLES, example, 'output.pdf')
  14. }
  15. async function loadContext(example) {
  16. const size = (await fs.promises.stat(pdfPath(example))).size
  17. let blob
  18. try {
  19. blob = await fs.promises.readFile(snapshotPath(example))
  20. } catch (e) {
  21. if (e.code !== 'ENOENT') {
  22. throw e
  23. }
  24. }
  25. const snapshot = blob ? JSON.parse(blob) : null
  26. return {
  27. size,
  28. snapshot,
  29. }
  30. }
  31. async function backFillSnapshot(example, size) {
  32. const table = await parseXrefTable(pdfPath(example), size, () => {})
  33. await fs.promises.mkdir(Path.dirname(snapshotPath(example)), {
  34. recursive: true,
  35. })
  36. await fs.promises.writeFile(
  37. snapshotPath(example),
  38. JSON.stringify(table, null, 2)
  39. )
  40. return table
  41. }
  42. describe('pdfjs', function () {
  43. describe('when the pdf is an empty file', function () {
  44. it('should yield no entries', async function () {
  45. const path = 'does/not/matter.pdf'
  46. let table
  47. try {
  48. table = await parseXrefTable(path, 0)
  49. } catch (e) {
  50. expect(e).to.be.an.instanceof(NoXrefTableError)
  51. }
  52. expect(table).to.not.exist
  53. })
  54. })
  55. for (const example of EXAMPLES) {
  56. describe(example, function () {
  57. let size, snapshot
  58. before('load snapshot', async function () {
  59. const ctx = await loadContext(example)
  60. size = ctx.size
  61. snapshot = ctx.snapshot
  62. })
  63. before('back fill new snapshot', async function () {
  64. if (snapshot === null) {
  65. console.error('back filling snapshot for', example)
  66. snapshot = await backFillSnapshot(example, size)
  67. }
  68. })
  69. it('should produce the expected xRef table', async function () {
  70. const table = await parseXrefTable(pdfPath(example), size, () => {})
  71. // compare the essential parts of the xref table only
  72. expect(table.xRefEntries[0]).to.include({ offset: 0 })
  73. expect(table.xRefEntries.slice(1)).to.deep.equal(
  74. snapshot.xRefEntries
  75. .slice(1)
  76. .filter(xref => xref.uncompressed) // we only use the uncompressed fields
  77. .map(xref => {
  78. return { offset: xref.offset, uncompressed: xref.uncompressed } // ignore unused gen field
  79. })
  80. )
  81. })
  82. })
  83. }
  84. })