openclaw/extensions/discord/src/monitor/thread-session-close.test.ts
Bob ea15819ecf
ACP: harden startup and move configured routing behind plugin seams (#48197)
* ACPX: keep plugin-local runtime installs out of dist

* Gateway: harden ACP startup and service PATH

* ACP: reinitialize error-state configured bindings

* ACP: classify pre-turn runtime failures as session init failures

* Plugins: move configured ACP routing behind channel seams

* Telegram tests: align startup probe assertions after rebase

* Discord: harden ACP configured binding recovery

* ACP: recover Discord bindings after stale runtime exits

* ACPX: replace dead sessions during ensure

* Discord: harden ACP binding recovery

* Discord: fix review follow-ups

* ACP bindings: load channel snapshots across workspaces

* ACP bindings: cache snapshot channel plugin resolution

* Experiments: add ACP pluginification holy grail plan

* Experiments: rename ACP pluginification plan doc

* Experiments: drop old ACP pluginification doc path

* ACP: move configured bindings behind plugin services

* Experiments: update bindings capability architecture plan

* Bindings: isolate configured binding routing and targets

* Discord tests: fix runtime env helper path

* Tests: fix channel binding CI regressions

* Tests: normalize ACP workspace assertion on Windows

* Bindings: isolate configured binding registry

* Bindings: finish configured binding cleanup

* Bindings: finish generic cleanup

* Bindings: align runtime approval callbacks

* ACP: delete residual bindings barrel

* Bindings: restore legacy compatibility

* Revert "Bindings: restore legacy compatibility"

This reverts commit ac2ed68fa2426ecc874d68278c71c71ad363fcfe.

* Tests: drop ACP route legacy helper names

* Discord/ACP: fix binding regressions

---------

Co-authored-by: Onur <2453968+osolmaz@users.noreply.github.com>
2026-03-17 17:27:52 +01:00

175 lines
5.1 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const hoisted = vi.hoisted(() => {
const updateSessionStore = vi.fn();
const resolveStorePath = vi.fn(() => "/tmp/openclaw-sessions.json");
return { updateSessionStore, resolveStorePath };
});
vi.mock("openclaw/plugin-sdk/config-runtime", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/config-runtime")>();
return {
...actual,
updateSessionStore: hoisted.updateSessionStore,
resolveStorePath: hoisted.resolveStorePath,
};
});
const { closeDiscordThreadSessions } = await import("./thread-session-close.js");
function setupStore(store: Record<string, { updatedAt: number }>) {
hoisted.updateSessionStore.mockImplementation(
async (_storePath: string, mutator: (s: typeof store) => unknown) => mutator(store),
);
}
const THREAD_ID = "999";
const OTHER_ID = "111";
const MATCHED_KEY = `agent:main:discord:channel:${THREAD_ID}`;
const UNMATCHED_KEY = `agent:main:discord:channel:${OTHER_ID}`;
describe("closeDiscordThreadSessions", () => {
beforeEach(() => {
hoisted.updateSessionStore.mockClear();
hoisted.resolveStorePath.mockClear();
hoisted.resolveStorePath.mockReturnValue("/tmp/openclaw-sessions.json");
});
it("resets updatedAt to 0 for sessions whose key contains the threadId", async () => {
const store = {
[MATCHED_KEY]: { updatedAt: 1_700_000_000_000 },
[UNMATCHED_KEY]: { updatedAt: 1_700_000_000_001 },
};
setupStore(store);
const count = await closeDiscordThreadSessions({
cfg: {},
accountId: "default",
threadId: THREAD_ID,
});
expect(count).toBe(1);
expect(store[MATCHED_KEY].updatedAt).toBe(0);
expect(store[UNMATCHED_KEY].updatedAt).toBe(1_700_000_000_001);
});
it("returns 0 and leaves store unchanged when no session matches", async () => {
const store = {
[UNMATCHED_KEY]: { updatedAt: 1_700_000_000_001 },
};
setupStore(store);
const count = await closeDiscordThreadSessions({
cfg: {},
accountId: "default",
threadId: THREAD_ID,
});
expect(count).toBe(0);
expect(store[UNMATCHED_KEY].updatedAt).toBe(1_700_000_000_001);
});
it("resets all matching sessions when multiple keys contain the threadId", async () => {
const keyA = `agent:main:discord:channel:${THREAD_ID}`;
const keyB = `agent:work:discord:channel:${THREAD_ID}`;
const keyC = `agent:main:discord:channel:${OTHER_ID}`;
const store = {
[keyA]: { updatedAt: 1_000 },
[keyB]: { updatedAt: 2_000 },
[keyC]: { updatedAt: 3_000 },
};
setupStore(store);
const count = await closeDiscordThreadSessions({
cfg: {},
accountId: "default",
threadId: THREAD_ID,
});
expect(count).toBe(2);
expect(store[keyA].updatedAt).toBe(0);
expect(store[keyB].updatedAt).toBe(0);
expect(store[keyC].updatedAt).toBe(3_000);
});
it("does not match a key that contains the threadId as a substring of a longer snowflake", async () => {
const longerSnowflake = `${THREAD_ID}00`;
const noMatchKey = `agent:main:discord:channel:${longerSnowflake}`;
const store = {
[noMatchKey]: { updatedAt: 9_999 },
};
setupStore(store);
const count = await closeDiscordThreadSessions({
cfg: {},
accountId: "default",
threadId: THREAD_ID,
});
expect(count).toBe(0);
expect(store[noMatchKey].updatedAt).toBe(9_999);
});
it("matching is case-insensitive for the session key", async () => {
const uppercaseKey = `agent:main:discord:channel:${THREAD_ID.toUpperCase()}`;
const store = {
[uppercaseKey]: { updatedAt: 5_000 },
};
setupStore(store);
const count = await closeDiscordThreadSessions({
cfg: {},
accountId: "default",
threadId: THREAD_ID.toLowerCase(),
});
expect(count).toBe(1);
expect(store[uppercaseKey].updatedAt).toBe(0);
});
it("returns 0 immediately when threadId is empty without touching the store", async () => {
const count = await closeDiscordThreadSessions({
cfg: {},
accountId: "default",
threadId: " ",
});
expect(count).toBe(0);
expect(hoisted.updateSessionStore).not.toHaveBeenCalled();
});
it("does not recount sessions that were already reset", async () => {
const store = {
[MATCHED_KEY]: { updatedAt: 0 },
[UNMATCHED_KEY]: { updatedAt: 1_700_000_000_001 },
};
setupStore(store);
const count = await closeDiscordThreadSessions({
cfg: {},
accountId: "default",
threadId: THREAD_ID,
});
expect(count).toBe(0);
expect(store[MATCHED_KEY].updatedAt).toBe(0);
expect(store[UNMATCHED_KEY].updatedAt).toBe(1_700_000_000_001);
});
it("resolves the store path using cfg.session.store and accountId", async () => {
const store = {};
setupStore(store);
await closeDiscordThreadSessions({
cfg: { session: { store: "/custom/path/sessions.json" } },
accountId: "my-bot",
threadId: THREAD_ID,
});
expect(hoisted.resolveStorePath).toHaveBeenCalledWith("/custom/path/sessions.json", {
agentId: "my-bot",
});
});
});