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
154
155
156
157 | // Golden-vector tests for the durable formats: the settings blob, the game
// history and the appState sync blob. All three are JSON.stringify'd — into
// localStorage and (for settings + high score) into social-kit's fsync — so
// their KEY ORDER is a durable format and is compared byte for byte.
import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
// localStorage / matchMedia stubs must exist before the lib module body runs.
const store = new Map();
globalThis.localStorage = {
getItem: (k) => (store.has(k) ? store.get(k) : null),
setItem: (k, val) => void store.set(k, String(val)),
removeItem: (k) => void store.delete(k),
};
let systemDark = false;
globalThis.window = globalThis.window ?? globalThis;
globalThis.window.matchMedia = (q) => ({ matches: q.includes("dark") ? systemDark : false });
const lib = await import("../test-dist/testlib.js");
const v = JSON.parse(readFileSync(new URL("./vectors/storage.json", import.meta.url), "utf8"));
const SETTINGS_KEY = "valley-blocks.settings";
const HISTORY_KEY = "valley-blocks.history";
const HS_KEY = "valley-blocks.highscore";
const byLabel = (rows, label) => rows.find((r) => r.label === label);
test("config constants are the deployed values", () => {
assert.equal(v.config.APP_SALT, "game2.ardegazu.ro/v2");
assert.equal(v.config.PROTOCOL_PREFIX, "/game2/2/");
assert.equal(v.config.MAX_PLAYERS, 16);
assert.equal(v.config.STATE_INTERVAL_MS, 150);
assert.equal(v.config.PING_INTERVAL_MS, 2000);
});
test("defaults, and their key order", () => {
store.clear();
const want = byLabel(v.settings, "defaults");
assert.deepEqual({ ...lib.loadSettings() }, want.settings);
assert.deepEqual(Object.keys(lib.defaultSettings()), Object.keys(want.settings));
});
test("saveSettings stamps ts and writes the TS byte shape", () => {
store.clear();
const want = byLabel(v.settings, "saveSettings-stamps-ts");
const s = lib.loadSettings();
s.showButtons = false;
s.darkMode = "dark";
s.muted = true;
const realNow = Date.now;
Date.now = () => 1756300000000;
lib.saveSettings(s);
Date.now = realNow;
assert.deepEqual({ ...s }, want.settings);
assert.equal(store.get(SETTINGS_KEY), want.raw[SETTINGS_KEY]);
assert.deepEqual({ ...lib.loadSettings() }, byLabel(v.settings, "reload").settings);
});
test("a legacy/partial blob merges over the defaults, keeping their order", () => {
store.clear();
store.set(SETTINGS_KEY, '{"muted":true}');
const want = byLabel(v.settings, "legacy-partial");
assert.deepEqual({ ...lib.loadSettings() }, want.settings);
assert.equal(JSON.stringify(lib.loadSettings()), want.reserialized);
});
test("a corrupt blob falls back to the defaults", () => {
store.clear();
store.set(SETTINGS_KEY, "not json{");
assert.deepEqual({ ...lib.loadSettings() }, byLabel(v.settings, "corrupt").settings);
});
test("persistSettings keeps a remote stamp (no Date.now)", () => {
store.clear();
const want = byLabel(v.settings, "persist-keeps-ts");
const remote = lib.defaultSettings();
remote.showButtons = true;
remote.darkMode = "light";
remote.muted = false;
remote.ts = 1700000000000;
lib.persistSettings(remote);
assert.equal(store.get(SETTINGS_KEY), want.raw[SETTINGS_KEY]);
assert.deepEqual({ ...lib.loadSettings() }, want.reloaded);
});
test("high score: empty, saved and garbage", () => {
store.clear();
assert.equal(lib.loadHighScore(), byLabel(v.settings, "highscore-empty").hs);
lib.saveHighScore(4321);
const saved = byLabel(v.settings, "highscore-saved");
assert.equal(lib.loadHighScore(), saved.hs);
assert.equal(store.get(HS_KEY), saved.raw[HS_KEY]);
store.set(HS_KEY, "not-a-number");
assert.equal(lib.loadHighScore(), byLabel(v.settings, "highscore-garbage").hs);
});
test("resolveDark follows the system only in auto", () => {
const want = byLabel(v.settings, "resolveDark");
systemDark = false;
for (const { mode, dark } of want.systemLight) assert.equal(lib.resolveDark(mode), dark, `light/${mode}`);
systemDark = true;
for (const { mode, dark } of want.systemDark) assert.equal(lib.resolveDark(mode), dark, `dark/${mode}`);
systemDark = false;
});
test("GameRecord key order is the durable format", () => {
const want = byLabel(v.history, "record-json-key-order");
assert.equal(JSON.stringify(lib.makeRecord(1756300000000, 1200, 14, 2, 183)), want.json);
});
test("history: empty, three records newest-first, and the stats", () => {
store.clear();
const empty = byLabel(v.history, "empty");
assert.deepEqual([...lib.loadHistory()], empty.history);
assert.deepEqual({ ...lib.historyStats([]) }, empty.stats);
const want = byLabel(v.history, "three");
for (const r of [
[1756300000000, 1200, 14, 2, 183],
[1756300100000, 300, 3, 1, 41],
[1756300200000, 9800, 62, 7, 902],
]) lib.addRecord(lib.makeRecord(...r));
assert.equal(store.get(HISTORY_KEY), want.raw);
assert.deepEqual([...lib.loadHistory()].map((x) => ({ ...x })), want.history);
assert.deepEqual({ ...lib.historyStats(lib.loadHistory()) }, want.stats);
});
test("history caps at 500, newest first", () => {
store.clear();
for (let i = 0; i < 520; i++) lib.addRecord(lib.makeRecord(i, i, i % 5, (i % 9) + 1, i));
const h = [...lib.loadHistory()];
const want = byLabel(v.history, "cap");
assert.equal(h.length, want.length);
assert.deepEqual({ ...h[0] }, want.first);
assert.deepEqual({ ...h[h.length - 1] }, want.last);
});
test("a corrupt history blob starts fresh", () => {
store.clear();
store.set(HISTORY_KEY, '{"not":"an array"}');
assert.deepEqual([...lib.loadHistory()], byLabel(v.history, "corrupt-object").history);
store.set(HISTORY_KEY, "[[");
assert.deepEqual([...lib.loadHistory()], byLabel(v.history, "corrupt-json").history);
});
test("the appState sync blob keeps its three-key order", () => {
// main.cljs's sync-state shape, rebuilt here from the same pieces
const settings = lib.defaultSettings();
settings.showButtons = false;
settings.darkMode = "dark";
settings.muted = true;
settings.ts = 1756300000000;
const blob = {};
blob.v = 1;
blob.settings = Object.assign({}, settings);
blob.highscore = 12345;
assert.equal(JSON.stringify(blob), v.syncState.json);
});
|