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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 | // Episode-JSONL byte contract: each game's recorder, driven through the exact
// choreography the fixture generator ran against the TS implementation, must
// write byte-identical JSONL (the trainer and the promotion gates parse it).
// Recorders come from the committed dist/; EpisodeWriter from train-kit.
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync, readdirSync, mkdtempSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
const bot = await import("../stack-a/dist/main.js");
// the INSTALLED train-kit dist, named by path: this file sits outside both
// npm roots, and train-kit is stack A's (see brains.test.mjs)
const { EpisodeWriter } = await import(
new URL("../stack-a/node_modules/ardegazu-train-kit/dist/index.js", import.meta.url).href
);
const vec = (name) => JSON.parse(readFileSync(new URL(`./vectors/${name}`, import.meta.url), "utf8"));
class FakeRoom {
handlers = {};
hostId = "HOSTPEER";
myId = "MYPEER";
seat = 0;
constructor(app) {
this.app = app;
}
on(ev, cb) {
(this.handlers[ev] ??= []).push(cb);
}
emit(ev, ...a) {
for (const cb of this.handlers[ev] ?? []) cb(...a);
}
}
function assertEpisodes(dir, game, expected) {
const d = join(dir, "episodes");
const files = readdirSync(d).sort();
assert.equal(files.length, expected.length);
for (let i = 0; i < files.length; i++) {
assert.match(files[i], new RegExp(`^${game}-\\d{4}-\\d{2}\\.jsonl$`));
assert.equal(readFileSync(join(d, files[i]), "utf8"), expected[i].bytes);
}
}
function withDir(fn) {
const dir = mkdtempSync(join(tmpdir(), "bot-ep-"));
try {
fn(dir);
} finally {
rmSync(dir, { recursive: true, force: true });
}
}
test("seance recorder JSONL (model, greedy)", () => {
const v = vec("seance-recorder.json");
withDir((dir) => {
const rec = bot.makeSeanceRecorder(() => v.model, v.epsilon, new EpisodeWriter(dir));
const room = new FakeRoom("seance");
rec.attach(room);
room.emit("frame", "HOSTPEER", { t: "st" });
const r1 = rec.onTurn(v.views[0]);
room.emit("frame", "HOSTPEER", { t: "rv", candles: [2, 3, 3] });
const r2 = rec.onTurn(v.views[1]);
room.emit("frame", "HOSTPEER", { t: "rv", candles: [2, 3, 3] });
const r3 = rec.onTurn(v.views[2]);
room.emit("frame", "HOSTPEER", { t: "ov", w: "MYPEER" });
assert.deepEqual([r1, r2, r3].map((x) => x ?? null), v.returns.map((x) => x ?? null));
assertEpisodes(dir, "seance", v.episodes);
});
});
test("seance recorder JSONL (no model, stock rule)", () => {
const v = vec("seance-recorder-stock.json");
withDir((dir) => {
const rec = bot.makeSeanceRecorder(() => null, v.epsilon, new EpisodeWriter(dir));
const room = new FakeRoom("seance");
rec.attach(room);
const r1 = rec.onTurn(v.views[0]);
room.emit("frame", "HOSTPEER", { t: "rv", candles: [0, 3, 3] });
const r2 = rec.onTurn(v.views[1]);
room.emit("frame", "HOSTPEER", { t: "ov", w: "OTHER" });
assert.deepEqual([r1, r2].map((x) => x ?? null), v.returns.map((x) => x ?? null));
assertEpisodes(dir, "seance", v.episodes);
});
});
test("neon-grid recorder JSONL (model, death + win terminals)", () => {
const v = vec("neon-recorder.json");
withDir((dir) => {
const rec = bot.makeNeonGridRecorder(() => v.model, v.epsilon, new EpisodeWriter(dir));
const room = new FakeRoom("neon-grid");
rec.attach(room);
room.emit("frame", "HOSTPEER", { t: "st" });
const r1 = rec.onTick(v.views[0]);
const r2 = rec.onTick(v.views[1]);
room.emit("frame", "HOSTPEER", { t: "tk", h: [[0, 4, 4, 0], [1, 2, 2, 1]] });
const r3 = rec.onTick(v.views[2]);
room.emit("frame", "HOSTPEER", { t: "ov", w: "MYPEER" });
assert.deepEqual([r1, r2, r3].map((x) => x ?? null), v.returns.map((x) => x ?? null));
assertEpisodes(dir, "neon-grid", v.episodes);
});
});
test("neon-grid recorder JSONL (no model, ray fallback, draw)", () => {
const v = vec("neon-recorder-stock.json");
withDir((dir) => {
const rec = bot.makeNeonGridRecorder(() => null, v.epsilon, new EpisodeWriter(dir));
const room = new FakeRoom("neon-grid");
rec.attach(room);
const r1 = rec.onTick(v.views[0]);
const r2 = rec.onTick(v.views[1]);
room.emit("frame", "HOSTPEER", { t: "ov", w: null });
assert.deepEqual([r1, r2].map((x) => x ?? null), v.returns.map((x) => x ?? null));
assertEpisodes(dir, "neon-grid", v.episodes);
});
});
test("valley-blocks recorder JSONL (model, standings fold)", () => {
const v = vec("vb-recorder.json");
withDir((dir) => {
const rec = bot.makeValleyBlocksRecorder(() => v.model, v.epsilon, new EpisodeWriter(dir));
const room = new FakeRoom("valley-blocks");
rec.attach(room);
room.emit("frame", "HOSTPEER", { t: "st" });
const r1 = rec.onPiece(v.views[0]);
const r2 = rec.onPiece(v.views[1]);
room.emit("frame", "HOSTPEER", {
t: "ov",
w: "MYPEER",
standings: [["MYPEER", "duhul", 800, 0], ["P2", "moroi", 300, 1]],
});
assert.deepEqual([r1, r2], v.returns);
assertEpisodes(dir, "valley-blocks", v.episodes);
});
});
test("valley-blocks recorder JSONL (no model, topped out)", () => {
const v = vec("vb-recorder-stock.json");
withDir((dir) => {
const rec = bot.makeValleyBlocksRecorder(() => null, v.epsilon, new EpisodeWriter(dir));
const room = new FakeRoom("valley-blocks");
rec.attach(room);
const r1 = rec.onPiece(v.views[0]);
const r2 = rec.onPiece(v.views[1]);
room.emit("frame", "HOSTPEER", {
t: "ov",
w: "P2",
standings: [["MYPEER", "duhul", 4100, 1], ["P2", "moroi", 5000, 0]],
});
assert.deepEqual([r1, r2], v.returns);
assertEpisodes(dir, "valley-blocks", v.episodes);
});
});
|