Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

4.7 KB
Raw Blame
import {test} from 'node:test';import assert from 'node:assert/strict';
import {fixtures,conservatory,signalGarden,nightFerry,revise} from './fixtures.mjs';
import {Game,simulate} from '../template/studio/engine.mjs';
import {validateProject,clone,canonical,history,digest} from '../template/studio/project.mjs';
import {componentSVG,sheets,kitFiles} from '../template/studio/formats.mjs';
const moduleFor=p=>import('data:text/javascript;base64,'+Buffer.from(p.rules).toString('base64'));
for(const p of await fixtures())test(p.title+' repeats, conserves game state and reopens every recorded playtest',async()=>{const rules=await moduleFor(p),a=simulate(p,rules,{count:80}),b=simulate(p,rules,{count:80});assert.deepEqual(a,b);assert.equal(a.completed,80);assert.equal(a.errors,0);assert.equal(a.capped,0);for(const row of a.games){const replay=new Game(p,rules,row.seed);replay.restore(row.replay);assert.equal(canonical(replay.state),row.replay.state);}const q=revise(p);assert.notEqual(await digest(p),await digest(q));for(const c of p.components.filter(c=>c.locked))assert.deepEqual(q.components.find(x=>x.id===c.id),c);});
test('only legal choices transition; undo and replay recover exact states',async()=>{const p=await conservatory(),rules=await moduleFor(p),g=new Game(p,rules),state=g.state;assert.throws(()=>g.step('invented'),/not legal/);assert.deepEqual(g.state,state);g.step(g.legal()[0].id);const replay=g.replay();g.undo();assert.deepEqual(g.state,state);g.restore(replay);assert.equal(canonical(g.state),replay.state);replay.state='{}';assert.throws(()=>new Game(p,rules,replay.seed).restore(replay),/does not match/);});
test('private observation contains only current hand and public gardens',async()=>{const p=await conservatory(),rules=await moduleFor(p),g=new Game(p,rules);const v=g.view();assert.equal(v.view.private,true);assert.equal(v.view.zones[0].items.length,g.state.hands[0].length);assert.equal(v.view.zones.flatMap(z=>z.items).length,g.state.hands[0].length);const seen=new Set();const bot={...rules,bot(view,actions){assert.equal(view.hands,undefined);seen.add(view.title);return actions[0].id;}};simulate(p,bot,{count:2,policy:'authored'});assert.ok(seen.size>1);});
test('the spatial puzzle has reachable win and loss states from legal human choices',async()=>{const p=signalGarden(),rules=await moduleFor(p),g=new Game(p,rules,'route-proof');for(let i=0;i<9&&!g.outcome();i++)while(g.state.rotations[i]&&!g.outcome())g.step('rotate-'+i);assert.deepEqual(g.outcome().winners,[0]);const lose=new Game(p,rules,'route-proof');while(!lose.outcome())lose.step('rotate-0');assert.deepEqual(lose.outcome().winners,[]);});
test('push-your-luck docking banks cargo and storms lose only the active voyage',async()=>{const p=nightFerry(),rules=await moduleFor(p),g=new Game(p,rules,'cargo-proof');while(g.state.cargo===0)g.step('draw');const who=g.state.player,cargo=g.state.cargo;g.step('dock');assert.equal(g.state.scores[who],cargo);const old=g.state.scores;while(g.state.player!==who)g.step('draw');assert.deepEqual(g.state.scores,old);});
test('dead ends, mutated inputs, invalid output and action no-ops are caught',async()=>{const p=await conservatory(),rules=await moduleFor(p);assert.throws(()=>new Game(p,{...rules,legal:()=>[]}),/Dead end/);const mut=new Game(p,{...rules,apply:s=>{s.player=1;return s;}});assert.throws(()=>mut.step(mut.legal()[0].id),/read only|readonly/);const noop=new Game(p,{...rules,apply:s=>clone(s)});assert.throws(()=>noop.step(noop.legal()[0].id),/must change/);assert.throws(()=>new Game(p,{...rules,initial:()=>({player:0,bad:Infinity})}),/non-finite/);});
test('source schema, edition history and printable inventory reject damaged input',async()=>{const p=await conservatory();for(const mutate of [q=>q.components[0].quantity=0,q=>q.components.push(q.components[0]),q=>q.settings[0].value=Infinity,q=>q.components[0].color='url(https://bad)',q=>q.components[0].face='javascript:bad']){const q=clone(p);mutate(q);assert.throws(()=>validateProject(q));}const h=history(p);h.push(revise(p));assert.deepEqual(h.undo(),p);assert.deepEqual(h.redo(),revise(p));const pages=sheets(p);assert.equal(pages.flat().length,p.components.reduce((n,c)=>n+c.quantity,0));for(const i of pages.flat()){assert.ok(i.x>=10&&i.y>=10&&i.x+i.width<=200&&i.y+i.height<=287);}const files=kitFiles(p,'<!doctype html>');assert.equal(files['source/rules.mjs'],p.rules);assert.deepEqual(JSON.parse(files['project.relay.json']),p);const custom=clone(p.components[0]);custom.face='data:image/svg+xml;base64,'+Buffer.from('<svg xmlns="http://www.w3.org/2000/svg"><text>Original artwork</text></svg>').toString('base64');assert.match(componentSVG(custom),/<image/);assert.doesNotMatch(componentSVG(custom),/<text>Original artwork/);});