Files
tech-log-frontend/tests/unit/realtime/event-codec.test.ts
DongHyeonka c80320dfc7 chore: sync the frontend template from ebea300 to af3d8e6
Carries 359 template commits, tag v0.2.0 plus one documentation commit: the
generator-built feature index with its app, bounded-context and feature
declarations, the src/platform kernel and scale tooling, the 2026-09-24 src
review fixes, and the 2026-09-25 maturity review round with its CHANGELOG
[보안]/[계약] rows. 691 of the 824 template paths are applied, 85 of them
merged with product changes; 127 are not carried.

Product identity is unchanged: name, version 0.1.0, routes, design and the
TechLog domain.

Three decisions worth naming. TechLog stays composed directly, as the last
two syncs recorded; its directory carries a NOT_INSTALLED marker that says so,
and every installed-* composer takes the template's projection with TechLog
added on top. The reference sample is no longer installed: the template now
installs a feature only through feature-module.ts and context-routes.ts,
which load the screens this product deleted, so its three operations, six
schemas and one topic leave the registries, recorded as registry change
evidence. And AppRouter mounts the platform and TechLog routes only; the
template's bounded-context route gateway waits for a generator-installed
feature.

Required fixes: readSubject on the BFF session owner (without it every HTTP
Studio session would close as integration-failed), zod through the facade in
nine TechLog files, noUnusedLocals, and public/config.json written out to
match the local profile. Derived baselines were recomputed after reproducing
both sides' values: CI contract 101 commands / 106 references / 141
artifacts, gate shape c9adcf85; registry approval 2dc6c3a3; the runtime
config schema baseline equals the schema at 8210d48.

Verified here: types, lint, architecture, adapter inventory, CI contract,
i18n, design system, diagnostics, browser security, dev release manifest,
TechLog contract and build pass; 220 component, 155 integration, 15
contract, 418 TechLog, 43 reference-feature, 18 recipe and 46
release-coherence tests pass, and 2381 of 2383 unit tests. Reproduced at
8210d48 first and unchanged: check:registries (byte-identical),
test:runtime-schema, verify:documentation. Brought by the template and
escalated rather than suppressed: check:request-fanout on TechLog's cursor
pagination, check:remediation-ledger (fails upstream too), the shared-chunk
hash test, and the registry-dependent sample removal and second-feature
tests. Playwright suites were not run.

Full record, the five open questions and the follow-ups in
docs/operations/template-sync-2026-09-29.md.

(cherry picked from commit b2170cf265dea45dc9cc05d3c61e627e97f31cdb)
2026-11-12 08:28:54 +09:00

