@@ -21,6 +21,25 @@ describe("anthropicMessagesUrl", () => {
2121 ] as const ) ( "normalizes %s" , ( input , expected ) => {
2222 expect ( anthropicMessagesUrl ( input ) ) . toBe ( expected ) ;
2323 } ) ;
24+
25+ test ( "malformed baseUrl throws without echoing the URL" , ( ) => {
26+ const sensitive = "https://user:[email protected] /v1?token=abc" ; 27+ expect ( ( ) => anthropicMessagesUrl ( sensitive ) ) . toThrow ( "anthropic provider has malformed baseUrl" ) ;
28+ try {
29+ anthropicMessagesUrl ( sensitive ) ;
30+ } catch ( err ) {
31+ expect ( String ( err ) ) . not . toContain ( "secret" ) ;
32+ expect ( String ( err ) ) . not . toContain ( "token=abc" ) ;
33+ }
34+ } ) ;
35+
36+ test . each ( [
37+ "https://example.test/v1?tenant=a" ,
38+ "https://example.test/v1#frag" ,
39+ "https://example.test/v1/messages?stream=1" ,
40+ ] as const ) ( "rejects search/hash in %s" , ( input ) => {
41+ expect ( ( ) => anthropicMessagesUrl ( input ) ) . toThrow ( "anthropic provider has malformed baseUrl" ) ;
42+ } ) ;
2443} ) ;
2544
2645describe ( "anthropic stream hardening" , ( ) => {
@@ -36,6 +55,18 @@ describe("anthropic stream hardening", () => {
3655 expect ( events . some ( e => e . type === "done" ) ) . toBe ( false ) ;
3756 } ) ;
3857
58+ test ( "EOF after message_delta stop_reason completes without message_stop" , async ( ) => {
59+ const response = new Response ( [
60+ "event: content_block_delta\n" ,
61+ 'data: {"type":"content_block_delta","delta":{"type":"text_delta","text":"hi"}}\n\n' ,
62+ "event: message_delta\n" ,
63+ 'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":2}}\n\n' ,
64+ ] . join ( "" ) ) ;
65+ const events = await collect ( createAnthropicAdapter ( provider ) . parseStream ( response ) ) ;
66+ expect ( events . at ( - 1 ) ) . toMatchObject ( { type : "done" , stopReason : "end_turn" } ) ;
67+ expect ( events . some ( e => e . type === "error" ) ) . toBe ( false ) ;
68+ } ) ;
69+
3970 test ( "input_json_delta outside tool_use is ignored" , async ( ) => {
4071 const response = new Response ( [
4172 "event: content_block_start\n" ,
@@ -67,6 +98,20 @@ describe("anthropic stream hardening", () => {
6798 expect ( start && "id" in start && start . id . startsWith ( "toolu_" ) ) . toBe ( true ) ;
6899 } ) ;
69100
101+ test ( "whitespace tool_use id is treated as missing" , async ( ) => {
102+ const response = new Response ( [
103+ "event: content_block_start\n" ,
104+ 'data: {"type":"content_block_start","content_block":{"type":"tool_use","id":" ","name":"get_weather"}}\n\n' ,
105+ "event: content_block_stop\n" ,
106+ 'data: {"type":"content_block_stop"}\n\n' ,
107+ "event: message_stop\n" ,
108+ 'data: {"type":"message_stop"}\n\n' ,
109+ ] . join ( "" ) ) ;
110+ const events = await collect ( createAnthropicAdapter ( provider ) . parseStream ( response ) ) ;
111+ const start = events . find ( e => e . type === "tool_call_start" ) ;
112+ expect ( start && "id" in start && start . id . startsWith ( "toolu_" ) ) . toBe ( true ) ;
113+ } ) ;
114+
70115 test ( "empty EOF without content still errors" , async ( ) => {
71116 const response = new Response ( "" ) ;
72117 const events = await collect ( createAnthropicAdapter ( provider ) . parseStream ( response ) ) ;
@@ -88,4 +133,14 @@ describe("anthropic non-stream tool_use input", () => {
88133 } ) ;
89134 expect ( events . at ( - 1 ) ?. type ) . toBe ( "done" ) ;
90135 } ) ;
136+
137+ test ( "whitespace tool_use id synthesizes in non-stream response" , async ( ) => {
138+ const adapter = createAnthropicAdapter ( provider ) ;
139+ const events = await adapter . parseResponse ! ( new Response ( JSON . stringify ( {
140+ content : [ { type : "tool_use" , id : " " , name : "get_weather" , input : { } } ] ,
141+ stop_reason : "tool_use" ,
142+ } ) ) ) ;
143+ const start = events . find ( e => e . type === "tool_call_start" ) ;
144+ expect ( start && "id" in start && start . id . startsWith ( "toolu_" ) ) . toBe ( true ) ;
145+ } ) ;
91146} ) ;
0 commit comments