Files
faunosteo/packages/pdf/tests/reexport.test.ts
T

84 lines
2.6 KiB
TypeScript

/**
* T1 (STEP6 §5, §7): an archive re-exports from its own envelope, with no
* sheet, no network and no configuration: same drawn text, same images,
* same view facts, same envelope but the export time.
*/
import { afterAll, describe, expect, it, vi } from 'vitest'
import {
buildSheetSnapshot,
ReportSchema,
type ReportArchiveEnvelope,
} from '@faunosteo/domain'
import {
defaultPdfLabels,
exportFromEnvelope,
exportReportArchive,
readReportArchive,
type ExportResult,
} from '../src'
import { loadDraftSheet, readFixtureJson } from './helpers/draft'
import {
FIXED_EXPORTED_AT,
logosBranding,
rasterizerOf,
recordDrawing,
snapshotViews,
type DrawRecording,
} from './helpers/goldens'
afterAll(() => vi.restoreAllMocks())
type Recorded = ExportResult & Pick<DrawRecording, 'texts' | 'images'>
async function recorded(run: () => Promise<ExportResult>): Promise<Recorded> {
const recording = recordDrawing()
try {
const result = await run()
return { ...result, texts: recording.texts, images: recording.images }
} finally {
recording.stop()
}
}
const withoutDate = ({ exportedAt: _e, ...rest }: ReportArchiveEnvelope) => rest
describe('re-export from the envelope alone', () => {
it('reproduces text, images, views and the envelope minus exportedAt', async () => {
// The draft is known here only, for the first export.
const draft = loadDraftSheet()
const report = ReportSchema.parse(readFixtureJson('report-v3.demo.json'))
const first = await recorded(() =>
exportReportArchive({
report,
sheet: buildSheetSnapshot(draft, report),
branding: logosBranding(),
labels: defaultPdfLabels,
locale: 'fr',
rasterizer: rasterizerOf('logos'),
exportedAt: FIXED_EXPORTED_AT,
}),
)
const outcome = await readReportArchive(first.pdf, draft.migrationMap)
expect(outcome.origin).toBe('envelope_v1')
const archive = outcome.archive!
// From here on, only the archive is used.
const later = new Date('2027-01-01T00:00:00.000Z')
const second = await recorded(() =>
exportFromEnvelope(archive, {
locale: 'fr',
rasterizer: rasterizerOf('logos'),
exportedAt: later,
}),
)
expect(second.warnings).toEqual([])
expect(second.texts).toEqual(first.texts)
expect(second.images).toEqual(first.images)
expect(
snapshotViews(second.envelope.sheetSnapshot, second.envelope.report),
).toEqual(snapshotViews(first.envelope.sheetSnapshot, report))
expect(second.envelope.exportedAt).toBe(later.toISOString())
expect(withoutDate(second.envelope)).toEqual(withoutDate(first.envelope))
})
})