/** Reproduce a fresh native page with no Session seat yet. */ import assert from 'node:assert/strict'; import {readFile} from 'node:fs/promises'; import vm from 'node:vm'; let plugin,session,subscriber,expanded=false,openedWorkspace=0,openedTabs=0; let workspaceSnapshot={phase:'pending',items:[]}; const effects=[]; const sandbox={window:{__ModuleLoader__:{load:value=>{plugin=value;}}}, setTimeout,clearTimeout,setInterval,clearInterval, requestAnimationFrame:fn=>setTimeout(fn,1),cancelAnimationFrame:clearTimeout, fetch:async url=>({json:async()=>url==='/dx/workspace'?{workspace_id:'owned-workspace'}:{}})}; vm.runInNewContext(await readFile(new URL('../integration/dsh/task-plugin/client.js',import.meta.url),'utf8'),sandbox); const exported=plugin.factory(name=>name==='react'?{createElement:()=>null}:{}); const ctx={slots:{inject:(_name,fn)=>fn(),register:()=>()=>{}}, workspaces:{list:{getSnapshot:()=>workspaceSnapshot}}, effect:(fn)=>{effects.push(fn());},sidebarRightTabs:{register:()=>{}}, sidebarRight:{mounted:{getSnapshot:()=>session,subscribe:fn=>{subscriber=fn;return()=>{};}}, openTab:()=>{if(!session)throw Error('Session seat absent');openedTabs++;expanded=true;},isExpanded:()=>expanded}, uiWorkspace:{openWorkspace:async id=>{ assert.equal(id,'owned-workspace');openedWorkspace++; await new Promise(r=>setTimeout(r,20));session='real-blank-session';subscriber?.(); },openSession:()=>{}},uiSession:{}}; try{ exported.apply(ctx); await new Promise(r=>setTimeout(r,150)); assert.equal(openedWorkspace,0,'native workspace adoption must wait for the official client workspace snapshot'); workspaceSnapshot={phase:'ready',items:[]}; await new Promise(r=>setTimeout(r,150)); assert.equal(openedWorkspace,0,'a ready snapshot must actually contain the native workspace before opening'); workspaceSnapshot={phase:'ready',items:[{workspaceId:'owned-workspace'}]}; await new Promise(r=>setTimeout(r,150)); assert.equal(openedWorkspace,1,'fresh page must adopt the owned native workspace once'); assert.equal(expanded,true,'right dock must open after delayed Session adoption'); const previous=openedTabs; subscriber?.();subscriber?.(); await new Promise(r=>setTimeout(r,150)); assert.equal(openedTabs,previous,'same-session updates must not reopen/remount the pane and reset its selected operations tab'); for(const cleanup of effects.reverse())cleanup?.();effects.length=0; session='already-selected-conversation';expanded=false;openedWorkspace=0; exported.apply(ctx); await new Promise(r=>setTimeout(r,150)); assert.equal(openedWorkspace,0,'existing selected conversations and drafts must stay selected'); assert.equal(expanded,true); console.log(JSON.stringify({status:'passed',initialSessionAbsent:true,ownedWorkspaceAdopted:true,dockOpened:true,existingSelectionPreserved:true})); }finally{for(const cleanup of effects.reverse())cleanup?.();}