pdfjsTests.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. describe('when the operation times out', function () {
  50. it('should bail out', async function () {
  51. const path = pdfPath(EXAMPLES[0])
  52. const { size } = await loadContext(EXAMPLES[0])
  53. const err = new Error()
  54. let table
  55. try {
  56. table = await parseXrefTable(path, size, () => {
  57. throw err
  58. })
  59. } catch (e) {
  60. expect(e).to.equal(err)
  61. return
  62. }
  63. expect(table).to.not.exist
  64. })
  65. })
  66. for (const example of EXAMPLES) {
  67. describe(example, function () {
  68. let size, snapshot
  69. before('load snapshot', async function () {
  70. const ctx = await loadContext(example)
  71. size = ctx.size
  72. snapshot = ctx.snapshot
  73. })
  74. before('back fill new snapshot', async function () {
  75. if (snapshot === null) {
  76. console.error('back filling snapshot for', example)
  77. snapshot = await backFillSnapshot(example, size)
  78. }
  79. })
  80. it('should produce the expected xRef table', async function () {
  81. const table = await parseXrefTable(pdfPath(example), size, () => {})
  82. expect(table).to.deep.equal(snapshot)
  83. })
  84. })
  85. }
  86. })