feat(semantics): version scene edits and refine paint geometry

This commit is contained in:
DCCONSTRUCTIONS
2026-11-04 00:42:55 +03:00
parent 7a3c3c00b3
commit b7b5ac9cf0
34 changed files with 3014 additions and 138 deletions
@@ -144,3 +144,15 @@ test('camera snapshot restores exact pose, orthographic scale and orbit anchor a
assert.deepEqual(next.c.snapshot(),old);next.c.dispose();
}
});
test('navigation blockers are independent and only their owners can release them',()=>{
const f=fixture();f.c.setProjection('orthographic');
const wheel=()=>f.event('wheel',{deltaX:0,deltaY:-120,deltaMode:0});
const span=()=>f.c.camera.top;
try{
const initial=span();f.c.setNavigation(false);f.c.setNavigation(false,'semantic');
f.c.setNavigation(true);wheel();assert.equal(span(),initial,'planner release must not clear semantic lock');
f.c.setNavigation(false);f.c.setNavigation(true,'semantic');wheel();assert.equal(span(),initial,'semantic release must not clear planner lock');
f.c.setNavigation(true);wheel();assert.ok(span()<initial,'input resumes immediately without blur');
}finally{f.c.dispose();}
});
@@ -5,7 +5,12 @@ import {fileURLToPath} from 'node:url';
import path from 'node:path';
import ts from 'typescript';
const url=s=>`data:text/javascript;base64,${Buffer.from(s).toString('base64')}`;
const hooks=url('export const useRef=x=>({current:x});export const useState=x=>[x,()=>{}];export const useEffect=f=>{globalThis.cleanups.push(f());};');
const hooks=url(`
export function useRef(x){const h=globalThis.semanticHooks,i=h.cursor++;return h.slots[i]??(h.slots[i]={current:x});}
export function useState(x){const h=globalThis.semanticHooks,i=h.cursor++;const s=h.slots[i]??(h.slots[i]={value:x});return [s.value,()=>{}];}
export function useEffect(f,deps){const h=globalThis.semanticHooks,i=h.cursor++,s=h.slots[i];
if(!s||!deps||deps.some((v,k)=>!Object.is(v,s.deps[k])))h.pending.push({i,f,deps,cleanup:s?.cleanup});}
`);
const cache=new Map();
function module(p){
p=path.resolve(p);if(cache.has(p))return cache.get(p);
@@ -26,19 +31,23 @@ class Target {
setPointerCapture(i){this.capture.add(i);}hasPointerCapture(i){return this.capture.has(i);}releasePointerCapture(i){this.capture.delete(i);}
}
function setup({marking=false,whole=false}={}){
globalThis.cleanups=[];globalThis.document=new Target();document.querySelector=()=>null;globalThis.window=new Target();
globalThis.semanticHooks={slots:[],cursor:0,pending:[]};globalThis.document=new Target();document.querySelector=()=>null;globalThis.window=new Target();
const canvas={clientWidth:100,clientHeight:100,getBoundingClientRect:()=>({left:0,top:0}),focus(){},closest:()=>null};
const viewport=new Target();viewport.querySelector=()=>canvas;
const map={features:[{id:'line',properties:{class:marking?'road_marking':'road_boundary',...(marking?{subtype:'linear_marking'}:{})},geometry:marking?{type:'Polygon',coordinates:[[[0,0,2],[10,0,2],[10,1,2],[0,1,2],[0,0,2]]]}:{type:'LineString',coordinates:[[0,0,2],[10,0,2],[20,0,2]]}}]};
const selected={feature_id:'line',path:0,index:marking?1:2,...(whole?{object:true}:{})};
const previews=[],commits=[],errors=[],selections=[];let navigation=true;
const p={map,viewport,layers:[map.features[0].properties.class],selected,selection:[selected],editing:true,blocked:false,tool:'select',template:null,axis:'x',areaMode:'union',
runtime:{semanticSegmentParameter:(_a,_b,t)=>t,projectSemantic:v=>[v[0]/100,v[1]/100],moveSemantic:(v,x,y)=>[x*100,y*100,v[2]],setNavigation:v=>{navigation=v;}},
const previews=[],commits=[],errors=[],selections=[];const navigationBlocks=new Set();
let p={map,viewport,layers:[map.features[0].properties.class],selected,selection:[selected],editing:true,blocked:false,tool:'select',template:null,axis:'x',areaMode:'union',
runtime:{semanticSegmentParameter:(_a,_b,t)=>t,projectSemantic:v=>[v[0]/100,v[1]/100],moveSemantic:(v,x,y)=>[x*100,y*100,v[2]],setNavigation:(v,owner='planner')=>{if(v)navigationBlocks.delete(owner);else navigationBlocks.add(owner);}},
onPreview:op=>previews.push(op),onOperation:op=>{semanticOperation(map,op);commits.push(op);},onSelection:v=>selections.push(v),onSelect:v=>selections.push(v?[v]:[]),onChoose(){},onError:e=>errors.push(e),onPlaced(){},onExit(){}};
useSemanticGestures(p);
const hooks=globalThis.semanticHooks;
const render=next=>{p={...p,...next};hooks.cursor=0;hooks.pending=[];useSemanticGestures(p);
for(const effect of hooks.pending)effect.cleanup?.();
for(const effect of hooks.pending)hooks.slots[effect.i]={deps:effect.deps,cleanup:effect.f()};};
render({});
const axis={closest:q=>q.includes('axis')?{dataset:{semanticAxis:'x'}}:null};
const event=(x,o={})=>({target:axis,button:0,pointerId:1,clientX:x,clientY:0,shiftKey:true,...o});
return {p,canvas,viewport,event,previews,commits,selections,errors,navigation:()=>navigation,dispose:()=>{for(const c of cleanups)c();}};
return {get p(){return p;},render,canvas,viewport,event,previews,commits,selections,errors,navigation:()=>navigationBlocks.size===0,dispose:()=>{for(const slot of hooks.slots)slot?.cleanup?.();}};
}
test('Shift pivot duplicates once on release; click and Escape do not commit',()=>{
const s=setup(),snapshot=JSON.stringify(s.p.map);
@@ -191,3 +200,32 @@ test('Shift Y at a facade endpoint extends around a corner and commits preview o
assert.deepEqual(semanticOperation(s.p.map,s.commits[0]).features[0].geometry.coordinates,[[0,.1,2],[10,0,2],[10,3,2]]);
}finally{s.dispose();}
});
test('version load releases camera without blur after overlay changes while blocked',()=>{
const s=setup();
try{
s.render({editing:false});
for(let i=0;i<3;i++){
s.render({blocked:true,layers:[...s.p.layers]});assert.equal(s.navigation(),false);
s.render({blocked:false});assert.equal(s.navigation(),true,'load completion must restore input without window blur');
}
}finally{s.dispose();}
});
test('loading cancels a captured edit and stale pointerup cannot commit it',()=>{
const s=setup();
try{
s.viewport.emit('pointerdown',s.event(30));s.viewport.emit('pointermove',s.event(45));
assert.equal(s.viewport.capture.size,1);
s.render({blocked:true});assert.equal(s.viewport.capture.size,0);assert.equal(s.navigation(),false);
s.viewport.emit('pointerup',s.event(45));assert.equal(s.commits.length,0);
s.render({blocked:false});assert.equal(s.navigation(),true);
}finally{s.dispose();}
});
test('semantic cleanup releases its lock but never clears an independent planner block',()=>{
const s=setup();
s.p.runtime.setNavigation(false);s.render({blocked:true});s.render({blocked:false});
assert.equal(s.navigation(),false,'planner remains blocked');
s.dispose();assert.equal(s.navigation(),false);
s.p.runtime.setNavigation(true);assert.equal(s.navigation(),true);
const t=setup();t.render({blocked:true});t.dispose();assert.equal(t.navigation(),true,'leaving semantic view releases its own loading lock');
});
@@ -0,0 +1,68 @@
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);
});