-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconformance.js
More file actions
251 lines (241 loc) · 8.39 KB
/
Copy pathconformance.js
File metadata and controls
251 lines (241 loc) · 8.39 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* =====================================================================
TEMPO CONFORMANCE HARNESS — zero dependencies, runs with `node`.
The vectors below ARE the spec. The JS engine must pass them; the
eventual Zig/WASM core must pass the exact same vectors (exported to
JSON via `node conformance.js --emit`).
Vector shape:
{
name: string,
config: { stages: [...] },
steps: [
{ op:'press'|'pause'|'resume'|'adjust'|'check',
player?: 0|1, delta?: int,
now: ms,
expect?: {
active?, running?, flaggedAny?,
sides?: [ {remaining?, flagged?, stageIdx?, movesInStage?, totalMoves?}, ... ]
}
}
]
}
`check` is a pure read at `now` (no state change). press/pause/resume/
adjust mutate, then assert against peek at the same `now`.
===================================================================== */
'use strict';
const E = require('./engine.js');
const SEC = 1000, MIN = 60000;
function eq(a, b) {
if (a === b) return true;
if (typeof a !== typeof b) return false;
if (Array.isArray(a)) {
if (!Array.isArray(b) || a.length !== b.length) return false;
return a.every((x, i) => eq(x, b[i]));
}
if (a && b && typeof a === 'object') {
return Object.keys(a).every(k => eq(a[k], b[k]));
}
return false;
}
function applyExpect(view, expect, ctx, fails) {
if ('active' in expect && view.active !== expect.active)
fails.push(`${ctx}: active ${view.active} ≠ ${expect.active}`);
if ('running' in expect && view.running !== expect.running)
fails.push(`${ctx}: running ${view.running} ≠ ${expect.running}`);
if ('flaggedAny' in expect && view.flaggedAny !== expect.flaggedAny)
fails.push(`${ctx}: flaggedAny ${view.flaggedAny} ≠ ${expect.flaggedAny}`);
if (expect.sides) {
expect.sides.forEach((es, i) => {
if (!es) return;
const vs = view.sides[i];
for (const k of Object.keys(es)) {
if (!eq(es[k], vs[k]))
fails.push(`${ctx}: side[${i}].${k} ${JSON.stringify(vs[k])} ≠ ${JSON.stringify(es[k])}`);
}
});
}
}
function runVector(v) {
const s = E.create(v.config);
const fails = [];
v.steps.forEach((step, idx) => {
const tag = `"${v.name}" step ${idx} (${step.op}@${step.now})`;
switch (step.op) {
case 'press': E.press(s, step.player, step.now); break;
case 'pause': E.pause(s, step.now); break;
case 'resume': E.resume(s, step.now); break;
case 'adjust': E.adjustMoves(s, step.player, step.delta); break;
case 'check': break;
default: fails.push(`${tag}: unknown op`); return;
}
if (step.expect) applyExpect(E.peek(s, step.now), step.expect, tag, fails);
});
return fails;
}
const fischer = (base, inc) => ({ stages: [{ moves: null, base, incType: inc ? 'fischer' : 'none', inc }] });
const bronstein = (base, inc) => ({ stages: [{ moves: null, base, incType: 'bronstein', inc }] });
const delay = (base, inc) => ({ stages: [{ moves: null, base, incType: 'delay', inc }] });
const fide = () => ({ stages: [
{ moves: 40, base: 90 * MIN, incType: 'fischer', inc: 30 * SEC },
{ moves: null, base: 30 * MIN, incType: 'fischer', inc: 30 * SEC },
]});
const VECTORS = [
{
name: 'start: presser moves, opponent clock runs',
config: fischer(5 * MIN, 0),
steps: [
{ op: 'press', player: 0, now: 0,
expect: { started: true, running: true, active: 1,
sides: [
{ remaining: 5*MIN, movesInStage: 1, totalMoves: 1 },
{ remaining: 5*MIN, movesInStage: 0, totalMoves: 0 } ] } },
],
},
{
name: 'start with fischer: presser earns increment on move 1',
config: fischer(3 * MIN, 2 * SEC),
steps: [
{ op: 'press', player: 0, now: 0,
expect: { active: 1,
sides: [ { remaining: 3*MIN + 2*SEC, totalMoves: 1 },
{ remaining: 3*MIN } ] } },
],
},
{
name: 'sudden death: simple elapse, no increment',
config: fischer(5 * MIN, 0),
steps: [
{ op: 'press', player: 0, now: 0 },
{ op: 'press', player: 1, now: 10 * SEC,
expect: { active: 0, sides: [ null, { remaining: 5*MIN - 10*SEC } ] } },
{ op: 'press', player: 0, now: 13 * SEC,
expect: { active: 1, sides: [ { remaining: 5*MIN - 3*SEC }, null ] } },
],
},
{
name: 'fischer: +2s added on each completed move',
config: fischer(3 * MIN, 2 * SEC),
steps: [
{ op: 'press', player: 0, now: 0 },
{ op: 'press', player: 1, now: 5 * SEC,
expect: { sides: [ null, { remaining: 3*MIN - 5*SEC + 2*SEC } ] } },
],
},
{
name: 'bronstein: refund equals time spent when under cap',
config: bronstein(5 * MIN, 3 * SEC),
steps: [
{ op: 'press', player: 0, now: 0 },
{ op: 'press', player: 1, now: 2 * SEC,
expect: { sides: [ null, { remaining: 5*MIN } ] } },
],
},
{
name: 'bronstein: refund capped at inc when over cap',
config: bronstein(5 * MIN, 3 * SEC),
steps: [
{ op: 'press', player: 0, now: 0 },
{ op: 'press', player: 1, now: 10 * SEC,
expect: { sides: [ null, { remaining: 5*MIN - 7*SEC } ] } },
],
},
{
name: 'simple delay: grace consumed before clock burns',
config: delay(5 * MIN, 5 * SEC),
steps: [
{ op: 'press', player: 0, now: 0 },
{ op: 'check', now: 3 * SEC,
expect: { sides: [ null, { remaining: 5*MIN } ] } },
{ op: 'check', now: 8 * SEC,
expect: { sides: [ null, { remaining: 5*MIN - 3*SEC } ] } },
],
},
{
name: 'flag: floors at zero, latches, game stops',
config: fischer(10 * SEC, 0),
steps: [
{ op: 'press', player: 0, now: 0 },
{ op: 'check', now: 15 * SEC,
expect: { flaggedAny: true,
sides: [ null, { remaining: 0, flagged: true } ] } },
{ op: 'press', player: 1, now: 16 * SEC,
expect: { running: false, active: 1 } },
],
},
{
name: 'pause/resume: no time charged while paused',
config: fischer(5 * MIN, 0),
steps: [
{ op: 'press', player: 0, now: 0 },
{ op: 'pause', now: 10 * SEC,
expect: { running: false, sides: [ null, { remaining: 5*MIN - 10*SEC } ] } },
{ op: 'resume', now: 100 * SEC,
expect: { running: true, sides: [ null, { remaining: 5*MIN - 10*SEC } ] } },
{ op: 'press', player: 1, now: 105 * SEC,
expect: { sides: [ null, { remaining: 5*MIN - 15*SEC } ] } },
],
},
{
name: 'multi-stage FIDE: advance after 40 moves, +30min carryover',
config: fide(),
steps: (() => {
// Start press already counts as p0's move 1, so p0 needs only 39
// more presses (39) while p1 needs 40 → 79 alternating presses,
// both land exactly on the stage boundary.
const steps = [{ op: 'press', player: 0, now: 0 }];
let t = 0, mover = 1;
for (let i = 0; i < 79; i++) {
t += 1;
steps.push({ op: 'press', player: mover, now: t });
mover = mover === 0 ? 1 : 0;
}
steps.push({ op: 'check', now: t,
expect: { sides: [ { stageIdx: 1, movesInStage: 0 },
{ stageIdx: 1, movesInStage: 0 } ] } });
return steps;
})(),
},
{
name: 'delay grace survives pause/resume (no free re-arm)',
config: delay(5 * MIN, 5 * SEC),
steps: [
{ op: 'press', player: 0, now: 0 },
{ op: 'check', now: 2 * SEC,
expect: { sides: [ null, { remaining: 5*MIN } ] } },
{ op: 'pause', now: 2 * SEC },
{ op: 'resume', now: 600 * SEC },
{ op: 'check', now: 600*SEC + 2*SEC,
expect: { sides: [ null, { remaining: 5*MIN } ] } },
{ op: 'check', now: 600*SEC + 5*SEC,
expect: { sides: [ null, { remaining: 5*MIN - 2*SEC } ] } },
],
},
{
name: 'manual move adjust: override count without touching clock',
config: fide(),
steps: [
{ op: 'press', player: 0, now: 0 },
{ op: 'press', player: 1, now: 1 * SEC,
expect: { sides: [ null, { movesInStage: 1, totalMoves: 1 } ] } },
{ op: 'adjust', player: 1, delta: -1, now: 1 * SEC,
expect: { sides: [ null, { movesInStage: 0, totalMoves: 0 } ] } },
{ op: 'adjust', player: 1, delta: -5, now: 1 * SEC,
expect: { sides: [ null, { movesInStage: 0, totalMoves: 0 } ] } },
],
},
];
if (process.argv.includes('--emit')) {
const portable = VECTORS.map(v => ({ name: v.name, config: v.config, steps: v.steps }));
process.stdout.write(JSON.stringify(portable, null, 2) + '\n');
process.exit(0);
}
let pass = 0, fail = 0;
const allFails = [];
for (const v of VECTORS) {
const fails = runVector(v);
if (fails.length === 0) { pass++; console.log(` ✓ ${v.name}`); }
else { fail++; console.log(` ✗ ${v.name}`); fails.forEach(f => allFails.push(' ' + f)); }
}
console.log('');
if (allFails.length) { console.log(allFails.join('\n')); console.log(''); }
console.log(`${pass}/${pass + fail} vectors passed`);
process.exit(fail ? 1 : 0);