cultnet-ts: CultNetErrorMessage carries routingHint, code (closed union) and details; decode tests on C#/Rust bytes
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b6f61a3869
commit
03b31f1f68
3 changed files with 120 additions and 0 deletions
|
|
@ -165,9 +165,38 @@ export interface CultNetLoginSuccessMessage {
|
|||
session: string;
|
||||
}
|
||||
|
||||
/** The closed refusal-code set of cultnet.error.v0 (contracts/cultnet/cultnet.error.schema.json). */
|
||||
export const CULTNET_ERROR_CODES = [
|
||||
"selection_invalid",
|
||||
"cursor_stale",
|
||||
"cursor_invalid",
|
||||
"reference_outside_target",
|
||||
"variant_unsupported",
|
||||
"unowned_schema",
|
||||
"write_not_authorized",
|
||||
] as const;
|
||||
|
||||
export type CultNetErrorCode = (typeof CULTNET_ERROR_CODES)[number];
|
||||
|
||||
export interface CultNetErrorRoutingHint {
|
||||
shard: Record<string, unknown> | null;
|
||||
reason: string;
|
||||
}
|
||||
|
||||
export interface CultNetErrorDetails {
|
||||
field: string | null;
|
||||
value: string | null;
|
||||
asOf: number | null;
|
||||
current: number | null;
|
||||
}
|
||||
|
||||
/** Every key is written on the wire, null included (the C# map formatter never omits a key). */
|
||||
export interface CultNetErrorMessage {
|
||||
schemaVersion: "cultnet.error.v0";
|
||||
error: string;
|
||||
routingHint: CultNetErrorRoutingHint | null;
|
||||
code: CultNetErrorCode | null;
|
||||
details: CultNetErrorDetails | null;
|
||||
}
|
||||
|
||||
export interface CultNetSampleChangeNameMessage {
|
||||
|
|
@ -842,6 +871,10 @@ function parseGameCultNetworkingMessage(input: unknown): CultNetMessage {
|
|||
return {
|
||||
schemaVersion: "cultnet.error.v0",
|
||||
error: requireLegacyString(payload[0], "ErrorMessage.Error"),
|
||||
// The legacy union ErrorMessage carries only Error; the R-N fields exist on cultnet.schema.v0 alone.
|
||||
routingHint: null,
|
||||
code: null,
|
||||
details: null,
|
||||
};
|
||||
case 5:
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,11 @@ export {
|
|||
type CultNetDatabaseChangeRawMessage,
|
||||
type CultNetRawDocumentRecord,
|
||||
type CultNetRawPayloadEncoding,
|
||||
CULTNET_ERROR_CODES,
|
||||
type CultNetErrorCode,
|
||||
type CultNetErrorDetails,
|
||||
type CultNetErrorMessage,
|
||||
type CultNetErrorRoutingHint,
|
||||
type CultNetHelloMessage,
|
||||
type CultNetLoginMessage,
|
||||
type CultNetLoginSuccessMessage,
|
||||
|
|
|
|||
83
packages/cultnet-ts/test/cultnet-error.test.ts
Normal file
83
packages/cultnet-ts/test/cultnet-error.test.ts
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { decode, encode } from "@msgpack/msgpack";
|
||||
|
||||
import {
|
||||
CULTNET_ERROR_CODES,
|
||||
cultNetSchemas,
|
||||
encodeCultNetMessageForWire,
|
||||
parseCultNetMessage,
|
||||
type CultNetErrorMessage,
|
||||
} from "../src";
|
||||
|
||||
// contracts/cultnet/interop/error-vectors.json: bytes captured from MessagePack C# 3.1.7 and pinned
|
||||
// by cultnet-rs/tests/error_message.rs (Rust encodes to exactly these bytes).
|
||||
const VECTORS = join(__dirname, "..", "..", "..", "..", "contracts", "cultnet", "interop", "error-vectors.json");
|
||||
const vectors = (JSON.parse(readFileSync(VECTORS, "utf8")) as {
|
||||
vectors: { name: string; messagePackHex: string; message: unknown }[];
|
||||
}).vectors;
|
||||
|
||||
const bytesOf = (hex: string): Uint8Array => Uint8Array.from(Buffer.from(hex, "hex"));
|
||||
const hexOf = (bytes: Uint8Array): string => Buffer.from(bytes).toString("hex").toUpperCase();
|
||||
const wireOf = (message: unknown): Uint8Array => encode(encodeCultNetMessageForWire(message as CultNetErrorMessage));
|
||||
|
||||
test("the error vector file carries a case for every closed code", () => {
|
||||
const codes = new Set(vectors.map((v) => (v.message as CultNetErrorMessage).code));
|
||||
for (const code of CULTNET_ERROR_CODES) {
|
||||
assert.ok(codes.has(code), `no vector for ${code}`);
|
||||
}
|
||||
});
|
||||
|
||||
test("a null code with null routingHint and details is a valid error message (no captured bytes carry one)", () => {
|
||||
const message = parseCultNetMessage({
|
||||
schemaVersion: "cultnet.error.v0", error: "boom", routingHint: null, code: null, details: null,
|
||||
});
|
||||
assert.equal((message as CultNetErrorMessage).code, null);
|
||||
});
|
||||
|
||||
test("the closed code union equals the schema's enum", () => {
|
||||
const schemaCodes = ((cultNetSchemas.errorSchema.properties.code as { enum?: (string | null)[] }).enum ?? []).filter((c) => c !== null);
|
||||
assert.deepEqual([...CULTNET_ERROR_CODES].sort(), [...schemaCodes].sort());
|
||||
});
|
||||
|
||||
test("C#/Rust-written error bytes decode into the typed message with every key", () => {
|
||||
for (const vector of vectors) {
|
||||
const parsed = parseCultNetMessage(decode(bytesOf(vector.messagePackHex)));
|
||||
assert.deepEqual(parsed, vector.message, vector.name);
|
||||
assert.deepEqual(Object.keys(parsed), ["schemaVersion", "error", "routingHint", "code", "details"], vector.name);
|
||||
}
|
||||
});
|
||||
|
||||
test("a parsed error message re-encodes to the same bytes", () => {
|
||||
for (const vector of vectors) {
|
||||
const parsed = parseCultNetMessage(decode(bytesOf(vector.messagePackHex)));
|
||||
assert.equal(hexOf(wireOf(parsed)), vector.messagePackHex, vector.name);
|
||||
}
|
||||
});
|
||||
|
||||
test("dropping code or details breaks the round trip and the decode", () => {
|
||||
const vector = vectors.find((v) => v.name === "cursor_stale")!;
|
||||
for (const dropped of ["code", "details", "routingHint"]) {
|
||||
const copy = { ...(decode(bytesOf(vector.messagePackHex)) as Record<string, unknown>) };
|
||||
delete copy[dropped];
|
||||
assert.throws(() => parseCultNetMessage(copy), /cultnet\.error\.v0/, `missing ${dropped} must be refused`);
|
||||
assert.notEqual(hexOf(encode(copy)), vector.messagePackHex);
|
||||
}
|
||||
});
|
||||
|
||||
test("a code outside the closed enum is refused, as is a malformed details object", () => {
|
||||
const base = decode(bytesOf(vectors.find((v) => v.name === "cursor_stale")!.messagePackHex)) as Record<string, unknown>;
|
||||
assert.throws(() => parseCultNetMessage({ ...base, code: "unsupported_schema_version" }), /cultnet\.error\.v0/);
|
||||
assert.throws(() => parseCultNetMessage({ ...base, code: 7 }), /cultnet\.error\.v0/);
|
||||
assert.throws(
|
||||
() => parseCultNetMessage({ ...base, details: { field: null, value: null, asOf: 1, current: 2, extra: 1 } }),
|
||||
/cultnet\.error\.v0/,
|
||||
);
|
||||
assert.throws(
|
||||
() => parseCultNetMessage({ ...base, details: { field: null, value: null, asOf: -1, current: 2 } }),
|
||||
/cultnet\.error\.v0/,
|
||||
);
|
||||
});
|
||||
Loading…
Reference in a new issue