Skip to content

Commit 321ff5a

Browse files
committed
fix(discord): preserve thread starter route ownership
1 parent 6797ae9 commit 321ff5a

4 files changed

Lines changed: 23 additions & 352 deletions

File tree

‎extensions/discord/src/monitor/message-handler.process.room-events.test.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Discord message processing coverage split by cohesive behavior.
22
import { describe, expect, it, onTestFinished, vi } from "vitest";
3-
import { ChannelType } from "../internal/discord.js";
43
import {
54
BASE_CHANNEL_ROUTE,
65
createBaseContext,
@@ -397,7 +396,6 @@ describe("processDiscordMessage session routing and room events", () => {
397396
baseText: "follow-up",
398397
threadChannel: { id: threadId, name: "child-thread" },
399398
threadParentId: "parent-1",
400-
threadParentType: ChannelType.GuildText,
401399
client: { rest },
402400
channelConfig: { allowed: true, users: ["U2"] },
403401
});

‎extensions/discord/src/monitor/threading.starter.test.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,23 @@ describe("resolveDiscordThreadStarter", () => {
354354
expect(get).toHaveBeenCalledOnce();
355355
});
356356

357-
it("does not cache an unresolved parent type before forum metadata recovers", async () => {
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 () => {
358374
const threadId = `recovering-forum-starter-${++threadIdIndex}`;
359375
const get = vi.fn(async (path: string) =>
360376
path === `/channels/${threadId}/messages/${threadId}`
@@ -373,13 +389,13 @@ describe("resolveDiscordThreadStarter", () => {
373389
await expect(
374390
resolveDiscordThreadStarter({ ...params, parentType: undefined }),
375391
).resolves.toBeNull();
376-
expect(get).not.toHaveBeenCalled();
392+
expect(get).toHaveBeenCalledExactlyOnceWith(`/channels/parent-1/messages/${threadId}`);
377393

378394
await expect(
379395
resolveDiscordThreadStarter({ ...params, parentType: ChannelType.GuildForum }),
380396
).resolves.toMatchObject({ text: "recovered forum starter" });
381-
expect(get).toHaveBeenCalledOnce();
382-
expect(firstRestGetPath(get)).toBe(`/channels/${threadId}/messages/${threadId}`);
397+
expect(get).toHaveBeenCalledTimes(2);
398+
expect(get).toHaveBeenNthCalledWith(2, `/channels/${threadId}/messages/${threadId}`);
383399
});
384400

385401
it("falls back to joined embed title and description when content is empty", async () => {

‎extensions/discord/src/monitor/threading.starter.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,16 @@ export async function resolveDiscordThreadStarter(params: {
112112
parentType?: ChannelType;
113113
resolveTimestampMs: (value?: string | null) => number | undefined;
114114
}): Promise<DiscordThreadStarter | null> {
115-
// A parent id without its type is provisional; caching the parent route could
116-
// hide a later forum route after the parent metadata recovers.
117-
if (params.parentType === undefined) {
115+
const messageChannelId = resolveDiscordThreadStarterMessageChannelId(params);
116+
if (!messageChannelId) {
118117
return null;
119118
}
120-
const cacheKey = `${params.accountId}:${params.channel.id}`;
119+
const cacheKey = `${params.accountId}:${params.channel.id}:${messageChannelId}`;
121120
const now = Date.now();
122121
const cached = getCachedThreadStarter(cacheKey, now);
123122
if (cached) {
124123
return cached.kind === "hit" ? cached.starter : null;
125124
}
126-
const messageChannelId = resolveDiscordThreadStarterMessageChannelId(params);
127-
if (!messageChannelId) {
128-
return null;
129-
}
130125
const inFlight = IN_FLIGHT_DISCORD_THREAD_STARTERS.get(cacheKey);
131126
if (inFlight) {
132127
return inFlight;

0 commit comments

Comments
 (0)