thing-contract-ts: every codec exported and held to its reference type name; every union to its own name in refusals

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-12 09:28:35 +02:00
commit aeede043ed
3 changed files with 93 additions and 47 deletions

View file

@ -0,0 +1,37 @@
// Every codec of the package is named GameCult.Thing.Contract.<RustName>, the reference's full type name, and every union
// names itself in its refusals. The exports are the lowerCamel of the Rust type names, so the test reads the expected name
// off the export.
import assert from "node:assert/strict";
import test from "node:test";
import { codecs } from "@gamecult/thing-contract";
const NS = "GameCult.Thing.Contract.";
const entries = Object.entries(codecs) as [string, { typeName?: string; decode(raw: unknown): unknown }][];
const rustName = (key: string) => key[0].toUpperCase() + key.slice(1);
test("the package exports ninety-five codecs", () => {
assert.equal(entries.length, 95);
});
test("every codec carries the reference's full type name", () => {
for (const [key, codec] of entries) assert.equal(codec.typeName, `${NS}${rustName(key)}`, key);
});
test("every union refuses an unknown arm by its own name, and the twenty-nine unions are all of them", () => {
const unions: string[] = [];
for (const [key, codec] of entries) {
let message: string;
try {
codec.decode([2 ** 30, []]);
continue;
} catch (error) {
message = (error as Error).message;
}
const named = /^Union (\w+): unknown arm key 1073741824 \(declared keys: [-\d, ]+\)\.$/.exec(message);
if (named === null) continue;
unions.push(key);
assert.equal(named[1], rustName(key), key);
}
assert.equal(unions.length, 29);
});

View file

@ -16,6 +16,14 @@ const NS = "GameCult.Thing.Contract.";
const STRING = "System.String";
const list = (item: string) => `System.Collections.Generic.List<${NS}${item}>`;
const rustName: Record<string, string> = {
"gamecult.thing.surface": "SurfaceDocument",
"gamecult.thing.composition": "CompositionDocument",
"gamecult.thing.command_invocation": "CommandInvocation",
"gamecult.thing.command_result": "CommandResult",
"gamecult.thing.asset_catalog": "AssetCatalog",
};
const expected = [
[surfaceDocument, "gamecult.thing.surface", [
"0:ProviderId:" + STRING,
@ -61,6 +69,7 @@ for (const [definition, type, members] of expected) {
assert.equal(definition.type, type);
assert.equal(definition.schemaName, type);
assert.equal(definition.schemaVersion, `${type}.v1`);
assert.equal(definition.codec?.typeName, `${NS}${rustName[type]}`);
assert.deepEqual(
definition.members?.map((member) => `${member.slot}:${member.memberName}:${member.typeName}`),
members,