refactor(test): dedupe agent and discord test fixtures

This commit is contained in:
Peter Steinberger
2026-02-22 20:01:43 +00:00
parent 5547a2275c
commit 3c75bc0e41
26 changed files with 632 additions and 737 deletions

View File

@@ -13,6 +13,18 @@ function asOpenClawConfig(config: Partial<OpenClawConfig>): OpenClawConfig {
return config as OpenClawConfig;
}
function createToolConfig() {
return asOpenClawConfig({ agents: { list: [{ id: "main", default: true }] } });
}
function createMemoryGetToolOrThrow(config: OpenClawConfig = createToolConfig()) {
const tool = createMemoryGetTool({ config });
if (!tool) {
throw new Error("tool missing");
}
return tool;
}
beforeEach(() => {
resetMemoryToolMockState({
backend: "builtin",
@@ -144,12 +156,7 @@ describe("memory tools", () => {
throw new Error("path required");
});
const cfg = { agents: { list: [{ id: "main", default: true }] } };
const tool = createMemoryGetTool({ config: cfg });
expect(tool).not.toBeNull();
if (!tool) {
throw new Error("tool missing");
}
const tool = createMemoryGetToolOrThrow();
const result = await tool.execute("call_2", { path: "memory/NOPE.md" });
expect(result.details).toEqual({
@@ -165,12 +172,7 @@ describe("memory tools", () => {
return { text: "", path: "memory/2026-02-19.md" };
});
const cfg = { agents: { list: [{ id: "main", default: true }] } };
const tool = createMemoryGetTool({ config: cfg });
expect(tool).not.toBeNull();
if (!tool) {
throw new Error("tool missing");
}
const tool = createMemoryGetToolOrThrow();
const result = await tool.execute("call_enoent", { path: "memory/2026-02-19.md" });
expect(result.details).toEqual({

View File

@@ -58,6 +58,34 @@ describe("handleSlackAction", () => {
};
}
function createReplyToFirstScenario() {
const cfg = { channels: { slack: { botToken: "tok" } } } as OpenClawConfig;
sendSlackMessage.mockClear();
const hasRepliedRef = { value: false };
const context = createReplyToFirstContext(hasRepliedRef);
return { cfg, context, hasRepliedRef };
}
function expectLastSlackSend(content: string, threadTs?: string) {
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", content, {
mediaUrl: undefined,
threadTs,
blocks: undefined,
});
}
async function sendSecondMessageAndExpectNoThread(params: {
cfg: OpenClawConfig;
context: ReturnType<typeof createReplyToFirstContext>;
}) {
await handleSlackAction(
{ action: "sendMessage", to: "channel:C123", content: "Second" },
params.cfg,
params.context,
);
expectLastSlackSend("Second");
}
async function resolveReadToken(cfg: OpenClawConfig): Promise<string | undefined> {
readSlackMessages.mockClear();
readSlackMessages.mockResolvedValueOnce({ messages: [], hasMore: false });
@@ -306,10 +334,7 @@ describe("handleSlackAction", () => {
});
it("replyToMode=first threads first message then stops", async () => {
const cfg = { channels: { slack: { botToken: "tok" } } } as OpenClawConfig;
sendSlackMessage.mockClear();
const hasRepliedRef = { value: false };
const context = createReplyToFirstContext(hasRepliedRef);
const { cfg, context, hasRepliedRef } = createReplyToFirstScenario();
// First message should be threaded
await handleSlackAction(
@@ -317,31 +342,14 @@ describe("handleSlackAction", () => {
cfg,
context,
);
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "First", {
mediaUrl: undefined,
threadTs: "1111111111.111111",
blocks: undefined,
});
expectLastSlackSend("First", "1111111111.111111");
expect(hasRepliedRef.value).toBe(true);
// Second message should NOT be threaded
await handleSlackAction(
{ action: "sendMessage", to: "channel:C123", content: "Second" },
cfg,
context,
);
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "Second", {
mediaUrl: undefined,
threadTs: undefined,
blocks: undefined,
});
await sendSecondMessageAndExpectNoThread({ cfg, context });
});
it("replyToMode=first marks hasRepliedRef even when threadTs is explicit", async () => {
const cfg = { channels: { slack: { botToken: "tok" } } } as OpenClawConfig;
sendSlackMessage.mockClear();
const hasRepliedRef = { value: false };
const context = createReplyToFirstContext(hasRepliedRef);
const { cfg, context, hasRepliedRef } = createReplyToFirstScenario();
await handleSlackAction(
{
@@ -353,23 +361,10 @@ describe("handleSlackAction", () => {
cfg,
context,
);
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "Explicit", {
mediaUrl: undefined,
threadTs: "2222222222.222222",
blocks: undefined,
});
expectLastSlackSend("Explicit", "2222222222.222222");
expect(hasRepliedRef.value).toBe(true);
await handleSlackAction(
{ action: "sendMessage", to: "channel:C123", content: "Second" },
cfg,
context,
);
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "Second", {
mediaUrl: undefined,
threadTs: undefined,
blocks: undefined,
});
await sendSecondMessageAndExpectNoThread({ cfg, context });
});
it("replyToMode=first without hasRepliedRef does not thread", async () => {

View File

@@ -421,11 +421,7 @@ describe("handleTelegramAction", () => {
});
it("allows inline buttons in DMs with tg: prefixed targets", async () => {
const cfg = {
channels: {
telegram: { botToken: "tok", capabilities: { inlineButtons: "dm" } },
},
} as OpenClawConfig;
const cfg = telegramConfig({ capabilities: { inlineButtons: "dm" } });
await handleTelegramAction(
{
action: "sendMessage",
@@ -439,11 +435,7 @@ describe("handleTelegramAction", () => {
});
it("allows inline buttons in groups with topic targets", async () => {
const cfg = {
channels: {
telegram: { botToken: "tok", capabilities: { inlineButtons: "group" } },
},
} as OpenClawConfig;
const cfg = telegramConfig({ capabilities: { inlineButtons: "group" } });
await handleTelegramAction(
{
action: "sendMessage",
@@ -457,11 +449,7 @@ describe("handleTelegramAction", () => {
});
it("sends messages with inline keyboard buttons when enabled", async () => {
const cfg = {
channels: {
telegram: { botToken: "tok", capabilities: { inlineButtons: "all" } },
},
} as OpenClawConfig;
const cfg = telegramConfig({ capabilities: { inlineButtons: "all" } });
await handleTelegramAction(
{
action: "sendMessage",
@@ -481,11 +469,7 @@ describe("handleTelegramAction", () => {
});
it("forwards optional button style", async () => {
const cfg = {
channels: {
telegram: { botToken: "tok", capabilities: { inlineButtons: "all" } },
},
} as OpenClawConfig;
const cfg = telegramConfig({ capabilities: { inlineButtons: "all" } });
await handleTelegramAction(
{
action: "sendMessage",