@@ -2,6 +2,8 @@ import { describe, expect, it } from "vitest";
22import {
33 extractLeadingHttpStatus ,
44 extractProviderWrappedHttpStatus ,
5+ formatRawAssistantErrorForUi ,
6+ parseApiErrorInfo ,
57} from "./assistant-error-format.js" ;
68
79describe ( "extractLeadingHttpStatus" , ( ) => {
@@ -55,3 +57,31 @@ describe("extractProviderWrappedHttpStatus", () => {
5557 expect ( extractProviderWrappedHttpStatus ( "API error (600): something" ) ) . toBeNull ( ) ;
5658 } ) ;
5759} ) ;
60+
61+ describe ( "HTTP status consumers" , ( ) => {
62+ it ( "formats only status lines inside the HTTP range" , ( ) => {
63+ expect ( formatRawAssistantErrorForUi ( "100 Continue" ) ) . toBe ( "HTTP 100: Continue" ) ;
64+ expect ( formatRawAssistantErrorForUi ( "599 Provider Error" ) ) . toBe ( "HTTP 599: Provider Error" ) ;
65+ expect ( formatRawAssistantErrorForUi ( "000 Invalid" ) ) . toBe ( "000 Invalid" ) ;
66+ expect ( formatRawAssistantErrorForUi ( "600 Invalid" ) ) . toBe ( "600 Invalid" ) ;
67+ expect ( formatRawAssistantErrorForUi ( "999 Invalid" ) ) . toBe ( "999 Invalid" ) ;
68+ } ) ;
69+
70+ it ( "does not attach invalid status prefixes to API payloads" , ( ) => {
71+ const payload = '{"type":"error","error":{"type":"server_error","message":"Provider failed."}}' ;
72+
73+ expect ( parseApiErrorInfo ( `599 ${ payload } ` ) ) . toMatchObject ( {
74+ httpCode : "599" ,
75+ type : "server_error" ,
76+ message : "Provider failed." ,
77+ } ) ;
78+ expect ( formatRawAssistantErrorForUi ( `599 ${ payload } ` ) ) . toBe (
79+ "HTTP 599 server_error: Provider failed." ,
80+ ) ;
81+
82+ for ( const code of [ "000" , "600" , "999" ] ) {
83+ expect ( parseApiErrorInfo ( `${ code } ${ payload } ` ) ) . toBeNull ( ) ;
84+ expect ( formatRawAssistantErrorForUi ( `${ code } ${ payload } ` ) ) . toBe ( `${ code } ${ payload } ` ) ;
85+ }
86+ } ) ;
87+ } ) ;
0 commit comments