184 lines
6.3 KiB
TypeScript
184 lines
6.3 KiB
TypeScript
/**
|
|
* T4 T5 T6 (STEP6 §4, §7, §14 "Read"): roundtrip, the frozen v2 PDFs and
|
|
* the 612-leaf legacy report, the bare v3 fixture, wrapper and report
|
|
* versions, stripped attachments, attachment selection, size cap.
|
|
*/
|
|
import { beforeAll, describe, expect, it } from 'vitest'
|
|
import {
|
|
buildSheetSnapshot,
|
|
type Report,
|
|
type ReportArchiveEnvelope,
|
|
} from '@faunosteo/domain'
|
|
import {
|
|
ArchiveReadError,
|
|
defaultPdfLabels,
|
|
exportReportArchive,
|
|
MAX_PDF_BYTES,
|
|
readReportArchive,
|
|
type ExportResult,
|
|
} from '../src'
|
|
import { readFixtureBytes } from './helpers/draft'
|
|
import {
|
|
FIXED_EXPORTED_AT,
|
|
nologosBranding,
|
|
rasterizerOf,
|
|
} from './helpers/goldens'
|
|
import { pdfWithAttachment, pdfWithoutAttachment } from './helpers/pdf'
|
|
import {
|
|
allLeavesMigrated,
|
|
loadGoldenReports,
|
|
type GoldenReports,
|
|
} from './helpers/reports'
|
|
|
|
let golden: GoldenReports
|
|
const map = () => golden.draft.migrationMap
|
|
|
|
beforeAll(() => {
|
|
golden = loadGoldenReports()
|
|
})
|
|
|
|
function exportOnDraft(report: Report): Promise<ExportResult> {
|
|
return exportReportArchive({
|
|
report,
|
|
sheet: buildSheetSnapshot(golden.draft, report),
|
|
branding: nologosBranding(),
|
|
labels: defaultPdfLabels,
|
|
locale: 'fr',
|
|
rasterizer: rasterizerOf('nologos'),
|
|
exportedAt: FIXED_EXPORTED_AT,
|
|
})
|
|
}
|
|
|
|
const unresolvedFindings = (report: Report) =>
|
|
report.observations.flatMap((o) => o.findings).filter((f) => f.unresolved)
|
|
.length
|
|
|
|
async function rejects(
|
|
bytes: Uint8Array,
|
|
expected: Partial<ArchiveReadError>,
|
|
): Promise<void> {
|
|
const promise = readReportArchive(bytes, map())
|
|
await expect(promise).rejects.toBeInstanceOf(ArchiveReadError)
|
|
await expect(promise).rejects.toMatchObject(expected)
|
|
}
|
|
|
|
describe('T4 roundtrip', () => {
|
|
it('reads back the exported envelope whole', async () => {
|
|
const { pdf, envelope } = await exportOnDraft(golden.reports.demo)
|
|
const outcome = await readReportArchive(pdf, map())
|
|
expect(outcome.origin).toBe('envelope_v1')
|
|
expect(outcome.migrated).toBe(false)
|
|
expect(outcome.snapshotRejected).toBeUndefined()
|
|
expect(outcome.report).toEqual(golden.reports.demo)
|
|
expect(outcome.archive).toEqual(envelope)
|
|
})
|
|
})
|
|
|
|
describe('T5 v2 archives and the bare v3 fixture', () => {
|
|
it.each(['report-v2.full.pdf', 'report-v2.minimal.pdf'])(
|
|
'%s: migrated, re-exported, imported again equal',
|
|
async (name) => {
|
|
const first = await readReportArchive(readFixtureBytes(name), map())
|
|
expect(first.origin).toBe('v2')
|
|
expect(first.migrated).toBe(true)
|
|
expect(first.archive).toBeNull()
|
|
expect(first.report.version).toBe(3)
|
|
const { pdf } = await exportOnDraft(first.report)
|
|
const again = await readReportArchive(pdf, map())
|
|
expect(again.origin).toBe('envelope_v1')
|
|
expect(again.report).toEqual(first.report)
|
|
},
|
|
)
|
|
|
|
it('keeps the 29 unresolved findings of the 612-leaf report through export and import', async () => {
|
|
const migrated = allLeavesMigrated(golden.draft)
|
|
expect(migrated.unresolvedFindings).toBe(29)
|
|
expect(unresolvedFindings(migrated.report)).toBe(29)
|
|
const { pdf } = await exportOnDraft(migrated.report)
|
|
const again = await readReportArchive(pdf, map())
|
|
expect(unresolvedFindings(again.report)).toBe(29)
|
|
expect(again.report).toEqual(migrated.report)
|
|
})
|
|
|
|
it('reads report-v3.bare.pdf as a bare v3 with no archive', async () => {
|
|
const outcome = await readReportArchive(
|
|
readFixtureBytes('report-v3.bare.pdf'),
|
|
map(),
|
|
)
|
|
expect(outcome.origin).toBe('bare_v3')
|
|
expect(outcome.archive).toBeNull()
|
|
expect(outcome.snapshotRejected).toBeUndefined()
|
|
expect(outcome.report).toEqual(golden.reports.demo)
|
|
})
|
|
})
|
|
|
|
describe('T6 versions', () => {
|
|
let envelope: ReportArchiveEnvelope
|
|
beforeAll(async () => {
|
|
envelope = (await exportOnDraft(golden.reports.demo)).envelope
|
|
})
|
|
|
|
it('envelope v2 wrapper: report kept, snapshot rejected as unsupported_envelope', async () => {
|
|
const bytes = await pdfWithAttachment('osteo-report.json', {
|
|
...envelope,
|
|
envelopeVersion: 2,
|
|
})
|
|
const outcome = await readReportArchive(bytes, map())
|
|
expect(outcome.report).toEqual(golden.reports.demo)
|
|
expect(outcome.archive).toBeNull()
|
|
expect(outcome.snapshotRejected).toBe('unsupported_envelope')
|
|
})
|
|
|
|
it('envelope v1 with a report v4: incompatible_version 4', async () => {
|
|
const bytes = await pdfWithAttachment('osteo-report.json', {
|
|
...envelope,
|
|
report: { ...envelope.report, version: 4 },
|
|
})
|
|
await rejects(bytes, { kind: 'incompatible_version', foundVersion: 4 })
|
|
})
|
|
|
|
it('bare report version 7: incompatible_version 7', async () => {
|
|
const bytes = await pdfWithAttachment('osteo-report.json', { version: 7 })
|
|
await rejects(bytes, { kind: 'incompatible_version', foundVersion: 7 })
|
|
})
|
|
})
|
|
|
|
describe('attachment presence and selection', () => {
|
|
it('a stamped PDF without attachment: attachment_stripped', async () => {
|
|
const bytes = await pdfWithoutAttachment(['faunosteo-archive/1'])
|
|
await rejects(bytes, { kind: 'attachment_stripped' })
|
|
})
|
|
|
|
it('a stamped PDF saved with object streams (pre-BLK-009 layout): the stamp is invisible, no_attachment', async () => {
|
|
const bytes = await pdfWithoutAttachment(['faunosteo-archive/1'], true)
|
|
await rejects(bytes, { kind: 'no_attachment' })
|
|
})
|
|
|
|
it('a foreign PDF: no_attachment', async () => {
|
|
await rejects(await pdfWithoutAttachment(), { kind: 'no_attachment' })
|
|
const foreign = await pdfWithAttachment('other.json', { hello: 'world' })
|
|
await rejects(foreign, { kind: 'no_attachment' })
|
|
})
|
|
|
|
it('the Filespec named osteo-report.json wins over an earlier EmbeddedFile', async () => {
|
|
const bytes = await pdfWithAttachment(
|
|
'osteo-report.json',
|
|
golden.reports.demo,
|
|
{ before: ['other.json', { hello: 'world', version: 2 }] },
|
|
)
|
|
const outcome = await readReportArchive(bytes, map())
|
|
expect(outcome.origin).toBe('bare_v3')
|
|
expect(outcome.report).toEqual(golden.reports.demo)
|
|
})
|
|
|
|
it('refuses an oversized file or one without a %PDF- header as invalid_pdf', async () => {
|
|
await rejects(new Uint8Array(MAX_PDF_BYTES + 1), { kind: 'invalid_pdf' })
|
|
await rejects(new TextEncoder().encode('not a pdf at all'), {
|
|
kind: 'invalid_pdf',
|
|
})
|
|
const late = new Uint8Array(2048)
|
|
late.set(new TextEncoder().encode('%PDF-1.7'), 1024)
|
|
await rejects(late, { kind: 'invalid_pdf' })
|
|
})
|
|
})
|