11import type { NavigationState , PartialState } from "@react-navigation/native" ;
2- import { Atom } from "effect/reactivity" ;
2+ import * as Option from "effect/Option" ;
3+ import { AsyncResult , Atom } from "effect/reactivity" ;
34import { describe , expect , it , vi } from "vite-plus/test" ;
45
56const fixture = vi . hoisted ( ( ) => ( {
67 applied : [ ] as { draftKey : string ; text : string } [ ] ,
78 root : undefined as NavigationState | undefined ,
9+ detailOpens : 0 ,
810} ) ) ;
911
1012vi . mock ( "../../components/ui/Dialog" , ( ) => ( { alert : ( ) => { } } ) ) ;
@@ -21,27 +23,48 @@ vi.mock("../../state/atom-registry", async () => {
2123 return { appAtomRegistry : AtomRegistry . make ( ) } ;
2224} ) ;
2325vi . mock ( "../../state/threads" , async ( ) => {
24- const { Atom } = await import ( "effect/reactivity" ) ;
26+ const { AsyncResult, Atom } = await import ( "effect/reactivity" ) ;
27+ const Option = await import ( "effect/Option" ) ;
2528 const shell = Atom . make < object | null > ( null ) ;
26- return { environmentThreadShells : { threadShellAtom : ( ) => shell } , fixtureShell : shell } ;
29+ const detailSource = Atom . make ( AsyncResult . success ( { data : Option . none ( ) } ) ) ;
30+ // Computing the real detail atom opens the thread's server stream.
31+ const detail = Atom . make ( ( get ) => {
32+ fixture . detailOpens += 1 ;
33+ return get ( detailSource ) ;
34+ } ) ;
35+ return {
36+ environmentThreadShells : { threadShellAtom : ( ) => shell } ,
37+ environmentThreads : { stateAtom : ( ) => detail } ,
38+ fixtureShell : shell ,
39+ fixtureDetail : detail ,
40+ fixtureDetailSource : detailSource ,
41+ } ;
2742} ) ;
2843
2944const { appAtomRegistry } = await import ( "../../state/atom-registry" ) ;
30- const { fixtureShell } = ( await import ( "../../state/threads" ) ) as unknown as {
31- fixtureShell : Atom . Writable < object | null > ;
32- } ;
45+ const { fixtureDetail, fixtureDetailSource, fixtureShell } =
46+ ( await import ( "../../state/threads" ) ) as unknown as {
47+ fixtureShell : Atom . Writable < object | null > ;
48+ fixtureDetail : Atom . Atom < unknown > ;
49+ fixtureDetailSource : Atom . Writable < AsyncResult . AsyncResult < { data : Option . Option < object > } > > ;
50+ } ;
3351const { resolvePendingThreadPromptLink, takeThreadPromptLink } = await import ( "./threadPromptLink" ) ;
3452
3553function setup ( ) {
3654 fixture . applied . length = 0 ;
3755 fixture . root = undefined ;
56+ appAtomRegistry . reset ( ) ;
3857 appAtomRegistry . set ( fixtureShell , null ) ;
58+ appAtomRegistry . set ( fixtureDetailSource , AsyncResult . success ( { data : Option . none ( ) } ) ) ;
59+ fixture . detailOpens = 0 ;
3960}
4061
62+ const loadedDetail = AsyncResult . success ( { data : Option . some ( { } ) } ) ;
63+
4164const threadParams = { environmentId : "env-1" , threadId : "thread-1" } ;
4265
43- function linkedState ( ) : PartialState < NavigationState > {
44- return { routes : [ { name : "Thread" , params : { ...threadParams , prompt : "Fix the bug" } } ] } ;
66+ function linkedState ( prompt = "Fix the bug" ) : PartialState < NavigationState > {
67+ return { routes : [ { name : "Thread" , params : { ...threadParams , prompt } } ] } ;
4568}
4669
4770function stack ( routes : NavigationState [ "routes" ] ) : NavigationState {
@@ -84,4 +107,38 @@ describe("takeThreadPromptLink", () => {
84107 appAtomRegistry . set ( fixtureShell , { id : "thread-1" , title : "renamed" } ) ;
85108 expect ( fixture . applied ) . toHaveLength ( 1 ) ;
86109 } ) ;
110+
111+ it ( "fills the draft of an archived thread its screen shows from detail" , ( ) => {
112+ setup ( ) ;
113+ takeThreadPromptLink ( linkedState ( ) ) ;
114+ fixture . root = threadRoute ;
115+ // Archived threads have no shell; the open screen subscribes to their detail instead.
116+ using _screen = { [ Symbol . dispose ] : appAtomRegistry . mount ( fixtureDetail ) } ;
117+ resolvePendingThreadPromptLink ( threadRoute ) ;
118+ expect ( fixture . applied ) . toEqual ( [ ] ) ;
119+
120+ appAtomRegistry . set ( fixtureDetailSource , loadedDetail ) ;
121+ expect ( fixture . applied ) . toEqual ( [ { draftKey : expect . any ( String ) , text : "Fix the bug" } ] ) ;
122+ } ) ;
123+
124+ it ( "opens no detail for a thread no screen has loaded" , ( ) => {
125+ setup ( ) ;
126+ takeThreadPromptLink ( linkedState ( ) ) ;
127+ fixture . root = threadRoute ;
128+ resolvePendingThreadPromptLink ( threadRoute ) ;
129+ appAtomRegistry . set ( fixtureDetailSource , loadedDetail ) ;
130+ expect ( fixture . detailOpens ) . toBe ( 0 ) ;
131+ expect ( fixture . applied ) . toEqual ( [ ] ) ;
132+ resolvePendingThreadPromptLink ( stack ( [ { key : "home" , name : "Home" } ] ) ) ;
133+ } ) ;
134+
135+ it ( "drops an earlier link to the thread when a newer one is invalid" , ( ) => {
136+ setup ( ) ;
137+ takeThreadPromptLink ( linkedState ( ) ) ;
138+ takeThreadPromptLink ( linkedState ( "\u200b" ) ) ;
139+ fixture . root = threadRoute ;
140+ resolvePendingThreadPromptLink ( threadRoute ) ;
141+ appAtomRegistry . set ( fixtureShell , { id : "thread-1" } ) ;
142+ expect ( fixture . applied ) . toEqual ( [ ] ) ;
143+ } ) ;
87144} ) ;
0 commit comments