Files
hapi/web/scripts/fixtures/generate.ts
T
weishu e1bfdd6265 feat(fixtures): SSE patch + pagination scenario fixtures (K7)
Two new golden-fixture suites, expectations machine-generated from the web
implementation (same source-of-truth principle as the chat suite):

- shared/fixtures/sse/ (12 cases): session-updated versioned-patch
  application via applySessionDetailPatch — strict version gates for
  metadata/agentState/todos/teamState, out-of-order arrival, teamState:null
  clear, max-monotonic updatedAt, flat last-write-wins fields, sub-minute
  activeAt keep-alive drop, scratchlistUpdatedAt trigger, and the pinned
  actual behavior that activeTurnStartedAt is NOT applied by the patch path.
  Inputs are stored schema-normalized and validated against SessionSchema /
  strict SessionPatchSchema at generation time; per-patch applied/unchanged
  verdicts are part of the contract.

- shared/fixtures/pagination/ (11 cases): op scripts driving the real
  message-window store with a scripted ApiClient — latest page + SSE ingest,
  before-cursor older pages, epoch-mismatch reset (window discard + recorded
  internal latest request), reset:true replace preserving optimistic rows,
  localId echo reconciliation, messages-consumed invokedAt stamping (no
  cursor advance), message-cancelled removal, cancel-too-late invoked-row
  ingest, hidden-row cursor advance, 400-row trim preserving queued rows,
  and queued-state gap recovery. Documents pin the exact requests the store
  issued, older-load outcomes, reconcile candidates, and a minimal window
  projection (ids/order, queued/optimistic flags, hasMore, epoch, viewMode,
  compound cursors).

Generator: web/scripts/fixtures/{sse,pagination}/ extend the K5 framework
(canonical serialization reused; suites pruned of stale files). Web
self-conformance tests replay every fixture against the real implementation
in bun run test:web. README documents schemas, projections, replay
contracts, and native consumption for both suites.

No time injection needed: the store's only Date.now() reads gate
notification throttling, which never reaches persisted or projected state.
2026-08-17 12:06:57 +08:00

87 lines
3.4 KiB
TypeScript

import { mkdirSync, readdirSync, unlinkSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { fixtureCases } from './cases'
import { FIXTURE_VERSION, toFixtureInput, type FixtureCase, type FixtureDocument, type FixtureInput } from './fixtureTypes'
import { buildPaginationFixtureDocument } from './pagination/build'
import { paginationFixtureCases } from './pagination/cases'
import { runFixturePipeline } from './pipeline'
import { toCanonicalJson } from './serialize'
import { buildSseFixtureDocument } from './sse/build'
import { sseFixtureCases } from './sse/cases'
const FIXTURES_DIR = fileURLToPath(new URL('../../../shared/fixtures', import.meta.url))
export function buildFixtureDocument(fixtureCase: FixtureCase): FixtureDocument {
// Round-trip the input through canonical JSON before running the pipeline,
// so `expected` is computed from exactly the bytes the file will carry —
// a consumer re-running `input` can never see values the generator had
// but the stored JSON does not (e.g. authored `undefined`).
const input = JSON.parse(toCanonicalJson(toFixtureInput(fixtureCase))) as FixtureInput
return {
fixtureVersion: FIXTURE_VERSION,
name: fixtureCase.name,
description: fixtureCase.description,
input,
expected: runFixturePipeline(input)
}
}
type SuiteWriter = {
dir: string
names: Set<string>
}
function openSuite(suite: string): SuiteWriter {
const dir = join(FIXTURES_DIR, suite)
mkdirSync(dir, { recursive: true })
return { dir, names: new Set() }
}
function writeSuiteDocument(writer: SuiteWriter, name: string, document: unknown): void {
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name)) {
throw new Error(`Fixture name must be kebab-case: ${name}`)
}
if (writer.names.has(name)) {
throw new Error(`Duplicate fixture name: ${name}`)
}
writer.names.add(name)
writeFileSync(join(writer.dir, `${name}.json`), toCanonicalJson(document))
}
// Remove stale fixtures from renamed/deleted cases so natives never keep
// passing against a file the web pipeline no longer generates.
function pruneSuite(writer: SuiteWriter): void {
for (const entry of readdirSync(writer.dir)) {
if (entry.endsWith('.json') && !writer.names.has(entry.slice(0, -'.json'.length))) {
unlinkSync(join(writer.dir, entry))
}
}
}
export async function generateAllFixtures(): Promise<void> {
const chat = openSuite('chat')
for (const fixtureCase of fixtureCases) {
writeSuiteDocument(chat, fixtureCase.name, buildFixtureDocument(fixtureCase))
}
pruneSuite(chat)
const sse = openSuite('sse')
for (const fixtureCase of sseFixtureCases) {
writeSuiteDocument(sse, fixtureCase.name, buildSseFixtureDocument(fixtureCase))
}
pruneSuite(sse)
const pagination = openSuite('pagination')
for (const fixtureCase of paginationFixtureCases) {
writeSuiteDocument(pagination, fixtureCase.name, await buildPaginationFixtureDocument(fixtureCase))
}
pruneSuite(pagination)
writeFileSync(join(FIXTURES_DIR, 'VERSION'), `${FIXTURE_VERSION}\n`)
console.log(
`Wrote ${chat.names.size} chat + ${sse.names.size} sse + ${pagination.names.size} pagination fixtures `
+ `(fixtureVersion ${FIXTURE_VERSION}) to ${FIXTURES_DIR}`
)
}