refactor(channels): dedupe transport and gateway test scaffolds

This commit is contained in:
Peter Steinberger
2026-02-16 14:52:15 +00:00
parent f717a13039
commit 93ca0ed54f
95 changed files with 4068 additions and 5221 deletions

View File

@@ -1,49 +1,18 @@
import type { Client } from "@buape/carbon";
import { ChannelType, MessageType } from "@buape/carbon";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createDiscordMessageHandler } from "./monitor.js";
import { __resetDiscordChannelInfoCacheForTest } from "./monitor/message-utils.js";
import { __resetDiscordThreadStarterCacheForTest } from "./monitor/threading.js";
import {
dispatchMock,
readAllowFromStoreMock,
sendMock,
updateLastRouteMock,
upsertPairingRequestMock,
} from "./monitor.tool-result.test-harness.js";
type Config = ReturnType<typeof import("../config/config.js").loadConfig>;
const sendMock = vi.fn();
const reactMock = vi.fn();
const updateLastRouteMock = vi.fn();
const dispatchMock = vi.fn();
const readAllowFromStoreMock = vi.fn();
const upsertPairingRequestMock = vi.fn();
vi.mock("./send.js", () => ({
sendMessageDiscord: (...args: unknown[]) => sendMock(...args),
reactMessageDiscord: async (...args: unknown[]) => {
reactMock(...args);
},
}));
vi.mock("../auto-reply/dispatch.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../auto-reply/dispatch.js")>();
return {
...actual,
dispatchInboundMessage: (...args: unknown[]) => dispatchMock(...args),
dispatchInboundMessageWithDispatcher: (...args: unknown[]) => dispatchMock(...args),
dispatchInboundMessageWithBufferedDispatcher: (...args: unknown[]) => dispatchMock(...args),
};
});
vi.mock("../pairing/pairing-store.js", () => ({
readChannelAllowFromStore: (...args: unknown[]) => readAllowFromStoreMock(...args),
upsertChannelPairingRequest: (...args: unknown[]) => upsertPairingRequestMock(...args),
}));
vi.mock("../config/sessions.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../config/sessions.js")>();
return {
...actual,
resolveStorePath: vi.fn(() => "/tmp/openclaw-sessions.json"),
updateLastRoute: (...args: unknown[]) => updateLastRouteMock(...args),
resolveSessionKey: vi.fn(),
};
});
beforeEach(() => {
vi.resetModules();
sendMock.mockReset().mockResolvedValue(undefined);
updateLastRouteMock.mockReset();
dispatchMock.mockReset().mockImplementation(async ({ dispatcher }) => {
@@ -52,8 +21,6 @@ beforeEach(() => {
});
readAllowFromStoreMock.mockReset().mockResolvedValue([]);
upsertPairingRequestMock.mockReset().mockResolvedValue({ code: "PAIRCODE", created: true });
__resetDiscordChannelInfoCacheForTest();
__resetDiscordThreadStarterCacheForTest();
});
const BASE_CFG = {
@@ -82,7 +49,8 @@ const CATEGORY_GUILD_CFG = {
routing: { allowFrom: [] },
} as Config;
function createDmHandler(opts: { cfg: Config; runtimeError?: (err: unknown) => void }) {
async function createDmHandler(opts: { cfg: Config; runtimeError?: (err: unknown) => void }) {
const { createDiscordMessageHandler } = await import("./monitor.js");
return createDiscordMessageHandler({
cfg: opts.cfg,
discordConfig: opts.cfg.channels.discord,
@@ -117,7 +85,8 @@ function createDmClient(fetchChannel?: ReturnType<typeof vi.fn>) {
return { fetchChannel: resolvedFetchChannel } as unknown as Client;
}
function createCategoryGuildHandler() {
async function createCategoryGuildHandler() {
const { createDiscordMessageHandler } = await import("./monitor.js");
return createDiscordMessageHandler({
cfg: CATEGORY_GUILD_CFG,
discordConfig: CATEGORY_GUILD_CFG.channels.discord,
@@ -164,7 +133,7 @@ describe("discord tool result dispatch", () => {
} as ReturnType<typeof import("../config/config.js").loadConfig>;
const runtimeError = vi.fn();
const handler = createDmHandler({ cfg, runtimeError });
const handler = await createDmHandler({ cfg, runtimeError });
const client = createDmClient();
await handler(
@@ -199,7 +168,7 @@ describe("discord tool result dispatch", () => {
channels: { discord: { dm: { enabled: true, policy: "open" } } },
} as ReturnType<typeof import("../config/config.js").loadConfig>;
const handler = createDmHandler({ cfg });
const handler = await createDmHandler({ cfg });
const fetchChannel = vi.fn().mockResolvedValue({
type: ChannelType.DM,
name: "dm",
@@ -251,7 +220,7 @@ describe("discord tool result dispatch", () => {
channels: { discord: { dm: { enabled: true, policy: "open" } } },
} as ReturnType<typeof import("../config/config.js").loadConfig>;
const handler = createDmHandler({ cfg });
const handler = await createDmHandler({ cfg });
const client = createDmClient();
await handler(
@@ -303,7 +272,7 @@ describe("discord tool result dispatch", () => {
return { queuedFinal: true, counts: { final: 1 } };
});
const handler = createCategoryGuildHandler();
const handler = await createCategoryGuildHandler();
const client = createCategoryGuildClient();
await handler(
@@ -340,7 +309,7 @@ describe("discord tool result dispatch", () => {
return { queuedFinal: true, counts: { final: 1 } };
});
const handler = createCategoryGuildHandler();
const handler = await createCategoryGuildHandler();
const client = createCategoryGuildClient();
await handler(
@@ -377,7 +346,7 @@ describe("discord tool result dispatch", () => {
},
} as Config;
const handler = createDmHandler({ cfg });
const handler = await createDmHandler({ cfg });
const client = createDmClient();
await handler(