2021-05-31 04:20:25 -04:00
|
|
|
const fs = require('fs')
|
|
|
|
const Path = require('path')
|
|
|
|
const { expect } = require('chai')
|
2022-08-02 05:09:22 -04:00
|
|
|
const { parseXrefTable } = require('../../../app/js/XrefParser')
|
|
|
|
const { NoXrefTableError } = require('../../../app/js/Errors')
|
2021-05-31 04:20:25 -04:00
|
|
|
const PATH_EXAMPLES = 'test/acceptance/fixtures/examples/'
|
2021-07-26 07:14:56 -04:00
|
|
|
const PATH_SNAPSHOTS = 'test/unit/js/snapshots/pdfjs/'
|
2021-05-31 04:20:25 -04:00
|
|
|
const EXAMPLES = fs.readdirSync(PATH_EXAMPLES)
|
|
|
|
|
|
|
|
function snapshotPath(example) {
|
|
|
|
return Path.join(PATH_SNAPSHOTS, example, 'XrefTable.json')
|
|
|
|
}
|
|
|
|
|
|
|
|
function pdfPath(example) {
|
|
|
|
return Path.join(PATH_EXAMPLES, example, 'output.pdf')
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadContext(example) {
|
|
|
|
const size = (await fs.promises.stat(pdfPath(example))).size
|
|
|
|
|
|
|
|
let blob
|
|
|
|
try {
|
|
|
|
blob = await fs.promises.readFile(snapshotPath(example))
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code !== 'ENOENT') {
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const snapshot = blob ? JSON.parse(blob) : null
|
|
|
|
return {
|
|
|
|
size,
|
2021-07-13 07:04:48 -04:00
|
|
|
snapshot,
|
2021-05-31 04:20:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function backFillSnapshot(example, size) {
|
2021-06-23 09:14:28 -04:00
|
|
|
const table = await parseXrefTable(pdfPath(example), size, () => {})
|
2021-05-31 04:20:25 -04:00
|
|
|
await fs.promises.mkdir(Path.dirname(snapshotPath(example)), {
|
2021-07-13 07:04:48 -04:00
|
|
|
recursive: true,
|
2021-05-31 04:20:25 -04:00
|
|
|
})
|
|
|
|
await fs.promises.writeFile(
|
|
|
|
snapshotPath(example),
|
|
|
|
JSON.stringify(table, null, 2)
|
|
|
|
)
|
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('pdfjs', function () {
|
|
|
|
describe('when the pdf is an empty file', function () {
|
|
|
|
it('should yield no entries', async function () {
|
|
|
|
const path = 'does/not/matter.pdf'
|
2021-06-23 09:14:28 -04:00
|
|
|
let table
|
|
|
|
try {
|
2022-08-02 05:09:22 -04:00
|
|
|
table = await parseXrefTable(path, 0)
|
2021-06-23 09:14:28 -04:00
|
|
|
} catch (e) {
|
2022-08-02 05:09:22 -04:00
|
|
|
expect(e).to.be.an.instanceof(NoXrefTableError)
|
2021-06-23 09:14:28 -04:00
|
|
|
}
|
|
|
|
expect(table).to.not.exist
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-05-31 04:20:25 -04:00
|
|
|
for (const example of EXAMPLES) {
|
|
|
|
describe(example, function () {
|
|
|
|
let size, snapshot
|
|
|
|
before('load snapshot', async function () {
|
|
|
|
const ctx = await loadContext(example)
|
|
|
|
size = ctx.size
|
|
|
|
snapshot = ctx.snapshot
|
|
|
|
})
|
|
|
|
|
|
|
|
before('back fill new snapshot', async function () {
|
|
|
|
if (snapshot === null) {
|
|
|
|
console.error('back filling snapshot for', example)
|
|
|
|
snapshot = await backFillSnapshot(example, size)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should produce the expected xRef table', async function () {
|
2021-06-23 09:14:28 -04:00
|
|
|
const table = await parseXrefTable(pdfPath(example), size, () => {})
|
2022-08-02 05:09:22 -04:00
|
|
|
// compare the essential parts of the xref table only
|
|
|
|
expect(table.xRefEntries[0]).to.include({ offset: 0 })
|
|
|
|
expect(table.xRefEntries.slice(1)).to.deep.equal(
|
|
|
|
snapshot.xRefEntries
|
|
|
|
.slice(1)
|
|
|
|
.filter(xref => xref.uncompressed) // we only use the uncompressed fields
|
|
|
|
.map(xref => {
|
|
|
|
return { offset: xref.offset, uncompressed: xref.uncompressed } // ignore unused gen field
|
|
|
|
})
|
|
|
|
)
|
2021-05-31 04:20:25 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|