Files
client/scripts/smoke-onboarding-offscreen.mjs
T

52 lines
4.2 KiB
JavaScript

import assert from 'node:assert/strict';
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { launchApp, waitForAsync } from './lib/electron-app.mjs';
const args=process.argv.slice(2),opt=k=>args.includes(`--${k}`)?args[args.indexOf(`--${k}`)+1]:undefined;
const profile=mkdtempSync(path.join(tmpdir(),'teamspeach-onboarding-qa-')),out=path.resolve(opt('out')??'smoke-out/onboarding');mkdirSync(out,{recursive:true});
const result={checks:[],errors:[],scope:'Actual disposable first-run theme persistence and native identity creation; delayed/rejected preference fixture only, no dialog or server'};let app;
try{
app=await launchApp({profile,exe:opt('exe')});let page=await app.firstWindow();page.on('pageerror',e=>result.errors.push(e.message));
await page.waitForSelector('#view-onboarding',{state:'visible'});
assert.equal((await page.evaluate(()=>window.teamspeach.getPrefs())).theme,'dark');
for(const theme of ['system','dark','light']){
await page.locator(`label:has(#onboarding-theme-${theme})`).click();
await waitForAsync(page, async theme=>(await window.teamspeach.getPrefs()).theme===theme&&!document.querySelector('#onboarding-submit').disabled,theme);
assert.equal(await app.evaluate(({nativeTheme})=>nativeTheme.themeSource),theme);
}
result.checks.push('System/Dark/Light first-run choices persist through real preferences and native theme');
await app.evaluate(({ipcMain})=>{
const original=ipcMain._invokeHandlers.get('prefs:set'),profile=ipcMain._invokeHandlers.get('profile:request');
globalThis.__themeQA={mode:'reject',creates:0};
ipcMain.removeHandler('prefs:set');ipcMain.handle('prefs:set',(event,partial)=>{
if(!Object.hasOwn(partial,'theme'))return original(event,partial);
if(__themeQA.mode==='reject')return {ok:false,message:'Controlled save rejection'};
if(__themeQA.mode==='delay')return new Promise(resolve=>{__themeQA.release=()=>resolve(original(event,partial));});
return original(event,partial);
});
ipcMain.removeHandler('profile:request');ipcMain.handle('profile:request',(event,cmd,args)=>{if(cmd==='profile_create')__themeQA.creates++;return profile(event,cmd,args);});
});
await page.locator('label:has(#onboarding-theme-dark)').click();
await page.waitForFunction(()=>document.querySelector('#onboarding-theme-light').checked&&!document.querySelector('#onboarding-submit').disabled);
assert.equal((await page.evaluate(()=>window.teamspeach.getPrefs())).theme,'light');
assert.equal(await app.evaluate(()=>__themeQA.creates),0);
result.checks.push('Rejected theme save reverts selection and cannot create identity');
await app.evaluate(()=>{__themeQA.mode='delay';});
await page.fill('#onboarding-nick','Theme QA');await page.locator('label:has(#onboarding-theme-dark)').click();
await page.evaluate(()=>document.querySelector('#onboarding-form').requestSubmit());
assert.equal(await app.evaluate(()=>__themeQA.creates),0);
await app.evaluate(()=>__themeQA.release());
await page.waitForFunction(()=>!document.querySelector('#onboarding-submit').disabled);
if(await page.locator('#view-onboarding').isVisible())await page.click('#onboarding-submit');
await page.waitForSelector('#view-app',{state:'visible'});
assert.equal(await app.evaluate(()=>__themeQA.creates),1);
assert.equal((await page.evaluate(()=>window.teamspeach.getPrefs())).theme,'dark');
result.checks.push('Delayed theme save completes before real native identity creation');
await page.screenshot({path:path.join(out,'first-run-selected-theme.png')});
await app.close();app=null;app=await launchApp({profile,exe:opt('exe')});page=await app.firstWindow();
await page.waitForSelector('#view-app',{state:'visible'});assert.equal(await page.locator('#view-onboarding').isHidden(),true);
assert.equal((await page.evaluate(()=>window.teamspeach.getPrefs())).theme,'dark');result.checks.push('Existing profile skips first-run and retains selected theme');
assert.deepEqual(result.errors,[]);result.ok=true;
}catch(error){result.ok=false;result.error=error.stack;process.exitCode=1;}finally{if(app)await app.close().catch(()=>{});rmSync(profile,{recursive:true,force:true});writeFileSync(path.join(out,'result.json'),JSON.stringify(result,null,2));console.log(JSON.stringify(result));}