game1 / client / test / frames.test.mjs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Golden-vector tests: wire frames must serialize byte-identically to the TS
// implementation's (fixtures generated while the TS was canon).
import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import * as lib from "../test-dist/testlib.js";

const vectors = JSON.parse(readFileSync(new URL("./vectors/frames.json", import.meta.url), "utf8"));
const byName = Object.fromEntries(vectors.map((v) => [v.name, v]));

const built = {
  hi: () => lib.mkHi("zed", 3),
  "ro-idle": () => lib.mkRo("12D3KooWA", [["12D3KooWA", "zed", 0, 2], ["12D3KooWB", "rex", 1, 0]], false),
  "ro-live": () => lib.mkRo("12D3KooWA", [["12D3KooWA", "zed", 0, 2]], true),
  in: () => lib.mkIn(3),
  st: () => lib.mkSt(2, 76, ["12D3KooWA", "12D3KooWB"], [[114, 48, 3], [14, 48, 1]]),
  tk: () => lib.mkTk(17, [[0, 10, 20, 1], [1, 30, 40, 0]]),
  ov: () => lib.mkOv("12D3KooWA", [["12D3KooWA", 3], ["12D3KooWB", 1]]),
  "ov-draw": () => lib.mkOv(null, [["12D3KooWA", 0]]),
  ma: () => lib.mkMa("12D3KooWA", [["12D3KooWA", 5]]),
  lo: () => lib.mkLo(),
  sn: () => lib.mkSn(1, 80, 42, ["12D3KooWA"], [[5, 6, 1, 0]], [0, 773, 1, 1, 0, 11514]),
};

for (const [name, make] of Object.entries(built)) {
  test(`frame ${name} serializes byte-identically to TS`, () => {
    assert.equal(JSON.stringify(make()), byName[name].json);
  });
}

test("isGameFrame accepts frames, rejects junk", () => {
  for (const v of vectors) assert.equal(lib.isGameFrame(v.frame), true);
  for (const junk of [null, undefined, 42, "hi", [], {}, { t: 7 }]) {
    assert.equal(Boolean(lib.isGameFrame(junk)), false, `junk: ${JSON.stringify(junk)}`);
  }
});

static mirror of HEAD · about · clone: git clone https://git.ardegazu.ro/game1.git