bot / test / vectors.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Golden-vector tests: the CLJS dist must reproduce the TS implementation's
// outputs byte-for-byte / value-for-value. Vectors were extracted from the
// TS sources while they were still canon (test/vectors/*.json).
// Runs against the committed dist/ — never src/.
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync, writeFileSync, mkdtempSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";

const bot = await import("../stack-a/dist/main.js");
const vec = (name) => JSON.parse(readFileSync(new URL(`./vectors/${name}`, import.meta.url), "utf8"));

test("room keys", () => {
  for (const c of vec("room-key.json")) {
    assert.equal(bot.roomKey(c.app, c.secret), c.key);
  }
});

test("app of host", () => {
  for (const c of vec("app-of-host.json")) {
    assert.equal(bot.appOfHost(c.host) ?? null, c.app);
  }
});

test("config merge", () => {
  const dir = mkdtempSync(join(tmpdir(), "botcfg-"));
  try {
    for (const c of vec("config-merge.json")) {
      const p = join(dir, `${c.name}.json`);
      writeFileSync(p, JSON.stringify(c.raw));
      assert.deepEqual(JSON.parse(JSON.stringify(bot.loadConfig(p))), c.merged, c.name);
    }
  } finally {
    rmSync(dir, { recursive: true, force: true });
  }
});

test("reaper decision table", () => {
  for (const c of vec("reaper.json")) {
    assert.equal(
      bot.shouldReap(c.provenance, c.matchEnded, c.idleMs),
      c.drop,
      `${c.provenance} matchEnded=${c.matchEnded} idle=${c.idleMs}`,
    );
  }
});

test("worker respawn backoff", () => {
  for (const c of vec("backoff.json")) {
    assert.equal(bot.backoffDelay(c.restarts), c.delayMs);
  }
});

test("seance state encoding", () => {
  for (const c of vec("seance-encode.json")) {
    assert.equal(bot.encodeSeanceState(c.view), c.key);
  }
});

test("seance qs + action mapping", () => {
  const v = vec("seance-qs.json");
  for (const c of v.cases) {
    const model = c.key === "nullmodel" ? null : v.model;
    const key = c.key === "nullmodel" ? "k1" : c.key;
    assert.deepEqual([...bot.seanceQs(model, key)], c.qs);
  }
  for (const c of v.actions) {
    assert.equal(bot.seanceAction(c.a) ?? null, c.out);
  }
});

test("neon-grid features", () => {
  for (const c of vec("neon-features.json")) {
    const out = bot.neonFeatures(c.view);
    assert.deepEqual([...out.dirs], c.out.dirs);
    assert.deepEqual(out.feats.map((f) => [...f]), c.out.feats);
  }
});

test("valley-blocks features + fallback", () => {
  for (const c of vec("vb-features.json")) {
    assert.deepEqual(bot.vbFeatures(c.candidates).map((f) => [...f]), c.feats);
    assert.equal(bot.vbFallback(c.candidates), c.fallback);
  }
});

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