Files
ucvl-home-vision/test_people_navigation.js
T

58 lines
4.0 KiB
JavaScript
Raw Permalink Normal View History

const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),vm=require('node:vm');
const read=path=>fs.readFileSync(path,'utf8');
const part=(text,start,end)=>text.slice(text.indexOf(start),text.indexOf(end,text.indexOf(start)+start.length));
function bridgeHarness(allow=true){
const calls=[];
const c=vm.createContext({window:{ZhaoVue:{}},bootApplication(){},api(){},refresh(){},toast(){},ZhaoGeography:{describe(){}},
state:{familyId:'a'},loggedIn:true,enterDevice(){},showPage:(...args)=>calls.push(args),
async switchHousehold(id){calls.push(['switch',id]);if(allow)c.state.familyId=id;return allow;}});
vm.runInContext(read('web/vue-bridge.js'),c);
return {c,calls,bridge:c.window.ZhaoVue};
}
test('opening a person selects the actual person instead of the account holder',async()=>{
const {bridge,calls}=bridgeHarness();await bridge.viewPerson('mother','a');
assert.equal(calls.length,1);assert.equal(calls[0][0],'family');assert.equal(calls[0][1].personId,'mother');
});
test('person navigation switches only to the explicitly selected authorized household',async()=>{
const {bridge,calls}=bridgeHarness();await bridge.viewMemberHistory('mother','妈妈','b');
assert.equal(calls[0][0],'switch');assert.equal(calls[0][1],'b');
assert.equal(calls[1][0],'journal');assert.equal(calls[1][1].person.id,'mother');
});
test('cancelled or denied household switch cannot open a person in the old scope',async()=>{
const {bridge,calls}=bridgeHarness(false);await bridge.viewPerson('mother','b');await bridge.viewMemberHistory('mother','妈妈','b');
assert.ok(calls.every(call=>call[0]==='switch'));
});
test('logging out blocks same-household person navigation',async()=>{
const {c,bridge,calls}=bridgeHarness();c.loggedIn=false;await bridge.viewPerson('mother','a');
assert.equal(calls.length,0);
});
test('member history preselects a real person without an account and clears stale filters',()=>{
const nodes={};const selector={options:[],value:'',add(option){this.options.push(option);}};
const c=vm.createContext({$:id=>id==='#journal-person'?selector:(nodes[id]??={value:'old'}),journalPage:7,
Option:function(text,value){this.text=text;this.value=value;}});
const journal=read('web/journal.js');
vm.runInContext(part(journal,'function resetJournalFilters(','function journalEmptyMessage(')+part(journal,'function selectJournalPerson(','async function loadJournal('),c);
const person={id:'person-only',name:'<亲人>'};c.selectJournalPerson(person);c.selectJournalPerson(person);
assert.equal(selector.value,'person-only');assert.equal(selector.options.length,1);assert.equal(selector.options[0].text,'<亲人>');
assert.equal(nodes['#journal-mode'].value,'person');assert.equal(nodes['#journal-search'].value,'');assert.equal(c.journalPage,0);
});
test('generation directory is the default and hardware graphs are confined to network mode',()=>{
const home=read('frontend/src/components/HomeExplorer.vue');
assert.match(home,/mode = ref\("generations"\)/);
assert.match(home,/<HomeInsights v-if="mode==='topology'/);
const workspace=read('frontend/src/components/Workspace.vue');
const overview=part(workspace,'<section id="page-overview"','<section id="page-device"');
assert.ok(!overview.includes('ScopeInsights'));
});
test('family overview puts people first and preserves an opened optional equipment section',()=>{
const nodes={'#overview-equipment':{open:true},'#scope-overview':{}};
const c=vm.createContext({currentHome:()=>({name:'家人',kind:'home',members:2}),state:{sites:[],cameras:[]},
ZhaoDevices:{catalog:()=>[]},isAdmin:()=>true,currentUser:{},scopeKind:()=>'家庭',esc:s=>s,$:id=>nodes[id]});
vm.runInContext(part(read('web/steward.js'),'function renderOverview(){','function renderDeviceDetail('),c);c.renderOverview();
const html=nodes['#scope-overview'].innerHTML;
assert.ok(html.indexOf('data-page="members"')<html.indexOf('data-page="objects"'));
assert.match(html,/<details id="overview-equipment"[^>]* open>/);
assert.match(html,/2 位家人/);
});