22import { describe , expect , it , vi } from "vitest" ;
33import {
44 classifyMSTeamsSendError ,
5+ formatMSTeamsDeliveryFailureGuidance ,
56 formatMSTeamsSendErrorHint ,
67 formatUnknownError ,
78 isRevokedProxyError ,
89} from "./errors.js" ;
910import { withRevokedProxyFallback } from "./revoked-context.js" ;
1011
12+ function replaySafeRetryAfterMs ( error : unknown ) : number | undefined {
13+ const classification = classifyMSTeamsSendError ( error ) ;
14+ return classification . kind === "replay-safe" ? classification . retryAfterMs : undefined ;
15+ }
16+
1117describe ( "msteams errors" , ( ) => {
1218 it ( "formats unknown errors" , ( ) => {
1319 expect ( formatUnknownError ( "oops" ) ) . toBe ( "oops" ) ;
@@ -35,61 +41,59 @@ describe("msteams errors", () => {
3541 expect ( result . errorCode ) . toBe ( "ContentStreamNotAllowed" ) ;
3642 } ) ;
3743
38- it ( "classifies throttling errors and parses retry-after" , ( ) => {
44+ it ( "classifies Teams rate limiting as replay-safe and parses retry-after" , ( ) => {
3945 const result = classifyMSTeamsSendError ( { statusCode : 429 , retryAfter : "1.5" } ) ;
40- expect ( result . kind ) . toBe ( "throttled " ) ;
46+ expect ( result . kind ) . toBe ( "replay-safe " ) ;
4147 expect ( result . statusCode ) . toBe ( 429 ) ;
42- expect ( result . retryAfterMs ) . toBe ( 1500 ) ;
48+ expect ( replaySafeRetryAfterMs ( { statusCode : 429 , retryAfter : "1.5" } ) ) . toBe ( 1500 ) ;
4349 } ) ;
4450
4551 it ( "does not parse partial retry-after values" , ( ) => {
52+ expect ( replaySafeRetryAfterMs ( { statusCode : 429 , retryAfter : "1.5s" } ) ) . toBeUndefined ( ) ;
4653 expect (
47- classifyMSTeamsSendError ( { statusCode : 429 , retryAfter : "1.5s" } ) . retryAfterMs ,
48- ) . toBeUndefined ( ) ;
49- expect (
50- classifyMSTeamsSendError ( {
54+ replaySafeRetryAfterMs ( {
5155 statusCode : 429 ,
5256 response : { headers : { "retry-after" : "2 seconds" } } ,
53- } ) . retryAfterMs ,
57+ } ) ,
5458 ) . toBeUndefined ( ) ;
5559 expect (
56- classifyMSTeamsSendError ( {
60+ replaySafeRetryAfterMs ( {
5761 statusCode : 429 ,
5862 response : { headers : new Headers ( { "retry-after" : "3 seconds" } ) } ,
59- } ) . retryAfterMs ,
63+ } ) ,
6064 ) . toBeUndefined ( ) ;
6165 } ) ;
6266
6367 it ( "ignores unsafe retry-after magnitudes" , ( ) => {
6468 expect (
65- classifyMSTeamsSendError ( {
69+ replaySafeRetryAfterMs ( {
6670 statusCode : 429 ,
6771 retryAfterMs : Number . MAX_SAFE_INTEGER + 1 ,
68- } ) . retryAfterMs ,
72+ } ) ,
6973 ) . toBeUndefined ( ) ;
7074 expect (
71- classifyMSTeamsSendError ( {
75+ replaySafeRetryAfterMs ( {
7276 statusCode : 429 ,
7377 retryAfter : Number . MAX_SAFE_INTEGER ,
74- } ) . retryAfterMs ,
78+ } ) ,
7579 ) . toBeUndefined ( ) ;
7680 expect (
77- classifyMSTeamsSendError ( {
81+ replaySafeRetryAfterMs ( {
7882 statusCode : 429 ,
7983 retryAfter : "9007199254741" ,
80- } ) . retryAfterMs ,
84+ } ) ,
8185 ) . toBeUndefined ( ) ;
8286 expect (
83- classifyMSTeamsSendError ( {
87+ replaySafeRetryAfterMs ( {
8488 statusCode : 429 ,
8589 response : { headers : { "retry-after" : "9007199254741" } } ,
86- } ) . retryAfterMs ,
90+ } ) ,
8791 ) . toBeUndefined ( ) ;
8892 expect (
89- classifyMSTeamsSendError ( {
93+ replaySafeRetryAfterMs ( {
9094 statusCode : 429 ,
9195 response : { headers : new Headers ( { "retry-after" : "9007199254741" } ) } ,
92- } ) . retryAfterMs ,
96+ } ) ,
9397 ) . toBeUndefined ( ) ;
9498 } ) ;
9599
@@ -101,10 +105,30 @@ describe("msteams errors", () => {
101105 ) . toBe ( "unknown" ) ;
102106 } ) ;
103107
104- it ( "classifies transient errors" , ( ) => {
105- const result = classifyMSTeamsSendError ( { statusCode : 503 } ) ;
106- expect ( result . kind ) . toBe ( "transient" ) ;
107- expect ( result . statusCode ) . toBe ( 503 ) ;
108+ it . each ( [ 408 , 500 , 502 , 503 , 504 ] ) ( "classifies HTTP %i as delivery-ambiguous" , ( statusCode ) => {
109+ const classification = classifyMSTeamsSendError ( { statusCode } ) ;
110+ expect ( classification ) . toMatchObject ( {
111+ kind : "ambiguous" ,
112+ source : "http" ,
113+ statusCode,
114+ } ) ;
115+ expect ( formatMSTeamsSendErrorHint ( classification ) ) . toContain ( "outcome is unknown" ) ;
116+ expect ( formatMSTeamsSendErrorHint ( classification ) ) . not . toMatch ( / r e t r y | r e s e n d / iu) ;
117+ expect ( formatMSTeamsDeliveryFailureGuidance ( classification ) ) . toContain (
118+ "may already have succeeded" ,
119+ ) ;
120+ expect ( formatMSTeamsDeliveryFailureGuidance ( classification ) ) . not . toContain (
121+ "Retrying later may succeed" ,
122+ ) ;
123+ } ) ;
124+
125+ it ( "keeps model guidance aligned with replay safety" , ( ) => {
126+ expect (
127+ formatMSTeamsDeliveryFailureGuidance ( classifyMSTeamsSendError ( { statusCode : 429 } ) ) ,
128+ ) . toBe ( "The request was rate-limited before delivery; retrying later may succeed." ) ;
129+ expect (
130+ formatMSTeamsDeliveryFailureGuidance ( classifyMSTeamsSendError ( { statusCode : 401 } ) ) ,
131+ ) . toBeUndefined ( ) ;
108132 } ) ;
109133
110134 it ( "classifies permanent 4xx errors" , ( ) => {
@@ -114,13 +138,19 @@ describe("msteams errors", () => {
114138 } ) ;
115139
116140 it ( "provides actionable hints for common cases" , ( ) => {
117- expect ( formatMSTeamsSendErrorHint ( { kind : "auth" } ) ) . toContain ( "msteams" ) ;
118- expect ( formatMSTeamsSendErrorHint ( { kind : "throttled" } ) ) . toContain ( "throttled" ) ;
141+ expect ( formatMSTeamsSendErrorHint ( classifyMSTeamsSendError ( { statusCode : 401 } ) ) ) . toContain (
142+ "msteams" ,
143+ ) ;
144+ expect ( formatMSTeamsSendErrorHint ( classifyMSTeamsSendError ( { statusCode : 429 } ) ) ) . toContain (
145+ "throttled" ,
146+ ) ;
119147 expect (
120- formatMSTeamsSendErrorHint ( {
121- kind : "permanent" ,
122- errorCode : "ContentStreamNotAllowed" ,
123- } ) ,
148+ formatMSTeamsSendErrorHint (
149+ classifyMSTeamsSendError ( {
150+ statusCode : 403 ,
151+ response : { body : { error : { code : "ContentStreamNotAllowed" } } } ,
152+ } ) ,
153+ ) ,
124154 ) . toContain ( "expired the content stream" ) ;
125155 } ) ;
126156
@@ -132,20 +162,35 @@ describe("msteams errors", () => {
132162 const etimedout = Object . assign ( new Error ( "ETIMEDOUT" ) , { code : "ETIMEDOUT" } ) ;
133163
134164 const econnrefusedResult = classifyMSTeamsSendError ( econnrefused ) ;
135- expect ( econnrefusedResult . kind ) . toBe ( "network" ) ;
165+ expect ( econnrefusedResult ) . toMatchObject ( { kind : "ambiguous" , source : "transport" } ) ;
136166 expect ( econnrefusedResult . errorCode ) . toBe ( "ECONNREFUSED" ) ;
137167 const enotfoundResult = classifyMSTeamsSendError ( enotfound ) ;
138- expect ( enotfoundResult . kind ) . toBe ( "network" ) ;
168+ expect ( enotfoundResult ) . toMatchObject ( { kind : "ambiguous" , source : "transport" } ) ;
139169 expect ( enotfoundResult . errorCode ) . toBe ( "ENOTFOUND" ) ;
140170 const etimedoutResult = classifyMSTeamsSendError ( etimedout ) ;
141- expect ( etimedoutResult . kind ) . toBe ( "network" ) ;
171+ expect ( etimedoutResult ) . toMatchObject ( { kind : "ambiguous" , source : "transport" } ) ;
142172 expect ( etimedoutResult . errorCode ) . toBe ( "ETIMEDOUT" ) ;
143173
144- // Hints for network errors must mention smba (Connector endpoint) and egress
145- expect ( formatMSTeamsSendErrorHint ( { kind : "network" } ) ) . toContain ( "smba" ) ;
146- expect ( formatMSTeamsSendErrorHint ( { kind : "network" } ) ) . toContain ( "egress" ) ;
174+ const hint = formatMSTeamsSendErrorHint ( econnrefusedResult ) ;
175+ expect ( hint ) . toContain ( "smba" ) ;
176+ expect ( hint ) . toContain ( "egress" ) ;
177+ expect ( hint ) . toContain ( "outcome is unknown" ) ;
178+ expect ( hint ) . not . toMatch ( / r e t r y | r e s e n d / iu) ;
147179 } ) ;
148180
181+ it . each ( [ "ECONNABORTED" , "ETIMEDOUT" , "ECONNRESET" ] ) (
182+ "keeps transport %s delivery ambiguous without retry guidance" ,
183+ ( code ) => {
184+ const classification = classifyMSTeamsSendError ( Object . assign ( new Error ( code ) , { code } ) ) ;
185+ expect ( classification ) . toMatchObject ( {
186+ kind : "ambiguous" ,
187+ source : "transport" ,
188+ errorCode : code ,
189+ } ) ;
190+ expect ( formatMSTeamsSendErrorHint ( classification ) ) . not . toMatch ( / r e t r y | r e s e n d / iu) ;
191+ } ,
192+ ) ;
193+
149194 it ( "still classifies HTTP errors as unknown when no status code and no network code" , ( ) => {
150195 expect ( classifyMSTeamsSendError ( new Error ( "unexpected error" ) ) . kind ) . toBe ( "unknown" ) ;
151196 expect ( classifyMSTeamsSendError ( null ) . kind ) . toBe ( "unknown" ) ;
0 commit comments