1- import { waitForNativeMainStartupGate } from "../src/codex/native-profile-startup" ;
21import { afterEach , beforeEach , expect , test } from "bun:test" ;
32import { managementFetch as fetch } from "./helpers/management-auth" ;
43import { logsFromApiBody } from "./helpers/logs-api" ;
@@ -12,6 +11,7 @@ import type { RequestLogContext } from "../src/server/request-log";
1211import { startServer } from "../src/server" ;
1312import {
1413 fetchWithHeaderDeadline ,
14+ handleClaudeMessages ,
1515 readBoundedPassthroughBody ,
1616 resolvePassthroughBodyGuard ,
1717 tapAnthropicSseForLog ,
@@ -24,6 +24,7 @@ import {
2424 acquireNativeMainProfileDrain ,
2525 getNativeMainProfileRequestCount ,
2626 resetLifecycleDrainStateForTests ,
27+ tryAdmitTurn ,
2728} from "../src/server/lifecycle" ;
2829import {
2930 blockNativeMainRecovery ,
@@ -820,8 +821,7 @@ test("routed Claude requests give OpenAI sidecars main auth without leaking it t
820821 writeFileSync ( join ( isolatedCodexHome ! . path , "auth.json" ) , JSON . stringify ( {
821822 tokens : { access_token : mainAccessToken , account_id : mainAccountId } ,
822823 } ) ) ;
823- const server = startServer ( 0 ) ;
824- await waitForNativeMainStartupGate ( ) ;
824+ let requestSequence = 0 ;
825825 const requestBody = {
826826 model : "routed/text-model" ,
827827 max_tokens : 128 ,
@@ -835,11 +835,33 @@ test("routed Claude requests give OpenAI sidecars main auth without leaking it t
835835 ] ,
836836 } ] ,
837837 } ;
838+ const invokeMessages = async ( ) : Promise < number > => {
839+ const turnAdmissionLease = tryAdmitTurn ( ) ;
840+ if ( ! turnAdmissionLease ) throw new Error ( "test turn admission unavailable" ) ;
841+ const start = Date . now ( ) ;
842+ try {
843+ const response = await handleClaudeMessages (
844+ new Request ( "http://localhost/v1/messages" , {
845+ method : "POST" ,
846+ headers : {
847+ "content-type" : "application/json" ,
848+ "x-api-key" : "placeholder" ,
849+ "anthropic-version" : "2023-06-01" ,
850+ } ,
851+ body : JSON . stringify ( requestBody ) ,
852+ } ) ,
853+ config ,
854+ { model : "unknown" , provider : "unknown" , inboundProtocol : "messages" } as RequestLogContext ,
855+ { requestId : `claude-sidecar-test-${ ++ requestSequence } ` , start, turnAdmissionLease } ,
856+ ) ;
857+ await response . text ( ) ;
858+ return response . status ;
859+ } finally {
860+ turnAdmissionLease . release ( ) ;
861+ }
862+ } ;
838863 try {
839- await waitForNativeMainStartupGate ( ) ;
840- const authenticated = await postMessages ( server . url . toString ( ) , requestBody ) ;
841- expect ( authenticated . status ) . toBe ( 200 ) ;
842- await authenticated . text ( ) ;
864+ expect ( await invokeMessages ( ) ) . toBe ( 200 ) ;
843865
844866 expect ( sidecarCalls . map ( call => call . kind ) . sort ( ) ) . toEqual ( [ "vision" , "web-search" ] ) ;
845867 for ( const call of sidecarCalls ) {
@@ -857,19 +879,16 @@ test("routed Claude requests give OpenAI sidecars main auth without leaking it t
857879
858880 rmSync ( join ( isolatedCodexHome ! . path , "auth.json" ) ) ;
859881 const sidecarCountBeforeNoLogin = sidecarCalls . length ;
860- const noLogin = await postMessages ( server . url . toString ( ) , requestBody ) ;
861- expect ( noLogin . status ) . toBe ( 200 ) ;
862- await noLogin . text ( ) ;
882+ expect ( await invokeMessages ( ) ) . toBe ( 200 ) ;
863883
864884 expect ( sidecarCalls . length ) . toBe ( sidecarCountBeforeNoLogin ) ;
865885 expect ( routedCalls . at ( - 1 ) ?. authorization ) . toBe ( "Bearer routed-provider-key" ) ;
866886 const noLoginBody = JSON . stringify ( routedCalls . at ( - 1 ) ?. body ) ;
867887 expect ( noLoginBody ) . toContain ( "[image omitted: this model is text-only and the vision sidecar is unavailable (no ChatGPT login)]" ) ;
868888 expect ( noLoginBody ) . not . toContain ( imageBytes ) ;
869889 } finally {
870- await server . stop ( true ) ;
871- forward . stop ( true ) ;
872- routed . stop ( true ) ;
890+ await forward . stop ( true ) ;
891+ await routed . stop ( true ) ;
873892 }
874893} ) ;
875894
0 commit comments