11// Slack tests cover monitor.thread resolution plugin behavior.
2+ import {
3+ WebAPIHTTPError ,
4+ WebAPIPlatformError ,
5+ WebAPIRateLimitedError ,
6+ WebAPIRequestError ,
7+ } from "@slack/web-api" ;
28import { afterEach , describe , expect , it , vi } from "vitest" ;
39import type { SlackMessageEvent } from "../types.js" ;
10+ import type { SlackIngressTurnLifecycle } from "./ingress.js" ;
411import { createSlackThreadTsResolver } from "./thread-resolution.js" ;
512
613type SlackThreadClient = Parameters < typeof createSlackThreadTsResolver > [ 0 ] [ "client" ] ;
@@ -9,6 +16,16 @@ function createThreadClient(history: ReturnType<typeof vi.fn>): SlackThreadClien
916 return { conversations : { history } } as unknown as SlackThreadClient ;
1017}
1118
19+ function createDurableTurnLifecycle ( ) : SlackIngressTurnLifecycle {
20+ return {
21+ admission : "exclusive" ,
22+ abortSignal : new AbortController ( ) . signal ,
23+ onAdopted : vi . fn ( ) ,
24+ onDeferred : vi . fn ( ) ,
25+ onAbandoned : vi . fn ( ) ,
26+ } ;
27+ }
28+
1229describe ( "createSlackThreadTsResolver" , ( ) => {
1330 afterEach ( ( ) => {
1431 vi . restoreAllMocks ( ) ;
@@ -63,6 +80,195 @@ describe("createSlackThreadTsResolver", () => {
6380 expect ( historyMock ) . toHaveBeenCalledTimes ( 1 ) ;
6481 } ) ;
6582
83+ it . each ( [
84+ {
85+ label : "an actual Slack HTTP 408 timeout" ,
86+ error : new WebAPIHTTPError ( 408 , "Request Timeout" , { } , "timeout" ) ,
87+ } ,
88+ {
89+ label : "an actual Slack HTTP 429 rejection" ,
90+ error : new WebAPIHTTPError ( 429 , "Too Many Requests" , { } , "rate limited" ) ,
91+ } ,
92+ {
93+ label : "an actual Slack HTTP 503 outage" ,
94+ error : new WebAPIHTTPError ( 503 , "Service Unavailable" , { } , "outage" ) ,
95+ } ,
96+ {
97+ label : "an actual Slack rate-limit error" ,
98+ error : new WebAPIRateLimitedError ( 1 ) ,
99+ } ,
100+ {
101+ label : "an actual Slack request timeout" ,
102+ error : new WebAPIRequestError (
103+ Object . assign ( new Error ( "request timed out" ) , { code : "ETIMEDOUT" } ) ,
104+ ) ,
105+ } ,
106+ {
107+ label : "the actual Slack SDK AbortSignal.timeout DOMException" ,
108+ error : new WebAPIRequestError ( new DOMException ( "request timed out" , "TimeoutError" ) ) ,
109+ } ,
110+ {
111+ label : "an actual Slack connection reset" ,
112+ error : new WebAPIRequestError (
113+ Object . assign ( new Error ( "socket was reset" ) , { code : "ECONNRESET" } ) ,
114+ ) ,
115+ } ,
116+ ] ) ( "hands $label to durable ingress without poisoning the cache" , async ( { error } ) => {
117+ const historyMock = vi
118+ . fn ( )
119+ . mockRejectedValueOnce ( error )
120+ . mockResolvedValueOnce ( { messages : [ { ts : "1" , thread_ts : "9" } ] } ) ;
121+ const resolver = createSlackThreadTsResolver ( {
122+ client : createThreadClient ( historyMock ) ,
123+ cacheTtlMs : 60_000 ,
124+ maxSize : 5 ,
125+ } ) ;
126+ const message = makeThreadReplyMessage ( "1" ) ;
127+ const turnAdoptionLifecycle = createDurableTurnLifecycle ( ) ;
128+
129+ await expect (
130+ resolver . resolve ( { message, source : "message" , turnAdoptionLifecycle } ) ,
131+ ) . rejects . toBe ( error ) ;
132+ await expect (
133+ resolver . resolve ( { message, source : "app_mention" , turnAdoptionLifecycle } ) ,
134+ ) . resolves . toMatchObject ( { thread_ts : "9" } ) ;
135+
136+ expect ( historyMock ) . toHaveBeenCalledTimes ( 2 ) ;
137+ } ) ;
138+
139+ it ( "keeps direct transient failures ambiguous without poisoning their future lookup" , async ( ) => {
140+ const historyMock = vi
141+ . fn ( )
142+ . mockRejectedValueOnce ( new WebAPIHTTPError ( 503 , "Service Unavailable" , { } , "outage" ) )
143+ . mockResolvedValueOnce ( { messages : [ { ts : "1" , thread_ts : "9" } ] } ) ;
144+ const resolver = createSlackThreadTsResolver ( {
145+ client : createThreadClient ( historyMock ) ,
146+ cacheTtlMs : 60_000 ,
147+ maxSize : 5 ,
148+ } ) ;
149+ const message = makeThreadReplyMessage ( "1" ) ;
150+
151+ await expect ( resolver . resolve ( { message, source : "message" } ) ) . resolves . toMatchObject ( {
152+ _ambiguousThreadReply : true ,
153+ } ) ;
154+ await expect ( resolver . resolve ( { message, source : "app_mention" } ) ) . resolves . toMatchObject ( {
155+ thread_ts : "9" ,
156+ } ) ;
157+
158+ expect ( historyMock ) . toHaveBeenCalledTimes ( 2 ) ;
159+ } ) ;
160+
161+ it ( "releases all coalesced durable twins for their existing queue retry" , async ( ) => {
162+ let rejectHistory ! : ( error : unknown ) => void ;
163+ const historyMock = vi
164+ . fn ( )
165+ . mockImplementationOnce (
166+ ( ) =>
167+ new Promise ( ( _resolve , reject ) => {
168+ rejectHistory = reject ;
169+ } ) ,
170+ )
171+ . mockResolvedValueOnce ( { messages : [ { ts : "1" , thread_ts : "9" } ] } ) ;
172+ const resolver = createSlackThreadTsResolver ( {
173+ client : createThreadClient ( historyMock ) ,
174+ cacheTtlMs : 60_000 ,
175+ maxSize : 5 ,
176+ } ) ;
177+ const message = makeThreadReplyMessage ( "1" ) ;
178+ const firstLifecycle = createDurableTurnLifecycle ( ) ;
179+ const secondLifecycle = createDurableTurnLifecycle ( ) ;
180+ const first = resolver . resolve ( {
181+ message,
182+ source : "message" ,
183+ turnAdoptionLifecycle : firstLifecycle ,
184+ } ) ;
185+ const second = resolver . resolve ( {
186+ message,
187+ source : "app_mention" ,
188+ turnAdoptionLifecycle : secondLifecycle ,
189+ } ) ;
190+ const outage = new WebAPIHTTPError ( 503 , "Service Unavailable" , { } , "outage" ) ;
191+
192+ expect ( historyMock ) . toHaveBeenCalledTimes ( 1 ) ;
193+ rejectHistory ( outage ) ;
194+ await expect ( Promise . allSettled ( [ first , second ] ) ) . resolves . toEqual ( [
195+ { status : "rejected" , reason : outage } ,
196+ { status : "rejected" , reason : outage } ,
197+ ] ) ;
198+ await expect (
199+ resolver . resolve ( { message, source : "message" , turnAdoptionLifecycle : firstLifecycle } ) ,
200+ ) . resolves . toMatchObject ( { thread_ts : "9" } ) ;
201+ expect ( historyMock ) . toHaveBeenCalledTimes ( 2 ) ;
202+ } ) ;
203+
204+ it . each ( [
205+ {
206+ label : "HTTP 400 rejection" ,
207+ error : new WebAPIHTTPError ( 400 , "Bad Request" , { } , "invalid request" ) ,
208+ } ,
209+ {
210+ label : "HTTP 403 denial" ,
211+ error : new WebAPIHTTPError ( 403 , "Forbidden" , { } , "missing scope" ) ,
212+ } ,
213+ {
214+ label : "HTTP 404 missing message" ,
215+ error : new WebAPIHTTPError ( 404 , "Not Found" , { } , "message not found" ) ,
216+ } ,
217+ {
218+ label : "Slack platform missing_scope" ,
219+ error : new WebAPIPlatformError ( { ok : false , error : "missing_scope" } ) ,
220+ } ,
221+ {
222+ label : "unclassified local failure" ,
223+ error : new Error ( "local lookup unavailable" ) ,
224+ } ,
225+ {
226+ label : "operator-canceled Slack request" ,
227+ error : new WebAPIRequestError ( new DOMException ( "request was canceled" , "AbortError" ) ) ,
228+ } ,
229+ ] ) ( "preserves cached ambiguity for definitive $label" , async ( { error } ) => {
230+ const historyMock = vi . fn ( ) . mockRejectedValue ( error ) ;
231+ const resolver = createSlackThreadTsResolver ( {
232+ client : createThreadClient ( historyMock ) ,
233+ cacheTtlMs : 60_000 ,
234+ maxSize : 5 ,
235+ } ) ;
236+ const message = makeThreadReplyMessage ( "1" ) ;
237+ const turnAdoptionLifecycle = createDurableTurnLifecycle ( ) ;
238+
239+ await expect (
240+ resolver . resolve ( { message, source : "message" , turnAdoptionLifecycle } ) ,
241+ ) . resolves . toMatchObject ( { _ambiguousThreadReply : true } ) ;
242+ await expect (
243+ resolver . resolve ( { message, source : "app_mention" , turnAdoptionLifecycle } ) ,
244+ ) . resolves . toMatchObject ( { _ambiguousThreadReply : true } ) ;
245+ expect ( historyMock ) . toHaveBeenCalledTimes ( 1 ) ;
246+ } ) ;
247+
248+ it ( "does not retain a durable transient failure forever with a non-expiring cache" , async ( ) => {
249+ const historyMock = vi
250+ . fn ( )
251+ . mockRejectedValueOnce ( new WebAPIHTTPError ( 503 , "Service Unavailable" , { } , "outage" ) )
252+ . mockResolvedValueOnce ( { messages : [ { ts : "1" , thread_ts : "9" } ] } ) ;
253+ const resolver = createSlackThreadTsResolver ( {
254+ client : createThreadClient ( historyMock ) ,
255+ cacheTtlMs : 0 ,
256+ maxSize : 5 ,
257+ } ) ;
258+ const message = makeThreadReplyMessage ( "1" ) ;
259+ const turnAdoptionLifecycle = createDurableTurnLifecycle ( ) ;
260+
261+ await expect (
262+ resolver . resolve ( { message, source : "message" , turnAdoptionLifecycle } ) ,
263+ ) . rejects . toBeInstanceOf ( WebAPIHTTPError ) ;
264+ await expect (
265+ resolver . resolve ( { message, source : "app_mention" , turnAdoptionLifecycle } ) ,
266+ ) . resolves . toMatchObject ( { thread_ts : "9" } ) ;
267+ await resolver . resolve ( { message, source : "message" , turnAdoptionLifecycle } ) ;
268+
269+ expect ( historyMock ) . toHaveBeenCalledTimes ( 2 ) ;
270+ } ) ;
271+
66272 it ( "falls back to the default ttl when cacheTtlMs is non-finite" , async ( ) => {
67273 vi . useFakeTimers ( ) ;
68274 const historyMock = vi . fn ( ) . mockResolvedValue ( {
0 commit comments