Files

36 lines
5.8 KiB
TypeScript

import {configureLanguage} from '../ui/src/i18n';
const wait=(ms=80)=>new Promise(r=>setTimeout(r,ms));
const until=async(f:()=>any)=>{for(let i=0;i<160;i++){if(f())return;await wait(25);}throw Error('Timeout '+document.body.innerText.slice(0,300));};
history.replaceState(null,'','/manager');
const plugin={manifest:{id:'demo',name:'Demo plugin',version:'1.0.0',description:'A demo plugin',author:'Community',ui:{windows:{}}},enabled:true,favorite:false,order:0};
const source={id:'community',name:'Community',url:'https://example.org/catalog.json',enabled:true,allowHttp:false};
const sub={id:'daily',name:'Daily sources',url:'https://example.org/subscription.json',enabled:true,autoRefresh:true,entries:[],pending:[],nextRefresh:0};
const database:any={plugins:{demo:plugin},sources:[source],subscriptions:[sub],safeMode:false,language:'en-US',launcher:{primaryTrigger:false,menuAutoClose:true,menuTimeoutSeconds:10,allCategories:['plugin','steam','lepton','desktop']},networkPanel:{enabled:true,port:15915,passwordEnabled:true},proxy:{http:'',github:''},updateSource:{url:'https://example.org/update.json'},updateCheck:{enabled:true,intervalHours:6}};
const calls:any[]=[];
window.fetch=async(_url,init)=>{const{method,params}=JSON.parse(String(init?.body));calls.push({method,params});let result:any=true;
if(method==='status')result={version:'0.4.3-preview.2',agreement:{version:1,accepted:true},database,running:[],notifications:[],inbox:[]};
else if(method==='language.list')result={packs:[],invalidFiles:[]};else if(method==='ui.events')result={cursor:0,events:[]};else if(method==='network.status')result={passwordConfigured:true,localOrigin:'http://localhost:15915',ips:[],listener:{}};
else if(method==='catalog.start')result={job:'catalog'};else if(method==='job.status')result={id:'catalog',phase:'done',result:[{source,catalog:{name:'Community',plugins:[{...plugin.manifest,apiVersion:1,url:'https://example.org/demo.framely',sha256:'a'.repeat(64)}]}}]};
else if(method==='network.save')Object.assign(database.networkPanel,params);else if(method==='safeMode')database.safeMode=params.enabled;else if(method==='sources.save')database.sources=params.sources;else if(method==='subscriptions.change')Object.assign(sub,params);else if(method==='system.check.settings.save')database.updateCheck=params.settings;
return new Response(JSON.stringify({result}));};
const nav=(label:string)=>[...document.querySelectorAll<HTMLButtonElement>('.workspace>nav button')].find(b=>b.textContent===label)!;
const width=(selector:string)=>{const node=document.querySelector<HTMLElement>(selector)!;if(!node)throw Error('Missing '+selector);const parent=node.parentElement!;const style=getComputedStyle(parent);const available=parent.clientWidth-parseFloat(style.paddingLeft)-parseFloat(style.paddingRight);if(Math.abs(node.getBoundingClientRect().width-available)>2)throw Error('Unused width '+selector+' '+node.getBoundingClientRect().width+'/'+available);};
const switcher=(label:string)=>document.querySelector<HTMLButtonElement>(`[role=switch][aria-label="${label}"]`)!;
(window as any).runInstallReviewChecks=async()=>{try{
await until(()=>!!document.querySelector('.plugin-row'));if(!switcher('Enable Demo plugin').classList.contains('ui-switch'))throw Error('Plugin switch is not shared');width('.plugin-row');
nav('Settings').click();await until(()=>!!document.querySelector('.network-password'));for(const selector of ['.language-picker','.network-password','.network-port label','.proxy-fields','.switch-row'])width(selector);
if(document.querySelector('.content input[type=checkbox]'))throw Error('Checkbox left in settings');
switcher('Safe mode').click();await until(()=>database.safeMode);await until(()=>!switcher('Safe mode').disabled);
switcher('Use an access password').click();await until(()=>!document.querySelector('.network-password'));switcher('Use an access password').click();await until(()=>!!document.querySelector('.network-password'));
await wait(450);console.log('FRAMELY_PREVIEW_INSTALL_MANAGER_SETTINGS_EN');
nav('Sources').click();await until(()=>document.querySelectorAll('.source-row').length===2);width('.source-row');switcher('Refresh daily').click();await until(()=>!sub.autoRefresh);await until(()=>!switcher('Refresh daily').disabled);
const add=[...document.querySelectorAll<HTMLButtonElement>('.content button')].find(b=>b.textContent==='Add plugin source')!;add.click();await until(()=>!!document.querySelector('.source-form'));width('.source-form');if(document.querySelector('.content input[type=checkbox]'))throw Error('Source still has a checkbox');
nav('Plugin library').click();await until(()=>!!document.querySelector('.store-card'));width('.store-card');document.querySelector<HTMLButtonElement>('.store-title')!.click();await until(()=>!!document.querySelector('.store-detail'));width('.store-detail');width('.detail-summary');
nav('Launcher').click();await until(()=>!!document.querySelector('.launcher-settings'));width('.launcher-settings .switch-row');width('.launcher-settings label');
nav('Notifications').click();await until(()=>!!document.querySelector('.notification-global-options'));width('.notification-global-options');width('.notification-plugin-list');
nav('About').click();await until(()=>!!document.querySelector('.automatic-update-settings'));width('.automatic-update-settings .framely-select');if(!switcher('Check for updates automatically').classList.contains('ui-switch'))throw Error('Update switch is not shared');
database.language='zh-CN';configureLanguage('zh-CN',[]);nav('Settings').click();await until(()=>document.documentElement.lang==='zh-CN');nav('设置').click();await until(()=>!!document.querySelector('.network-password'));width('.network-password');await wait(450);console.log('FRAMELY_PREVIEW_INSTALL_MANAGER_SETTINGS_ZH');
console.log('FRAMELY_BRIDGE_PASS');
}catch(e){console.error('FRAMELY_BRIDGE_FAIL '+e+' '+(e as Error).stack);}};
void import('../ui/src/main');