1+ import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection" ;
12import { RegistryContext } from "@effect/atom-react" ;
2- import { type AssetCreateUrlResult , EnvironmentId , ThreadId } from "@t3tools/contracts" ;
3+ import {
4+ AssetAccessError ,
5+ AssetWorkspaceAssetNotFoundError ,
6+ AssetPreviewTypeValidationError ,
7+ type AssetCreateUrlResult ,
8+ EnvironmentId ,
9+ ThreadId ,
10+ } from "@t3tools/contracts" ;
311import * as Effect from "effect/Effect" ;
12+ import * as Cause from "effect/Cause" ;
13+ import * as Schema from "effect/Schema" ;
414import { AtomRegistry } from "effect/reactivity" ;
515import { act , type ReactNode } from "react" ;
616import { createRoot , type Root } from "react-dom/client" ;
717import { afterEach , beforeEach , expect , it , vi } from "vite-plus/test" ;
818
919const mint = vi . hoisted ( ( ) => vi . fn < ( ) => Promise < AssetCreateUrlResult > > ( ) ) ;
20+ const remoteOpen = vi . hoisted ( ( ) => ( {
21+ state : { mode : "local-exec" as "local-exec" | "remote-unavailable" } ,
22+ isResolved : true ,
23+ label : "media-host" ,
24+ httpBaseUrl : "https://host.test" ,
25+ } ) ) ;
1026// Atoms derived from the server config run in the real registry over a missing config.
1127vi . mock ( import ( "../state/server" ) , async ( original ) => {
1228 const actual = await original ( ) ;
@@ -21,8 +37,14 @@ vi.mock(import("../state/server"), async (original) => {
2137vi . mock ( import ( "../state/assets" ) , async ( original ) => {
2238 const actual = await original ( ) ;
2339 const { Atom } = await import ( "effect/reactivity" ) ;
40+ const isAssetError = Schema . is ( AssetAccessError ) ;
2441 const family = Atom . family ( ( _key : string ) =>
25- Atom . make ( Effect . tryPromise ( ( ) => mint ( ) ) ) . pipe ( Atom . keepAlive ) ,
42+ Atom . make (
43+ Effect . tryPromise ( {
44+ try : ( ) => mint ( ) ,
45+ catch : ( error ) => ( isAssetError ( error ) ? error : new Cause . UnknownError ( error ) ) ,
46+ } ) ,
47+ ) . pipe ( Atom . keepAlive ) ,
2648 ) ;
2749 return {
2850 ...actual ,
@@ -40,14 +62,30 @@ vi.mock(import("../state/session"), async (original) => ({
4062 ...( await original ( ) ) ,
4163 readEnvironmentScope : ( ) => false ,
4264 useEnvironmentScope : ( ) => false ,
43- readPreparedConnection : ( ( ) => ( { httpBaseUrl : "https://host.test" } ) ) as never ,
65+ readPreparedConnection : ( ( ) => ( {
66+ httpBaseUrl : "https://host.test" ,
67+ target : remoteOpen . isResolved
68+ ? new PrimaryConnectionTarget ( {
69+ environmentId : EnvironmentId . make ( "media-environment" ) ,
70+ label : remoteOpen . label ,
71+ httpBaseUrl : remoteOpen . httpBaseUrl ,
72+ wsBaseUrl : "wss://host.test" ,
73+ } )
74+ : undefined ,
75+ } ) ) as never ,
4476 usePreparedConnection : ( ( ) => ( {
4577 _tag : "Some" ,
4678 value : { httpBaseUrl : "https://host.test" } ,
4779 } ) ) as never ,
4880} ) ) ;
4981
82+ vi . mock ( import ( "../remoteOpen" ) , async ( original ) => ( {
83+ ...( await original ( ) ) ,
84+ useRemoteOpenResolution : ( ) => remoteOpen ,
85+ } ) ) ;
86+
5087import ChatMarkdown from "./ChatMarkdown" ;
88+ import { toastManager } from "./ui/toast" ;
5189
5290const roots : Root [ ] = [ ] ;
5391let registry : AtomRegistry . AtomRegistry ;
@@ -68,9 +106,24 @@ afterEach(async () => {
68106 for ( const root of roots . splice ( 0 ) ) await act ( async ( ) => root . unmount ( ) ) ;
69107 registry . dispose ( ) ;
70108 document . body . replaceChildren ( ) ;
109+ remoteOpen . isResolved = true ;
110+ remoteOpen . state . mode = "local-exec" ;
111+ remoteOpen . label = "media-host" ;
112+ remoteOpen . httpBaseUrl = "https://host.test" ;
113+ vi . restoreAllMocks ( ) ;
71114 vi . unstubAllGlobals ( ) ;
72115} ) ;
73116
117+ function deferred < T > ( ) {
118+ let resolve ! : ( value : T ) => void ;
119+ let reject ! : ( error : unknown ) => void ;
120+ const promise = new Promise < T > ( ( resolvePromise , rejectPromise ) => {
121+ resolve = resolvePromise ;
122+ reject = rejectPromise ;
123+ } ) ;
124+ return { promise, resolve, reject } ;
125+ }
126+
74127function createTestRoot ( ) {
75128 const container = document . body . appendChild ( document . createElement ( "div" ) ) ;
76129 const root = createRoot ( container ) ;
@@ -88,7 +141,8 @@ function createTestRoot() {
88141
89142it ( "opens host media through server authorization before the client grant loads" , async ( ) => {
90143 mint . mockResolvedValue ( { relativeUrl : "/api/assets/image.png" , expiresAt : 1 } ) ;
91- const onImageExpand = vi . fn ( ) ;
144+ const expanded = deferred < unknown > ( ) ;
145+ const onImageExpand = vi . fn ( expanded . resolve ) ;
92146 const { container, render } = createTestRoot ( ) ;
93147 await render (
94148 < ChatMarkdown
@@ -104,20 +158,89 @@ it("opens host media through server authorization before the client grant loads"
104158 const link = container . querySelector < HTMLAnchorElement > ( 'a[href="/tmp/image.png"]' ) ;
105159 expect ( link ) . not . toBeNull ( ) ;
106160 await act ( async ( ) => link ! . click ( ) ) ;
107- await vi . waitFor ( ( ) =>
108- expect ( onImageExpand ) . toHaveBeenCalledWith (
109- expect . objectContaining ( {
110- images : [ expect . objectContaining ( { src : "https://host.test/api/assets/image.png" } ) ] ,
111- } ) ,
112- ) ,
113- ) ;
161+ expect ( await expanded . promise ) . toMatchObject ( {
162+ images : [ expect . objectContaining ( { src : "https://host.test/api/assets/image.png" } ) ] ,
163+ } ) ;
114164} ) ;
115165
116166const threadRef = {
117167 environmentId : EnvironmentId . make ( "media-environment" ) ,
118168 threadId : ThreadId . make ( "media-thread" ) ,
119169} ;
120170
171+ it . each ( [
172+ {
173+ name : "a remote lookup whose host resolves while it is pending" ,
174+ checkedPath : "/home/remote/Downloads/image.png" ,
175+ description : "The file was not found on settled-host at /home/remote/Downloads/image.png." ,
176+ } ,
177+ {
178+ name : "an older remote server without a checked path" ,
179+ description : "The file was not found on settled-host at ~/Downloads/image.png." ,
180+ } ,
181+ {
182+ name : "a missing file on this device" ,
183+ local : true ,
184+ checkedPath : "/home/local/Downloads/image.png" ,
185+ description : "Media file was not found." ,
186+ } ,
187+ {
188+ name : "a different remote failure" ,
189+ unsupported : true ,
190+ description : "Only images, videos, audio, HTML, and PDF files can be previewed." ,
191+ } ,
192+ ] ) ( "reports the media failure for $name" , async ( scenario ) => {
193+ const lookup = deferred < AssetCreateUrlResult > ( ) ;
194+ const started = deferred < void > ( ) ;
195+ const reported = deferred < Parameters < typeof toastManager . add > [ 0 ] > ( ) ;
196+ mint . mockImplementation ( ( ) => {
197+ started . resolve ( ) ;
198+ return lookup . promise ;
199+ } ) ;
200+ vi . spyOn ( toastManager , "add" ) . mockImplementation ( ( toast ) => {
201+ reported . resolve ( toast ) ;
202+ return "media-toast" ;
203+ } ) ;
204+ remoteOpen . isResolved = false ;
205+ const { container, render } = createTestRoot ( ) ;
206+ const content = (
207+ < ChatMarkdown cwd = "/repo" threadRef = { threadRef } text = "[Open image](~/Downloads/image.png)" />
208+ ) ;
209+ await render ( content ) ;
210+ const link = container . querySelector < HTMLAnchorElement > ( 'a[href="~/Downloads/image.png"]' ) ;
211+ expect ( link ) . not . toBeNull ( ) ;
212+ await act ( async ( ) => {
213+ link ! . click ( ) ;
214+ await started . promise ;
215+ } ) ;
216+
217+ remoteOpen . isResolved = true ;
218+ remoteOpen . state . mode = scenario . local ? "local-exec" : "remote-unavailable" ;
219+ remoteOpen . httpBaseUrl = scenario . local ? "http://localhost:3773" : "https://host.test" ;
220+ remoteOpen . label = "settled-host" ;
221+ await render ( content ) ;
222+ const resource = {
223+ _tag : "media-file" as const ,
224+ path : "~/Downloads/image.png" ,
225+ threadId : threadRef . threadId ,
226+ } ;
227+ await act ( async ( ) => {
228+ lookup . reject (
229+ scenario . unsupported
230+ ? new AssetPreviewTypeValidationError ( { resource } )
231+ : new AssetWorkspaceAssetNotFoundError ( {
232+ resource,
233+ ...( scenario . checkedPath ? { checkedPath : scenario . checkedPath } : { } ) ,
234+ } ) ,
235+ ) ;
236+ await reported . promise ;
237+ } ) ;
238+ expect ( await reported . promise ) . toMatchObject ( {
239+ title : "Media unavailable" ,
240+ description : scenario . description ,
241+ } ) ;
242+ } ) ;
243+
121244function markdown ( workspaceMutationId ?: string | null ) {
122245 return (
123246 < ChatMarkdown
0 commit comments