Repository navigation
Expand file tree
/
Copy pathproof.test.mjs
More file actions
1455 lines (1383 loc) · 92.8 KB
/
Copy pathproof.test.mjs
File metadata and controls
1455 lines (1383 loc) · 92.8 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {cdpBrowser} from '../scripts/agent-launch.mjs';
import {runFocusCommands} from '../host/web/navigation.js';
import {captureWorld, diffWorlds, formatWorldDiff} from './proof.mjs';
import {spawnSync} from 'node:child_process';
import {checkSteadyResidency} from './render/tests/residency.mjs';
import {test, expect} from 'bun:test';
import {mkdtempSync, mkdirSync, writeFileSync, rmSync, readFileSync, readdirSync, symlinkSync} from 'node:fs';
import {resolve, dirname} from 'node:path';
import {tmpdir} from 'node:os';
import {agreePins, nativeProofHost, webUnavailable, pinRecorder, proofStatus, facilityReport, artifactDigest, closeSessions, equal, paranoidRuns, buildInputHash, ensureBuildReceipt, proofInputFiles} from './proof.mjs';
import {assertNoLiveEdits, proofCommand, worldObservations, pinRevision} from './proof.mjs';
import {comparePlacement} from './games/placement-fixture/proof.mjs';
import {typeArguments, typeFor, browserKey, nativeKey, render, worldView, tapRefusal, assertWebDistApp, clockSpan, main} from '../scripts/agent.mjs';
test('external app sources and assets include every extension while outputs stay excluded', () => {
const directory = mkdtempSync(resolve(tmpdir(), 'external-proof-inputs-'));
const root = resolve(directory, 'engine'), app = resolve(directory, 'my-game');
try {
for (const file of ['engine/game/engine/src/lib.rs', 'engine/game/README.md',
'engine/game/engine/README.md', 'my-game/logic/src/lib.rs',
'my-game/app.contract', 'my-game/Cargo.toml', 'my-game/Cargo.lock',
'my-game/art/model.glb', 'my-game/assets/texture.png', 'my-game/deck/image.bin',
'my-game/artifacts/replies.json', 'my-game/dist.previous/module.wasm',
'my-game/.shells/host/src/lib.rs', 'my-game/target/build.rs', 'my-game/proof.mjs']) {
const path = resolve(directory, file);
mkdirSync(resolve(path, '..'), {recursive:true}); writeFileSync(path, file);
}
const files = proofInputFiles(root, app);
expect(files).toEqual(['../my-game/Cargo.lock', '../my-game/Cargo.toml',
'../my-game/app.contract', '../my-game/art/model.glb', '../my-game/assets/texture.png',
'../my-game/deck/image.bin', '../my-game/logic/src/lib.rs', 'game/engine/src/lib.rs']);
const digest = () => {
const hash = buildInputHash('linux', 'target');
for (const file of proofInputFiles(root, app)) hash.update(file).update(readFileSync(resolve(root, file)));
return hash.digest('hex');
};
const before = digest();
writeFileSync(resolve(app, 'logic/src/lib.rs'), 'changed game source');
expect(digest()).not.toBe(before);
const after = digest();
writeFileSync(resolve(app, 'artifacts/replies.json'), 'new proof output');
expect(digest()).toBe(after);
writeFileSync(resolve(root, 'game/README.md'), 'changed game guide');
expect(digest()).toBe(after);
} finally { rmSync(directory, {recursive:true, force:true}); }
});
test('held keys release the original carrier and retain partial failure steps', async () => {
const calls = [], node = {id:17};
let clockRan = false;
const carrier = {input:async (id, kind, options) => {
calls.push([id, kind, options.phase]);
if (options.phase === 'up') expect(clockRan).toBe(true);
return {delivery:'platform'};
}};
let failure;
try {
await typeFor({node, target:'world', options:{key:'KeyW',for:1500}, carrier,
clock:async () => { clockRan = true; throw new Error('world disappeared'); }, tagged:r => r});
} catch (error) { failure = error; }
expect(calls).toEqual([[17,'key','down'],[17,'key','up']]);
expect(failure.message).toBe('world disappeared');
expect(failure.steps.map(s => s.op)).toEqual(['type','clock','type']);
expect(render('type', {steps:failure.steps})).toContain('ERROR world disappeared');
});
test('a failed down still releases; a failed up keeps the original error and both failures', async () => {
const calls = [];
const carrier = {input:async (_, __, {phase}) => {calls.push(phase); throw new Error(phase);}};
let failure;
try { await typeFor({node:{id:1},target:'world',options:{key:'KeyW',for:1},carrier,clock:()=>{throw new Error('unexpected clock');},tagged:r=>r}); }
catch(error) {failure=error;}
expect(calls).toEqual(['down','up']);
expect(failure.message).toBe('down');
expect(failure.steps.map(s=>s.error)).toEqual(['down','up']);
});
test('CLI held key syntax cannot capture an ordinary text suffix', () => {
expect(typeArguments(['world','key','KeyW','for','1500'])).toEqual(['world',{key:'KeyW',for:1500}]);
expect(typeArguments(['editor','hello','for','100'])).toEqual(['editor','hello for 100']);
expect(typeArguments(['world','KeyW','for','100'])).toEqual(['world','KeyW for 100']);
});
test('proof receipts bind the web manifest and the actual app executable', () => {
const dir=mkdtempSync(resolve(tmpdir(),'g1b-receipt-')), bundle=resolve(dir,'Game.app');
try {
expect(artifactDigest('web',dir)).toBe(null);
writeFileSync(resolve(dir,'exact.json'),'one');
const web=artifactDigest('web',dir);
writeFileSync(resolve(dir,'exact.json'),'two');
expect(artifactDigest('web',dir)).not.toBe(web);
writeFileSync(resolve(dir,'built-macos'),'');
expect(artifactDigest('macos',dir,{bundle,executable:'Game'})).toBe(null);
mkdirSync(resolve(bundle,'Contents/MacOS'),{recursive:true});
const exe=resolve(bundle,'Contents/MacOS/Game');
writeFileSync(exe,'native one'); const mac=artifactDigest('macos',dir,{bundle,executable:'Game'});
writeFileSync(exe,'native two'); expect(artifactDigest('macos',dir,{bundle,executable:'Game'})).not.toBe(mac);
const binary=resolve(dir,'standalone');
writeFileSync(binary,'carrier one'); const carrier=artifactDigest('macos',dir,{bundle,executable:'Game',binary});
writeFileSync(binary,'carrier two'); expect(artifactDigest('macos',dir,{bundle,executable:'Game',binary})).not.toBe(carrier);
rmSync(binary); expect(artifactDigest('macos',dir,{bundle,executable:'Game',binary})).toBe(null);
rmSync(bundle,{recursive:true}); expect(artifactDigest('macos',dir,{bundle,executable:'Game'})).toBe(null);
} finally {rmSync(dir,{recursive:true,force:true});}
});
test('inventory failure clears the timer before unconditional session cleanup', async () => {
let polls=0; const monitor=setInterval(()=>polls++,1), calls=[];
await closeSessions(monitor,()=>{throw new Error('ps failed');},[
{close:async()=>{calls.push('first');throw new Error('close failed');}},
{close:async()=>{await new Promise(r=>setTimeout(r,15));calls.push('second');}},
],(...args)=>calls.push(args[0]));
expect(polls).toBe(0);
expect(calls).toEqual(['first','session cleanup','second']);
});
test('Windows receipts bind the executable, GPU DLL, packaged assets and build profile', () => {
const dir=mkdtempSync(resolve(tmpdir(),'game-windows-receipt-'));
const artifacts={binary:resolve(dir,'game.exe'),module:resolve(dir,'game_gpu.dll')};
try {
expect(artifactDigest('windows',dir,artifacts)).toBe(null);
writeFileSync(artifacts.binary,'exe'); writeFileSync(artifacts.module,'gpu');
const first=artifactDigest('windows',dir,artifacts);
mkdirSync(resolve(dir,'assets')); writeFileSync(resolve(dir,'assets/terrain.tex'),'terrain');
expect(artifactDigest('windows',dir,artifacts)).not.toBe(first);
rmSync(artifacts.module);
expect(artifactDigest('windows',dir,artifacts)).toBe(null);
expect(buildInputHash('windows','target','0','release').digest('hex'))
.not.toBe(buildInputHash('windows','target','0','gpu-dev').digest('hex'));
} finally {rmSync(dir,{recursive:true,force:true});}
});
for (const fails of [false, true]) test(`browser held key release survives canvas removal (clock failure=${fails})`, async () => {
let canvas = true, down = false;
const carrier = {input: async (id, _, opts) => browserKey({id, opts,
evaluate: async () => { if (!canvas) throw new Error('canvas removed'); return true; },
ask: async () => { if (!canvas) throw new Error('canvas removed'); return {ok:true}; },
call: async (_, event) => { down=event.type==='keyDown'; }, frame: async () => {},
})};
let failure;
try { await typeFor({node:{id:17},target:'world',options:{key:'KeyW',for:10},carrier,
clock:async () => {canvas=false; if (fails) throw new Error('clock failed'); return {};},tagged:async r=>r}); }
catch(error) {failure=error;}
expect(down).toBe(false);
if (fails) expect(failure.message).toBe('clock failed');
else expect(failure).toBeUndefined();
});
test('native receipt changes with game dylibs and embedded plan/assets', () => {
const dir=mkdtempSync(resolve(tmpdir(),'g1c-receipt-')), bundle=resolve(dir,'Game.app');
try {
mkdirSync(resolve(bundle,'Contents/MacOS'),{recursive:true});
mkdirSync(resolve(bundle,'Contents/Resources'),{recursive:true});
writeFileSync(resolve(bundle,'Contents/MacOS/Game'),'executable');
for (const file of ['Contents/MacOS/libexact_gpu.dylib','Contents/MacOS/libexact_web.dylib','Contents/Resources/app.plan','Contents/Resources/texture.bin']) {
writeFileSync(resolve(bundle,file),'before'); const before=artifactDigest('macos',dir,{bundle,executable:'Game'});
writeFileSync(resolve(bundle,file),'after'); expect(artifactDigest('macos',dir,{bundle,executable:'Game'})).not.toBe(before);
}
} finally {rmSync(dir,{recursive:true,force:true});}
});
test('held key receipts await asynchronous tags on success and failure', async () => {
for (const fails of [false, true]) {
const carrier = {input: async (_, __, {phase}) => ({phase, delivery:'recognized'})};
const tagged = async reply => { await Promise.resolve(); return {...reply, epoch:7}; };
let result;
try {
result = await typeFor({node:{id:17},target:'world',options:{key:'KeyW',for:10},carrier,tagged,
clock:async () => {if (fails) throw new Error('clock failed'); return {now:10};}});
} catch (error) { result = error; }
const steps = JSON.parse(JSON.stringify(result.steps));
expect(steps[0].reply).toMatchObject({epoch:7,phase:'down',delivery:'recognized'});
expect(steps[2].reply).toMatchObject({epoch:7,phase:'up',delivery:'recognized'});
if (!fails) expect(result.delivery).toBe('recognized');
}
});
test('web receipts bind every dist path and byte in deterministic order', () => {
const dir=mkdtempSync(resolve(tmpdir(),'g1d-web-'));
try {
writeFileSync(resolve(dir,'exact.json'),'manifest');
mkdirSync(resolve(dir,'assets'));
writeFileSync(resolve(dir,'assets/texture.bin'),'one');
const original=artifactDigest('web',dir);
rmSync(resolve(dir,'exact.json'));
writeFileSync(resolve(dir,'exact.json'),'manifest'); // Opposite creation order, identical manifest.
expect(artifactDigest('web',dir)).toBe(original);
writeFileSync(resolve(dir,'assets/texture.bin'),'two');
expect(artifactDigest('web',dir)).not.toBe(original);
writeFileSync(resolve(dir,'assets/texture.bin'),'one');
expect(artifactDigest('web',dir)).toBe(original);
writeFileSync(resolve(dir,'gpu_bg.wasm'),'module');
expect(artifactDigest('web',dir)).not.toBe(original);
rmSync(resolve(dir,'gpu_bg.wasm'));
expect(artifactDigest('web',dir)).toBe(original);
rmSync(resolve(dir,'assets/texture.bin'));
writeFileSync(resolve(dir,'assets/renamed.bin'),'one');
expect(artifactDigest('web',dir)).not.toBe(original);
} finally {rmSync(dir,{recursive:true,force:true});}
});
for (const fails of [false, true]) test(`native held key owns release after canvas removal (clock failure=${fails})`, async () => {
let canvas=true, down=false, focused=0;
const releases=new Map();
const ask=async request => {
if (request.releaseKey && request.phase === 'up') {
const release=releases.get(request.releaseKey);
expect(release).toBeDefined();
releases.delete(request.releaseKey);
release();
return {phase:'up',delivery:'recognized'};
}
if (!canvas) return {error:'canvas removed'};
focused++;
down=request.phase === 'down';
if (request.releaseKey) releases.set(request.releaseKey, () => {down=false;});
return {phase:request.phase,delivery:'recognized'};
};
const carrier={input:async (id, _, opts) => nativeKey({id,opts,ask})};
let failure;
try { await typeFor({node:{id:17},target:'world',options:{key:'KeyW',for:10},carrier,tagged:async r=>r,
clock:async () => {canvas=false; if (fails) throw new Error('clock failed'); return {};}}); }
catch(error) {failure=error;}
expect(down).toBe(false);
expect(focused).toBe(1);
expect(releases.size).toBe(0);
if (fails) expect(failure.message).toBe('clock failed');
else expect(failure).toBeUndefined();
});
test('native receipt cache misses when only the standalone game dylib changes', () => {
const dir=mkdtempSync(resolve(tmpdir(),'g1d-native-')), bundle=resolve(dir,'Game.app'), products=resolve(dir,'products');
try {
mkdirSync(resolve(bundle,'Contents/MacOS'),{recursive:true});
mkdirSync(products);
writeFileSync(resolve(bundle,'Contents/MacOS/Game'),'bundled executable');
const binary=resolve(products,'Game'), dylib=resolve(products,'libgreybox_gpu.dylib');
writeFileSync(binary,'standalone executable');
writeFileSync(dylib,'game before');
const artifacts={bundle,executable:'Game',binary,products};
const stamp=() => JSON.stringify({inputs:'unchanged sources',artifact:artifactDigest('macos',dir,artifacts)});
const cached=stamp();
expect(stamp()).toBe(cached);
writeFileSync(dylib,'game after');
expect(stamp()).not.toBe(cached);
rmSync(dylib);
expect(stamp()).not.toBe(cached);
} finally {rmSync(dir,{recursive:true,force:true});}
});
test('proof equality compares complete nested objects independently of key order', () => {
expect(equal({Mesh:{Capsule:{radius:0.4,height:1.8}}, rows:[{a:1,b:null},2]},
{rows:[{b:null,a:1},2], Mesh:{Capsule:{height:1.8,radius:0.4}}})).toBe(true);
for (const [a,b] of [
[{Capsule:{radius:0.4,height:1.8}}, {Capsule:{radius:0.4,height:1.8,extra:0}}],
[{a:null}, {}], [[1,2], [2,1]], [[1], [1,2]], [[], {}], [null, {}], [1, '1'],
]) expect(equal(a,b)).toBe(false);
expect(equal([null,true,{a:[]}], [null,true,{a:[]}])).toBe(true);
});
test('world convenience keeps simulation fields only and dispatches the existing operations', async () => {
const calls = [], entities = [{name:'player', components:{Transform:{position:[0,0.9,0]}}}];
let clock=0, epoch=1, incarnation=1;
const raw = {
world(name) { return worldView(this, name); },
async clock(value) { return {settled:value === 'settle'}; },
async state(target) { if (target === 'arena:missing') throw Object.assign(new Error('state: no entity named `missing`'), {reply:{tick:90,error:'no entity named `missing`'}}); return {entity: target === 'arena:player' ? entities[0] : undefined, tick:90, hash:'0x123', entities, truncated:false, clock, epoch, incarnation}; },
async type(...args) { return {clock, epoch, incarnation, args}; },
async screenshot(...args) { return {clock, epoch, incarnation, args}; },
};
// Like proof's proxy, every underlying operation is recorded with its full reply.
const session = new Proxy(raw, {get(target, method) {
if (method === 'world') return target[method];
return async (...args) => { try { const reply=await target[method](...args); calls.push({method,args,reply}); return reply; } catch (error) { calls.push({method,args,reply:error.reply}); throw error; } };
}});
const w=session.world('arena'), before=await w.snapshot();
clock+=2000;
expect(await w.snapshot()).toEqual(before);
clock=0; epoch++; incarnation++;
expect(await w.snapshot()).toEqual(before);
expect(Object.keys(before)).toEqual(['tick','hash','entities','truncated']);
expect(before).toEqual({tick:90,hash:'0x123',entities,truncated:false});
await w.state('player'); await w.save('checkpoint.world');
await w.key_down('KeyW'); await w.tap('KeyE'); await w.hold('KeyW',1500);
await w.key_up('KeyW'); await w.run(100);
expect(await w.settle()).toBe(true);
expect(await w.local_position('player')).toEqual([0,0.9,0]);
expect(await w.get('player','Transform')).toEqual({position:[0,0.9,0]});
expect(await w.local_position('missing')).toBeUndefined();
expect(await w.get('player','Missing')).toBeUndefined();
for (const ms of [-1, NaN, Infinity]) expect(() => w.run(ms)).toThrow();
expect(calls.map(c => [c.method,...c.args])).toEqual([
['state','arena:*'], ['state','arena:*'], ['state','arena:*'], ['state','arena:player'],
['screenshot','checkpoint.world','arena','save'], ['type','arena',{key:'KeyW',phase:'down'}],
['type','arena',{key:'KeyE'}], ['type','arena',{key:'KeyW',for:1500}],
['type','arena',{key:'KeyW',phase:'up'}], ['clock','+0'], ['clock','+100'], ['clock','settle'],
['state','arena:player'], ['state','arena:player'], ['state','arena:missing'], ['state','arena:player'],
]);
expect(calls[1].reply).toMatchObject({clock:2000,epoch:1,incarnation:1});
expect(calls[2].reply).toMatchObject({clock:0,epoch:2,incarnation:2});
});
test('a complete world snapshot reads every page at one tick and hash', async () => {
const all = Array.from({length:12}, (_, id) => ({id, name:null, components:{}}));
const pages = [];
let hash = '0x1';
const session = {
async state(target, under, pose, busy, page = {}) {
pages.push(page);
const from = page.from ?? 0, to = Math.min(all.length, from + 5);
return {tick:7, hash, tableEdits:2, entities:all.slice(from, to), truncated:to < all.length, total:all.length, ...(to < all.length ? {next:to} : {})};
},
};
const w = worldView(session, 'world');
// The 5-entity pages stand in for the driver's limit of 5,000.
const {entities, truncated, tableEdits} = await w.snapshot({all:true}); expect(tableEdits).toBe(2);
expect(entities.map(e => e.id)).toEqual(all.map(e => e.id));
expect(truncated).toBe(false);
expect(pages.map(p => p.from ?? 0)).toEqual([0, 5, 10]);
pages.length = 0;
session.state = async (target, under, pose, busy, page = {}) => {
pages.push(page);
if (page.from) hash = '0x2';
return {tick:7, hash, entities:all.slice(0, 5), truncated:true, next:5};
};
await expect(w.snapshot({all:true})).rejects.toThrow(/world changed while paging/);
});
test('world get translates only the named missing-entity refusal', async () => {
for (const error of ['no view matches arena', 'no entity named `other`', 'device lost']) {
const w = worldView({state: async () => {throw Object.assign(new Error(`state: ${error}`), {reply:{tick:0,error}});}}, 'arena');
await expect(w.get('missing', 'Transform')).rejects.toThrow(error);
}
const failure = new Error('no entity named `missing`');
await expect(worldView({state: async () => {throw failure;}}, 'arena').get('missing', 'Transform')).rejects.toBe(failure);
});
for (const failure of ['none', 'save', 'fresh-throw', 'off-before-receipt']) test(`paranoid receipt lifecycle: ${failure}`, async () => {
const dir = mkdtempSync(resolve(tmpdir(), 'r5-receipt-')), dist = resolve(dir, 'dist');
const receipt = resolve(dir, 'build-web.sha256'), modes = [];
mkdirSync(dist);
const inputs = mode => buildInputHash('web', 'target', mode).update('source bytes').digest('hex');
let bakes = 0;
const ordinary = (mode, die = false) => ensureBuildReceipt({receipt, inputs:inputs(mode),
artifact:() => artifactDigest('web', dist), build:async () => {
bakes++;
writeFileSync(resolve(dist, 'exact.json'), '{"module":"gpu_bg.wasm"}');
writeFileSync(resolve(dist, 'gpu_bg.wasm'), `wasm compiled with ${mode}`);
if (die) throw new Error('child died before receipt');
}});
try {
await ordinary('fresh-game');
expect(await ordinary('0')).toBe(true); // Ordinary rejects a real paranoid receipt.
expect(bakes).toBe(2);
const failed = await paranoidRuns(async mode => {
modes.push(mode);
await ordinary(mode);
if (failure === 'fresh-throw' && mode === 'fresh-game') throw new Error('proof child threw');
return failure === 'save' && mode === '1' ? 1 : 0;
}, async () => { await ordinary('0', failure === 'off-before-receipt'); return 0; });
expect(modes).toEqual(['0', '1', 'fresh-game']);
expect(failed).toBe(failure !== 'none');
const stamp = JSON.parse(readFileSync(receipt, 'utf8'));
expect(stamp.inputs).toBe(inputs(failure === 'off-before-receipt' ? 'fresh-game' : '0'));
const before = bakes;
expect(await ordinary('0')).toBe(failure === 'off-before-receipt');
expect(bakes - before).toBe(failure === 'off-before-receipt' ? 1 : 0);
expect(JSON.parse(readFileSync(receipt, 'utf8'))).toEqual({inputs:inputs('0'), artifact:artifactDigest('web', dist)});
expect(readFileSync(resolve(dist, 'gpu_bg.wasm'), 'utf8')).toBe('wasm compiled with 0');
expect(buildInputHash('linux', 'target', '0').digest('hex'))
.toBe(buildInputHash('linux', 'target', 'fresh-game').digest('hex'));
} finally { rmSync(dir, {recursive:true, force:true}); }
});
test('steady residency skips no-device worlds and asserts only device-backed work', () => {
const checks = [], lines = [], check = (...args) => checks.push(args), say = line => lines.push(line);
const gpu = {afterReady:{textureUploads:0, meshUploads:0, pipelineCreations:0, modelSkinBufferReallocations:0}};
checkSteadyResidency({device:false, ready:false, gpu}, check, say, "linux");
expect(checks).toEqual([]);
expect(lines).toEqual(['SKIP: no device — after-ready GPU residency']);
checkSteadyResidency({device:true, ready:true, gpu}, check, say);
expect(checks.at(-1)[1]).toBe(true);
checkSteadyResidency({device:true, ready:false, gpu}, check, say);
expect(checks.at(-1)[1]).toBe(false);
checkSteadyResidency({device:true, ready:true, gpu:{afterReady:{textureUploads:1}}}, check, say);
expect(checks.at(-1)[1]).toBe(false);
});
test('inventory parses both lstart day widths and ignores zombies', async () => {
const {parseInventoryLine} = await import('./proof.mjs');
for (const stamp of ['Tue Sep 8 12:34:56 2026','Fri Sep 18 12:34:56 2026']) {
expect(parseInventoryLine(` 123 45 S ${stamp} /path/app --flag`)).toEqual({pid:123,parent:45,stamp,command:'/path/app --flag'});
expect(parseInventoryLine(` 123 45 Z+ ${stamp} <defunct>`)).toBeNull();
}
});
test('only linux may skip missing GPU residency', () => {
for (const host of ['web','macos','ios']) for (const device of [false,undefined]) {
const checks=[], lines=[];
checkSteadyResidency({device,ready:false,gpu:{afterReady:{}}},(...args)=>checks.push(args),line=>lines.push(line),host);
expect(lines).toEqual([]);
expect(checks[0][1]).toBe(false);
}
});
test('explicit focus in a commit precedes autofocus and its authored side effects', () => {
const source=readFileSync(resolve(import.meta.dir,'../host/web/glue.js'),'utf8');
// The commit's tail: explicit focus commands, then autofocus (6dbf594b moved autofocus into navigation.js).
const start=source.indexOf(' for (const command of focusCommands)'), end=source.indexOf(' positionContexts(); presence.live?.after');
expect(start).toBeGreaterThan(-1); expect(end).toBeGreaterThan(start);
const code=runFocusCommands.toString()+';\n'+source.slice(start,end);
const calls=[], explicit={id:'chosen',isConnected:true,matches:()=>false,getClientRects:()=>[{}],focus:()=>{calls.push('explicit');document.activeElement=explicit;}};
const document={activeElement:null};
// `inputHandlers` (the tail's route-arrival call, optional there) has no
// part in the order under test, so it stands in as absent.
new Function('focusAutofocus','focusCommands','inputReady','root','inertAncestor','getComputedStyle','log','document','inputHandlers','popoverCommand',code)(
()=>{if(!document.activeElement) calls.push('autofocus side effect');},[{name:'focus',args:['chosen']}],true,{querySelectorAll:()=>[explicit]},()=>false,()=>({visibility:'visible'}),()=>{},document,undefined,()=>{});
expect(calls).toEqual(['explicit']);
});
test('KeyP forbids texture uploads and pipeline creation as well as requiring new geometry', async () => {
const {checkResidency}=await import('./render/tests/residency.mjs');
const state=(textureUploads=0,meshUploads=0,pipelineCreations=0)=>({ready:true,gpu:{afterReady:{textureUploads,meshUploads,pipelineCreations,modelSkinBufferReallocations:0}}});
for (const error of ['none','texture','pipeline']) {
const checks=[], responses=[{before:state(),after:state()}, {before:state(),after:state(1)}, {before:state(1),after:state(1)},
{before:state(1),after:state(error==='texture'?2:1,1,error==='pipeline'?1:0)}];
await checkResidency({run:async()=>responses.shift()}, {}, (name,ok)=>checks.push([name,ok]), ()=>{});
expect(checks.find(([name])=>name==='new model name reuses textures and pipelines')[1]).toBe(error==='none');
}
});
test('local and global position helpers preserve parent-space distinction', async () => {
const w = worldView({
async state() { return {entity:{components:{Transform:{position:[1,2,3]}}}}; },
async layout() { return {entity:{world:{position:[11,2,3]}}}; },
}, 'arena');
expect(await w.local_position('child')).toEqual([1,2,3]);
expect(await w.global_position('child')).toEqual([11,2,3]);
expect(w.position).toBeUndefined();
});
const syntheticHash = '0x' + '12345678' + '9abcdef0';
const repeatedHash = digit => '0x' + digit.repeat(16);
const candidates = (hosts = ['linux','web']) => [...hosts.flatMap(host => ['0','1','fresh-game'].map(mode => ({
name:'fixture', host, mode, failures:[], pins:{ticks:{60:syntheticHash}, saves:{continuation:'a'.repeat(64)}},
}))), ...[...hosts.includes('web') ? ['web'] : [], nativeProofHost(hosts)].map(host => ({name:'fixture',host,mode:'0',profile:'release',failures:[],pins:{ticks:{60:syntheticHash},saves:{continuation:'a'.repeat(64)}}}))];
test('Windows baselines require all native and web modes plus matching release evidence', () => {
const hosts=['windows','web'], rows=candidates(hosts), old=rows[0].pins;
expect(agreePins(rows,old,hosts,'.')).toEqual({...old,hosts,modes:['0','1','fresh-game'],profiles:['gpu-dev','release']});
expect(()=>agreePins(rows.slice(0,-1),old,hosts,'.')).toThrow('windows release');
expect(()=>agreePins(rows.filter(row=>!(row.host==='windows'&&row.mode==='fresh-game')),old,hosts,'.')).toThrow('windows fresh-game missing');
const bad=structuredClone(rows); bad[4].pins.saves.continuation='b'.repeat(64);
expect(()=>agreePins(bad,old,hosts,'.')).toThrow('web 1 saves');
expect(()=>agreePins(rows,old,['web'],'.')).toThrow('linux or windows');
expect(nativeProofHost(['windows','linux','web'])).toBe('linux');
});
test('repin requires all modes and hosts to agree on every tick and save', () => {
const rows=candidates(), old=structuredClone(rows[0].pins);
expect(agreePins(rows, old, ['linux','web'], '.')).toEqual({...old,hosts:['linux','web'],modes:['0','1','fresh-game'],profiles:['gpu-dev','release']});
for (const section of ['ticks','saves']) {
const bad=structuredClone(rows), key=Object.keys(bad[4].pins[section])[0];
bad[4].pins[section][key]=section==='ticks'?repeatedHash('1'):'b'.repeat(64);
expect(()=>agreePins(bad,old,['linux','web'], '.')).toThrow(`web 1 ${section} ${key}`);
expect(rows[0].pins).toEqual(old);
}
expect(()=>agreePins(rows.slice(1),old,['linux','web'], '.')).toThrow('linux 0 missing');
expect(()=>agreePins(rows,{...old,ticks:{...old.ticks,90:syntheticHash}},['linux','web'], '.')).toThrow('did not observe ticks 90');
});
test('no-web repin records only linux and still requires three modes', () => {
const rows=candidates(['linux']), old=rows[0].pins;
expect(agreePins(rows,old,['linux'], '.').hosts).toEqual(['linux']);
expect(()=>agreePins(rows,old,['linux','web'], '.')).toThrow('web 0 missing');
expect(()=>agreePins(rows.slice(0,2),old,['linux'], '.')).toThrow('linux fresh-game missing');
});
test('pin failure gives the one regeneration command; collection bypasses only old pins', () => {
const calls=[], old={ticks:{60:syntheticHash},saves:{}};
const normal=pinRecorder(old,'fixture',(...args)=>calls.push(args));
normal.pin(60,{tick:60,hash:repeatedHash('1')});
expect(calls.at(-1)).toEqual([`pin 60 differs (expected ${syntheticHash}, got ${repeatedHash('1')}); if the change is intended: ${proofCommand(resolve(import.meta.dir,'prove.mjs'),'fixture','--repin')}`,false]);
expect(proofStatus({failures:calls.filter(([,ok])=>!ok),expected:old,pins:normal.pins})).toBe('FAIL');
const collecting=pinRecorder(old,'fixture',(...args)=>calls.push(args),true);
collecting.pin(60,{tick:59,hash:repeatedHash('1')});
expect(calls.at(-1)[1]).toBe(false);
collecting.pin(60,{tick:60,hash:repeatedHash('2')});
expect(calls.at(-1)).toEqual(['pin 60 repeated consistently',false]);
});
test('proof success requires both saved baselines and complete observations', () => {
const pins = {ticks:{60:syntheticHash}, saves:{continuation:'a'.repeat(64)}};
const checked = {failures:[], expected:structuredClone(pins), pins};
expect(proofStatus(checked)).toBe('PASS');
expect(proofStatus({...checked, expected:{}})).toBe('UNVERIFIED');
for (const section of ['ticks','saves']) {
expect(proofStatus({...checked, expected:{...pins,[section]:{}}})).toBe('UNVERIFIED');
expect(proofStatus({...checked, pins:{...pins,[section]:{}}})).toBe('UNVERIFIED');
}
expect(proofStatus({...checked, collecting:true})).toBe('UNVERIFIED');
expect(proofStatus({...checked, partial:true})).toBe('UNVERIFIED');
expect(proofStatus({...checked, failures:['gameplay assertion']})).toBe('FAIL');
});
test('facility report connects observed stalls/refusals to unused operations', () => {
expect(facilityReport([{method:'clock',reply:{settled:false}},{method:'tap',args:['sign'],error:'hidden behind camera'}]).join(' ')).toContain('layout unused');
expect(facilityReport([{method:'tap',args:['sign'],error:'hidden behind camera'},{method:'layout',args:['sign'],reply:{}}]).join(' ')).not.toContain('layout unused');
expect(facilityReport([{method:'clock',reply:{settled:true}}])).toEqual(['no recorded stalls or refusals']);
});
test('hidden placed-child refusal uses observed camera visibility and names layout', async () => {
const calls=[], s={tree:async target=>target ? {entities:[{name:'sign'}]} : {nodes:[{id:7,props:{testId:'world'},world:{}}]},
state:async name=>{calls.push(name);return {entity:{placed:{hidden:true}}};},
layout:async name=>({entity:{visible:{behindCamera:true}}})};
const error=await tapRefusal(s,'sign',new Error('no view matches sign'));
expect(error.message).toContain('hidden (behind the camera): `layout world:sign` (with --json before the quoted operation) shows visibility');
expect(calls).toEqual(['world:sign']);
});
test('automatic no-web fallback is only a missing configured browser, never a failed proof', () => {
expect(webUnavailable('web carrier unavailable: /missing/chrome: ENOENT; install Playwright\'s Chromium (docs/testing.md, Browsers), or set CHROME')).toBe(true);
for (const log of ['asset ENOENT', 'Chrome exited (1)', 'web carrier unavailable: chrome: EACCES;', 'FAIL web pixel assertion']) expect(webUnavailable(log)).toBe(false);
});
test('tap diagnostics never manufacture a missing-entity refusal for a UI-only target', async () => {
let queried=0;
const s={tree:async target=>target?{entities:[{name:'sign'}]}:{nodes:[{id:7,world:{}}]},
state:async()=>{queried++;throw new Error('must not query an unobserved name');}};
const error=await tapRefusal(s,'play',new Error('restore refused'));
expect(error.message).toBe('restore refused');
expect(queried).toBe(0);
});
test('layout CLI names behind-camera and unavailable projection without undefined coordinates', () => {
const text=render('layout',{entity:{name:'sign',screen:{unavailable:true},visible:{behindCamera:true,inFrustum:false}}});
expect(text).toContain('behindCamera'); expect(text).toContain('screen unavailable'); expect(text).not.toContain('undefined');
});
test('facility use must succeed and answer the relevant refusal', () => {
const failed={method:'tap',args:['sign'],error:'sign is hidden (behind the camera)'};
for(const call of [{method:'layout',args:['other'],reply:{}},{method:'layout',args:['sign'],error:'unavailable'}])
expect(facilityReport([failed,call]).join(' ')).toContain('layout unused');
expect(facilityReport([failed,{method:'layout',args:['world:sign'],reply:{entity:{}}}]).join(' ')).not.toContain('layout unused');
expect(facilityReport([{method:'tap',args:['hitbox'],error:'restore refused'}]).join(' ')).not.toContain('layout');
expect(facilityReport([{method:'tap',error:'assets pending'},{method:'state',args:['world:player'],reply:{}}]).join(' ')).toContain('state unused');
expect(facilityReport([{method:'clock',reply:{settled:true}}])).toEqual(['no recorded stalls or refusals']);
});
test('stale-build repair command names the rejected web dist', async () => {
const dist=mkdtempSync(resolve(tmpdir(),'r8b-dist-'));
try { await expect(assertWebDistApp(dist,{id:'com.test',dir:'/app',crate:()=> 'test-web'})).rejects.toThrow(`EXACT_WEB_DIST='${dist}'`); }
finally { rmSync(dist,{recursive:true,force:true}); }
});
test('repin derives manifests and shells without writing an authored file', async () => {
const {gameDefaults,gameShells}=await import('./app/shells.mjs');
const dir=mkdtempSync(resolve(tmpdir(),'r8b-manifest-'));
const before=process.env.EXACT_PROOF_REPIN;
try {
mkdirSync(resolve(dir,'logic/src'),{recursive:true});
writeFileSync(resolve(dir,'logic/src/lib.rs'),`impl Game for Test { const ID: &'static str = "fixture"; }`);
writeFileSync(resolve(dir,'app.json'),'{}');
process.env.EXACT_PROOF_REPIN='1';
gameShells(dir,gameDefaults(dir).game,import.meta.dir);
expect(readFileSync(resolve(dir,'app.json'),'utf8')).toBe('{}');
expect(readdirSync(dir).sort()).toEqual(['.shells','app.json','logic']);
expect(readdirSync(resolve(dir,'logic'))).toEqual(['src']);
} finally { if(before===undefined) delete process.env.EXACT_PROOF_REPIN; else process.env.EXACT_PROOF_REPIN=before; rmSync(dir,{recursive:true,force:true}); }
});
test('direct placement comparison rejects two hosts passing a two-pixel oracle', () => {
const a={initial:{tick:0,x:0,y:0,w:10,h:10},moving:{tick:60,x:5,y:5,w:10,h:10}}, b=structuredClone(a);
expect(comparePlacement(a,b)).toBe(true);
b.moving.x+=1.31;
expect(()=>comparePlacement(a,b)).toThrow('placement parity moving.x');
b.moving.x=a.moving.x+0.5;
expect(comparePlacement(a,b)).toBe(true);
});
for (const scenario of ['report','repin','external-repin', ...['ordinary','repeat','cwd','failure','UNVERIFIED','PASS'].map(command => `external-report-${command}`)]) test(`prove retains refused summaries and refuses missing requested repin hosts (${scenario})`, async () => {
const name=`r8b-tooling-${process.pid}-${scenario.toLowerCase()}`;
const native=process.platform==='win32'?'windows':'linux';
const directory = scenario.startsWith('external-') ? mkdtempSync(resolve(tmpdir(), 'prove external-')) : null;
const app=resolve(directory ?? resolve(import.meta.dir,'games'),name);
const pins={ticks:{1:syntheticHash},saves:{continuation:'a'.repeat(64)}};
mkdirSync(app);
if (scenario === 'external-repin') expect(spawnSync('git', ['init', '-q', app]).status).toBe(0);
try {
writeFileSync(resolve(app,'pins.json'),JSON.stringify(pins));
writeFileSync(resolve(app,'proof.mjs'),`
import {appendFileSync,mkdirSync,writeFileSync} from 'node:fs';
export function compare(rows) { if (rows.length !== 2) throw new Error('missing comparison rows'); console.log('COMPARE generic authored proof'); }
if (import.meta.main) {
const out=process.env.EXACT_PROOF_OUT, host=process.argv[2];
appendFileSync(${JSON.stringify(resolve(app,'calls.jsonl'))},JSON.stringify({host,build:process.argv.includes('--build-only'),mode:process.env.EXACT_GAME_PARANOID})+'\\n');
mkdirSync(out,{recursive:true});
const failed=!process.argv.includes('--build-only') && (process.env.R8B_FAIL==='1' || host==='web' && process.env.R8B_PASS_WEB!=='1');
const status=failed?'FAIL':process.env.R8B_UNVERIFIED==='1'||process.env.R8B_UNVERIFIED_HOST===host||process.env.EXACT_PROOF_REPIN==='1'?'UNVERIFIED':'PASS';
const row={name:${JSON.stringify(name)},inputs:'c'.repeat(64),sourceInputs:'d'.repeat(64),host,status,mode:process.env.EXACT_GAME_PARANOID,pins:${JSON.stringify(pins)},failures:failed?['refusal']:[],facilities:failed?['state unused; pending assets']:['no recorded stalls or refusals'],seconds:0,worlds:[{session:1,tick:1,hash:'same'}],saves:[{name:'a',sha256:'same'}]};
if(process.env.R15_DRIFT==='1' && process.env.EXACT_GAME_PROOF_PROFILE==='release') row.pins.ticks[1]='0x'+'d'.repeat(16);
writeFileSync(out+'/summary.json',JSON.stringify(row));
if(host==='web' && process.env.R8B_PASS_WEB!=='1') console.error('web carrier unavailable: /missing/chrome: ENOENT; set CHROME');
process.exit(failed?1:status==='UNVERIFIED'?3:0);
}
`);
const run=async(args,extra={},current=false)=>{
writeFileSync(resolve(app,'calls.jsonl'),'');
const p=Bun.spawn([process.execPath,resolve(import.meta.dir,'prove.mjs'),current ? '.' : directory ? app : name,...args],{cwd:current ? app : undefined,env:{...process.env,...extra},stdout:'pipe',stderr:'pipe'});
const [code,stdout,stderr]=await Promise.all([p.exited,new Response(p.stdout).text(),new Response(p.stderr).text()]);
const calls=readFileSync(resolve(app,'calls.jsonl'),'utf8').trim().split('\n').filter(Boolean).map(line=>JSON.parse(line));
return {code,text:stdout+stderr,calls,root:stdout.match(/^ARTIFACTS (.+)$/m)?.[1]};
};
if (scenario === 'report' || scenario.startsWith('external-report-')) {
const selected = command => !directory || scenario === `external-report-${command}`;
if (selected('ordinary')) {
const ordinary=await run(['--report']);
expect(ordinary.code).toBe(0);
expect(ordinary.calls).toEqual([{host:native,build:false,mode:'0'}]);
expect(ordinary.text).toContain(`REPORT ${native} 0: no recorded stalls or refusals`);
expect(JSON.parse(readFileSync(resolve(ordinary.root,'summary.json'),'utf8')).rows.map(row=>row.host)).toEqual([native]);
}
if (selected('repeat')) {
const repeated=await run(['--repeat','2']);
expect(repeated.code).toBe(0);
expect(repeated.calls).toEqual(Array(2).fill({host:native,build:false,mode:'0'}));
}
if (directory && selected('cwd')) {
const here=await run(['--report'],{},true);
expect(here.code).toBe(0);
expect(here.calls).toEqual([{host:native,build:false,mode:'0'}]);
}
if (selected('failure')) {
const failed=await run(['--hosts','linux','--report'],{R8B_FAIL:'1'});
// Time the proof child's entrypoint; SDK snapshotting precedes this leg.
if (directory) expect(Object.values(JSON.parse(readFileSync(resolve(failed.root,'legs.json'))).legs)[0].durationMs).toBeLessThan(1000);
expect(failed.code).toBe(1); expect(failed.text).toContain('REPORT linux 0: state unused');
const summary=JSON.parse(readFileSync(resolve(failed.root,'summary.json'),'utf8'));
expect(summary.rows.length).toBe(1); expect(summary.rows[0].failures).toEqual(['refusal']);
expect(summary.status).toBe('FAIL');
}
for (const status of ['UNVERIFIED','PASS'].filter(selected)) {
const completed=await run(['--hosts','linux','--report','--compare-saves'],{R8B_UNVERIFIED:status==='UNVERIFIED'?'1':'0'});
expect(completed.code).toBe(status === 'PASS' ? 0 : 3);
expect(completed.text).toContain(`| identical | ${status} |`);
expect(completed.text).toContain(`PROOF ${status} ${name}`);
expect(JSON.parse(readFileSync(resolve(completed.root,'summary.json'),'utf8')).status).toBe(status);
if (status === 'UNVERIFIED') expect(completed.text).toContain(`No complete tick/save baseline was checked. Generate it with ${proofCommand(resolve(import.meta.dir,'prove.mjs'),directory ? app : name,'--repin')}`);
expect(JSON.parse(readFileSync(resolve(app,'pins.json'),'utf8'))).toEqual(pins);
}
if (scenario === 'report') {
const web=await run(['--hosts','web'],{R8B_PASS_WEB:'1'});
expect(web.code).toBe(0);
expect(web.calls).toEqual([{host:'web',build:false,mode:'0'}]);
const compared=await run(['--compare-saves'],{R8B_PASS_WEB:'1'});
expect(compared.code).toBe(0);
expect(compared.text).toContain('COMPARE generic authored proof');
expect(compared.calls.length).toBe(4);
expect(compared.calls.filter(call=>!call.build).map(call=>call.host).sort()).toEqual([native,'web'].sort());
const mixed=await run(['--hosts','linux,web','--compare-saves'],{R8B_PASS_WEB:'1',R8B_UNVERIFIED_HOST:'web'});
expect(mixed.calls.slice(0,2)).toEqual([{host:'linux',build:true,mode:'0'},{host:'web',build:true,mode:'0'}]);
expect(mixed.calls.slice(2).map(call=>call.host).sort()).toEqual(['linux','web']);
expect(mixed.calls.slice(2).every(call=>!call.build)).toBe(true);
expect(mixed.code).toBe(3);
const summary=JSON.parse(readFileSync(resolve(mixed.root,'summary.json'),'utf8'));
expect(summary.rows.map(row=>row.status)).toEqual(['PASS','UNVERIFIED']);
expect(summary.status).toBe('UNVERIFIED');
expect(mixed.text).toContain(`PROOF UNVERIFIED ${name}`);
}
} else {
const refused=await run(['--repin']);
expect(refused.code).toBe(1); expect(refused.text).toContain('repin refused');
expect(refused.calls.map(call=>call.host)).toEqual([native,native,native,'web']);
expect(JSON.parse(readFileSync(resolve(app,'pins.json'),'utf8'))).toEqual(pins);
const allowed=await run(['--repin','--hosts','linux','--reason','Saved glow is a Tween sampled by the renderer']);
expect(allowed.code).toBe(0);
const written=JSON.parse(readFileSync(resolve(app,'pins.json'),'utf8'));
expect(written.reason).toBe('Saved glow is a Tween sampled by the renderer');
expect(written.inputs).toBe('c'.repeat(64));
expect(written.sourceInputs).toBe('d'.repeat(64));
if (scenario === 'external-repin') expect(written.at).toBe('inputs:'+'d'.repeat(64));
expect(written.game).toBe(name); expect(written.hosts).toEqual(['linux']); expect(written.generated).toEndWith('--hosts linux');
const empty={ticks:{},saves:{}};
writeFileSync(resolve(app,'pins.json'),JSON.stringify(empty));
for (const args of [['--repin'], ['--hosts','linux']]) {
const refused = await run(args);
expect(refused.code).toBe(1);
expect(refused.text).toContain(args[0] === '--repin' ? 'omit `--repin` for the first baseline' : 'first baseline requires a native host (linux or windows) and web');
expect(refused.calls).toEqual([]);
}
const firstRefused=await run([]);
expect(firstRefused.code).toBe(1);
expect(firstRefused.calls.map(call=>call.host)).toEqual([native,native,native,'web']);
expect(JSON.parse(readFileSync(resolve(app,'pins.json'),'utf8'))).toEqual(empty);
const first=await run([],{R8B_PASS_WEB:'1'});
expect(first.code).toBe(0);
expect(first.calls).toEqual([
...[native,'web'].flatMap(host=>['0','1','fresh-game'].map(mode=>({host,mode,build:false}))),
{host:'web',mode:'0',build:true},
]);
expect(JSON.parse(readFileSync(resolve(app,'pins.json'),'utf8')).hosts).toEqual([native,'web']);
}
} finally { rmSync(directory ?? app,{recursive:true,force:true}); }
}, 120000);
test('clock settle diagnostic names busy, held input, and logs on a real unsettled reply', async () => {
const source=readFileSync(resolve(import.meta.dir,'../scripts/agent.mjs'),'utf8');
const a=source.indexOf(" async clock("), b=source.indexOf('\n /** Pixels as PNG',a);
expect(a).toBeGreaterThanOrEqual(0); expect(b).toBeGreaterThan(a);
const s={now:0,op:async req=>{expect(req).toEqual({op:'clock',settle:true});return {clock:100,settled:false,world:{changing:['player']}};}};
// The method reads the module's step constant and growth rule; pass them in as the module would.
// A settle asks each drawing world for its tells (agent-inspect.mjs): none here.
const clock=new Function('s','CLOCK_STEP_MS','clockSpan','worldTells',`return ({${source.slice(a,b)}}).clock;`)(s,1000,clockSpan,async (_, r) => r);
const reply=await clock();
expect(reply.diagnostic).toContain('clock settle did not reach quiescence');
expect(reply.diagnostic).toContain('state world:* busy');
expect(reply.diagnostic).toContain('state shows held input');
expect(reply.diagnostic).toContain('logs shows reload/refusals');
});
function pinLiterals(path, text) {
const evidence=/(^|\/)(artifacts|diaries)\/|^game\/(bench|bakeoff\/[^/]+)\/results\/|^(llp|issues|vendor)\//;
if(/(^|\/)(Cargo\.lock|bun\.lock|shells\.lock)$/.test(path) || path.endsWith('/pins.json') || evidence.test(path) || path==='scripts/fixtures/fonts/SOURCE.md') return [];
const generated = path === '.llp/skills-receipt.json' || path.endsWith('/.baked-assets.json') || ['update/tests/it/fixtures/publisher/canonical.bin','update/tests/it/fixtures/publisher/exact.json'].includes(path);
if (generated) return [];
const samples = new Set(['b510eca2e2ef33f62f9ed57d6e7ce2d10'+'ebb2bdebc4a8e59d347719ba81abdf4', 'a1e3b04de97b11de564ce6e53b95f02954'+'a297f0008183ac63a4f5974f6b32d8', 'd97044e701822bac5a62696459b27d7b3'+'75aada5de8574ed4362edbba94771f7']);
const constants=new Set(['9e3779b1'+'85ebca87','c2b2ae3d'+'27d4eb4f'].map(v=>'0x'+v));
return [...text.matchAll(/0x[0-9a-fA-F]{16}|(?<![0-9a-fA-F])[0-9a-fA-F]{64}(?![0-9a-fA-F])/g)].map(m=>m[0])
.filter(value=>!(path==='game/render/src/world/upload.rs' && constants.has(value)) && !(path==='game/bake/tests/samples.rs' && samples.has(value)) && !(path==='game/engine/src/data/text.rs' && value==='0'.repeat(64)));
}
test('pin scan includes authored benchmark tests and ordinary digest strings', () => {
const digest='abcdef01'.repeat(8), hash='0x'+'12345678'.repeat(2);
expect(pinLiterals('game/bench/feel.test.mjs',hash)).toEqual([hash]);
for(const quote of ['"',"'",'`','']) expect(pinLiterals('game/engine/tests/foo.rs',quote+digest+quote).length).toBe(1);
expect(pinLiterals('game/bench/results/receipt.md',digest)).toEqual([]);
expect(pinLiterals('game/bakeoff/garden/results/2026-10-06-r1/exact2/DIARY.md',hash)).toEqual([]); // a results diary is evidence, as the bench's is
expect(pinLiterals('Cargo.lock',digest)).toEqual([]);
expect(pinLiterals('game/engine/src/data/text.rs','0'.repeat(64))).toEqual([]); // Decimal padding, not a world pin.
expect(pinLiterals('game/engine/src/data/text.rs',digest)).toEqual([digest]);
expect(pinLiterals('game/engine/tests/foo.rs','0'.repeat(64))).toEqual(['0'.repeat(64)]);
});
test('game pin literals stay in fixture pins across the game workspace', () => {
const root=resolve(import.meta.dir,'..');
// The game workspace's own files: the core's tests and experiments keep their own digests.
const tracked=Bun.spawnSync(['git','ls-files','-z','--','game'],{cwd:root}); expect(tracked.exitCode).toBe(0);
const violations=[];
for(const path of tracked.stdout.toString().split('\0').filter(Boolean)) {
let source;
try { source = readFileSync(resolve(root,path),'utf8'); }
catch (error) { if (error.code === 'ENOENT') continue; throw error; }
for(const value of pinLiterals(path,source)) violations.push(`${path}: ${value}`);
}
expect(violations).toEqual([]);
});
test('moving placement oracle rejects the old 1.31 pixel discrepancy', () => {
const source=readFileSync(resolve(import.meta.dir,'games/placement-fixture/proof.mjs'),'utf8');
const line=source.split('\n').find(s=>s.includes("check('moving displayed sign"));
const projected={x:492.88443,y:285.10403,w:93.112885,h:38.104492};
let accepted;
new Function('check','projected','after',line)((_,ok)=>accepted=ok,projected,{...projected,x:projected.x+1.31});
expect(accepted).toBe(false);
});
test('paranoid placement pins the reconstructed endpoint with the same half pixel limit', () => {
const source=readFileSync(resolve(import.meta.dir,'games/placement-fixture/proof.mjs'),'utf8');
const declaration=source.split('\n').find(s=>s.includes('const projected='));
const checkLine=source.split('\n').find(s=>s.includes("check('moving displayed sign"));
const run=new Function('check','projected','after',checkLine);
for(const mode of ['0','1','fresh-game']) {
const projected=new Function('process',`${declaration};return projected;`)({env:{EXACT_GAME_PARANOID:mode}});
expect(projected.x).toBe(mode==='0'?492.88443:494.20934);
let accepted;
run((_,ok)=>accepted=ok,projected,{...projected,x:projected.x+0.49});expect(accepted).toBe(true);
run((_,ok)=>accepted=ok,projected,{...projected,x:projected.x+0.51});expect(accepted).toBe(false);
}
});
test('forty-child capture does not mislabel painter timing as CPU cost', async () => {
const source=readFileSync(resolve(import.meta.dir,'games/placement-fixture/proof.mjs'),'utf8');
const a=source.indexOf(" if(process.argv.includes('--capture40')) {"),b=source.indexOf(' const start=',a);
const messages=[];
const session={tap:async()=>{},clock:async()=>{},screenshot:async()=>{},logs:async()=>[],close:async()=>{},
state:async()=>{throw new Error('paint.ms does not establish CPU cost');},world:()=>({snapshot:async()=>({entities:Array.from({length:40},()=>({components:{Placed:{}}}))})})};
const AsyncFunction=Object.getPrototypeOf(async function(){}).constructor;
await new AsyncFunction('process','open','resolve','out','host','say','check',source.slice(a,b))(
{argv:['--capture40']},async()=>session,resolve,'/tmp','linux',s=>messages.push(s),(name,ok)=>expect(ok).toBe(true));
expect(messages.join(' ')).toContain('no CPU-cost claim');
});
test('report does not count diagnostics from another session or infer geometry from asset names', () => {
expect(facilityReport([{session:1,method:'tap',args:['play'],error:'restore refused: asset hidden.model pending'}]).join(' ')).not.toContain('layout');
expect(facilityReport([{session:1,method:'tap',args:['sign'],error:'sign is hidden'}, {session:2,method:'layout',args:['sign'],reply:{}}]).join(' ')).toContain('layout unused');
expect(facilityReport([{session:1,method:'clock',reply:{settled:false}}, {session:1,method:'state',args:['world:*',null,false,true],reply:{busy:[]}}]).join(' ')).not.toContain('state unused');
});
test('report requires relevant diagnostics after the failure and at its clock or later',()=>{
for(const [method,args,error] of [['layout',['sign'],'sign is hidden'],['state',[],'restore asset refused'],['logs',[],'refused'],['state',['world:*',null,false,true],null]]) {
const diagnostic={session:1,method,args,reply:{},clock:10};
const failure={session:1,method:error?'tap':'clock',args:['sign'],...(error?{error}:{reply:{settled:false}}),clock:20};
const hint=method==='state'?'state unused':`${method} unused`;
expect(facilityReport([diagnostic,failure]).join(' ')).toContain(hint);
expect(facilityReport([failure,diagnostic]).join(' ')).toContain(hint);
expect(facilityReport([failure,{...diagnostic,clock:20}]).join(' ')).not.toContain(hint);
}
});
test('direct placement comparison refuses different simulation ticks',()=>{
const sample={x:1,y:2,w:3,h:4,tick:0};
expect(()=>comparePlacement({initial:sample,moving:{...sample,tick:60}},{initial:sample,moving:{...sample,tick:61}})).toThrow(/tick/);
});
test('refusal advice executes as real driver CLI operations with a global JSON flag', async()=>{
const s={tree:async target=>target?{entities:[{name:'sign'}]}:{nodes:[{id:7,props:{testId:'world'},world:{}}]},state:async()=>({entity:{placed:{hidden:true}}}),layout:async()=>({entity:{visible:{behindCamera:true}}})};
const refusal=await tapRefusal(s,'sign',Error('no view matches sign'));
const advised=[...refusal.message.matchAll(/`([^`]+)`/g)].map(m=>m[1]);
expect(advised).toEqual(['layout world:sign']);
const calls=[], output=[];
// The real CLI (`main`), with a fake session and no app server. Captured
// output must parse as JSON: if `--json` were ignored, `render`'s text
// would reach `JSON.parse` and throw here. `driveNotes` also reads
// `state`, so only the layout call and the replies are pinned exactly.
const fakeOpen=async()=>({layout:async target=>{calls.push(['layout',target]);return {visible:true};},state:async()=>{calls.push(['state']);return {world:[{loading:['crate.model'],assets:[]}]};},close:async()=>{}});
const realLog=console.log, realError=console.error;
console.log=x=>output.push(JSON.parse(x)); console.error=()=>{};
try {
for(const op of [...advised,'state']) expect(await main(['web','--json',op],{open:fakeOpen,driveServer:async()=>null})).toBe(0);
} finally { console.log=realLog; console.error=realError; }
expect(calls[0]).toEqual(['layout','world:sign']);
expect(calls).toContainEqual(['state']);
expect(output).toHaveLength(2);
expect(output[1].world[0].loading).toEqual(['crate.model']);
});
test('Fox predicate rejects a 95 percent white crop retaining orange pixels', async () => {
const {foxPixels}=await import('./games/skinned-fixture/proof.mjs');
const data=new Uint8Array(160*90*4).fill(255);
for(let y=0;y<90;y++) for(let x=0;x<160;x++) if((y*160+x)%20===0) data.set([180,90,30,255],(y*160+x)*4);
expect(foxPixels({width:160,height:90,data},{x:0,y:0,w:160,h:90},[160,90]).ok).toBe(false);
});
test('Fox predicate crops reported bounds and requires varied fur at the declared aspect',async()=>{
const {foxPixels}=await import('./games/skinned-fixture/proof.mjs');
const image={width:160,height:90,data:new Uint8Array(160*90*4).fill(255)};
for(let y=10;y<30;y++) for(let x=10;x<50;x++) image.data.set([150+(x%10)*8,70+(y%4)*4,20,255],(y*160+x)*4);
expect(foxPixels(image,{x:10,y:10,w:40,h:20},[160,90]).ok).toBe(true);
expect(foxPixels(image,{x:80,y:10,w:40,h:20},[160,90]).ok).toBe(false);
expect(foxPixels(image,{x:10,y:10,w:40,h:20},[90,160]).ok).toBe(false);
});
test('R12 proof inputs from game include host code and exclude other games', async () => {
const {proofInputFiles}=await import('./proof.mjs');
const files=proofInputFiles(import.meta.dir,resolve(import.meta.dir,'games/beacons'));
expect(files).toContain('../host/web/gpu-glue.js');
expect(files.some(f=>f.startsWith('games/greybox/'))).toBe(false);
expect(files.some(f=>f.startsWith('bench/'))).toBe(false);
});
test('R12 Fox crop accepts approximately 16:9 real screenshot coordinates', async () => {
const {foxPixels}=await import('./games/skinned-fixture/proof.mjs');
const width=321,height=180,data=new Uint8Array(width*height*4).fill(255);
for(let y=40;y<120;y++) for(let x=240;x<300;x++) data.set([160+(x%12)*8,70,25,255],(y*width+x)*4);
const image={width,height,data}, screen={x:240,y:40,w:60,h:80};
expect(foxPixels(image,screen,[width,height]).ok).toBe(true);
});
test('reused Chrome reads current-page GPU timing and clears storage, history and held input', async () => {
const {open} = await import('../scripts/agent.mjs');
let generation = 0;
const released = [];
const server = Bun.serve({port:0, fetch(request) {
const url = new URL(request.url);
if (url.pathname === '/released') { released.push(url.searchParams.get('event')); return new Response('ok'); }
return new Response(`<div id="exact-root" data-boot-ms="${++generation}"><button id="button">Input</button></div><script>
const button = document.getElementById('button');
for (const event of ['keyup','touchcancel']) addEventListener(event, e => fetch('/released?event='+event+(e.code || '')));
const database = () => new Promise((resolve,reject) => { const r=indexedDB.open('stage',1); r.onupgradeneeded=()=>r.result.createObjectStore('data'); r.onsuccess=()=>resolve(r.result); r.onerror=()=>reject(r.error); });
// glue.js's agent-mode surface: the driver asks through agentSettled.
const agent=async request=>{
if(request.op==='tags') return {clock:0};
if(request.op==='layout') return {nodes:[{id:1,x:0,y:0,w:100,h:40}]};
if(request.op==='timing') { document.getElementById('exact-root').dataset.gpuMs=request.ms; return {}; }
const db=await database();
if(request.op==='write') { const tx=db.transaction('data','readwrite'); tx.objectStore('data').put('secret','key'); await new Promise(r=>tx.oncomplete=r); history.pushState({},'', '/one'); history.pushState({},'', '/two'); db.close(); return {}; }
const tx=db.transaction('data'); const r=tx.objectStore('data').get('key'); const value=await new Promise(ok=>r.onsuccess=()=>ok(r.result??null)); db.close(); return {value,history:history.length};
};
window.exact={ready:Promise.resolve(), views:new Map([[1,button]]), agent, agentSettled:agent};
</script>`, {headers:{'content-type':'text/html'}});
}});
let first, second;
try {
first = await open({host:'web',url:server.url.href});
expect(await first.gpuMs()).toBeNull();
await first.carrier.ask({op:'timing',ms:0});
expect(await first.gpuMs()).toBe(0);
await first.carrier.ask({op:'timing',ms:12.5});
expect(await first.gpuMs()).toBe(12.5);
await first.carrier.ask({op:'write'});
await first.carrier.input(1,'key',{key:'Shift',phase:'down'});
await first.carrier.input(1,'down',{});
second = await open({host:'web',url:server.url.href,reuse:first.carrier});
expect(second.carrier).toBe(first.carrier);
expect(await second.gpuMs()).toBeNull();
await second.carrier.ask({op:'timing',ms:9.25});
expect(await second.gpuMs()).toBe(9.25);
expect(await second.carrier.ask({op:'state'})).toEqual({value:null,history:1});
expect(released).toContain('keyupShiftLeft');
expect(released).toContain('touchcancel');
expect(second.boot).toBeGreaterThan(first.boot);
} finally { await (second ?? first)?.close(); server.stop(true); }
}, 30000);
test('paranoid traversal restores the ordinary artifact only on web', async () => {
const restored = [];
for (const host of ['linux','web']) {
const modes = [];
expect(await paranoidRuns(async mode => {modes.push(mode); return 0;}, async () => {restored.push(host); return 0;}, host)).toBe(false);
expect(modes).toEqual(['0','1','fresh-game']);
}
expect(restored).toEqual(['web']);
});
test('shared residency probe takes the authored replacement model name', async () => {
const {residencyProbe} = await import('./render/tests/residency.mjs');
expect(residencyProbe('sample.model', 'texture.tex', 'reload.model').source).toContain("encode('reload.model')");
expect(() => residencyProbe('sample.model', 'texture.tex', 'longer.model-name')).toThrow('same byte length');
});
test('R13 output names nested in logic remain proof inputs and change the hash', async () => {
const {proofInputExcluded, proofInputFiles}=await import('./proof.mjs');
for(const name of ['target','.shells','dist','dist.previous','artifacts']) {
expect(proofInputExcluded(`game/games/beacons/logic/src/${name}/mod.rs`,'beacons')).toBe(false);
expect(proofInputExcluded(`game/games/beacons/${name}/mod.rs`,'beacons')).toBe(true);
}
const dir=mkdtempSync(resolve(tmpdir(),'r13-proof-'));
try {
const path=resolve(dir,'logic/src/target/mod.rs');mkdirSync(dirname(path),{recursive:true});writeFileSync(path,'before');
expect(proofInputFiles(dir,dir)).toContain('logic/src/target/mod.rs');
const hash=()=>{const value=buildInputHash('linux','target');for(const file of proofInputFiles(dir,dir)) value.update(file).update(readFileSync(resolve(dir,file)));return value.digest('hex');};
const before=hash();writeFileSync(path,'after');expect(hash()).not.toBe(before);
} finally {rmSync(dir,{recursive:true,force:true});}
});
test('a game\'s helper scripts are not proof inputs; its build inputs are', async () => {
const {proofInputExcluded}=await import('./proof.mjs');
for(const file of ['bench.mjs','live.mjs','jev-proxy.mjs','tools/probe.js','tools/levels.ops','proof.mjs','pins.json','README.md','drive.sh','shots/exported.json','repros/bug.contract','app.test.contract','logic/src/foo.test.rs'])
expect(proofInputExcluded(`game/games/forest/${file}`,'forest')).toBe(true);
for(const file of ['logic/src/lib.rs','logic/build.mjs','data/src/lib.rs','gpu/shaders/sky.wgsl','assets/x.js','render/view.mjs','app.contract','app.json','island.level.json','Cargo.toml','Cargo.lock'])
expect(proofInputExcluded(`game/games/forest/${file}`,'forest')).toBe(false);
// Shared SDK scripts stay inputs: the web glue is JavaScript.
expect(proofInputExcluded('host/web/gpu-glue.js','forest')).toBe(false);
expect(proofInputExcluded('../forest/bench.mjs','forest','../forest/')).toBe(true);
});
test('R13 Fox screenshot reply scales logical bounds at DPR 2 and 3', async () => {
const {foxScreenshotPixels}=await import('./games/skinned-fixture/proof.mjs');
for(const scale of [2,3]) {
const w=160,h=90,width=w*scale,height=h*scale,data=new Uint8Array(width*height*4).fill(255);
for(let y=20*scale;y<40*scale;y++) for(let x=90*scale;x<120*scale;x++) data.set([150+(x%10)*8,70,20,255],(y*width+x)*4);
const result=foxScreenshotPixels({width,height,data},{x:90,y:20,w:30,h:20},{w,h,scale});
expect(result.ok).toBe(true);expect(result.crop).toEqual([90*scale,20*scale,120*scale,40*scale]);
}
});
test('engine test sources are not bake inputs: editing one leaves the proof input hash alone', () => {
const root = mkdtempSync(resolve(tmpdir(), 'r14-inputs-'));
const app = resolve(root, 'game/games/fixture');
const file = resolve(root, 'game/engine/tests/regression.rs');
try {
mkdirSync(app, {recursive:true}); mkdirSync(dirname(file), {recursive:true});
writeFileSync(resolve(root, 'game/engine/Cargo.toml'), '[package]\nname = "engine"\nversion = "0.1.0"\n');
writeFileSync(file, 'before');
const hash = () => {
const h = buildInputHash('linux', 'target');
for (const path of proofInputFiles(root, app)) h.update(path).update(readFileSync(resolve(root,path)));
return h.digest('hex');
};
// Integration tests are never compiled into a bake; hashing them would rebake every game for a test edit.
const before = hash(); writeFileSync(file, 'after'); expect(hash()).toBe(before);
} finally { rmSync(root, {recursive:true,force:true}); }
});
test('E10 Linux proof profile changes invalidate the build receipt, native paranoid mode does not', () => {
expect(buildInputHash('linux', 'target', '0', 'gpu-dev').digest('hex'))
.not.toBe(buildInputHash('linux', 'target', '0', 'release').digest('hex'));
expect(buildInputHash('linux', 'target', '1', 'gpu-dev').digest('hex'))
.toBe(buildInputHash('linux', 'target', '0', 'gpu-dev').digest('hex'));
});
test('E10 snapshot-only proof records the same world observation as state', async () => {
const observations=new Map();
const record=worldObservations(observations, 1);
const session={state:async target => {
const reply=target ? {tick:180,hash:'abc',entities:[]} : {world:[{tick:180,hash:'abc'}]};
record(reply); return reply;
}};
await worldView(session,'world').snapshot();
const snapshot=[...observations.values()];
expect(snapshot).toEqual([{session:1,tick:180,hash:'abc'}]);
observations.clear(); await session.state();
expect([...observations.values()]).toEqual(snapshot);
});
test('E10 browser buttons retain UA keyboard focus and hover feedback', async () => {