11// Discord tests cover threading.starter plugin behavior.
22import { StickerFormatType } from "discord-api-types/v10" ;
33import { describe , expect , it , vi } from "vitest" ;
4- import { ChannelType , type Client } from "../internal/discord.js" ;
4+ import { ChannelType , DiscordError , type Client } from "../internal/discord.js" ;
55import { getCachedThreadStarter , setCachedThreadStarter } from "./threading.cache.js" ;
66import { resolveDiscordThreadStarter } from "./threading.js" ;
77
@@ -69,6 +69,10 @@ function createStarterMessage(overrides: ThreadStarterRestMessage = {}): ThreadS
6969 } ;
7070}
7171
72+ function createDiscordError ( status : number ) : DiscordError {
73+ return new DiscordError ( new Response ( null , { status } ) , { } ) ;
74+ }
75+
7276function requireThreadStarter (
7377 result : Awaited < ReturnType < typeof resolveDiscordThreadStarter > > ,
7478) : ResolvedThreadStarter {
@@ -78,6 +82,13 @@ function requireThreadStarter(
7882 return result ;
7983}
8084
85+ function requireCachedThreadStarter ( value : ReturnType < typeof getCachedThreadStarter > ) {
86+ if ( ! value || value . kind !== "hit" ) {
87+ throw new Error ( "expected cached Discord thread starter" ) ;
88+ }
89+ return value . starter ;
90+ }
91+
8192function firstRestGetPath ( get : ReturnType < typeof vi . fn > ) : unknown {
8293 const [ call ] = get . mock . calls ;
8394 if ( ! call ) {
@@ -99,6 +110,7 @@ async function resolveStarter(params: {
99110 const result = await resolveDiscordThreadStarter ( {
100111 channel : { id : threadId } ,
101112 client,
113+ accountId : "test-account" ,
102114 parentId : params . parentId ?? "parent-1" ,
103115 parentType : params . parentType ?? ChannelType . GuildText ,
104116 resolveTimestampMs : params . resolveTimestampMs ?? ( ( ) => undefined ) ,
@@ -119,6 +131,7 @@ describe("resolveDiscordThreadStarter", () => {
119131 const params = {
120132 channel : { id : `active-thread-${ ++ threadIdIndex } ` } ,
121133 client,
134+ accountId : "test-account" ,
122135 parentId : "parent-1" ,
123136 parentType : ChannelType . GuildText ,
124137 resolveTimestampMs : ( ) => undefined ,
@@ -148,7 +161,11 @@ describe("resolveDiscordThreadStarter", () => {
148161 { name : "a clock rollback before the starter was fetched" , now : 999_999 } ,
149162 ] ) ( "invalidates cached thread starters at $name" , ( { now } ) => {
150163 const key = `expired-thread-${ ++ threadIdIndex } ` ;
151- setCachedThreadStarter ( key , { text : "stale" , author : "Alice" } , 1_000_000 ) ;
164+ setCachedThreadStarter (
165+ key ,
166+ { kind : "hit" , starter : { text : "stale" , author : "Alice" } } ,
167+ 1_000_000 ,
168+ ) ;
152169
153170 expect ( getCachedThreadStarter ( key , now ) ) . toBeUndefined ( ) ;
154171 } ) ;
@@ -158,17 +175,227 @@ describe("resolveDiscordThreadStarter", () => {
158175 for ( let index = 0 ; index < 500 ; index += 1 ) {
159176 setCachedThreadStarter (
160177 `${ prefix } ${ index } ` ,
161- { text : `starter-${ index } ` , author : "Alice" } ,
178+ { kind : "hit" , starter : { text : `starter-${ index } ` , author : "Alice" } } ,
162179 1_000_000 ,
163180 ) ;
164181 }
165182
166- expect ( getCachedThreadStarter ( `${ prefix } 0` , 1_000_001 ) ?. text ) . toBe ( "starter-0" ) ;
167- setCachedThreadStarter ( `${ prefix } 500` , { text : "new starter" , author : "Alice" } , 1_000_002 ) ;
168-
169- expect ( getCachedThreadStarter ( `${ prefix } 0` , 1_000_003 ) ?. text ) . toBe ( "starter-0" ) ;
183+ expect ( requireCachedThreadStarter ( getCachedThreadStarter ( `${ prefix } 0` , 1_000_001 ) ) . text ) . toBe (
184+ "starter-0" ,
185+ ) ;
186+ setCachedThreadStarter (
187+ `${ prefix } 500` ,
188+ { kind : "hit" , starter : { text : "new starter" , author : "Alice" } } ,
189+ 1_000_002 ,
190+ ) ;
191+
192+ expect ( requireCachedThreadStarter ( getCachedThreadStarter ( `${ prefix } 0` , 1_000_003 ) ) . text ) . toBe (
193+ "starter-0" ,
194+ ) ;
170195 expect ( getCachedThreadStarter ( `${ prefix } 1` , 1_000_003 ) ) . toBeUndefined ( ) ;
171- expect ( getCachedThreadStarter ( `${ prefix } 500` , 1_000_003 ) ?. text ) . toBe ( "new starter" ) ;
196+ expect ( requireCachedThreadStarter ( getCachedThreadStarter ( `${ prefix } 500` , 1_000_003 ) ) . text ) . toBe (
197+ "new starter" ,
198+ ) ;
199+ } ) ;
200+
201+ it . each ( [
202+ { name : "a missing starter" , get : ( ) => vi . fn ( ) . mockResolvedValue ( null ) } ,
203+ {
204+ name : "an inaccessible starter" ,
205+ get : ( ) => vi . fn ( ) . mockRejectedValue ( createDiscordError ( 403 ) ) ,
206+ } ,
207+ ] ) ( "negative-caches $name for 30 seconds" , async ( { get : createGet } ) => {
208+ vi . useFakeTimers ( ) ;
209+ try {
210+ const now = new Date ( "2026-08-24T00:00:00.000Z" ) ;
211+ vi . setSystemTime ( now ) ;
212+ const get = createGet ( ) ;
213+ const client = { rest : { get } } as unknown as Client ;
214+ const params = {
215+ channel : { id : `missing-starter-${ ++ threadIdIndex } ` } ,
216+ client,
217+ accountId : "test-account" ,
218+ parentId : "parent-1" ,
219+ parentType : ChannelType . GuildText ,
220+ resolveTimestampMs : ( ) => undefined ,
221+ } ;
222+
223+ await expect ( resolveDiscordThreadStarter ( params ) ) . resolves . toBeNull ( ) ;
224+ await expect ( resolveDiscordThreadStarter ( params ) ) . resolves . toBeNull ( ) ;
225+ expect ( get ) . toHaveBeenCalledOnce ( ) ;
226+
227+ vi . setSystemTime ( now . getTime ( ) + 30_000 ) ;
228+ await expect ( resolveDiscordThreadStarter ( params ) ) . resolves . toBeNull ( ) ;
229+ expect ( get ) . toHaveBeenCalledTimes ( 2 ) ;
230+ } finally {
231+ vi . useRealTimers ( ) ;
232+ }
233+ } ) ;
234+
235+ it ( "coalesces concurrent starter lookups for the same account and thread" , async ( ) => {
236+ let releaseGet ! : ( message : ThreadStarterRestMessage | null ) => void ;
237+ const response = new Promise < ThreadStarterRestMessage | null > ( ( resolve ) => {
238+ releaseGet = resolve ;
239+ } ) ;
240+ const get = vi . fn ( ( ) => response ) ;
241+ const client = { rest : { get } } as unknown as Client ;
242+ const params = {
243+ channel : { id : `concurrent-starter-${ ++ threadIdIndex } ` } ,
244+ client,
245+ accountId : "test-account" ,
246+ parentId : "parent-1" ,
247+ parentType : ChannelType . GuildText ,
248+ resolveTimestampMs : ( ) => undefined ,
249+ } ;
250+
251+ const first = resolveDiscordThreadStarter ( params ) ;
252+ const second = resolveDiscordThreadStarter ( params ) ;
253+ expect ( get ) . toHaveBeenCalledOnce ( ) ;
254+
255+ releaseGet ( null ) ;
256+ await expect ( Promise . all ( [ first , second ] ) ) . resolves . toEqual ( [ null , null ] ) ;
257+ expect ( get ) . toHaveBeenCalledOnce ( ) ;
258+ } ) ;
259+
260+ it ( "does not coalesce missing metadata with a lookup that has a parent id" , async ( ) => {
261+ const get = vi . fn ( ) . mockResolvedValue ( createStarterMessage ( { content : "resolved" } ) ) ;
262+ const client = { rest : { get } } as unknown as Client ;
263+ const params = {
264+ channel : { id : `metadata-single-flight-${ ++ threadIdIndex } ` } ,
265+ client,
266+ accountId : "test-account" ,
267+ parentType : ChannelType . GuildText ,
268+ resolveTimestampMs : ( ) => undefined ,
269+ } ;
270+
271+ const missingMetadata = resolveDiscordThreadStarter ( params ) ;
272+ const completeMetadata = resolveDiscordThreadStarter ( { ...params , parentId : "parent-1" } ) ;
273+
274+ await expect ( missingMetadata ) . resolves . toBeNull ( ) ;
275+ await expect ( completeMetadata ) . resolves . toMatchObject ( { text : "resolved" } ) ;
276+ expect ( get ) . toHaveBeenCalledOnce ( ) ;
277+ } ) ;
278+
279+ it ( "does not negative-cache transient REST failures" , async ( ) => {
280+ const get = vi
281+ . fn ( )
282+ . mockRejectedValueOnce ( new Error ( "temporary network failure" ) )
283+ . mockResolvedValue ( createStarterMessage ( { content : "recovered starter" } ) ) ;
284+ const client = { rest : { get } } as unknown as Client ;
285+ const params = {
286+ channel : { id : `transient-starter-${ ++ threadIdIndex } ` } ,
287+ client,
288+ accountId : "test-account" ,
289+ parentId : "parent-1" ,
290+ parentType : ChannelType . GuildText ,
291+ resolveTimestampMs : ( ) => undefined ,
292+ } ;
293+
294+ await expect ( resolveDiscordThreadStarter ( params ) ) . resolves . toBeNull ( ) ;
295+ await expect ( resolveDiscordThreadStarter ( params ) ) . resolves . toMatchObject ( {
296+ text : "recovered starter" ,
297+ } ) ;
298+ expect ( get ) . toHaveBeenCalledTimes ( 2 ) ;
299+ } ) ;
300+
301+ it ( "scopes negative cache entries to the Discord account" , async ( ) => {
302+ const deniedGet = vi . fn ( ) . mockRejectedValue ( createDiscordError ( 403 ) ) ;
303+ const allowedGet = vi . fn ( ) . mockResolvedValue ( createStarterMessage ( { content : "visible" } ) ) ;
304+ const threadId = `account-scoped-starter-${ ++ threadIdIndex } ` ;
305+ const baseParams = {
306+ channel : { id : threadId } ,
307+ parentId : "parent-1" ,
308+ parentType : ChannelType . GuildText ,
309+ resolveTimestampMs : ( ) => undefined ,
310+ } ;
311+
312+ await expect (
313+ resolveDiscordThreadStarter ( {
314+ ...baseParams ,
315+ client : { rest : { get : deniedGet } } as unknown as Client ,
316+ accountId : "account-a" ,
317+ } ) ,
318+ ) . resolves . toBeNull ( ) ;
319+ await expect (
320+ resolveDiscordThreadStarter ( {
321+ ...baseParams ,
322+ client : { rest : { get : allowedGet } } as unknown as Client ,
323+ accountId : "account-b" ,
324+ } ) ,
325+ ) . resolves . toMatchObject ( { text : "visible" } ) ;
326+ expect ( deniedGet ) . toHaveBeenCalledOnce ( ) ;
327+ expect ( allowedGet ) . toHaveBeenCalledOnce ( ) ;
328+ } ) ;
329+
330+ it ( "does not cache missing parent metadata" , async ( ) => {
331+ const get = vi . fn ( ) . mockResolvedValue ( createStarterMessage ( { content : "resolved" } ) ) ;
332+ const client = { rest : { get } } as unknown as Client ;
333+ const channel = { id : `metadata-starter-${ ++ threadIdIndex } ` } ;
334+
335+ await expect (
336+ resolveDiscordThreadStarter ( {
337+ channel,
338+ client,
339+ accountId : "test-account" ,
340+ parentType : ChannelType . GuildText ,
341+ resolveTimestampMs : ( ) => undefined ,
342+ } ) ,
343+ ) . resolves . toBeNull ( ) ;
344+ await expect (
345+ resolveDiscordThreadStarter ( {
346+ channel,
347+ client,
348+ accountId : "test-account" ,
349+ parentId : "parent-1" ,
350+ parentType : ChannelType . GuildText ,
351+ resolveTimestampMs : ( ) => undefined ,
352+ } ) ,
353+ ) . resolves . toMatchObject ( { text : "resolved" } ) ;
354+ expect ( get ) . toHaveBeenCalledOnce ( ) ;
355+ } ) ;
356+
357+ it ( "resolves thread starters when their parent type is unavailable" , async ( ) => {
358+ const threadId = `unknown-parent-type-starter-${ ++ threadIdIndex } ` ;
359+ const get = vi . fn ( ) . mockResolvedValue ( createStarterMessage ( { content : "visible starter" } ) ) ;
360+
361+ await expect (
362+ resolveDiscordThreadStarter ( {
363+ channel : { id : threadId } ,
364+ client : { rest : { get } } as unknown as Client ,
365+ accountId : "test-account" ,
366+ parentId : "parent-1" ,
367+ resolveTimestampMs : ( ) => undefined ,
368+ } ) ,
369+ ) . resolves . toMatchObject ( { text : "visible starter" } ) ;
370+ expect ( get ) . toHaveBeenCalledExactlyOnceWith ( `/channels/parent-1/messages/${ threadId } ` ) ;
371+ } ) ;
372+
373+ it ( "keeps parent-route misses separate when forum metadata recovers" , async ( ) => {
374+ const threadId = `recovering-forum-starter-${ ++ threadIdIndex } ` ;
375+ const get = vi . fn ( async ( path : string ) =>
376+ path === `/channels/${ threadId } /messages/${ threadId } `
377+ ? createStarterMessage ( { content : "recovered forum starter" } )
378+ : null ,
379+ ) ;
380+ const client = { rest : { get } } as unknown as Client ;
381+ const params = {
382+ channel : { id : threadId } ,
383+ client,
384+ accountId : "test-account" ,
385+ parentId : "parent-1" ,
386+ resolveTimestampMs : ( ) => undefined ,
387+ } ;
388+
389+ await expect (
390+ resolveDiscordThreadStarter ( { ...params , parentType : undefined } ) ,
391+ ) . resolves . toBeNull ( ) ;
392+ expect ( get ) . toHaveBeenCalledExactlyOnceWith ( `/channels/parent-1/messages/${ threadId } ` ) ;
393+
394+ await expect (
395+ resolveDiscordThreadStarter ( { ...params , parentType : ChannelType . GuildForum } ) ,
396+ ) . resolves . toMatchObject ( { text : "recovered forum starter" } ) ;
397+ expect ( get ) . toHaveBeenCalledTimes ( 2 ) ;
398+ expect ( get ) . toHaveBeenNthCalledWith ( 2 , `/channels/${ threadId } /messages/${ threadId } ` ) ;
172399 } ) ;
173400
174401 it ( "falls back to joined embed title and description when content is empty" , async ( ) => {
0 commit comments