Skip to content

Commit 144a598

Browse files
authored
refactor(discord): remove thread binding test API (#122744)
1 parent 508dd47 commit 144a598

13 files changed

Lines changed: 124 additions & 132 deletions

‎extensions/discord/session-binding-contract-api.ts‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎extensions/discord/src/monitor/message-handler.preflight.test.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Discord tests cover message handler.preflight plugin behavior.
2-
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { afterEach, beforeAll, beforeEach, describe, expect, it, onTestFinished, vi } from "vitest";
33
import { ChannelType, MessageType } from "../internal/discord.js";
44
import { createPartialDiscordChannelWithThrowingGetters } from "../test-support/partial-channel.js";
55

@@ -57,7 +57,6 @@ vi.mock("openclaw/plugin-sdk/media-runtime", { spy: true });
5757
let preflightDiscordMessage: typeof import("./message-handler.preflight.js").preflightDiscordMessage;
5858
let resolvePreflightMentionRequirement: typeof import("./message-handler.preflight.js").resolvePreflightMentionRequirement;
5959
let shouldIgnoreBoundThreadWebhookMessage: typeof import("./message-handler.preflight.js").shouldIgnoreBoundThreadWebhookMessage;
60-
let threadBindingTesting: typeof import("./thread-bindings.js").testing;
6160
let createThreadBindingManager: typeof import("./thread-bindings.js").createThreadBindingManager;
6261

6362
beforeAll(async () => {
@@ -66,8 +65,7 @@ beforeAll(async () => {
6665
resolvePreflightMentionRequirement,
6766
shouldIgnoreBoundThreadWebhookMessage,
6867
} = await import("./message-handler.preflight.js"));
69-
({ testing: threadBindingTesting, createThreadBindingManager } =
70-
await import("./thread-bindings.js"));
68+
({ createThreadBindingManager } = await import("./thread-bindings.js"));
7169
});
7270

7371
beforeEach(() => {
@@ -2476,7 +2474,6 @@ describe("preflightDiscordMessage", () => {
24762474
describe("shouldIgnoreBoundThreadWebhookMessage", () => {
24772475
beforeEach(() => {
24782476
sessionBindingTesting.resetSessionBindingAdaptersForTests();
2479-
threadBindingTesting.resetThreadBindingsForTests();
24802477
});
24812478

24822479
afterEach(() => {
@@ -2534,6 +2531,7 @@ describe("shouldIgnoreBoundThreadWebhookMessage", () => {
25342531
persist: false,
25352532
enableSweeper: false,
25362533
});
2534+
onTestFinished(() => manager.stop());
25372535
const binding = await manager.bindTarget({
25382536
threadId: "thread-1",
25392537
channelId: "parent-1",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Discord message processing coverage split by cohesive behavior.
2-
import { describe, expect, it, vi } from "vitest";
2+
import { describe, expect, it, onTestFinished, vi } from "vitest";
33
import {
44
BASE_CHANNEL_ROUTE,
55
createBaseContext,
@@ -299,6 +299,7 @@ describe("processDiscordMessage session routing and room events", () => {
299299
persist: false,
300300
enableSweeper: false,
301301
});
302+
onTestFinished(() => threadBindings.stop());
302303
await threadBindings.bindTarget({
303304
threadId: "thread-1",
304305
channelId: "c-parent",

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { setReplyPayloadMetadata } from "openclaw/plugin-sdk/reply-payload-testi
44
import { logVerbose, sleepWithAbort } from "openclaw/plugin-sdk/runtime-env";
55
import { afterEach, beforeAll, beforeEach, vi } from "vitest";
66
import type { DiscordMessagePreflightContext } from "./message-handler.preflight.js";
7+
import { resetThreadBindingsForTests } from "./thread-bindings.test-support.js";
78

89
vi.mock("openclaw/plugin-sdk/runtime-env", { spy: true });
910

@@ -276,7 +277,6 @@ export const recordInboundSessionForTest = recordInboundSession;
276277
export const createDiscordRestClientSpyForTest = createDiscordRestClientSpy;
277278
let createBaseDiscordMessageContext: typeof import("./message-handler.test-harness.js").createBaseDiscordMessageContext;
278279
let createDiscordDirectMessageContextOverrides: typeof import("./message-handler.test-harness.js").createDiscordDirectMessageContextOverrides;
279-
let threadBindingTesting: typeof import("./thread-bindings.js").testing;
280280
export let createThreadBindingManager: typeof import("./thread-bindings.js").createThreadBindingManager;
281281
let processDiscordMessage: typeof import("./message-handler.process.js").processDiscordMessage;
282282
export let formatDiscordReplySkip: typeof import("./message-handler.process.js").formatDiscordReplySkip;
@@ -570,8 +570,7 @@ export function registerDiscordProcessTestLifecycle() {
570570
vi.useRealTimers();
571571
({ createBaseDiscordMessageContext, createDiscordDirectMessageContextOverrides } =
572572
await import("./message-handler.test-harness.js"));
573-
({ testing: threadBindingTesting, createThreadBindingManager } =
574-
await import("./thread-bindings.js"));
573+
({ createThreadBindingManager } = await import("./thread-bindings.js"));
575574
({ processDiscordMessage, formatDiscordReplySkip } =
576575
await import("./message-handler.process.js"));
577576
({ discordInboundEventDelivery } = await import("../inbound-event-delivery.js"));
@@ -603,7 +602,7 @@ export function registerDiscordProcessTestLifecycle() {
603602
readLatestAssistantTextByIdentity.mockResolvedValue(undefined);
604603
resolveStorePath.mockReturnValue("/tmp/openclaw-discord-process-test-sessions.json");
605604
getGlobalHookRunner.mockReturnValue(null);
606-
threadBindingTesting.resetThreadBindingsForTests();
605+
resetThreadBindingsForTests();
607606
});
608607

609608
afterEach(() => {

‎extensions/discord/src/monitor/thread-bindings.lifecycle.test.ts‎

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { createRequireRecord } from "openclaw/plugin-sdk/test-fixtures";
1919
import { beforeEach, describe, expect, it, vi } from "vitest";
2020
import { setDiscordRuntime } from "../runtime.js";
2121
import { EMPTY_DISCORD_TEST_CONFIG } from "../test-support/config.js";
22+
import { resetThreadBindingsForTests } from "./thread-bindings.test-support.js";
2223

2324
type DiscordRuntime = Parameters<typeof setDiscordRuntime>[0];
2425

@@ -67,7 +68,7 @@ vi.mock("../send.messages.js", () => ({
6768
createThreadDiscord: hoisted.createThreadDiscord,
6869
}));
6970

70-
const { testing, createThreadBindingManager } = await import("./thread-bindings.manager.js");
71+
const { createThreadBindingManager } = await import("./thread-bindings.manager.js");
7172
const {
7273
autoBindSpawnedDiscordSubagent,
7374
reconcileAcpThreadBindingsOnStartup,
@@ -77,7 +78,6 @@ const {
7778
} = await import("./thread-bindings.lifecycle.js");
7879
const { resolveThreadBindingInactivityExpiresAt, resolveThreadBindingMaxAgeExpiresAt } =
7980
await import("./thread-bindings.state.js");
80-
const { resolveThreadBindingIntroText } = await import("./thread-bindings.messages.js");
8181
const discordClientModule = await import("../client.js");
8282
const discordThreadBindingApi = await import("./thread-bindings.discord-api.js");
8383
const acpRuntime = await import("openclaw/plugin-sdk/acp-runtime");
@@ -128,7 +128,7 @@ function mockCallArg(mock: unknown, callIndex: number, argIndex: number, label:
128128
describe("thread binding lifecycle", () => {
129129
beforeEach(() => {
130130
resetPluginStateStoreForTests();
131-
testing.resetThreadBindingsForTests();
131+
resetThreadBindingsForTests();
132132
setDiscordRuntime({
133133
state: {
134134
openSyncKeyedStore: (options: OpenKeyedStoreOptions) =>
@@ -264,7 +264,7 @@ describe("thread binding lifecycle", () => {
264264
createTestThreadBindingManager({
265265
accountId: "default",
266266
persist: false,
267-
enableSweeper: false,
267+
enableSweeper: true,
268268
idleTimeoutMs: 24 * 60 * 60 * 1000,
269269
maxAgeMs: 0,
270270
});
@@ -294,27 +294,6 @@ describe("thread binding lifecycle", () => {
294294
return binding;
295295
};
296296

297-
it("includes idle and max-age details in intro text", () => {
298-
const intro = resolveThreadBindingIntroText({
299-
agentId: "main",
300-
label: "worker",
301-
idleTimeoutMs: 24 * 60 * 60 * 1000,
302-
maxAgeMs: 48 * 60 * 60 * 1000,
303-
});
304-
expect(intro).toContain("idle auto-unfocus after 24h inactivity");
305-
expect(intro).toContain("max age 48h");
306-
});
307-
308-
it("includes cwd near the top of intro text", () => {
309-
const intro = resolveThreadBindingIntroText({
310-
agentId: "codex",
311-
idleTimeoutMs: 24 * 60 * 60 * 1000,
312-
sessionCwd: "/home/bob/clawd",
313-
sessionDetails: ["session ids: pending (available after the first reply)"],
314-
});
315-
expect(intro).toContain("\ncwd: /home/bob/clawd\nsession ids: pending");
316-
});
317-
318297
it.each([
319298
{
320299
name: "auto-unfocuses idle-expired bindings and sends inactivity message",
@@ -338,7 +317,7 @@ describe("thread binding lifecycle", () => {
338317
accountId: "default",
339318
cfg: EMPTY_DISCORD_TEST_CONFIG,
340319
persist: false,
341-
enableSweeper: false,
320+
enableSweeper: true,
342321
idleTimeoutMs,
343322
maxAgeMs,
344323
});
@@ -361,7 +340,6 @@ describe("thread binding lifecycle", () => {
361340
hoisted.sendWebhookMessageDiscord.mockClear();
362341

363342
await vi.advanceTimersByTimeAsync(120_000);
364-
await testing.runThreadBindingSweepForAccount("default");
365343

366344
expect(manager.getByThreadId("thread-1")).toBeUndefined();
367345
if (expectNoProbe) {
@@ -402,7 +380,6 @@ describe("thread binding lifecycle", () => {
402380
hoisted.restGet.mockRejectedValueOnce(probeError);
403381

404382
await vi.advanceTimersByTimeAsync(120_000);
405-
await testing.runThreadBindingSweepForAccount("default");
406383

407384
if (keepsBinding) {
408385
expectFields(requireBinding(manager, "thread-1"), "thread binding", {
@@ -570,7 +547,7 @@ describe("thread binding lifecycle", () => {
570547
const manager = createTestThreadBindingManager({
571548
accountId: "default",
572549
persist: false,
573-
enableSweeper: false,
550+
enableSweeper: true,
574551
idleTimeoutMs: 60_000,
575552
maxAgeMs: 0,
576553
});
@@ -594,7 +571,6 @@ describe("thread binding lifecycle", () => {
594571
expect(updated[0]?.idleTimeoutMs).toBe(0);
595572

596573
await vi.advanceTimersByTimeAsync(240_000);
597-
await testing.runThreadBindingSweepForAccount("default");
598574

599575
expectFields(requireBinding(manager, "thread-1"), "thread binding", {
600576
threadId: "thread-1",
@@ -612,7 +588,7 @@ describe("thread binding lifecycle", () => {
612588
const manager = createTestThreadBindingManager({
613589
accountId: "default",
614590
persist: false,
615-
enableSweeper: false,
591+
enableSweeper: true,
616592
idleTimeoutMs: 60_000,
617593
maxAgeMs: 0,
618594
});
@@ -658,7 +634,6 @@ describe("thread binding lifecycle", () => {
658634
hoisted.sendMessageDiscord.mockClear();
659635

660636
await vi.advanceTimersByTimeAsync(120_000);
661-
await testing.runThreadBindingSweepForAccount("default");
662637

663638
expectFields(requireBinding(manager, "thread-2"), "thread binding", {
664639
threadId: "thread-2",
@@ -716,7 +691,7 @@ describe("thread binding lifecycle", () => {
716691
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-thread-bindings-"));
717692
process.env.OPENCLAW_STATE_DIR = stateDir;
718693
try {
719-
testing.resetThreadBindingsForTests();
694+
resetThreadBindingsForTests();
720695
vi.setSystemTime(new Date("2026-02-20T00:00:00.000Z"));
721696
const manager = createTestThreadBindingManager({
722697
accountId: "default",
@@ -740,7 +715,7 @@ describe("thread binding lifecycle", () => {
740715
vi.setSystemTime(touchedAt);
741716
manager.touchThread({ threadId: "thread-1" });
742717

743-
testing.resetThreadBindingsForTests();
718+
resetThreadBindingsForTests();
744719
const reloaded = createTestThreadBindingManager({
745720
accountId: "default",
746721
persist: true,
@@ -758,7 +733,7 @@ describe("thread binding lifecycle", () => {
758733
}),
759734
).toBe(new Date("2026-02-20T00:01:30.000Z").getTime());
760735
} finally {
761-
testing.resetThreadBindingsForTests();
736+
resetThreadBindingsForTests();
762737
if (previousStateDir === undefined) {
763738
delete process.env.OPENCLAW_STATE_DIR;
764739
} else {
@@ -1853,7 +1828,7 @@ describe("thread binding lifecycle", () => {
18531828
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-thread-bindings-"));
18541829
process.env.OPENCLAW_STATE_DIR = stateDir;
18551830
try {
1856-
testing.resetThreadBindingsForTests();
1831+
resetThreadBindingsForTests();
18571832
const now = Date.now();
18581833
const store = createPluginStateSyncKeyedStoreForTests("discord", {
18591834
namespace: "thread-bindings",
@@ -1879,7 +1854,7 @@ describe("thread binding lifecycle", () => {
18791854
expect(removed).toHaveLength(1);
18801855
expect(store.entries()).toStrictEqual([]);
18811856
} finally {
1882-
testing.resetThreadBindingsForTests();
1857+
resetThreadBindingsForTests();
18831858
if (previousStateDir === undefined) {
18841859
delete process.env.OPENCLAW_STATE_DIR;
18851860
} else {

‎extensions/discord/src/monitor/thread-bindings.manager.ts‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import {
5151
setBindingRecord,
5252
THREAD_BINDING_TOUCH_PERSIST_MIN_INTERVAL_MS,
5353
shouldDefaultPersist,
54-
resetThreadBindingsForTests,
5554
} from "./thread-bindings.state.js";
5655
import {
5756
DEFAULT_THREAD_BINDING_IDLE_TIMEOUT_MS,
@@ -72,8 +71,6 @@ function unregisterManager(accountId: string, manager: ThreadBindingManager) {
7271
}
7372
}
7473

75-
const SWEEPERS_BY_ACCOUNT_ID = new Map<string, () => Promise<void>>();
76-
7774
function createNoopManager(accountIdRaw?: string): ThreadBindingManager {
7875
const accountId = normalizeAccountId(accountIdRaw);
7976
return {
@@ -232,8 +229,6 @@ export function createThreadBindingManager(params: {
232229
}
233230
}
234231
};
235-
SWEEPERS_BY_ACCOUNT_ID.set(accountId, runSweepOnce);
236-
237232
const manager: ThreadBindingManager = {
238233
accountId,
239234
getIdleTimeoutMs: () => idleTimeoutMs,
@@ -499,7 +494,6 @@ export function createThreadBindingManager(params: {
499494
clearInterval(sweepTimer);
500495
sweepTimer = null;
501496
}
502-
SWEEPERS_BY_ACCOUNT_ID.delete(accountId);
503497
unregisterManager(accountId, manager);
504498
unregisterSessionBindingAdapter({
505499
channel: "discord",
@@ -543,14 +537,3 @@ export function getThreadBindingManager(accountId?: string): ThreadBindingManage
543537
const normalized = normalizeAccountId(accountId);
544538
return MANAGERS_BY_ACCOUNT_ID.get(normalized) ?? null;
545539
}
546-
547-
export const testing = {
548-
resolveThreadBindingThreadName,
549-
resetThreadBindingsForTests,
550-
runThreadBindingSweepForAccount: async (accountId?: string) => {
551-
const sweep = SWEEPERS_BY_ACCOUNT_ID.get(normalizeAccountId(accountId));
552-
if (sweep) {
553-
await sweep();
554-
}
555-
},
556-
};

‎extensions/discord/src/monitor/thread-bindings.persona.test.ts‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// Discord tests cover thread bindings.persona plugin behavior.
22
import { describe, expect, it } from "vitest";
3-
import {
4-
resolveThreadBindingPersona,
5-
resolveThreadBindingPersonaFromRecord,
6-
} from "./thread-bindings.persona.js";
7-
import type { ThreadBindingRecord } from "./thread-bindings.types.js";
3+
import { resolveThreadBindingPersona } from "./thread-bindings.persona.js";
84

95
describe("thread binding persona", () => {
106
it("prefers explicit label and prefixes with gear", () => {
@@ -17,22 +13,6 @@ describe("thread binding persona", () => {
1713
expect(resolveThreadBindingPersona({ agentId: "codex" })).toBe("⚙️ codex");
1814
});
1915

20-
it("builds persona from binding record", () => {
21-
const record = {
22-
accountId: "default",
23-
channelId: "parent-1",
24-
threadId: "thread-1",
25-
targetKind: "acp",
26-
targetSessionKey: "agent:codex:acp:session-1",
27-
agentId: "codex",
28-
boundBy: "system",
29-
boundAt: Date.now(),
30-
lastActivityAt: Date.now(),
31-
label: "codex-thread",
32-
} satisfies ThreadBindingRecord;
33-
expect(resolveThreadBindingPersonaFromRecord(record)).toBe("⚙️ codex-thread");
34-
});
35-
3616
it("does not split a surrogate pair at the length limit", () => {
3717
const prefix = "a".repeat(76);
3818
expect(resolveThreadBindingPersona({ label: `${prefix}😀tail` })).toBe(`⚙️ ${prefix}`);

‎extensions/discord/src/monitor/thread-bindings.shared-state.test.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
// Discord tests cover thread bindings.shared state plugin behavior.
22
import { beforeEach, describe, expect, it } from "vitest";
33
import { EMPTY_DISCORD_TEST_CONFIG } from "../test-support/config.js";
4-
import {
5-
testing as threadBindingsTesting,
6-
createThreadBindingManager,
7-
getThreadBindingManager,
8-
} from "./thread-bindings.js";
4+
import { createThreadBindingManager, getThreadBindingManager } from "./thread-bindings.js";
5+
import { resetThreadBindingsForTests } from "./thread-bindings.test-support.js";
96

107
type ThreadBindingsModule = {
118
getThreadBindingManager: typeof getThreadBindingManager;
@@ -18,7 +15,7 @@ async function loadThreadBindingsViaAlternateLoader(): Promise<ThreadBindingsMod
1815

1916
describe("thread binding manager state", () => {
2017
beforeEach(() => {
21-
threadBindingsTesting.resetThreadBindingsForTests();
18+
resetThreadBindingsForTests();
2219
});
2320

2421
it("shares managers between ESM and alternate-loaded module instances", async () => {

‎extensions/discord/src/monitor/thread-bindings.state.ts‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -489,19 +489,3 @@ export function resolveBindingIdsForSession(params: {
489489
}
490490
return out;
491491
}
492-
493-
export function resetThreadBindingsForTests() {
494-
for (const manager of MANAGERS_BY_ACCOUNT_ID.values()) {
495-
manager.stop();
496-
}
497-
MANAGERS_BY_ACCOUNT_ID.clear();
498-
BINDINGS_BY_THREAD_ID.clear();
499-
BINDINGS_BY_SESSION_KEY.clear();
500-
REUSABLE_WEBHOOKS_BY_ACCOUNT_CHANNEL.clear();
501-
TOKENS_BY_ACCOUNT_ID.clear();
502-
PERSIST_BY_ACCOUNT_ID.clear();
503-
THREAD_BINDINGS_STATE.loadedBindings = false;
504-
THREAD_BINDINGS_STATE.loadedPersistentBindings = false;
505-
THREAD_BINDINGS_STATE.persistenceAvailable = true;
506-
THREAD_BINDINGS_STATE.lastPersistedAtMs = 0;
507-
}

0 commit comments

Comments
 (0)