refactor(test): dedupe discord status tool-result test setup

This commit is contained in:
Peter Steinberger
2026-02-14 23:48:48 +00:00
parent 98f2ad56a6
commit 371446456c

View File

@@ -56,7 +56,7 @@ beforeEach(() => {
__resetDiscordThreadStarterCacheForTest(); __resetDiscordThreadStarterCacheForTest();
}); });
const CATEGORY_GUILD_CFG = { const BASE_CFG = {
agents: { agents: {
defaults: { defaults: {
model: "anthropic/claude-opus-4-5", model: "anthropic/claude-opus-4-5",
@@ -64,6 +64,10 @@ const CATEGORY_GUILD_CFG = {
}, },
}, },
session: { store: "/tmp/openclaw-sessions.json" }, session: { store: "/tmp/openclaw-sessions.json" },
} as const;
const CATEGORY_GUILD_CFG = {
...BASE_CFG,
channels: { channels: {
discord: { discord: {
dm: { enabled: true, policy: "open" }, dm: { enabled: true, policy: "open" },
@@ -78,6 +82,41 @@ const CATEGORY_GUILD_CFG = {
routing: { allowFrom: [] }, routing: { allowFrom: [] },
} as Config; } as Config;
function createDmHandler(opts: { cfg: Config; runtimeError?: (err: unknown) => void }) {
return createDiscordMessageHandler({
cfg: opts.cfg,
discordConfig: opts.cfg.channels.discord,
accountId: "default",
token: "token",
runtime: {
log: vi.fn(),
error: opts.runtimeError ?? vi.fn(),
exit: (code: number): never => {
throw new Error(`exit ${code}`);
},
},
botUserId: "bot-id",
guildHistories: new Map(),
historyLimit: 0,
mediaMaxBytes: 10_000,
textLimit: 2000,
replyToMode: "off",
dmEnabled: true,
groupDmEnabled: false,
});
}
function createDmClient(fetchChannel?: ReturnType<typeof vi.fn>) {
const resolvedFetchChannel =
fetchChannel ??
vi.fn().mockResolvedValue({
type: ChannelType.DM,
name: "dm",
});
return { fetchChannel: resolvedFetchChannel } as unknown as Client;
}
function createCategoryGuildHandler() { function createCategoryGuildHandler() {
return createDiscordMessageHandler({ return createDiscordMessageHandler({
cfg: CATEGORY_GUILD_CFG, cfg: CATEGORY_GUILD_CFG,
@@ -119,46 +158,14 @@ function createCategoryGuildClient() {
describe("discord tool result dispatch", () => { describe("discord tool result dispatch", () => {
it("sends status replies with responsePrefix", async () => { it("sends status replies with responsePrefix", async () => {
const cfg = { const cfg = {
agents: { ...BASE_CFG,
defaults: {
model: "anthropic/claude-opus-4-5",
workspace: "/tmp/openclaw",
},
},
session: { store: "/tmp/openclaw-sessions.json" },
messages: { responsePrefix: "PFX" }, messages: { responsePrefix: "PFX" },
channels: { discord: { dmPolicy: "open", allowFrom: ["*"], dm: { enabled: true } } }, channels: { discord: { dmPolicy: "open", allowFrom: ["*"], dm: { enabled: true } } },
} as ReturnType<typeof import("../config/config.js").loadConfig>; } as ReturnType<typeof import("../config/config.js").loadConfig>;
const runtimeError = vi.fn(); const runtimeError = vi.fn();
const handler = createDiscordMessageHandler({ const handler = createDmHandler({ cfg, runtimeError });
cfg, const client = createDmClient();
discordConfig: cfg.channels.discord,
accountId: "default",
token: "token",
runtime: {
log: vi.fn(),
error: runtimeError,
exit: (code: number): never => {
throw new Error(`exit ${code}`);
},
},
botUserId: "bot-id",
guildHistories: new Map(),
historyLimit: 0,
mediaMaxBytes: 10_000,
textLimit: 2000,
replyToMode: "off",
dmEnabled: true,
groupDmEnabled: false,
});
const client = {
fetchChannel: vi.fn().mockResolvedValue({
type: ChannelType.DM,
name: "dm",
}),
} as unknown as Client;
await handler( await handler(
{ {
@@ -188,43 +195,16 @@ describe("discord tool result dispatch", () => {
it("caches channel info lookups between messages", async () => { it("caches channel info lookups between messages", async () => {
const cfg = { const cfg = {
agents: { ...BASE_CFG,
defaults: {
model: "anthropic/claude-opus-4-5",
workspace: "/tmp/openclaw",
},
},
session: { store: "/tmp/openclaw-sessions.json" },
channels: { discord: { dm: { enabled: true, policy: "open" } } }, channels: { discord: { dm: { enabled: true, policy: "open" } } },
} as ReturnType<typeof import("../config/config.js").loadConfig>; } as ReturnType<typeof import("../config/config.js").loadConfig>;
const handler = createDiscordMessageHandler({ const handler = createDmHandler({ cfg });
cfg,
discordConfig: cfg.channels.discord,
accountId: "default",
token: "token",
runtime: {
log: vi.fn(),
error: vi.fn(),
exit: (code: number): never => {
throw new Error(`exit ${code}`);
},
},
botUserId: "bot-id",
guildHistories: new Map(),
historyLimit: 0,
mediaMaxBytes: 10_000,
textLimit: 2000,
replyToMode: "off",
dmEnabled: true,
groupDmEnabled: false,
});
const fetchChannel = vi.fn().mockResolvedValue({ const fetchChannel = vi.fn().mockResolvedValue({
type: ChannelType.DM, type: ChannelType.DM,
name: "dm", name: "dm",
}); });
const client = { fetchChannel } as unknown as Client; const client = createDmClient(fetchChannel);
const baseMessage = { const baseMessage = {
content: "hello", content: "hello",
channelId: "cache-channel-1", channelId: "cache-channel-1",
@@ -267,44 +247,12 @@ describe("discord tool result dispatch", () => {
}); });
const cfg = { const cfg = {
agents: { ...BASE_CFG,
defaults: {
model: "anthropic/claude-opus-4-5",
workspace: "/tmp/openclaw",
},
},
session: { store: "/tmp/openclaw-sessions.json" },
channels: { discord: { dm: { enabled: true, policy: "open" } } }, channels: { discord: { dm: { enabled: true, policy: "open" } } },
} as ReturnType<typeof import("../config/config.js").loadConfig>; } as ReturnType<typeof import("../config/config.js").loadConfig>;
const handler = createDiscordMessageHandler({ const handler = createDmHandler({ cfg });
cfg, const client = createDmClient();
discordConfig: cfg.channels.discord,
accountId: "default",
token: "token",
runtime: {
log: vi.fn(),
error: vi.fn(),
exit: (code: number): never => {
throw new Error(`exit ${code}`);
},
},
botUserId: "bot-id",
guildHistories: new Map(),
historyLimit: 0,
mediaMaxBytes: 10_000,
textLimit: 2000,
replyToMode: "off",
dmEnabled: true,
groupDmEnabled: false,
});
const client = {
fetchChannel: vi.fn().mockResolvedValue({
type: ChannelType.DM,
name: "dm",
}),
} as unknown as Client;
await handler( await handler(
{ {
@@ -423,46 +371,14 @@ describe("discord tool result dispatch", () => {
it("replies with pairing code and sender id when dmPolicy is pairing", async () => { it("replies with pairing code and sender id when dmPolicy is pairing", async () => {
const cfg = { const cfg = {
agents: { ...BASE_CFG,
defaults: {
model: "anthropic/claude-opus-4-5",
workspace: "/tmp/openclaw",
},
},
session: { store: "/tmp/openclaw-sessions.json" },
channels: { channels: {
discord: { dm: { enabled: true, policy: "pairing", allowFrom: [] } }, discord: { dm: { enabled: true, policy: "pairing", allowFrom: [] } },
}, },
} as Config; } as Config;
const handler = createDiscordMessageHandler({ const handler = createDmHandler({ cfg });
cfg, const client = createDmClient();
discordConfig: cfg.channels.discord,
accountId: "default",
token: "token",
runtime: {
log: vi.fn(),
error: vi.fn(),
exit: (code: number): never => {
throw new Error(`exit ${code}`);
},
},
botUserId: "bot-id",
guildHistories: new Map(),
historyLimit: 0,
mediaMaxBytes: 10_000,
textLimit: 2000,
replyToMode: "off",
dmEnabled: true,
groupDmEnabled: false,
});
const client = {
fetchChannel: vi.fn().mockResolvedValue({
type: ChannelType.DM,
name: "dm",
}),
} as unknown as Client;
await handler( await handler(
{ {