Files
faunosteo/packages/domain/tests/archive.envelope.test.ts
T

212 lines
7.1 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
ENVELOPE_FORMAT,
ENVELOPE_VERSION,
ReportArchiveEnvelopeSchema,
UnsupportedReportVersionError,
buildSheetSnapshot,
migrateReportV2ToV3,
readArchiveDocument,
readReportDocument,
type ReportArchiveEnvelope,
} from '../src'
import fullV2 from './fixtures/report-v2.full.json'
import { sampleBranding, sampleLabels } from './archive.branding-labels.test'
import { loadDraftSheet } from './helpers/draft-sheet'
import {
leavesAsV2Report,
legacyLeaves,
loadLegacyBaseline,
} from './helpers/legacy-leaves'
const draft = loadDraftSheet()
const map = draft.migrationMap
const { report } = readReportDocument(fullV2, map)
/** A fresh envelope as JSON.parse of an attachment would give it. */
function envelope(): ReportArchiveEnvelope {
const env: ReportArchiveEnvelope = {
format: ENVELOPE_FORMAT,
envelopeVersion: ENVELOPE_VERSION,
exportedAt: '2026-10-11T10:00:00.000Z',
locale: 'fr',
report,
sheetSnapshot: buildSheetSnapshot(draft, report),
branding: sampleBranding(),
labels: { fr: sampleLabels(), en: sampleLabels() },
}
return JSON.parse(JSON.stringify(env)) as ReportArchiveEnvelope
}
type Raw = Record<string, unknown>
const raw = (env: ReportArchiveEnvelope): Raw => env as unknown as Raw
describe('readArchiveDocument: bare reports', () => {
it('reads a bare v3 as is', () => {
const outcome = readArchiveDocument(JSON.parse(JSON.stringify(report)), map)
expect(outcome).toEqual({
report,
origin: 'bare_v3',
migrated: false,
unresolvedZones: 0,
unresolvedFindings: 0,
archive: null,
})
})
it('migrates a bare v2 with the counts of the migration', () => {
const outcome = readArchiveDocument(fullV2, map)
expect(outcome.origin).toBe('v2')
expect(outcome.migrated).toBe(true)
expect(outcome.archive).toBeNull()
expect(outcome.report).toEqual(report)
expect(outcome.snapshotRejected).toBeUndefined()
const leaves = leavesAsV2Report(legacyLeaves(loadLegacyBaseline()))
const expected = migrateReportV2ToV3(leaves, map)
const migrated = readArchiveDocument(leaves, map)
expect(expected.unresolvedFindings).toBeGreaterThan(0)
expect(migrated.unresolvedFindings).toBe(expected.unresolvedFindings)
expect(migrated.unresolvedZones).toBe(expected.unresolvedZones)
expect(migrated.report).toEqual(expected.report)
})
it('throws for a report version it cannot read, or a non-object', () => {
expect(() => readArchiveDocument({ ...report, version: 1 }, map)).toThrow(
UnsupportedReportVersionError,
)
for (const data of [null, undefined, 'report', 42, [report]]) {
expect(() => readArchiveDocument(data, map)).toThrow(
UnsupportedReportVersionError,
)
}
})
})
describe('readArchiveDocument: envelopes', () => {
it('accepts a valid v1 envelope whole', () => {
const env = envelope()
const outcome = readArchiveDocument(env, map)
expect(outcome.origin).toBe('envelope_v1')
expect(outcome.migrated).toBe(false)
expect(outcome.unresolvedZones).toBe(0)
expect(outcome.snapshotRejected).toBeUndefined()
expect(outcome.report).toEqual(report)
expect(outcome.archive).toEqual(env)
expect(ReportArchiveEnvelopeSchema.parse(env)).toEqual(env)
})
it('keeps the report of an envelope version it does not know', () => {
const outcome = readArchiveDocument(
{ ...envelope(), envelopeVersion: 2 },
map,
)
expect(outcome.report).toEqual(report)
expect(outcome.origin).toBe('bare_v3')
expect(outcome.archive).toBeNull()
expect(outcome.snapshotRejected).toBe('unsupported_envelope')
})
it('names the report version when the wrapped report is unknown', () => {
const env = raw(envelope())
env.report = { ...report, version: 4 }
let thrown: unknown
try {
readArchiveDocument(env, map)
} catch (error) {
thrown = error
}
expect(thrown).toBeInstanceOf(UnsupportedReportVersionError)
expect((thrown as UnsupportedReportVersionError).foundVersion).toBe(4)
})
it('ignores unknown metadata keys, __proto__ included, within v1', () => {
const env = raw(envelope())
env.futureField = { anything: true }
const outcome = readArchiveDocument(env, map)
expect(outcome.archive).not.toBeNull()
expect('futureField' in outcome.archive!).toBe(false)
const text = JSON.stringify(envelope()).replace(
'"format"',
'"__proto__":{"polluted":true},"format"',
)
const polluted = readArchiveDocument(JSON.parse(text), map)
expect(polluted.archive).not.toBeNull()
expect(({} as { polluted?: boolean }).polluted).toBeUndefined()
expect(Object.hasOwn(polluted.archive!, '__proto__')).toBe(false)
})
it('drops the envelope on a wrapper fault (schema): branding, labels, metadata', () => {
const branding = raw(envelope())
branding.branding = { ...sampleBranding(), siteUrl: 'javascript:alert(1)' }
expect(readArchiveDocument(branding, map)).toMatchObject({
report,
origin: 'envelope_v1',
archive: null,
snapshotRejected: 'schema',
})
const labels = raw(envelope())
labels.labels = { fr: sampleLabels() }
expect(readArchiveDocument(labels, map).snapshotRejected).toBe('schema')
const locale = raw(envelope())
locale.locale = 'de'
expect(readArchiveDocument(locale, map).snapshotRejected).toBe('schema')
const format = raw(envelope())
format.format = 'other'
expect(readArchiveDocument(format, map).snapshotRejected).toBe('schema')
})
it('drops a snapshot over the caps or failing its own schema', () => {
const caps = envelope()
caps.sheetSnapshot.document.name.fr = 'x'.repeat(513)
expect(readArchiveDocument(caps, map)).toMatchObject({
report,
archive: null,
snapshotRejected: 'caps',
})
const schema = raw(envelope())
schema.sheetSnapshot = { ...envelope().sheetSnapshot, extra: 1 }
expect(readArchiveDocument(schema, map).snapshotRejected).toBe('schema')
const missing = raw(envelope())
delete missing.sheetSnapshot
expect(readArchiveDocument(missing, map).snapshotRejected).toBe('schema')
const integrity = envelope()
integrity.sheetSnapshot.assets = []
expect(readArchiveDocument(integrity, map).snapshotRejected).toBe(
'integrity',
)
})
it('drops a snapshot of another sheet than the report (sheet_mismatch)', () => {
const sha = envelope()
sha.sheetSnapshot.ref.documentSha256 = 'f'.repeat(64)
expect(readArchiveDocument(sha, map)).toMatchObject({
report,
origin: 'envelope_v1',
archive: null,
snapshotRejected: 'sheet_mismatch',
})
const id = envelope()
id.sheetSnapshot.ref.sheetId = 'another-sheet'
expect(readArchiveDocument(id, map).snapshotRejected).toBe('sheet_mismatch')
})
it('migrates a v2 report found inside an envelope and still checks the snapshot', () => {
const env = raw(envelope())
env.report = fullV2
const outcome = readArchiveDocument(env, map)
expect(outcome.origin).toBe('envelope_v1')
expect(outcome.migrated).toBe(true)
expect(outcome.report).toEqual(report)
expect(outcome.archive?.report).toEqual(report)
})
})