@@ -15,6 +15,7 @@ import {
1515 createMattermostClient ,
1616 createMattermostPost ,
1717 normalizeMattermostBaseUrl ,
18+ readMattermostError ,
1819 updateMattermostPost ,
1920} from "./client.js" ;
2021
@@ -155,6 +156,38 @@ describe("normalizeMattermostBaseUrl", () => {
155156 } ) ;
156157} ) ;
157158
159+ // ── readMattermostError ───────────────────────────────────────────────
160+
161+ describe ( "readMattermostError" , ( ) => {
162+ it ( "bounds null-body JSON errors without response.json/text" , async ( ) => {
163+ const response = new Response ( null , {
164+ status : 401 ,
165+ headers : { "content-type" : "application/json" } ,
166+ } ) ;
167+ const jsonSpy = vi . spyOn ( response , "json" ) . mockRejectedValue ( new Error ( "unbounded" ) ) ;
168+ const textSpy = vi . spyOn ( response , "text" ) . mockRejectedValue ( new Error ( "unbounded" ) ) ;
169+
170+ await expect ( readMattermostError ( response ) ) . resolves . toBe ( "" ) ;
171+
172+ expect ( jsonSpy ) . not . toHaveBeenCalled ( ) ;
173+ expect ( textSpy ) . not . toHaveBeenCalled ( ) ;
174+ } ) ;
175+
176+ it ( "parses bounded JSON error messages from response bodies" , async ( ) => {
177+ const response = new Response ( JSON . stringify ( { message : "invalid token" , id : "app.error" } ) , {
178+ status : 401 ,
179+ headers : { "content-type" : "application/json" } ,
180+ } ) ;
181+ const jsonSpy = vi . spyOn ( response , "json" ) . mockRejectedValue ( new Error ( "unbounded" ) ) ;
182+ const textSpy = vi . spyOn ( response , "text" ) . mockRejectedValue ( new Error ( "unbounded" ) ) ;
183+
184+ await expect ( readMattermostError ( response ) ) . resolves . toBe ( "invalid token" ) ;
185+
186+ expect ( jsonSpy ) . not . toHaveBeenCalled ( ) ;
187+ expect ( textSpy ) . not . toHaveBeenCalled ( ) ;
188+ } ) ;
189+ } ) ;
190+
158191// ── createMattermostClient ───────────────────────────────────────────
159192
160193describe ( "createMattermostClient" , ( ) => {
@@ -173,6 +206,29 @@ describe("createMattermostClient", () => {
173206 expect ( release ) . toHaveBeenCalledTimes ( 1 ) ;
174207 } ) ;
175208
209+ it ( "reads guarded null-body Mattermost errors without response.json/text" , async ( ) => {
210+ const release = vi . fn ( async ( ) => { } ) ;
211+ const response = new Response ( null , {
212+ status : 503 ,
213+ statusText : "Service Unavailable" ,
214+ headers : { "content-type" : "application/json" } ,
215+ } ) ;
216+ const jsonSpy = vi . spyOn ( response , "json" ) . mockRejectedValue ( new Error ( "unbounded" ) ) ;
217+ const textSpy = vi . spyOn ( response , "text" ) . mockRejectedValue ( new Error ( "unbounded" ) ) ;
218+ fetchWithSsrFGuardMock . mockResolvedValueOnce ( { response, release } ) ;
219+ const client = createMattermostClient ( {
220+ baseUrl : "https://chat.example.com" ,
221+ botToken : "test-token" ,
222+ } ) ;
223+
224+ await expect ( client . request ( "/users/me" ) ) . rejects . toThrow (
225+ "Mattermost API 503 Service Unavailable: unknown error" ,
226+ ) ;
227+ expect ( jsonSpy ) . not . toHaveBeenCalled ( ) ;
228+ expect ( textSpy ) . not . toHaveBeenCalled ( ) ;
229+ expect ( release ) . toHaveBeenCalledTimes ( 1 ) ;
230+ } ) ;
231+
176232 it ( "bounds and cancels guarded Mattermost error bodies" , async ( ) => {
177233 const release = vi . fn ( async ( ) => { } ) ;
178234 const tracked = cancelTrackedResponse ( `${ "upstream unavailable " . repeat ( 512 ) } tail` , {
0 commit comments