@@ -87,21 +87,24 @@ describe("createApiRouter", () => {
8787 tokenManager,
8888 } ) ;
8989
90- await withServer ( ( req , res ) => {
91- const url = new URL ( req . url ?? "/" , "http://127.0.0.1" ) ;
92- void tryHandleApiRequest ( req , res , url ) ;
93- } , async ( baseUrl ) => {
94- const response = await request ( baseUrl , "/api/pairing?ttl=300" ) ;
95- const body = JSON . parse ( response . body ) as {
96- pairingUrl : string ;
97- serverUrl : string ;
98- expiresAt : string ;
99- } ;
100- expect ( response . statusCode ) . toBe ( 200 ) ;
101- expect ( body . serverUrl ) . toBe ( "http://127.0.0.1:31337" ) ;
102- expect ( body . pairingUrl ) . toContain ( "okcode://pair?server=" ) ;
103- expect ( body . expiresAt ) . toMatch ( / ^ \d { 4 } - \d { 2 } - \d { 2 } T / ) ;
104- } ) ;
90+ await withServer (
91+ ( req , res ) => {
92+ const url = new URL ( req . url ?? "/" , "http://127.0.0.1" ) ;
93+ void tryHandleApiRequest ( req , res , url ) ;
94+ } ,
95+ async ( baseUrl ) => {
96+ const response = await request ( baseUrl , "/api/pairing?ttl=300" ) ;
97+ const body = JSON . parse ( response . body ) as {
98+ pairingUrl : string ;
99+ serverUrl : string ;
100+ expiresAt : string ;
101+ } ;
102+ expect ( response . statusCode ) . toBe ( 200 ) ;
103+ expect ( body . serverUrl ) . toBe ( "http://127.0.0.1:31337" ) ;
104+ expect ( body . pairingUrl ) . toContain ( "okcode://pair?server=" ) ;
105+ expect ( body . expiresAt ) . toMatch ( / ^ \d { 4 } - \d { 2 } - \d { 2 } T / ) ;
106+ } ,
107+ ) ;
105108 } ) ;
106109
107110 it ( "proxies Anthropic message requests with the Claude Code envelope" , async ( ) => {
@@ -142,49 +145,50 @@ describe("createApiRouter", () => {
142145 anthropicBaseUrl : `http://127.0.0.1:${ upstreamAddress . port } ` ,
143146 } ) ;
144147
145- await withServer ( ( req , res ) => {
146- const url = new URL ( req . url ?? "/" , "http://127.0.0.1" ) ;
147- void tryHandleApiRequest ( req , res , url ) ;
148- } , async ( baseUrl ) => {
149- const response = await request ( baseUrl , "/api/auth/anthropic/v1/messages" , {
150- method : "POST" ,
151- headers : {
152- "Content-Type" : "application/json" ,
153- "x-api-key" : "test-key" ,
154- "anthropic-version" : "2023-06-01" ,
155- "anthropic-beta" : "tools-2024-04-04" ,
156- } ,
157- body : JSON . stringify ( {
148+ await withServer (
149+ ( req , res ) => {
150+ const url = new URL ( req . url ?? "/" , "http://127.0.0.1" ) ;
151+ void tryHandleApiRequest ( req , res , url ) ;
152+ } ,
153+ async ( baseUrl ) => {
154+ const response = await request ( baseUrl , "/api/auth/anthropic/v1/messages" , {
155+ method : "POST" ,
156+ headers : {
157+ "Content-Type" : "application/json" ,
158+ "x-api-key" : "test-key" ,
159+ "anthropic-version" : "2023-06-01" ,
160+ "anthropic-beta" : "tools-2024-04-04" ,
161+ } ,
162+ body : JSON . stringify ( {
163+ model : "claude-sonnet-4-20250514" ,
164+ max_tokens : 64 ,
165+ system : "Original system prompt" ,
166+ messages : [ { role : "user" , content : "Hello" } ] ,
167+ stream : true ,
168+ } ) ,
169+ } ) ;
170+
171+ expect ( response . statusCode ) . toBe ( 200 ) ;
172+ expect ( JSON . parse ( response . body ) ) . toEqual ( { ok : true , proxied : true } ) ;
173+ expect ( upstreamHeaders ?. [ "x-api-key" ] ) . toBe ( "test-key" ) ;
174+ expect ( upstreamHeaders ?. [ "anthropic-version" ] ) . toBe ( "2023-06-01" ) ;
175+ expect ( upstreamHeaders ?. [ "anthropic-beta" ] ) . toBe ( "claude-code-20250219,tools-2024-04-04" ) ;
176+ expect ( upstreamBody ) . toMatchObject ( {
158177 model : "claude-sonnet-4-20250514" ,
159- max_tokens : 64 ,
160- system : "Original system prompt" ,
161- messages : [ { role : "user" , content : "Hello" } ] ,
162178 stream : true ,
163- } ) ,
164- } ) ;
165-
166- expect ( response . statusCode ) . toBe ( 200 ) ;
167- expect ( JSON . parse ( response . body ) ) . toEqual ( { ok : true , proxied : true } ) ;
168- expect ( upstreamHeaders ?. [ "x-api-key" ] ) . toBe ( "test-key" ) ;
169- expect ( upstreamHeaders ?. [ "anthropic-version" ] ) . toBe ( "2023-06-01" ) ;
170- expect ( upstreamHeaders ?. [ "anthropic-beta" ] ) . toBe (
171- "claude-code-20250219,tools-2024-04-04" ,
172- ) ;
173- expect ( upstreamBody ) . toMatchObject ( {
174- model : "claude-sonnet-4-20250514" ,
175- stream : true ,
176- } ) ;
177- expect ( upstreamBody ?. system ) . toEqual ( [
178- {
179- type : "text" ,
180- text : "You are Claude Code, Anthropic's official CLI for Claude." ,
181- } ,
182- {
183- type : "text" ,
184- text : "Original system prompt" ,
185- } ,
186- ] ) ;
187- } ) ;
179+ } ) ;
180+ expect ( upstreamBody ?. system ) . toEqual ( [
181+ {
182+ type : "text" ,
183+ text : "You are Claude Code, Anthropic's official CLI for Claude." ,
184+ } ,
185+ {
186+ type : "text" ,
187+ text : "Original system prompt" ,
188+ } ,
189+ ] ) ;
190+ } ,
191+ ) ;
188192 } ) ;
189193
190194 it ( "returns a JSON 404 for unknown API routes" , async ( ) => {
@@ -195,13 +199,16 @@ describe("createApiRouter", () => {
195199 tokenManager : new TokenManager ( undefined ) ,
196200 } ) ;
197201
198- await withServer ( ( req , res ) => {
199- const url = new URL ( req . url ?? "/" , "http://127.0.0.1" ) ;
200- void tryHandleApiRequest ( req , res , url ) ;
201- } , async ( baseUrl ) => {
202- const response = await request ( baseUrl , "/api/unknown" ) ;
203- expect ( response . statusCode ) . toBe ( 404 ) ;
204- expect ( JSON . parse ( response . body ) ) . toEqual ( { error : "Not Found" } ) ;
205- } ) ;
202+ await withServer (
203+ ( req , res ) => {
204+ const url = new URL ( req . url ?? "/" , "http://127.0.0.1" ) ;
205+ void tryHandleApiRequest ( req , res , url ) ;
206+ } ,
207+ async ( baseUrl ) => {
208+ const response = await request ( baseUrl , "/api/unknown" ) ;
209+ expect ( response . statusCode ) . toBe ( 404 ) ;
210+ expect ( JSON . parse ( response . body ) ) . toEqual ( { error : "Not Found" } ) ;
211+ } ,
212+ ) ;
206213 } ) ;
207214} ) ;
0 commit comments