OneAlign / web /tests /grounded.test.mjs
loveaa's picture
Deploy Home Pulse: grounded setup, isolated workspaces and shared HF visual inference
3f3b6c0 verified
Raw History Blame Contribute Delete
13.2 kB
import test from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import {createGroundedService,GroundedRepository,assertActivation,trialVerdict} from '../server/grounded-service.mjs'
import {Journal} from '../server/local-api.mjs'
const picture=n=>'data:image/jpeg;base64,'+Buffer.from('test-frame-'+n).toString('base64')
const binding=()=>({source:{id:'camera-a',name:'测试画面',camera:'客厅',url:'/media/test.mp4',type:'video'},referenceImage:picture(0),annotatedReference:picture(1),targetCrop:picture(2),targetBox:{x:.2,y:.2,w:.2,h:.2},regionBox:{x:.1,y:.1,w:.8,h:.8},targetName:'小狗',regionName:'围栏',targetDescriptionEn:'Old automatic guess',regionDescriptionEn:'Old region guess',referenceMediaTime:0,matchMode:'instance'})
const frame=(t,sourceId='camera-a')=>({image:picture(t+10),annotated:picture(t+20),pairImage:picture(t+30),mediaTime:t,capturedAt:new Date().toISOString(),frameId:'frame-'+t,sourceId})
const fakeRouter=async()=>({model:'fake-test-model',choices:[{message:{content:JSON.stringify({targetDescriptionEn:'Small white dog selected by the user',regionDescriptionEn:'Black pet enclosure behind the railing'})}}]})
function fixture(choices=[]){
const root=fs.mkdtempSync(path.join(os.tmpdir(),'hp-grounded-'));fs.writeFileSync(path.join(root,'.env.local'),'ANKER_KIMI_API_KEY=test-only')
const journal=new Journal(path.join(root,'.local','test-telemetry.jsonl'));let calls=0
const fetcher=async()=>{calls++;const choice=choices.shift()||'other';return new Response(JSON.stringify({answers:{q1:{choice,abstained:choice==='unknown'}},usage:{total_ms:10}}))}
const service=createGroundedService(root,{journal:()=>journal,enqueue:fn=>fn(),router:fakeRouter,visionFetch:fetcher})
return {root,journal,service,calls:()=>calls,call:(method,url,body={})=>service.handle(method,new URL(url,'http://localhost'),body),cleanup:()=>{assert.ok(path.resolve(root).startsWith(path.resolve(os.tmpdir())+path.sep+'hp-grounded-'));fs.rmSync(root,{recursive:true,force:true})}}
}
async function prepare(f,kind='dog_boundary'){
return f.call('POST','/api/setup/prepare',{request:kind==='keys_check'?'出门时看看钥匙还在不在':'小狗整个身体出了笼子告诉我',binding:binding(),taskKind:kind,answers:[{question:'人为抱出是否提醒',answer:'不提醒'}],notification:'auto',confirmed:true})
}
test('an explicit carried-out exclusion in the original request survives without repeated questions',async()=>{
const f=fixture()
try{
const r=await f.call('POST','/api/setup/prepare',{request:'小狗整个身体出笼就提醒我,人抱出去不算',binding:binding(),taskKind:'dog_boundary',answers:[],notification:'auto',confirmed:true})
assert.match(r.plan.summary_zh,/有人抱出时不提醒/)
assert.match(r.plan.observers[0].options.target,/without being visibly held or carried/)
const included=await f.call('POST','/api/setup/prepare',{request:'小狗整个身体出笼就提醒我,人抱出去也提醒',binding:binding(),taskKind:'dog_boundary',answers:[],notification:'auto',confirmed:true})
assert.doesNotMatch(included.plan.observers[0].options.target,/without being visibly held or carried/)
}finally{f.cleanup()}
})
test('activation requires both confirmations and a recent passing trial',()=>{
const setup={plan:{status:'ready'},trial:{passed:true,at:new Date().toISOString()}}
assert.throws(()=>assertActivation(setup,{target:false,rule:true}))
assert.throws(()=>assertActivation({...setup,trial:{...setup.trial,passed:false}},{target:true,rule:true}))
assert.throws(()=>assertActivation(setup,{target:true,rule:true},Date.now()+90000000))
assert.doesNotThrow(()=>assertActivation(setup,{target:true,rule:true}))
})
test('Unknown trial results do not count as clear observations',()=>{
const setup={plan:{observers:[{id:'q1',title:'观察',input:'reference'}]}}
const ground=['target','region'].map(kind=>({kind,answer:{choice:'clear',abstained:false}}))
assert.equal(trialVerdict(setup,ground,[{answers:{q1:{choice:'target',abstained:true}}},{answers:{q1:{choice:'other',abstained:false}}}]).passed,false)
})
test('corrected selection replaces old visual description, and invalidated setup cannot activate',async()=>{
const f=fixture();try{const s=await prepare(f);assert.equal(s.binding.targetDescriptionEn,'Small white dog selected by the user');assert.equal(s.plan.rules[0].mode,'change');assert.match(s.plan.summary_zh,/有人抱出时不提醒/);await assert.rejects(()=>f.call('POST','/api/setup/activate',{setupId:s.id,confirmation:{target:true,rule:true}}));assert.equal(f.service.repo.list('memory').length,0)}finally{f.cleanup()}
})
test('trial rejects duplicate frames and source changes before inference',async()=>{
const f=fixture();try{const s=await prepare(f);await assert.rejects(()=>f.call('POST','/api/setup/trial',{setupId:s.id,frames:[frame(0),frame(0),frame(2)]}));await assert.rejects(()=>f.call('POST','/api/setup/trial',{setupId:s.id,frames:[frame(0),frame(1,'other-camera'),frame(2)]}));assert.equal(f.calls(),0)}finally{f.cleanup()}
})
test('passing registration, memory reuse, source isolation and idempotent activation work together',async()=>{
const f=fixture(['clear','clear','other','other','other','other']);try{
const s=await prepare(f),trial=await f.call('POST','/api/setup/trial',{setupId:s.id,frames:[frame(0),frame(1),frame(2)]});assert.equal(trial.passed,true)
const body={setupId:s.id,confirmation:{target:true,rule:true},remember:true,interval:3}
const r=await f.call('POST','/api/setup/activate',body),same=await f.call('POST','/api/setup/activate',body);assert.equal(r.id,same.id);assert.equal(f.service.repo.list('memory').length,1)
await assert.rejects(()=>f.call('POST','/api/contracts/observe',{registrationId:r.id,frame:frame(5,'another-camera')}));assert.equal(f.calls(),5)
await f.call('POST','/api/contracts/observe',{registrationId:r.id,frame:frame(5)});const dup=await f.call('POST','/api/contracts/observe',{registrationId:r.id,frame:frame(5)});assert.equal(dup.duplicate,true);assert.equal(f.calls(),6)
await f.call('POST','/api/memories/archive',{id:r.memoryId});assert.equal((await f.call('GET','/api/memories')).length,0);assert.equal((await f.call('GET','/api/contracts/'+r.id)).binding.targetName,'小狗')
assert.ok(f.journal.list().every(x=>x.status!=='running'))
}finally{f.cleanup()}
})
test('failed grounding never becomes an enabled memory',async()=>{
const f=fixture(['unknown','clear','other','other','other']);try{const s=await prepare(f),trial=await f.call('POST','/api/setup/trial',{setupId:s.id,frames:[frame(0),frame(1),frame(2)]});assert.equal(trial.passed,false);await assert.rejects(()=>f.call('POST','/api/setup/activate',{setupId:s.id,confirmation:{target:true,rule:true}}));assert.equal(f.service.repo.list('registration').length,0)}finally{f.cleanup()}
})
test('keys check requires an explicit departure signal and does not infer carrying',async()=>{
const f=fixture(['clear','clear','target','target','target','target','target']);try{
const s=await prepare(f,'keys_check');assert.deepEqual(s.plan.rules[0].requires,['away']);assert.match(s.plan.observers[0].options.other,/absent/);assert.doesNotMatch(s.plan.observers[0].options.other,/carried/)
await f.call('POST','/api/setup/trial',{setupId:s.id,frames:[frame(0),frame(1),frame(2)]});const r=await f.call('POST','/api/setup/activate',{setupId:s.id,confirmation:{target:true,rule:true}})
const silent=await f.call('POST','/api/contracts/observe',{registrationId:r.id,frame:frame(3),signals:[]});assert.equal(silent.notices.length,0)
const triggered=await f.call('POST','/api/contracts/observe',{registrationId:r.id,frame:frame(4),signals:['away']});assert.equal(triggered.notices.length,1)
}finally{f.cleanup()}
})
test('saved runtime evidence survives service restart and prevents duplicate inference',async()=>{
const f=fixture(['clear','clear','other','other','other','other']);try{
const s=await prepare(f);await f.call('POST','/api/setup/trial',{setupId:s.id,frames:[frame(0),frame(1),frame(2)]});const r=await f.call('POST','/api/setup/activate',{setupId:s.id,confirmation:{target:true,rule:true}})
await f.call('POST','/api/contracts/observe',{registrationId:r.id,frame:frame(3)})
let called=false;const reloaded=createGroundedService(f.root,{journal:()=>f.journal,enqueue:fn=>fn(),visionFetch:async()=>{called=true;throw new Error('should not run')}})
const duplicate=await reloaded.handle('POST',new URL('http://localhost/api/contracts/observe'),{registrationId:r.id,frame:frame(3)});assert.equal(duplicate.duplicate,true);assert.equal(called,false)
const restored=await reloaded.handle('GET',new URL('http://localhost/api/contracts/notices?ids='+r.id),{});assert.equal(restored[0].observations[0].frameKey,'frame-3');assert.ok(restored[0].observations[0].evidence)
await f.call('POST','/api/contracts/reset',{registrationId:r.id});assert.equal(f.service.repo.get('state',r.id).observations.length,1)
}finally{f.cleanup()}
})
test('repository refuses traversal-like identifiers',()=>{
const f=fixture();try{const repo=new GroundedRepository(f.root);assert.throws(()=>repo.get('memory','../.env.local'))}finally{f.cleanup()}
})
test('quiet preference suppresses routine notices and restoring auto preserves original rule levels',async()=>{
const f=fixture(['clear','clear','other','other','other','other','other','target','target','other','other','target','target']);try{
const s=await prepare(f);await f.call('POST','/api/setup/trial',{setupId:s.id,frames:[frame(0),frame(1),frame(2)]});const r=await f.call('POST','/api/setup/activate',{setupId:s.id,confirmation:{target:true,rule:true}})
for(const t of [3,4])await f.call('POST','/api/contracts/observe',{registrationId:r.id,frame:frame(t)})
await f.call('POST','/api/contracts/preferences',{registrationId:r.id,notification:'quiet'})
for(const t of [5,6]){const result=await f.call('POST','/api/contracts/observe',{registrationId:r.id,frame:frame(t)});assert.equal(result.notices.length,0)}
await f.call('POST','/api/contracts/preferences',{registrationId:r.id,notification:'auto'})
for(const t of [7,8,9])await f.call('POST','/api/contracts/observe',{registrationId:r.id,frame:frame(t)})
const final=await f.call('POST','/api/contracts/observe',{registrationId:r.id,frame:frame(10)});assert.equal(final.notices.length,1)
assert.equal((await f.call('GET','/api/contracts/'+r.id)).plan.rules[0].level,2)
}finally{f.cleanup()}
})
test('archived registrations cannot run against their retained reference',async()=>{
const f=fixture(['clear','clear','other','other','other']);try{const s=await prepare(f);await f.call('POST','/api/setup/trial',{setupId:s.id,frames:[frame(0),frame(1),frame(2)]});const r=await f.call('POST','/api/setup/activate',{setupId:s.id,confirmation:{target:true,rule:true}});await f.call('POST','/api/contracts/archive',{registrationId:r.id});await assert.rejects(()=>f.call('POST','/api/contracts/observe',{registrationId:r.id,frame:frame(3)}));assert.equal(f.calls(),5)}finally{f.cleanup()}
})
test('a stationary scene remains observable when separate video times have identical pixels',async()=>{
const f=fixture(['clear','clear','other','other','other']);try{const s=await prepare(f),frames=[frame(0),frame(1),frame(2)].map(x=>({...x,image:picture(50)}));assert.equal((await f.call('POST','/api/setup/trial',{setupId:s.id,frames})).passed,true)}finally{f.cleanup()}
})
test('a new explicit departure check can remind again while keys remain in place',async()=>{
const f=fixture(['clear','clear','target','target','target','target','target']);try{const s=await prepare(f,'keys_check');await f.call('POST','/api/setup/trial',{setupId:s.id,frames:[frame(0),frame(1),frame(2)]});const r=await f.call('POST','/api/setup/activate',{setupId:s.id,confirmation:{target:true,rule:true}});const saved=f.service.repo.get('registration',r.id);saved.plan.rules[0].cooldown_seconds=0;f.service.repo.save('registration',r.id,saved);for(const t of [3,4]){const result=await f.call('POST','/api/contracts/observe',{registrationId:r.id,frame:frame(t),signals:['away']});assert.equal(result.notices.length,1)}}finally{f.cleanup()}
})
test('memory reuses confirmed conditions only for the same request',async()=>{
const f=fixture(['clear','clear','other','other','other']);try{
const s=await prepare(f);await f.call('POST','/api/setup/trial',{setupId:s.id,frames:[frame(0),frame(1),frame(2)]});const r=await f.call('POST','/api/setup/activate',{setupId:s.id,confirmation:{target:true,rule:true}})
const input={request:'小狗整个身体出了笼子告诉我',binding:{...binding(),memoryId:r.memoryId},taskKind:'dog_boundary',answers:[],notification:'auto',confirmed:true}
const reused=await f.call('POST','/api/setup/prepare',input);assert.match(reused.plan.summary_zh,/有人抱出时不提醒/)
const changed=await f.call('POST','/api/setup/prepare',{...input,request:'小狗整个身体出了笼子告诉我,有人抱出来也提醒'});assert.doesNotMatch(changed.plan.summary_zh,/有人抱出时不提醒/)
}finally{f.cleanup()}
})