pdfjsTests.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const fs = require('fs')
  2. const Path = require('path')
  3. const { expect } = require('chai')
  4. const { parseXrefTable } = require('../../../app/lib/pdfjs/parseXrefTable')
  5. const PATH_EXAMPLES = 'test/acceptance/fixtures/examples/'
  6. const PATH_SNAPSHOTS = 'test/unit/lib/snapshots/'
  7. const EXAMPLES = fs.readdirSync(PATH_EXAMPLES)
  8. function snapshotPath(example) {
  9. return Path.join(PATH_SNAPSHOTS, example, 'XrefTable.json')
  10. }
  11. function pdfPath(example) {
  12. return Path.join(PATH_EXAMPLES, example, 'output.pdf')
  13. }
  14. async function loadContext(example) {
  15. const size = (await fs.promises.stat(pdfPath(example))).size
  16. let blob
  17. try {
  18. blob = await fs.promises.readFile(snapshotPath(example))
  19. } catch (e) {
  20. if (e.code !== 'ENOENT') {
  21. throw e
  22. }
  23. }
  24. const snapshot = blob ? JSON.parse(blob) : null
  25. return {
  26. size,
  27. snapshot
  28. }
  29. }
  30. async function backFillSnapshot(example, size) {
  31. const table = await parseXrefTable(pdfPath(example), size)
  32. await fs.promises.mkdir(Path.dirname(snapshotPath(example)), {
  33. recursive: true
  34. })
  35. await fs.promises.writeFile(
  36. snapshotPath(example),
  37. JSON.stringify(table, null, 2)
  38. )
  39. return table
  40. }
  41. describe('pdfjs', function () {
  42. describe('when the pdf is an empty file', function () {
  43. it('should yield no entries', async function () {
  44. const path = 'does/not/matter.pdf'
  45. const table = await parseXrefTable(path, 0)
  46. expect(table).to.deep.equal([])
  47. })
  48. })
  49. for (const example of EXAMPLES) {
  50. describe(example, function () {
  51. let size, snapshot
  52. before('load snapshot', async function () {
  53. const ctx = await loadContext(example)
  54. size = ctx.size
  55. snapshot = ctx.snapshot
  56. })
  57. before('back fill new snapshot', async function () {
  58. if (snapshot === null) {
  59. console.error('back filling snapshot for', example)
  60. snapshot = await backFillSnapshot(example, size)
  61. }
  62. })
  63. it('should produce the expected xRef table', async function () {
  64. const table = await parseXrefTable(pdfPath(example), size)
  65. expect(table).to.deep.equal(snapshot)
  66. })
  67. })
  68. }
  69. })