Files
NODEDC_MISSION_CORE/apps/control-station/test/semanticVersions.test.mjs
T

69 lines
5.9 KiB
JavaScript

import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {test} from 'node:test';
import ts from 'typescript';
const url=code=>`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`;
const compile=path=>ts.transpileModule(readFileSync(new URL(path,import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.ESNext,target:ts.ScriptTarget.ES2022}}).outputText;
const versionsUrl=url(compile('../src/core/simulation/semanticVersions.ts'));
const draftUrl=url(`export const semanticDraft=base=>({base,map:base,operations:[],future:[]});export const semanticRequest=(...args)=>globalThis.docRequest(...args);`);
const sessionUrl=url(compile('../src/core/simulation/semanticDocument.ts').replace('"./semanticEdits"',JSON.stringify(draftUrl)).replace('"./semanticVersions"',JSON.stringify(versionsUrl)));
const {SemanticDocumentSession,getSemanticSession,hasUnsavedSemanticSessions}=await import(sessionUrl);
const {chooseVersion,versionQuery,versionOptions}=await import(versionsUrl);
const list={raw_sha256:'raw-sha',algorithmic_sha256:'auto-sha',manual:[{branch_id:null,revision:'legacy',model_sha256:'old-sha',created_at:'2026-10-29T09:00:00Z'}]};
const map={source:{world_sha256:'world'},features:[]};
const value=(sha='auto-sha')=>({schema_version:'missioncore.semantic-editor/v1',branch_id:null,revision:null,model_sha256:sha,latest_model_sha256:sha,model:map,edited:map,history:[]});
const deferred=()=>{let resolve,reject;const promise=new Promise((a,b)=>{resolve=a;reject=b;});return {promise,resolve,reject};};
const setup=()=>{let ids=0;const bodies=[];const transport={uuid:()=>`uuid-${++ids}`,versions:async()=>list,request:async(_w,body,_s,q)=>{if(body)bodies.push(body);return value(q?.model_sha256);}};return {s:new SemanticDocumentSession('id','world','algorithmic',transport),transport,bodies};};
const modify=s=>{s.setEditing(true);s.setDraft(d=>({...d,operations:[{kind:'move'}],map:{...d.map,modified:true}}));};
const saved=body=>({...value(body.model_sha256),branch_id:body.branch_id??null,revision:'saved',edited:{...map,modified:true},history:[{revision:'saved',created_at:'2026-10-29T10:00:00Z',actions:1}]});
test('three representations and no fabricated manual',()=>{
assert.equal(versionOptions({...list,manual:[]}).length,2);
assert.equal(chooseVersion('manual',list),'manual:legacy');
assert.equal(chooseVersion('raw',{...list,raw_sha256:null}),'algorithmic');
assert.deepEqual(versionQuery('manual:legacy',list),{source:'manual'});
assert.deepEqual(versionQuery('raw',list),{source:'raw',model_sha256:'raw-sha'});
assert.throws(()=>versionQuery('manual:missing',list));
});
test('failed selection retains matching old identity and geometry; late GET cannot replace newer selection',async()=>{
const {s,transport}=setup();await s.open();const before=s.snapshot();
transport.request=async()=>{throw new Error('unavailable');};
await s.open('manual:legacy');
assert.equal(s.snapshot().selected,'algorithmic');assert.equal(s.snapshot().revision,before.revision);assert.equal(s.snapshot().error,'unavailable');
const old=deferred(),fresh=deferred();let calls=0;
transport.request=()=>++calls===1?old.promise:fresh.promise;
const a=s.open('raw');await Promise.resolve();const b=s.open('algorithmic');await Promise.resolve();
fresh.resolve(value('fresh'));await b;old.resolve(value('old'));await a;
assert.equal(s.snapshot().selected,'algorithmic');assert.equal(s.snapshot().revision.model_sha256,'fresh');
});
test('raw remains read-only including direct session mutation',async()=>{
const {s}=setup();await s.open('raw');modify(s);await s.save();
assert.equal(s.canEdit,false);assert.equal(s.snapshot().editing,false);assert.equal(s.dirty,false);
});
test('save survives detached subscriber, blocks duplicate save/edit, and publishes manual only after success',async()=>{
const {s,transport,bodies}=setup();await s.open();modify(s);const pending=deferred();
transport.request=async(_w,body)=>{bodies.push(body);return pending.promise;};
let oldNotices=0;const detach=s.subscribe(()=>oldNotices++);const saving=s.save();detach();
let newNotices=0;const detachNew=s.subscribe(()=>newNotices++);
assert.equal(s.snapshot().saving,true);assert.equal(s.protected,true);assert.equal(s.snapshot().selected,'algorithmic');
await s.save();s.select('raw');s.setDraft(null);assert.equal(bodies.length,1);assert.equal(s.dirty,true);
pending.resolve(saved(bodies[0]));await saving;detachNew();
assert.ok(oldNotices>0);assert.ok(newNotices>0);assert.equal(s.dirty,false);assert.equal(s.snapshot().selected,'manual:uuid-1');
assert.equal(s.snapshot().versions.manual.length,2);assert.equal(s.snapshot().versions.manual[1].revision,'legacy');
});
test('409 retains draft and exact idempotency key for retry; cancellation never creates manual',async()=>{
const {s,transport,bodies}=setup();await s.open();modify(s);
transport.request=async(_w,body)=>{bodies.push(body);throw new Error('409 conflict');};
await s.save();await s.save();assert.equal(bodies[0].request_id,bodies[1].request_id);
assert.equal(s.dirty,true);assert.equal(s.snapshot().revision.revision,null);assert.equal(s.snapshot().versions.manual.length,1);
s.discard();assert.equal(s.dirty,false);assert.equal(s.snapshot().versions.manual.length,1);
});
test('dirty scene remains protected while another scene mounts and pending request belongs to same session',async()=>{
globalThis.fetch=async()=>({ok:true,json:async()=>list});
globalThis.docRequest=async(_w,body,_signal,q)=>value(q?.model_sha256);
const a=getSemanticSession('A','world','algorithmic');await a.open();assert.equal(a.snapshot().error,null);modify(a);assert.equal(a.dirty,true,JSON.stringify(a.snapshot()));
const b=getSemanticSession('B','clean','algorithmic');assert.notEqual(a,b);
assert.equal(hasUnsavedSemanticSessions(),true);assert.equal(getSemanticSession('A','world','raw'),a);
a.discard();assert.equal(hasUnsavedSemanticSessions(),false);
});