repositories / will
will
owned by admin
src/shared/contracts.snapshot.ts
Rawimport assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { test } from 'node:test'
import { fileURLToPath } from 'node:url'
import type { ChatViewRow, ServiceStatus, TimelineRow } from './api.ts'
import type { AnalysisRecord, AuditEvent, ChatMessageIn, DeploymentEventMirror } from './events.ts'
import type { SegmentSidecar } from './retention.ts'
/**
* Snapshot tier (spec AC23): pin serialization contracts against a committed
* golden file with deterministic values only (fixed ids, clocks, digests).
* A golden change is a wire-contract change and needs a compatibility note.
*/
const here = fileURLToPath(new URL('.', import.meta.url))
const fixedEvent: AuditEvent = {
id: '00000000-0000-4000-8000-000000000001',
ts: '2026-09-12T00:00:00.000Z',
kind: 'tool_execution_start',
sessionId: 'session-1',
provenance: { revision: 'rev-9', worktreeDirty: false },
payload: { tool: 'bash', args: { command: 'ls' } },
}
const fixedChat: ChatMessageIn = {
id: 'tg-1',
chatId: -100,
messageId: 42,
direction: 'in',
ts: '2026-09-12T00:00:00.000Z',
updateId: 7,
senderId: '5',
senderName: 'friend',
replyToMessageId: 41,
triggerReason: 'mention',
text: 'hello',
media: [{ digest: 'a'.repeat(64), type: 'image/png', size: 12 }],
}
const fixedTimelineRow: TimelineRow = {
seq: 3,
id: fixedEvent.id,
ts: fixedEvent.ts,
kind: fixedEvent.kind,
sessionId: fixedEvent.sessionId,
provenance: fixedEvent.provenance,
payload: fixedEvent.payload,
archived: false,
}
const fixedChatRow: ChatViewRow = {
id: fixedChat.id,
chatId: fixedChat.chatId,
messageId: fixedChat.messageId,
direction: 'in',
ts: fixedChat.ts,
senderId: fixedChat.senderId,
senderName: fixedChat.senderName,
replyToMessageId: fixedChat.replyToMessageId,
triggerReason: 'mention',
text: fixedChat.text,
media: fixedChat.media,
hidden: false,
archived: true,
}
const fixedDeploy: DeploymentEventMirror = {
id: 'dep-1',
ts: '2026-09-12T00:00:00.000Z',
kind: 'probation-failed',
revision: 'rev-9',
detail: { probe: 'healthz', misses: 3 },
}
const fixedSidecar: SegmentSidecar = {
version: 1,
kind: 'audit',
month: '2026-09',
segment: 'seg-1',
count: 2,
firstId: 'a',
lastId: 'b',
firstTs: '2026-09-01T00:00:00.000Z',
lastTs: '2026-09-02T00:00:00.000Z',
sha256: '0'.repeat(64),
bytes: 123,
}
const fixedAnalysis: AnalysisRecord = {
digest: 'b'.repeat(64),
capability: 'zai_analyze_media',
model: 'glm-5.3-flash',
schemaVersion: 1,
result: { answer: 'a cat' },
createdAt: '2026-09-12T00:00:00.000Z',
}
const fixedStatus: ServiceStatus = {
ready: true,
schemaVersion: 1,
lastAuditSeq: 9,
counts: { audit: 1, chat: 2, deployments: 3, media: 4 },
dbSizeBytes: 1024,
storage: { freeBytes: 1, totalBytes: 2, freePct: 50 },
storageState: 'ok',
}
const actual = {
auditEvent: fixedEvent,
chatMessage: fixedChat,
timelineRow: fixedTimelineRow,
chatViewRow: fixedChatRow,
deploymentEvent: fixedDeploy,
archiveSidecar: fixedSidecar,
mediaAnalysis: fixedAnalysis,
serviceStatus: fixedStatus,
}
test('snapshot: wire contracts match the committed golden file', async () => {
const golden = JSON.parse(await readFile(join(here, 'contracts.golden.json'), 'utf8')) as Record<
string,
unknown
>
assert.deepEqual(
JSON.parse(JSON.stringify(actual)),
golden,
'wire contract drifted: update the golden with a compatibility note',
)
})
test('snapshot: JSON serialization is stable across round trips', () => {
for (const [name, value] of Object.entries(actual)) {
const wire = JSON.stringify(value)
const back = JSON.parse(wire)
assert.deepEqual(back, value, `${name} round-trip drift`)
}
})