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.

3.2 KB
Raw Blame
import {test} from 'node:test';import {get as httpGet} from 'node:http';import assert from 'node:assert/strict';
import {mkdtemp,cp,rm,readFile,readdir,writeFile,mkdir} from 'node:fs/promises';import {tmpdir} from 'node:os';import {join} from 'node:path';import {fileURLToPath} from 'node:url';
import {createGameViewer} from '../viewer/viewer.mjs';
import {buildGame} from '../template/tools/build.mjs';
const packageRoot=fileURLToPath(new URL('..',import.meta.url));
test('source saves preserve both files and a full prior edition; stale or foreign writes cannot replace it',async()=>{const scratch=await mkdtemp(join(tmpdir(),'relay-viewer-')),root=join(scratch,'workspace');process.env.GAME_DSH_DIR=packageRoot;await cp(join(packageRoot,'template'),root,{recursive:true});const s=await createGameViewer(root,{home:scratch});await new Promise(r=>s.listen(0,'127.0.0.1',r));const url='http://127.0.0.1:'+s.address().port;try{
 const get=()=>fetch(url+'/api/project').then(r=>r.json()),original=(await get()).project;assert.ok(original.revision);const next={...original,title:'Changed original game',rules:original.rules+'\n// revised rules source\n'};
 const put=(p,origin=url,rev=original.revision)=>fetch(url+'/api/project',{method:'PUT',headers:{origin,'content-type':'application/json','if-match':rev},body:JSON.stringify(p)});
 assert.equal((await put(next,'https://other.example')).status,403);assert.equal((await put(next)).status,200);const saved=(await get()).project;assert.equal(saved.title,next.title);assert.equal(saved.rules,next.rules);assert.equal(JSON.parse(await readFile(join(root,'game/project.json'),'utf8')).rules,undefined);assert.equal(await readFile(join(root,'game/rules.mjs'),'utf8'),next.rules);
 const previous=JSON.parse(await readFile(join(root,'.harness/history',(await readdir(join(root,'.harness/history')))[0]),'utf8'));assert.equal(previous.rules,original.rules);assert.equal(previous.title,original.title);assert.equal((await put(original)).status,409);assert.equal((await get()).project.title,next.title);
 const broken={...saved,rules:'export function initial( {'};assert.equal((await put(broken,url,saved.revision)).status,400);assert.equal((await get()).project.revision,saved.revision);
 const html=await fetch(url).then(r=>r.text());assert.match(html,/Changed original game/);assert.equal((await fetch(url+'/files/.harness/history')).status,404);assert.equal(await new Promise((resolve,reject)=>{httpGet(url+'/api/project',{headers:{host:'other.example'}},res=>{res.resume();resolve(res.statusCode);}).on('error',reject);}),403);
 }finally{s.closeAllConnections();await new Promise(r=>s.close(r));await rm(scratch,{recursive:true,force:true});}});
test('build rejects imported dependencies so a game source remains self-contained',async()=>{const scratch=await mkdtemp(join(tmpdir(),'relay-build-'));process.env.GAME_DSH_DIR=packageRoot;await cp(join(packageRoot,'template'),scratch,{recursive:true});try{const raw=JSON.parse(await readFile(join(scratch,'game/project.json'),'utf8'));await assert.rejects(buildGame(scratch,{project:{...raw,rules:"import x from 'https://example.org/rules.js'; export {x}"}}),/self-contained/);}finally{await rm(scratch,{recursive:true,force:true});}});