import test from 'node:test' import assert from 'node:assert/strict' import { adjudicate,withinWindow } from '../.local/tests/domain.mjs' const item=(patch={})=>({id:'x',templateId:'package',enabled:true,grounded:true,outcome:null,unknown:false,notifyMode:'auto',observations:[],...patch}) test('unclear is Unknown and never a normal outcome',()=>{const v=adjudicate(item(),'unclear',false,'a','door');assert.equal(v.unknown,true);assert.equal(v.notified,false);assert.equal(v.outcome,null)}) test('model abstention stays Unknown even with a choice',()=>assert.equal(adjudicate(item(),'visible',true,'a','door').unknown,true)) test('package absent needs two distinct frames',()=>{const first=adjudicate(item({outcome:'visible',sceneKey:'door'}),'absent',false,'1','door');assert.equal(first.pending,true);const i=item({outcome:'visible',sceneKey:'door',pending:{choice:'absent',frameKey:'1',sceneKey:'door'},lastFrameKey:'1'});const v=adjudicate(i,'absent',false,'2','door');assert.equal(v.notified,true);assert.equal(v.level,2)}) test('same frame cannot provide two confirmations',()=>{const i=item({pending:{choice:'absent',frameKey:'1',sceneKey:'door'},lastFrameKey:'1'});assert.equal(adjudicate(i,'absent',false,'1','door').duplicate,true)}) test('first empty porch establishes a baseline without alerting',()=>{const i=item({pending:{choice:'absent',frameKey:'1',sceneKey:'door'}});assert.equal(adjudicate(i,'absent',false,'2','door').notified,false)}) test('changing scene cannot prove package removal',()=>{const i=item({outcome:'visible',sceneKey:'door-a',pending:{choice:'absent',frameKey:'1',sceneKey:'door-b'}});assert.equal(adjudicate(i,'absent',false,'2','door-b').notified,false)}) test('confirmed state remains confirmed on subsequent frames',()=>{const i=item({templateId:'destroy',sceneKey:'living',outcome:'destroying'});const v=adjudicate(i,'destroying',false,'3','living');assert.equal(v.pending,false);assert.equal(v.stateChanged,false);assert.equal(v.notified,false)}) test('quiet mode silences routine changes',()=>{assert.equal(adjudicate(item({notifyMode:'quiet'}),'visible',false,'1','door').notified,false)}) test('paused watch never records or notifies',()=>{const v=adjudicate(item({enabled:false}),'visible',false,'1','door');assert.equal(v.stateChanged,false);assert.equal(v.notified,false)}) test('window supports crossing midnight in Shanghai time',()=>{assert.equal(withinWindow('22:00','06:00',new Date('2026-10-12T16:30:00Z')),true);assert.equal(withinWindow('22:00','06:00',new Date('2026-10-12T08:30:00Z')),false)}) test('preference rollback creates a new version and deletion removes linked notices',async()=>{ const data=new Map();Object.defineProperty(globalThis,'localStorage',{configurable:true,value:{getItem:k=>data.get(k)||null,setItem:(k,v)=>data.set(k,v)}}) const {store,templateOf}=await import('../.local/tests/engine.mjs?store-test') const first=store.getState().items[0];store.setPreference(first.id,'quiet');let i=store.getState().items[0];assert.equal(i.preferences.length,2);assert.equal(i.preferences[1].mode,'auto') store.setPreference(first.id,'auto','恢复历史偏好');i=store.getState().items[0];assert.equal(i.preferences.length,3);assert.equal(i.notifyMode,'auto') const at=new Date().toISOString();store.applyObservation(first.id,{abstained:false,choice:'visible'},{id:'request1',capturedAt:at,time:'10:00',evidence:null,latencyMs:12,source:'test',sourceName:'test',mediaTime:1,frameKey:'1',sceneKey:'door'}) assert.equal(store.getState().notices.length,1);store.removeItem(first.id);assert.equal(store.getState().notices.length,0);assert.equal(store.getState().items.some(x=>x.id===first.id),false);assert.ok(templateOf(store.getState().items[0])) }) test('a notification poll from a stale tab preserves observations written in another tab',async()=>{ const data=new Map();Object.defineProperty(globalThis,'localStorage',{configurable:true,value:{getItem:k=>data.get(k)||null,setItem:(k,v)=>data.set(k,v)}}) const {Store,templateOf}=await import('../.local/tests/engine.mjs?multi-tab-test');const first=new Store() const item=first.addItem(templateOf(first.getState().items[0]),'shared',{contractId:'registration-a'}) const stale=new Store();first.setItem(item.id,{outcomeText:'newest observation',observations:[{id:'newest',capturedAt:new Date().toISOString(),evidence:null}]}) const before=data.get('home-pulse-v3');stale.setModelStatus('ready');assert.equal(data.get('home-pulse-v3'),before) stale.applyContractNotices('registration-a',[{eventId:'event-a',title:'A change',level:2,at:Date.now()/1000}]) const saved=JSON.parse(data.get('home-pulse-v3'));assert.equal(saved.items.find(i=>i.id===item.id).observations[0].id,'newest');assert.equal(saved.notices[0].id,'event-a') }) test('storage events update the other open view without writing the archive back',async()=>{ const data=new Map(),callbacks=[];let writes=0 Object.defineProperty(globalThis,'localStorage',{configurable:true,value:{getItem:k=>data.get(k)||null,setItem:(k,v)=>{writes++;data.set(k,v)}}}) const oldWindow=globalThis.window;globalThis.window={addEventListener:(name,fn)=>{if(name==='storage')callbacks.push(fn)}} try{const {Store}=await import('../.local/tests/engine.mjs?storage-event-test');const first=new Store(),second=new Store();first.setItem(first.getState().items[0].id,{name:'updated in another tab'});const count=writes;callbacks.forEach(fn=>fn({key:'home-pulse-v3',newValue:data.get('home-pulse-v3')}));assert.equal(second.getState().items[0].name,'updated in another tab');assert.equal(writes,count)}finally{globalThis.window=oldWindow} }) test('server history restores latest evidence and is idempotent after a refresh',async()=>{ const data=new Map();Object.defineProperty(globalThis,'localStorage',{configurable:true,value:{getItem:k=>data.get(k)||null,setItem:(k,v)=>data.set(k,v)}}) const {Store,templateOf}=await import('../.local/tests/engine.mjs?history-sync-test');const s=new Store(),item=s.addItem(templateOf(s.getState().items[0]),'registered',{contractId:'registered-1'}) const history=[{id:'server-observation',capturedAt:new Date().toISOString(),time:'12:00',outcome:'attention',unknown:false,text:'confirmed change',evidence:'data:image/jpeg;base64,eA==',latencyMs:500,source:'test',sourceName:'test',frameKey:'a:1',sceneKey:'a',mediaTime:1}] s.syncContractHistory('registered-1',history);s.syncContractHistory('registered-1',history) const fresh=new Store().getState().items.find(i=>i.id===item.id);assert.equal(fresh.outcomeText,'confirmed change');assert.equal(fresh.observations.length,1);assert.equal(fresh.evidence,history[0].evidence) })