267 lines
8.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
compareRealtimeSequences,
isCanonicalRealtimeSequence,
isRealtimeResumeCursor,
isStrictRealtimeTimestamp,
nextRealtimeSequence,
REALTIME_MAX_SEQUENCE,
} from "../../../src/contracts/realtime-events.ts";
import {
isValidatedRealtimeEventDto,
realtimeDecodeFailureStream,
} from "../../../src/adapters/realtime/event-codec.ts";
import {
STREAM_ID,
TEST_LIMITS,
createTestRealtimeCodec,
createTestRealtimeRegistry,
realtimeEventJson,
realtimeEventValue,
} from "./fixture.ts";
describe("REALTIME_EVENT_V1 codec", () => {
it("accepts an exact registered envelope and snapshots schema output", () => {
const codec = createTestRealtimeCodec();
const result = codec.decode(realtimeEventJson());
expect(result).toMatchObject({
ok: true,
value: {
envelope: {
protocol: "REALTIME_EVENT_V1",
sequence: "1",
recoveryMode: "CURSOR",
resumeCursor: "cursor-00000001",
payload: { value: "changed" },
},
},
});
if (!result.ok) return;
expect(isValidatedRealtimeEventDto(result.value)).toBe(true);
expect(Object.isFrozen(result.value)).toBe(true);
expect(Object.isFrozen(result.value.envelope)).toBe(true);
expect(Object.isFrozen(result.value.envelope.payload)).toBe(true);
expect(result.value.wireBytes).toBeGreaterThan(0);
expect(result.value.fingerprintBytes).toBeGreaterThan(0);
});
it("rejects extra keys, unknown registrations and schema failures", () => {
const codec = createTestRealtimeCodec();
expect(
codec.decode(
JSON.stringify({
...realtimeEventValue(),
arbitrary: "override",
}),
),
).toMatchObject({
ok: false,
error: { kind: "MALFORMED_EVENT", operation: "DECODE" },
});
const unknownStream = codec.decode(
realtimeEventJson({ streamId: "UNKNOWN_STREAM" }),
);
expect(unknownStream).toMatchObject({
ok: false,
error: { kind: "MALFORMED_EVENT" },
});
expect(realtimeDecodeFailureStream(unknownStream)).toBeNull();
// A type added by a newer backend is a version mismatch, not corruption.
const unknownType = codec.decode(
realtimeEventJson({ eventType: "UNKNOWN_EVENT" }),
);
expect(unknownType).toMatchObject({
ok: false,
error: { kind: "PROTOCOL_MISMATCH", operation: "REGISTRY" },
});
expect(realtimeDecodeFailureStream(unknownType)).toBe(STREAM_ID);
const badPayload = codec.decode(
realtimeEventJson({ payload: { value: 42 } }),
);
expect(badPayload).toMatchObject({
ok: false,
error: { kind: "MALFORMED_EVENT" },
});
expect(realtimeDecodeFailureStream(badPayload)).toBe(STREAM_ID);
const duplicateEnvelopeMember =
`{"eventId":"shadowed",${JSON.stringify(
realtimeEventValue(),
).slice(1)}`;
expect(codec.decode(duplicateEnvelopeMember)).toMatchObject({
ok: false,
error: { kind: "MALFORMED_EVENT", operation: "DECODE" },
});
const duplicatePayloadMember = realtimeEventJson().replace(
'"payload":{"value":"changed"}',
'"payload":{"value":"first","\\u0076alue":"changed"}',
);
expect(codec.decode(duplicatePayloadMember)).toMatchObject({
ok: false,
error: { kind: "MALFORMED_EVENT", operation: "DECODE" },
});
});
it("fails closed on version, sequence, timestamp, scope and cursor syntax", () => {
const codec = createTestRealtimeCodec();
expect(
codec.decode(realtimeEventJson({ protocol: "REALTIME_EVENT_V2" })),
).toMatchObject({
ok: false,
error: { kind: "PROTOCOL_MISMATCH" },
});
for (const overrides of [
{ sequence: "01" },
{ sequence: "18446744073709551616" },
{ occurredAt: "2026-02-30T01:02:03Z" },
{ occurredAt: "2026-07-28 01:02:03Z" },
{ scopeBinding: "scope\r\ninjected" },
{ resumeCursor: "" },
{ resumeCursor: "cursor\ninjected" },
{ recoveryMode: "SNAPSHOT_ONLY", resumeCursor: null },
]) {
expect(codec.decode(realtimeEventJson(overrides))).toMatchObject({
ok: false,
error: { kind: "MALFORMED_EVENT" },
});
}
});
it("enforces global, per-stream and payload structure ceilings", () => {
const strictCodec = createTestRealtimeCodec(
createTestRealtimeRegistry({
limits: {
...TEST_LIMITS,
maxEventBytes: 512,
maxPayloadNodes: 1,
},
}),
);
expect(
strictCodec.decode(realtimeEventJson()),
).toMatchObject({
ok: false,
error: { kind: "MALFORMED_EVENT" },
});
const byteBoundCodec = createTestRealtimeCodec(
createTestRealtimeRegistry({
limits: {
...TEST_LIMITS,
maxEventBytes: 512,
},
}),
);
expect(
byteBoundCodec.decode(
realtimeEventJson({ payload: { value: "x".repeat(600) } }),
),
).toMatchObject({
ok: false,
error: { kind: "EVENT_TOO_LARGE" },
});
expect(
createTestRealtimeCodec().decode(
"x".repeat(64 * 1024 + 1),
),
).toMatchObject({
ok: false,
error: { kind: "EVENT_TOO_LARGE" },
});
});
it("supports the exact null-cursor SESSION_REBUILD discriminant", () => {
const registry = createTestRealtimeRegistry({
recovery: {
mode: "SESSION_REBUILD",
rebuildInputId: "referenceRealtimeRebuild",
},
delivery: "EPHEMERAL",
stateBearing: false,
});
const codec = createTestRealtimeCodec(registry);
expect(
codec.decode(
realtimeEventJson({
recoveryMode: "SESSION_REBUILD",
resumeCursor: null,
}),
),
).toMatchObject({
ok: true,
value: {
envelope: {
recoveryMode: "SESSION_REBUILD",
resumeCursor: null,
},
},
});
expect(
codec.decode(
realtimeEventJson({
recoveryMode: "SESSION_REBUILD",
resumeCursor: "synthetic",
}),
),
).toMatchObject({
ok: false,
error: { kind: "MALFORMED_EVENT" },
});
});
it("normalizes semantic identity independently of JSON key order", () => {
const codec = createTestRealtimeCodec();
const value = realtimeEventValue();
const reversed = Object.fromEntries(
Object.entries(value).reverse(),
);
const first = codec.decode(JSON.stringify(value));
const second = codec.decode(JSON.stringify(reversed));
expect(first.ok).toBe(true);
expect(second.ok).toBe(true);
if (!first.ok || !second.ok) return;
expect(first.value.semanticFingerprint).toBe(
second.value.semanticFingerprint,
);
});
it("keeps a fixed-size semantic fingerprint however large the payload", () => {
const codec = createTestRealtimeCodec();
const small = codec.decode(realtimeEventJson());
const large = codec.decode(
realtimeEventJson({ payload: { value: "x".repeat(3_000) } }),
);
expect(small.ok && large.ok).toBe(true);
if (!small.ok || !large.ok) return;
expect(large.value.fingerprintBytes).toBe(
small.value.fingerprintBytes,
);
expect(large.value.fingerprintBytes).toBeLessThanOrEqual(32);
expect(large.value.semanticFingerprint).not.toBe(
small.value.semanticFingerprint,
);
});
it("uses bounded uint64 sequence and header-safe cursor helpers", () => {
expect(isCanonicalRealtimeSequence("0")).toBe(true);
expect(isCanonicalRealtimeSequence(REALTIME_MAX_SEQUENCE)).toBe(true);
expect(isCanonicalRealtimeSequence("00")).toBe(false);
expect(isCanonicalRealtimeSequence("18446744073709551616")).toBe(false);
expect(compareRealtimeSequences("9", "10")).toBe(-1);
expect(nextRealtimeSequence("9")).toBe("10");
expect(nextRealtimeSequence(REALTIME_MAX_SEQUENCE)).toBeNull();
expect(isRealtimeResumeCursor("cursor:/+=._~-")).toBe(true);
expect(isRealtimeResumeCursor("cursor\nunsafe")).toBe(false);
expect(isStrictRealtimeTimestamp("2024-02-29T23:59:59Z")).toBe(true);
expect(isStrictRealtimeTimestamp("2023-02-29T23:59:59Z")).toBe(false);
});
});