// The realtime frame codec, pinned rule by rule against the C# reference. // // The bytes themselves are pinned by two committed files under // `contracts/cultmesh/`. `realtime-frame-vectors.json` is written by // `CultMeshRealtimeWireProtocolTests.RealtimeFrameVectorsAreSharedWithTypeScript` // under CULTMESH_WRITE_VECTORS=1: this side decodes it and re-encodes to the // same bytes. `realtime-frame-vectors.ts-written.json` is written by // `scripts/write-cultmesh-realtime-frame-vectors.mjs` with this codec and // decoded by the C# reference, so parity is proven in both directions and in // two processes. A fixture authored on one side alone would only agree with // itself, which is what the authority vectors already taught. import test from "node:test"; import assert from "node:assert/strict"; import { createHash } from "node:crypto"; import { readFileSync } from "node:fs"; import { join } from "node:path"; import { documentPayloadBytes, documentPayloadFromBytes } from "@gamecult/cultcache-ts"; import { CULTMESH_REALTIME_ALPN, CULTMESH_REALTIME_CONNECTION_CLOSE_CODE, CULTMESH_REALTIME_LATEST_ONLY_STREAM, CULTMESH_REALTIME_MAX_ENCODED_FRAME_BYTES, CULTMESH_REALTIME_MAX_PAYLOAD_BYTES, CULTMESH_REALTIME_RELIABLE_STREAM, CULTMESH_REALTIME_STREAM_ABORT_CODE, decodeRealtimeFrame, encodeRealtimeFrame, type CultMeshRealtimeDelivery, type CultMeshRealtimeFrame, } from "../src/realtime-wire"; const CONTRACTS = join(__dirname, "..", "..", "..", "..", "contracts", "cultmesh"); const CSHARP_VECTORS = join(CONTRACTS, "realtime-frame-vectors.json"); const TS_VECTORS = join(CONTRACTS, "realtime-frame-vectors.ts-written.json"); interface IdentityRule { repeat: string; byteLength: number } interface RampRule { ramp: number } interface VectorFields { channelId: string | IdentityRule; schemaId: string | IdentityRule; bodyId: string | IdentityRule; producerEpoch: number; sequence: number; delivery: CultMeshRealtimeDelivery; payload: string | RampRule; } interface Vectors { frames: { label: string; fields: VectorFields; encoding: string | null; encodedLength: number; sha256OfEncoding: string; }[]; malformed: { label: string; encoding: string; message: string }[]; } /** The repeat unit written whole until the UTF-8 byte length is exactly `byteLength`. */ function repeatToByteLength(unit: string, byteLength: number): string { const unitBytes = Buffer.byteLength(unit, "utf8"); assert.equal(byteLength % unitBytes, 0, `'${unit}' is ${unitBytes} UTF-8 bytes and does not divide ${byteLength}`); return unit.repeat(byteLength / unitBytes); } /** The deterministic payload both runtimes build the same way. */ function ramp(length: number): Uint8Array { const bytes = new Uint8Array(length); for (let index = 0; index < length; index += 1) bytes[index] = index % 251; return bytes; } function identity(value: string | IdentityRule): string { return typeof value === "string" ? value : repeatToByteLength(value.repeat, value.byteLength); } function payload(value: string | RampRule): Uint8Array { return typeof value === "string" ? new Uint8Array(Buffer.from(value, "base64")) : ramp(value.ramp); } function frameOf(fields: VectorFields): CultMeshRealtimeFrame { return { channelId: identity(fields.channelId), schemaId: identity(fields.schemaId), bodyId: identity(fields.bodyId), // The C# `long` fields are written as JSON integers and can exceed the safe // integer range, so they are read from the raw text, never through // `JSON.parse`'s number. producerEpoch: 0n, sequence: 0n, delivery: fields.delivery, payload: documentPayloadFromBytes(payload(fields.payload)), }; } // `JSON.parse` would round `long.MaxValue` to 9223372036854775808, which is not // a valid i64 and not what either side encoded. The two i64 fields are lifted // out of the raw text as bigints before the document is parsed. function readVectors(path: string): { doc: Vectors; int64s: Map } { const text = readFileSync(path, "utf8"); const int64s = new Map(); const pattern = /"label": "(?