@@ -71,6 +71,64 @@ export async function createAuthenticatedFixture(
7171 runtimePaths = createTemporaryRuntimePaths ( )
7272) : Promise < SpawnedAgentFixture > {
7373 const apiKey = requireLiveApiKey ( ) ;
74+ return await createSpawnedFixture ( async ( connection , authMethods ) => {
75+ if ( ! authMethods . some ( ( method ) => method . id === "api-key" ) ) {
76+ throw new Error ( "API key authentication is not available." ) ;
77+ }
78+
79+ await connection . authenticate ( {
80+ methodId : "api-key" ,
81+ _meta : {
82+ "api-key" : {
83+ apiKey,
84+ } ,
85+ } ,
86+ } ) ;
87+
88+ const authenticationStatus = await getAuthenticationStatus ( connection ) ;
89+ if ( authenticationStatus [ "type" ] !== "api-key" ) {
90+ throw new Error ( `Unexpected authentication status: ${ JSON . stringify ( authenticationStatus ) } ` ) ;
91+ }
92+ } , runtimePaths ) ;
93+ }
94+
95+ export interface GatewayFixtureOptions {
96+ readonly baseUrl : string ;
97+ readonly headers ?: Record < string , string > ;
98+ }
99+
100+ export async function createGatewayFixture (
101+ options : GatewayFixtureOptions ,
102+ runtimePaths = createTemporaryRuntimePaths ( ) ,
103+ ) : Promise < SpawnedAgentFixture > {
104+ return await createSpawnedFixture ( async ( connection , authMethods ) => {
105+ if ( ! authMethods . some ( ( method ) => method . id === "gateway" ) ) {
106+ throw new Error ( "Gateway authentication is not available." ) ;
107+ }
108+
109+ await connection . authenticate ( {
110+ methodId : "gateway" ,
111+ _meta : {
112+ gateway : {
113+ baseUrl : options . baseUrl ,
114+ headers : options . headers ?? { } ,
115+ } ,
116+ } ,
117+ } ) ;
118+
119+ const authenticationStatus = await getAuthenticationStatus ( connection ) ;
120+ if ( authenticationStatus [ "type" ] !== "gateway" || authenticationStatus [ "name" ] !== "custom-gateway" ) {
121+ throw new Error ( `Unexpected authentication status: ${ JSON . stringify ( authenticationStatus ) } ` ) ;
122+ }
123+ } , runtimePaths ) ;
124+ }
125+
126+ type Authenticator = ( connection : acp . ClientSideConnection , authMethods : acp . AuthMethod [ ] ) => Promise < void > ;
127+
128+ async function createSpawnedFixture (
129+ authenticate : Authenticator ,
130+ runtimePaths : RuntimePaths ,
131+ ) : Promise < SpawnedAgentFixture > {
74132 const agentProcess = spawn ( "npm" , [ "run" , "--silent" , "start" ] , {
75133 cwd : process . cwd ( ) ,
76134 env : {
@@ -101,23 +159,7 @@ export async function createAuthenticatedFixture(
101159 throw new Error ( `Unexpected protocol version: ${ initializeResponse . protocolVersion } ` ) ;
102160 }
103161
104- if ( ! initializeResponse . authMethods ?. some ( ( method ) => method . id === "api-key" ) ) {
105- throw new Error ( "API key authentication is not available." ) ;
106- }
107-
108- await connection . authenticate ( {
109- methodId : "api-key" ,
110- _meta : {
111- "api-key" : {
112- apiKey,
113- } ,
114- } ,
115- } ) ;
116-
117- const authenticationStatus = await getAuthenticationStatus ( connection ) ;
118- if ( authenticationStatus [ "type" ] !== "api-key" ) {
119- throw new Error ( `Unexpected authentication status: ${ JSON . stringify ( authenticationStatus ) } ` ) ;
120- }
162+ await authenticate ( connection , initializeResponse . authMethods ?? [ ] ) ;
121163
122164 const createSession = async ( ) : Promise < SpawnedSessionFixture > => {
123165 const newSessionResponse = await connection . newSession ( {
@@ -153,7 +195,7 @@ export async function createAuthenticatedFixture(
153195 } ;
154196}
155197
156- function requireLiveApiKey ( ) : string {
198+ export function requireLiveApiKey ( ) : string {
157199 const apiKey = process . env [ "CODEX_API_KEY" ] ?? process . env [ "OPENAI_API_KEY" ] ;
158200 if ( ! apiKey ) {
159201 throw new Error ( "Live integration test requires CODEX_API_KEY or OPENAI_API_KEY." ) ;
0 commit comments