@@ -15,16 +15,16 @@ import { threadFamilies } from "../../state/threads";
1515import { ThreadContextChip } from "../ThreadContextChip" ;
1616import { MarkdownThreadLink } from "./MarkdownThreadLink" ;
1717
18+ type FamilyResult = ReturnType < ReturnType < typeof threadFamilies . familyAtom > [ "read" ] > ;
19+
1820const mocks = await vi . hoisted ( async ( ) => {
1921 const { Atom, AsyncResult } = await import ( "effect/reactivity" ) ;
2022 return {
2123 shells : Atom . make < ReadonlyMap < string , OrchestrationV2ThreadShell > > ( new Map ( ) ) . pipe (
2224 Atom . keepAlive ,
2325 ) ,
2426 reads : Atom . family ( ( _key : string ) =>
25- Atom . make < AsyncResult . AsyncResult < OrchestrationV2ThreadFamily , never > > (
26- AsyncResult . initial ( ) ,
27- ) . pipe ( Atom . keepAlive ) ,
27+ Atom . make < FamilyResult > ( AsyncResult . initial ( ) ) . pipe ( Atom . keepAlive ) ,
2828 ) ,
2929 } ;
3030} ) ;
@@ -33,7 +33,15 @@ const key = (ref: ScopedThreadRef) => JSON.stringify([ref.environmentId, ref.thr
3333
3434vi . mock ( "../../state/entities" , ( ) => ( {
3535 useProject : ( ) => null ,
36- useThreadRefs : ( ) => [ ] ,
36+ useThreadRefs : ( enabled = true ) => {
37+ const rows = useAtomValue ( mocks . shells ) ;
38+ return enabled
39+ ? [ ...rows . keys ( ) ] . map ( ( key ) => {
40+ const [ environmentId , threadId ] = JSON . parse ( key ) as [ EnvironmentId , ThreadId ] ;
41+ return { environmentId, threadId } ;
42+ } )
43+ : [ ] ;
44+ } ,
3745 useThreadShell : ( ref : ScopedThreadRef | null ) => {
3846 const rows = useAtomValue ( mocks . shells ) ;
3947 if ( ref === null ) return null ;
@@ -106,6 +114,13 @@ function setup(shells: ReadonlyMap<string, OrchestrationV2ThreadShell> = new Map
106114 } ) ;
107115 return {
108116 requested,
117+ fail : ( target : ScopedThreadRef , waiting = false ) =>
118+ act ( ( ) =>
119+ appAtomRegistry . set (
120+ mocks . reads ( key ( target ) ) ,
121+ AsyncResult . fail ( new Error ( "offline" ) , { waiting } ) ,
122+ ) ,
123+ ) ,
109124 resolve : ( target : ScopedThreadRef , threads : ReadonlyArray < OrchestrationV2ThreadShell > ) =>
110125 act ( ( ) =>
111126 appAtomRegistry . set (
@@ -120,13 +135,17 @@ function setup(shells: ReadonlyMap<string, OrchestrationV2ThreadShell> = new Map
120135 } ;
121136}
122137
123- function view ( target : ScopedThreadRef , kind : "context" | "markdown" = "markdown" ) {
138+ function view ( target : ScopedThreadRef , kind : "context" | "markdown" = "markdown" , legacy = false ) {
124139 return (
125140 < AppAtomRegistryProvider >
126141 { kind === "context" ? (
127142 < ThreadContextChip record = { { ...target , title : "Saved title" } } />
128143 ) : (
129- < MarkdownThreadLink environmentId = { ENV } link = { target } label = { null } />
144+ < MarkdownThreadLink
145+ environmentId = { ENV }
146+ link = { { ...target , environmentId : legacy ? null : target . environmentId } }
147+ label = { null }
148+ />
130149 ) }
131150 </ AppAtomRegistryProvider >
132151 ) ;
@@ -153,23 +172,48 @@ describe("thread link family lookups", () => {
153172 } ,
154173 ) ;
155174
156- it ( "prefers a literal percent escape to the decoded thread " , async ( ) => {
175+ it . each ( [ false , true ] ) ( "prefers a literal percent escape, legacy link %s " , async ( legacy ) => {
157176 const literal = ref ( "provider%3A1" ) ;
158177 const decoded = ref ( "provider:1" ) ;
159178 using fixture = setup ( new Map ( [ [ key ( decoded ) , row ( decoded . threadId , "Decoded thread" ) ] ] ) ) ;
160- await using screen = await render ( view ( literal ) ) ;
179+ await using screen = await render ( view ( literal , "markdown" , legacy ) ) ;
161180 await expect
162181 . element ( screen . getByRole ( "link" , { name : "Thread, Decoded thread" } ) )
163182 . not . toBeInTheDocument ( ) ;
164183 expect ( fixture . requested . has ( key ( literal ) ) ) . toBe ( true ) ;
165184 expect ( fixture . requested . has ( key ( decoded ) ) ) . toBe ( false ) ;
185+ await fixture . fail ( literal ) ;
186+ await expect
187+ . element ( screen . getByRole ( "link" , { name : "Thread, Decoded thread" } ) )
188+ . not . toBeInTheDocument ( ) ;
166189 await fixture . resolve ( literal , [ row ( literal . threadId , "Literal thread" ) ] ) ;
167190 await expect
168191 . element ( screen . getByRole ( "link" , { name : "Thread, Literal thread" } ) )
169192 . toBeInTheDocument ( ) ;
170193 expect ( fixture . requested . has ( key ( decoded ) ) ) . toBe ( false ) ;
171194 } ) ;
172195
196+ it . each ( [ "written" , "decoded" ] as const ) (
197+ "resolves a legacy link remotely when the %s family read fails" ,
198+ async ( lookup ) => {
199+ const literal = ref ( "provider%3A1" ) ;
200+ const decoded = ref ( "provider:1" ) ;
201+ const failed = lookup === "written" ? literal : decoded ;
202+ const remote = ref ( failed . threadId , EnvironmentId . make ( "remote-environment" ) ) ;
203+ using fixture = setup ( new Map ( [ [ key ( remote ) , row ( remote . threadId , "Remote thread" ) ] ] ) ) ;
204+ await using screen = await render ( view ( literal , "markdown" , true ) ) ;
205+ if ( lookup === "decoded" ) await fixture . resolve ( literal , [ ] ) ;
206+ await fixture . fail ( failed , true ) ;
207+ await expect
208+ . element ( screen . getByRole ( "link" , { name : "Thread, Remote thread" } ) )
209+ . not . toBeInTheDocument ( ) ;
210+ await fixture . fail ( failed ) ;
211+ await expect
212+ . element ( screen . getByRole ( "link" , { name : "Thread, Remote thread" } ) )
213+ . toBeInTheDocument ( ) ;
214+ } ,
215+ ) ;
216+
173217 it ( "tries the decoded ID only after a successful empty literal lookup" , async ( ) => {
174218 using fixture = setup ( ) ;
175219 const literal = ref ( "provider%3A1" ) ;
0 commit comments