| 1 | import assert from "node:assert/strict"; |
| 2 | import { act } from "react"; |
| 3 | import { installDom, installBridgeApp, renderComposer } from "./composerInboxHarness"; |
| 4 | import { makeSessionUIMock } from "../lib/sessionUIMock"; |
| 5 | import { emptySessionInput, flushAllSessionComposers, resumeSessionComposerEditing } from "../lib/sessionComposerPersistence"; |
| 6 | |
| 7 | const dom=installDom(); |
| 8 | const backend=makeSessionUIMock(async()=>{}); |
| 9 | installBridgeApp({...backend,GetSessionComposerState:async (ref:Parameters<typeof backend.GetSessionComposerState>[0])=>{ |
| 10 | const result=await backend.GetSessionComposerState(ref); |
| 11 | return {...result, historyChanged:ref.sessionId==="advanced",submissionId:ref.sessionId==="unknown"?"pending":""}; |
| 12 | }}); |
| 13 | for (const sessionId of ["advanced","unknown","live","editable"]) { |
| 14 | await backend.SaveSessionComposerState({ref:{hostId:"local",sessionId},expectedRevision:"0",contentVersion:1,contentJson:JSON.stringify({...emptySessionInput(),goalDraft:true})}); |
| 15 | } |
| 16 | const modes:string[]=[]; |
| 17 | const {root,rerender}=await renderComposer({tabId:"advanced",sessionKey:"advanced",formalSessionRef:{hostId:"local",sessionId:"advanced"},onSetCollaborationMode:mode=>modes.push(mode)}); |
| 18 | try { |
| 19 | for (const id of ["advanced","unknown","live"]) { |
| 20 | await rerender({tabId:id,sessionKey:id,formalSessionRef:{hostId:"local",sessionId:id},goal:id==="live"?"existing active goal":""}); |
| 21 | await act(async()=>new Promise(resolve=>setTimeout(resolve,10))); |
| 22 | assert.equal(modes.length,0,`${id} must not reactivate or clear the live goal`); |
| 23 | } |
| 24 | await rerender({tabId:"editable",sessionKey:"editable",formalSessionRef:{hostId:"local",sessionId:"editable"},goal:""}); |
| 25 | await act(async()=>new Promise(resolve=>setTimeout(resolve,10))); |
| 26 | assert.deepEqual(modes,["goal"]); |
| 27 | await rerender({collaborationMode:"goal",goal:"now active"}); |
| 28 | const stop=document.querySelector<HTMLButtonElement>(".composer-task-mode-trigger"); |
| 29 | assert.ok(stop,"recovered goal draft has its cancel control"); |
| 30 | await act(async()=>stop.click()); |
| 31 | await act(async()=>{await flushAllSessionComposers();resumeSessionComposerEditing();}); |
| 32 | assert.equal(JSON.parse((await backend.GetSessionComposerState({hostId:"local",sessionId:"editable"})).contentJson).goalDraft,false); |
| 33 | console.log("PASS Goal restore guards: unknown send, changed history, active goal and persisted cancellation"); |
| 34 | } finally {await act(async()=>root.unmount());resumeSessionComposerEditing();dom.window.close();} |
| 35 |