1+ // @effect -diagnostics nodeBuiltinImport:off globalFetchInEffect:off - Hosted handoff test uses a real localhost listener without an OpenAI account.
2+ import * as NodeHttp from "node:http" ;
3+ import { codexAuthHandoffUrl , readCodexAuthDelivery } from "@t3tools/shared/codexAuthHandoff" ;
4+ import { EnvironmentId , ProviderInstanceId } from "@t3tools/contracts" ;
5+ import { HostProcessArguments } from "@t3tools/shared/hostProcess" ;
16import { assert , describe , it } from "@effect/vitest" ;
27import * as Cause from "effect/Cause" ;
38import * as Effect from "effect/Effect" ;
@@ -22,14 +27,24 @@ vi.mock("@clerk/electron/storage", () => ({
2227 storage : storageMock ,
2328} ) ) ;
2429
30+ import * as Option from "effect/Option" ;
2531import * as Exit from "effect/Exit" ;
2632import * as FileSystem from "effect/FileSystem" ;
2733import * as ElectronApp from "../electron/ElectronApp.ts" ;
34+ import * as ElectronShell from "../electron/ElectronShell.ts" ;
2835import * as ElectronWindow from "../electron/ElectronWindow.ts" ;
2936import * as DesktopClerk from "./DesktopClerk.ts" ;
3037import * as DesktopEnvironment from "./DesktopEnvironment.ts" ;
3138
32- const makeDesktopClerkLayer = ( isDevelopment = true , events : string [ ] = [ ] ) => {
39+ const makeDesktopClerkLayer = (
40+ isDevelopment = true ,
41+ events : string [ ] = [ ] ,
42+ shell : ElectronShell . ElectronShell [ "Service" ] = {
43+ openExternal : ( ) => Effect . succeed ( true ) ,
44+ openSystemSettings : ( ) => Effect . succeed ( false ) ,
45+ copyText : ( ) => Effect . void ,
46+ } ,
47+ ) => {
3348 const environment = DesktopEnvironment . DesktopEnvironment . of ( {
3449 stateDir : "/tmp/t3-state" ,
3550 isDevelopment,
@@ -51,6 +66,7 @@ const makeDesktopClerkLayer = (isDevelopment = true, events: string[] = []) => {
5166 Layer . mergeAll (
5267 Layer . succeed ( DesktopEnvironment . DesktopEnvironment , environment ) ,
5368 Layer . succeed ( ElectronApp . ElectronApp , electronApp ) ,
69+ Layer . succeed ( ElectronShell . ElectronShell , shell ) ,
5470 FileSystem . layerNoop ( { exists : ( ) => Effect . succeed ( false ) } ) ,
5571 ) ,
5672 ) ,
@@ -162,7 +178,7 @@ describe("DesktopClerk", () => {
162178
163179 assert . isTrue ( Exit . isSuccess ( exit ) ) ;
164180 assert . equal ( quit . mock . calls . length , 0 ) ;
165- assert . deepEqual ( registeredEvents , [ "second-instance" ] ) ;
181+ assert . deepEqual ( registeredEvents , [ "open-url" , " second-instance"] ) ;
166182 } ) . pipe (
167183 Effect . provide ( makeDesktopClerkLayer ( ) ) ,
168184 Effect . provideService ( ElectronApp . ElectronApp , electronApp ) ,
@@ -198,3 +214,131 @@ describe("DesktopClerk", () => {
198214 ) ;
199215 } ) ;
200216} ) ;
217+
218+ it . effect (
219+ "provider auth deep links navigate and reveal the running desktop without handling Clerk URLs" ,
220+ ( ) => {
221+ storageMock . mockReturnValue ( storageAdapter ) ;
222+ createClerkBridgeMock . mockReturnValue ( { cleanup : vi . fn ( ) , isPrimaryInstance : true } ) ;
223+ const listeners = new Map < string , ( ...args : unknown [ ] ) => void > ( ) ;
224+ const revealed = Promise . withResolvers < void > ( ) ;
225+ const loadURL = vi . fn ( async ( _url : string ) => undefined ) ;
226+ const window = { loadURL } ;
227+ const electronApp = {
228+ on : ( name : string , listener : ( ...args : unknown [ ] ) => void ) =>
229+ Effect . sync ( ( ) => {
230+ listeners . set ( name , listener ) ;
231+ } ) ,
232+ } as unknown as ElectronApp . ElectronApp [ "Service" ] ;
233+ const electronWindow = {
234+ currentMainOrFirst : Effect . succeed ( Option . some ( window ) ) ,
235+ reveal : ( ) => Effect . sync ( ( ) => revealed . resolve ( ) ) ,
236+ } as unknown as ElectronWindow . ElectronWindow [ "Service" ] ;
237+ return Effect . gen ( function * ( ) {
238+ const clerk = yield * DesktopClerk . DesktopClerk ;
239+ yield * clerk . configure ;
240+ const event = { preventDefault : vi . fn ( ) } ;
241+ listeners . get ( "open-url" ) ! ( event , "t3code-dev://app/auth/callback?code=clerk-code" ) ;
242+ listeners . get ( "open-url" ) ! ( event , "t3code://app/welcome" ) ;
243+ assert . equal ( loadURL . mock . calls . length , 0 ) ;
244+ assert . equal ( event . preventDefault . mock . calls . length , 0 ) ;
245+ listeners . get ( "second-instance" ) ! ( { } , [
246+ "t3" ,
247+ "t3code-dev://app/settings/providers?instanceId=work&code=never-forward" ,
248+ ] ) ;
249+ yield * Effect . promise ( ( ) => revealed . promise ) ;
250+ assert . deepEqual ( loadURL . mock . calls , [
251+ [ "t3code-dev://app/settings/providers?instanceId=work" ] ,
252+ ] ) ;
253+ listeners . get ( "open-url" ) ! ( event , "t3code-dev://app/welcome#agents:machine-id" ) ;
254+ assert . equal ( event . preventDefault . mock . calls . length , 1 ) ;
255+ } ) . pipe (
256+ Effect . scoped ,
257+ Effect . provide ( makeDesktopClerkLayer ( ) ) ,
258+ Effect . provideService ( ElectronApp . ElectronApp , electronApp ) ,
259+ Effect . provideService ( ElectronWindow . ElectronWindow , electronWindow ) ,
260+ ) ;
261+ } ,
262+ ) ;
263+
264+ for ( const entry of [ "startup" , "open-url" ] as const ) {
265+ it . effect ( `receives hosted web sign-in through the desktop ${ entry } handler` , ( ) =>
266+ Effect . gen ( function * ( ) {
267+ storageMock . mockReturnValue ( storageAdapter ) ;
268+ createClerkBridgeMock . mockReturnValue ( { cleanup : vi . fn ( ) , isPrimaryInstance : true } ) ;
269+ const port = yield * Effect . promise ( async ( ) => {
270+ const server = NodeHttp . createServer ( ) ;
271+ await new Promise < void > ( ( resolve ) => server . listen ( 0 , "127.0.0.1" , resolve ) ) ;
272+ const address = server . address ( ) ;
273+ if ( ! address || typeof address === "string" ) throw new Error ( "address" ) ;
274+ await new Promise < void > ( ( resolve ) => server . close ( ( ) => resolve ( ) ) ) ;
275+ return address . port ;
276+ } ) ;
277+ const authorize = new URL ( "https://auth.openai.com/api/accounts/authorize" ) ;
278+ authorize . search = new URLSearchParams ( {
279+ client_id : "dynamic_agent_client" ,
280+ response_type : "code" ,
281+ redirect_uri : `http://127.0.0.1:${ port } /auth/callback` ,
282+ state : "a" . repeat ( 43 ) ,
283+ code_challenge_method : "S256" ,
284+ code_challenge : "b" . repeat ( 43 ) ,
285+ } ) . toString ( ) ;
286+ const request = {
287+ authorizationUrl : authorize . toString ( ) ,
288+ returnUrl : "https://app.t3.codes/welcome#agents:remote-one" ,
289+ environmentId : EnvironmentId . make ( "remote-one" ) ,
290+ instanceId : ProviderInstanceId . make ( "work" ) ,
291+ flowId : "flow-one" ,
292+ } ;
293+ const link = codexAuthHandoffUrl ( request , true ) ;
294+ const delivered = Promise . withResolvers < string > ( ) ;
295+ const shell = ElectronShell . ElectronShell . of ( {
296+ openExternal : ( value ) =>
297+ Effect . promise ( async ( ) => {
298+ const url = new URL ( String ( value ) ) ;
299+ const callback = new URL ( url . searchParams . get ( "redirect_uri" ) ! ) ;
300+ callback . search = new URLSearchParams ( {
301+ state : url . searchParams . get ( "state" ) ! ,
302+ code : "test-code" ,
303+ client_id : "oaiapp_test" ,
304+ } ) . toString ( ) ;
305+ const response = await fetch ( callback , { redirect : "manual" } ) ;
306+ delivered . resolve ( response . headers . get ( "location" ) ! ) ;
307+ return true ;
308+ } ) ,
309+ openSystemSettings : ( ) => Effect . succeed ( false ) ,
310+ copyText : ( ) => Effect . void ,
311+ } ) ;
312+ const listeners = new Map < string , ( ...args : unknown [ ] ) => void > ( ) ;
313+ const electronApp = {
314+ whenReady : Effect . void ,
315+ on : ( name : string , listener : ( ...args : unknown [ ] ) => void ) =>
316+ Effect . sync ( ( ) => {
317+ listeners . set ( name , listener ) ;
318+ } ) ,
319+ } as unknown as ElectronApp . ElectronApp [ "Service" ] ;
320+ yield * Effect . gen ( function * ( ) {
321+ const clerk = yield * DesktopClerk . DesktopClerk ;
322+ yield * clerk . configure ;
323+ if ( entry === "open-url" ) {
324+ const event = { preventDefault : vi . fn ( ) } ;
325+ listeners . get ( "open-url" ) ! ( event , link ) ;
326+ assert . strictEqual ( event . preventDefault . mock . calls . length , 1 ) ;
327+ }
328+ const delivery = readCodexAuthDelivery ( yield * Effect . promise ( ( ) => delivered . promise ) ) ;
329+ assert . strictEqual ( delivery ?. environmentId , request . environmentId ) ;
330+ assert . strictEqual ( delivery ?. instanceId , request . instanceId ) ;
331+ assert . strictEqual ( delivery ?. flowId , request . flowId ) ;
332+ assert . strictEqual ( delivery ?. returnUrl , request . returnUrl ) ;
333+ } ) . pipe (
334+ Effect . provide ( makeDesktopClerkLayer ( true , [ ] , shell ) ) ,
335+ Effect . provideService ( HostProcessArguments , entry === "startup" ? [ "t3" , link ] : [ "t3" ] ) ,
336+ Effect . provideService ( ElectronApp . ElectronApp , electronApp ) ,
337+ Effect . provideService (
338+ ElectronWindow . ElectronWindow ,
339+ { } as ElectronWindow . ElectronWindow [ "Service" ] ,
340+ ) ,
341+ ) ;
342+ } ) . pipe ( Effect . scoped ) ,
343+ ) ;
344+ }
0 commit comments