@@ -2,7 +2,10 @@ import { act, createContext, type ComponentProps, type ReactNode } from "react";
22import { createRoot } from "react-dom/client" ;
33import * as NodeModule from "node:module" ;
44import { expect , it , vi } from "vite-plus/test" ;
5- import { EnvironmentId , ThreadId } from "@t3tools/contracts" ;
5+ import { EnvironmentId , MessageId , ThreadId , type ScopedThreadRef } from "@t3tools/contracts" ;
6+ import { RegistryContext } from "@effect/atom-react" ;
7+ import { AtomRegistry } from "effect/reactivity" ;
8+ import type { OrchestrationV2ThreadProjection } from "@t3tools/contracts" ;
69import { setTemporaryThread } from "@t3tools/client-runtime/temporary-threads" ;
710
811const { JSDOM } = NodeModule . createRequire ( import . meta. url ) ( "jsdom" ) as {
@@ -12,6 +15,8 @@ const { JSDOM } = NodeModule.createRequire(import.meta.url)("jsdom") as {
1215const state = vi . hoisted ( ( ) => ( {
1316 shell : { temporary : true } ,
1417 listeners : new Set < ( ) => void > ( ) ,
18+ detailReads : vi . fn ( ) ,
19+ projections : new Map < string , OrchestrationV2ThreadProjection > ( ) ,
1520} ) ) ;
1621vi . mock ( "react-native" , ( ) => ( {
1722 Platform : { OS : "android" } ,
@@ -126,9 +131,9 @@ vi.mock("../../components/FilePreviewGallery", () => ({
126131vi . mock ( "../../components/ui/NativePresentation" , ( ) => ( {
127132 PresentationSource : ( { children } : { children : ReactNode } ) => children ,
128133} ) ) ;
129- vi . mock ( "./thread-workflow-opening " , ( ) => ( {
130- useWorkflowOpening : ( ) => null ,
131- ThreadWorkflowOpening : ( ) => null ,
134+ vi . mock ( "./SubagentRow " , ( ) => ( { SubagentRow : ( ) => null } ) ) ;
135+ vi . mock ( "./work-log-layout" , ( ) => ( {
136+ WorkLogBlock : ( { children } : { children ?: ReactNode } ) => < div > { children } </ div > ,
132137} ) ) ;
133138vi . mock ( "./useFileChipShare" , ( ) => ( { useFileChipShare : ( ) => ( ) => { } } ) ) ;
134139vi . mock ( "../../native/useMarkdownImageSource" , ( ) => ( {
@@ -149,7 +154,28 @@ vi.mock("../../state/session", () => ({
149154 usePreparedConnection : ( ) => null ,
150155} ) ) ;
151156vi . mock ( "../../state/atom-registry" , ( ) => ( { appAtomRegistry : { } } ) ) ;
152- vi . mock ( "../../state/threads" , ( ) => ( { environmentThreadShells : { } , threadEnvironment : { } } ) ) ;
157+ vi . mock ( "../../state/threads" , async ( ) => {
158+ const { createEnvironmentThreadDetailAtoms, EMPTY_ENVIRONMENT_THREAD_STATE } =
159+ await import ( "@t3tools/client-runtime/state/threads" ) ;
160+ const { AsyncResult, Atom } = await import ( "effect/reactivity" ) ;
161+ const Option = await import ( "effect/Option" ) ;
162+ const stateAtom = Atom . family ( ( threadId : string ) =>
163+ Atom . make ( ( ) => {
164+ state . detailReads ( threadId ) ;
165+ return AsyncResult . success ( {
166+ ...EMPTY_ENVIRONMENT_THREAD_STATE ,
167+ data : Option . fromNullishOr ( state . projections . get ( threadId ) ) ,
168+ } ) ;
169+ } ) ,
170+ ) ;
171+ return {
172+ environmentThreadShells : { } ,
173+ threadEnvironment : { } ,
174+ environmentThreadDetails : createEnvironmentThreadDetailAtoms ( ( _ , threadId ) =>
175+ stateAtom ( threadId ) ,
176+ ) ,
177+ } ;
178+ } ) ;
153179vi . mock ( "../../components/ui/Button" , ( ) => ( { Button : ( ) => null } ) ) ;
154180vi . mock ( "../../components/ComposerContextSheet" , ( ) => ( { ComposerContextSheet : ( ) => null } ) ) ;
155181vi . mock ( "../../components/ui/AppActivityIndicator" , ( ) => ( { AppActivityIndicator : ( ) => null } ) ) ;
@@ -205,15 +231,20 @@ vi.mock("../../components/ui/AppText", () => ({
205231 AppText : ( { children } : { children ?: ReactNode } ) => < span > { children } </ span > ,
206232} ) ) ;
207233
234+ import { v2Projection } from "../../../../../packages/client-runtime/src/state/orchestrationV2TestFixtures" ;
208235import { ThreadFeed } from "./ThreadFeed" ;
236+ import { useWorkflowOpening } from "./thread-workflow-opening" ;
209237
210238it . each ( [ true , false ] ) (
211- "opens existing media rows after Keep without a parent render with shell temporary=%s" ,
239+ "admits detail after launch and opens media after Keep with shell temporary=%s" ,
212240 async ( temporary ) => {
213241 const environmentId = EnvironmentId . make ( "env" ) ;
214242 const threadId = ThreadId . make ( "thread" ) ;
215243 state . shell = { temporary } ;
216244 state . listeners . clear ( ) ;
245+ state . detailReads . mockClear ( ) ;
246+ state . projections . clear ( ) ;
247+ const registry = AtomRegistry . make ( ) ;
217248 setTemporaryThread ( environmentId , threadId , true ) ;
218249 const dom = new JSDOM ( "<html><body></body></html>" ) ;
219250 vi . stubGlobal ( "window" , dom . window ) ;
@@ -237,32 +268,45 @@ it.each([true, false])(
237268 } ,
238269 { id : "viewed" , type : "activity-group" , activities : [ ] , continuesWorkLog : false } ,
239270 ] as unknown as ComponentProps < typeof ThreadFeed > [ "feed" ] ;
271+ const feedView = (
272+ < ThreadFeed
273+ environmentId = { environmentId }
274+ threadId = { threadId }
275+ selectedThreadDetailRef = { null }
276+ threadTitle = "Thread"
277+ feed = { feed }
278+ queuedMessages = { [ ] }
279+ dispatchingMessageId = { null }
280+ onEditPendingMessage = { ( ) => { } }
281+ contentPresentation = { { kind : "ready" } }
282+ agentLabel = "Codex"
283+ latestRun = { null }
284+ activeWorkStartedAt = { null }
285+ listRef = { { current : null } }
286+ freeze = { { value : false } as ComponentProps < typeof ThreadFeed > [ "freeze" ] }
287+ anchorMessageId = { null }
288+ submittedMessageId = { null }
289+ contentInsetEndAdjustment = {
290+ { value : 0 } as ComponentProps < typeof ThreadFeed > [ "contentInsetEndAdjustment" ]
291+ }
292+ keyboardOffset = { 0 }
293+ />
294+ ) ;
240295 try {
241296 await act ( async ( ) =>
242297 root . render (
243- < ThreadFeed
244- environmentId = { environmentId }
245- threadId = { threadId }
246- threadTitle = "Thread"
247- feed = { feed }
248- queuedMessages = { [ ] }
249- dispatchingMessageId = { null }
250- onEditPendingMessage = { ( ) => { } }
251- contentPresentation = { { kind : "ready" } }
252- agentLabel = "Codex"
253- latestRun = { null }
254- activeWorkStartedAt = { null }
255- listRef = { { current : null } }
256- freeze = { { value : false } as ComponentProps < typeof ThreadFeed > [ "freeze" ] }
257- anchorMessageId = { null }
258- submittedMessageId = { null }
259- contentInsetEndAdjustment = {
260- { value : 0 } as ComponentProps < typeof ThreadFeed > [ "contentInsetEndAdjustment" ]
261- }
262- keyboardOffset = { 0 }
263- /> ,
298+ < RegistryContext . Provider value = { registry } > { feedView } </ RegistryContext . Provider > ,
299+ ) ,
300+ ) ;
301+ expect ( state . detailReads ) . not . toHaveBeenCalled ( ) ;
302+ await act ( async ( ) =>
303+ root . render (
304+ < RegistryContext . Provider value = { registry } >
305+ < ThreadFeed { ...feedView . props } selectedThreadDetailRef = { { environmentId, threadId } } />
306+ </ RegistryContext . Provider > ,
264307 ) ,
265308 ) ;
309+ expect ( state . detailReads . mock . calls ) . toEqual ( [ [ threadId ] ] ) ;
266310 expect ( container . textContent ?. match ( / K e e p t h r e a d t o o p e n m e d i a / g) ) . toHaveLength ( 4 ) ;
267311 expect ( container . querySelectorAll ( "img" ) ) . toHaveLength ( 0 ) ;
268312 await act ( async ( ) => {
@@ -279,9 +323,87 @@ it.each([true, false])(
279323 ] ) ;
280324 } finally {
281325 await act ( async ( ) => root . unmount ( ) ) ;
326+ registry . dispose ( ) ;
282327 setTemporaryThread ( environmentId , threadId , false ) ;
283328 vi . unstubAllGlobals ( ) ;
284329 dom . window . close ( ) ;
285330 }
286331 } ,
287332) ;
333+
334+ function WorkflowReader ( props : { readonly detailRef : ScopedThreadRef | null } ) {
335+ const opening = useWorkflowOpening ( props . detailRef ) ;
336+ return < span > { opening ?. script } </ span > ;
337+ }
338+
339+ it ( "loads the workflow and its parent after detail admission" , async ( ) => {
340+ const environmentId = EnvironmentId . make ( "env" ) ;
341+ const threadId = ThreadId . make ( "workflow" ) ;
342+ const parentId = ThreadId . make ( "parent" ) ;
343+ const script = 'export const meta = { name: "probe" };' ;
344+ state . detailReads . mockClear ( ) ;
345+ state . projections . clear ( ) ;
346+ state . projections . set ( threadId , {
347+ ...v2Projection ,
348+ thread : {
349+ ...v2Projection . thread ,
350+ id : threadId ,
351+ creationSource : "provider" ,
352+ lineage : {
353+ parentThreadId : parentId ,
354+ rootThreadId : parentId ,
355+ relationshipToParent : "subagent" ,
356+ } ,
357+ } ,
358+ messages : [
359+ {
360+ id : MessageId . make ( "script" ) ,
361+ threadId,
362+ runId : null ,
363+ nodeId : null ,
364+ role : "user" ,
365+ text : script ,
366+ workflowScript : true ,
367+ attachments : [ ] ,
368+ streaming : false ,
369+ createdBy : "user" ,
370+ creationSource : "mobile" ,
371+ createdAt : v2Projection . updatedAt ,
372+ updatedAt : v2Projection . updatedAt ,
373+ } ,
374+ ] ,
375+ } ) ;
376+ state . projections . set ( parentId , v2Projection ) ;
377+ const registry = AtomRegistry . make ( ) ;
378+ const dom = new JSDOM ( "<html><body></body></html>" ) ;
379+ vi . stubGlobal ( "window" , dom . window ) ;
380+ vi . stubGlobal ( "document" , dom . window . document ) ;
381+ vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
382+ const container = document . createElement ( "div" ) ;
383+ const root = createRoot ( container ) ;
384+ try {
385+ await act ( async ( ) =>
386+ root . render (
387+ < RegistryContext . Provider value = { registry } >
388+ < WorkflowReader detailRef = { null } />
389+ </ RegistryContext . Provider > ,
390+ ) ,
391+ ) ;
392+ expect ( state . detailReads ) . not . toHaveBeenCalled ( ) ;
393+ expect ( container . textContent ) . toBe ( "" ) ;
394+ await act ( async ( ) =>
395+ root . render (
396+ < RegistryContext . Provider value = { registry } >
397+ < WorkflowReader detailRef = { { environmentId, threadId } } />
398+ </ RegistryContext . Provider > ,
399+ ) ,
400+ ) ;
401+ expect ( state . detailReads . mock . calls ) . toEqual ( [ [ threadId ] , [ parentId ] ] ) ;
402+ expect ( container . textContent ) . toBe ( script ) ;
403+ } finally {
404+ await act ( async ( ) => root . unmount ( ) ) ;
405+ registry . dispose ( ) ;
406+ vi . unstubAllGlobals ( ) ;
407+ dom . window . close ( ) ;
408+ }
409+ } ) ;
0 commit comments