2026-01-23 21:01:15 +01:00
|
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
2026-02-16 22:33:27 +00:00
|
|
|
import { createBaseDiscordMessageContext } from "./message-handler.test-harness.js";
|
2026-01-23 21:01:15 +01:00
|
|
|
|
|
|
|
|
const reactMessageDiscord = vi.fn(async () => {});
|
|
|
|
|
const removeReactionDiscord = vi.fn(async () => {});
|
2026-02-17 04:38:39 +09:00
|
|
|
const dispatchInboundMessage = vi.fn(async () => ({
|
|
|
|
|
queuedFinal: false,
|
|
|
|
|
counts: { final: 0, tool: 0, block: 0 },
|
|
|
|
|
}));
|
2026-02-16 23:17:18 +00:00
|
|
|
const recordInboundSession = vi.fn(async () => {});
|
|
|
|
|
const readSessionUpdatedAt = vi.fn(() => undefined);
|
|
|
|
|
const resolveStorePath = vi.fn(() => "/tmp/openclaw-discord-process-test-sessions.json");
|
2026-01-23 21:01:15 +01:00
|
|
|
|
|
|
|
|
vi.mock("../send.js", () => ({
|
2026-02-17 12:00:18 +09:00
|
|
|
reactMessageDiscord,
|
|
|
|
|
removeReactionDiscord,
|
2026-01-23 21:01:15 +01:00
|
|
|
}));
|
|
|
|
|
|
2026-02-17 04:38:39 +09:00
|
|
|
vi.mock("../../auto-reply/dispatch.js", () => ({
|
2026-02-17 12:00:18 +09:00
|
|
|
dispatchInboundMessage,
|
2026-01-23 21:01:15 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../../auto-reply/reply/reply-dispatcher.js", () => ({
|
|
|
|
|
createReplyDispatcherWithTyping: vi.fn(() => ({
|
2026-02-14 00:41:27 +01:00
|
|
|
dispatcher: {
|
|
|
|
|
sendToolResult: vi.fn(() => true),
|
|
|
|
|
sendBlockReply: vi.fn(() => true),
|
|
|
|
|
sendFinalReply: vi.fn(() => true),
|
|
|
|
|
waitForIdle: vi.fn(async () => {}),
|
|
|
|
|
getQueuedCounts: vi.fn(() => ({ tool: 0, block: 0, final: 0 })),
|
|
|
|
|
markComplete: vi.fn(),
|
|
|
|
|
},
|
2026-01-23 21:01:15 +01:00
|
|
|
replyOptions: {},
|
|
|
|
|
markDispatchIdle: vi.fn(),
|
|
|
|
|
})),
|
|
|
|
|
}));
|
|
|
|
|
|
2026-02-16 23:17:18 +00:00
|
|
|
vi.mock("../../channels/session.js", () => ({
|
2026-02-17 12:00:18 +09:00
|
|
|
recordInboundSession,
|
2026-02-16 23:17:18 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../../config/sessions.js", () => ({
|
2026-02-17 12:00:18 +09:00
|
|
|
readSessionUpdatedAt,
|
|
|
|
|
resolveStorePath,
|
2026-02-16 23:17:18 +00:00
|
|
|
}));
|
|
|
|
|
|
2026-02-01 10:41:31 +09:00
|
|
|
const { processDiscordMessage } = await import("./message-handler.process.js");
|
2026-01-23 21:01:15 +01:00
|
|
|
|
2026-02-16 22:33:27 +00:00
|
|
|
const createBaseContext = createBaseDiscordMessageContext;
|
2026-02-17 04:38:39 +09:00
|
|
|
|
2026-01-23 21:01:15 +01:00
|
|
|
beforeEach(() => {
|
2026-02-17 04:38:39 +09:00
|
|
|
vi.useRealTimers();
|
2026-01-23 21:01:15 +01:00
|
|
|
reactMessageDiscord.mockClear();
|
|
|
|
|
removeReactionDiscord.mockClear();
|
2026-02-17 04:38:39 +09:00
|
|
|
dispatchInboundMessage.mockReset();
|
2026-02-16 23:17:18 +00:00
|
|
|
recordInboundSession.mockReset();
|
|
|
|
|
readSessionUpdatedAt.mockReset();
|
|
|
|
|
resolveStorePath.mockReset();
|
2026-02-17 04:38:39 +09:00
|
|
|
dispatchInboundMessage.mockResolvedValue({
|
|
|
|
|
queuedFinal: false,
|
|
|
|
|
counts: { final: 0, tool: 0, block: 0 },
|
|
|
|
|
});
|
2026-02-16 23:17:18 +00:00
|
|
|
recordInboundSession.mockResolvedValue(undefined);
|
|
|
|
|
readSessionUpdatedAt.mockReturnValue(undefined);
|
|
|
|
|
resolveStorePath.mockReturnValue("/tmp/openclaw-discord-process-test-sessions.json");
|
2026-01-23 21:01:15 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("processDiscordMessage ack reactions", () => {
|
|
|
|
|
it("skips ack reactions for group-mentions when mentions are not required", async () => {
|
2026-02-17 04:38:39 +09:00
|
|
|
const ctx = await createBaseContext({
|
2026-01-23 21:01:15 +01:00
|
|
|
shouldRequireMention: false,
|
|
|
|
|
effectiveWasMentioned: false,
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-02 15:45:05 +09:00
|
|
|
// oxlint-disable-next-line typescript/no-explicit-any
|
2026-01-23 21:01:15 +01:00
|
|
|
await processDiscordMessage(ctx as any);
|
|
|
|
|
|
|
|
|
|
expect(reactMessageDiscord).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("sends ack reactions for mention-gated guild messages when mentioned", async () => {
|
2026-02-17 04:38:39 +09:00
|
|
|
const ctx = await createBaseContext({
|
2026-01-23 21:01:15 +01:00
|
|
|
shouldRequireMention: true,
|
|
|
|
|
effectiveWasMentioned: true,
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-02 15:45:05 +09:00
|
|
|
// oxlint-disable-next-line typescript/no-explicit-any
|
2026-01-23 21:01:15 +01:00
|
|
|
await processDiscordMessage(ctx as any);
|
|
|
|
|
|
2026-02-17 04:38:39 +09:00
|
|
|
expect(reactMessageDiscord.mock.calls[0]).toEqual(["c1", "m1", "👀", { rest: {} }]);
|
2026-01-23 21:01:15 +01:00
|
|
|
});
|
2026-02-16 02:30:17 +00:00
|
|
|
|
|
|
|
|
it("uses preflight-resolved messageChannelId when message.channelId is missing", async () => {
|
2026-02-17 04:38:39 +09:00
|
|
|
const ctx = await createBaseContext({
|
2026-02-16 02:30:17 +00:00
|
|
|
message: {
|
|
|
|
|
id: "m1",
|
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
|
attachments: [],
|
|
|
|
|
},
|
|
|
|
|
messageChannelId: "fallback-channel",
|
|
|
|
|
shouldRequireMention: true,
|
|
|
|
|
effectiveWasMentioned: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// oxlint-disable-next-line typescript/no-explicit-any
|
|
|
|
|
await processDiscordMessage(ctx as any);
|
|
|
|
|
|
2026-02-17 04:38:39 +09:00
|
|
|
expect(reactMessageDiscord.mock.calls[0]).toEqual([
|
|
|
|
|
"fallback-channel",
|
|
|
|
|
"m1",
|
|
|
|
|
"👀",
|
|
|
|
|
{ rest: {} },
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("debounces intermediate phase reactions and jumps to done for short runs", async () => {
|
|
|
|
|
dispatchInboundMessage.mockImplementationOnce(
|
|
|
|
|
async (params: {
|
|
|
|
|
replyOptions?: {
|
|
|
|
|
onReasoningStream?: () => Promise<void> | void;
|
|
|
|
|
onToolStart?: (payload: { name?: string }) => Promise<void> | void;
|
|
|
|
|
};
|
|
|
|
|
}) => {
|
|
|
|
|
await params.replyOptions?.onReasoningStream?.();
|
|
|
|
|
await params.replyOptions?.onToolStart?.({ name: "exec" });
|
|
|
|
|
return { queuedFinal: false, counts: { final: 0, tool: 0, block: 0 } };
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const ctx = await createBaseContext();
|
|
|
|
|
|
|
|
|
|
// oxlint-disable-next-line typescript/no-explicit-any
|
|
|
|
|
await processDiscordMessage(ctx as any);
|
|
|
|
|
|
2026-02-17 14:32:57 +09:00
|
|
|
const emojis = (
|
|
|
|
|
reactMessageDiscord.mock.calls as unknown as Array<[unknown, unknown, string]>
|
|
|
|
|
).map((call) => call[2]);
|
2026-02-17 04:38:39 +09:00
|
|
|
expect(emojis).toContain("👀");
|
|
|
|
|
expect(emojis).toContain("✅");
|
|
|
|
|
expect(emojis).not.toContain("🧠");
|
|
|
|
|
expect(emojis).not.toContain("💻");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("shows stall emojis for long no-progress runs", async () => {
|
|
|
|
|
vi.useFakeTimers();
|
|
|
|
|
dispatchInboundMessage.mockImplementationOnce(async () => {
|
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
|
setTimeout(resolve, 31_000);
|
|
|
|
|
});
|
|
|
|
|
return { queuedFinal: false, counts: { final: 0, tool: 0, block: 0 } };
|
2026-02-16 02:30:17 +00:00
|
|
|
});
|
2026-02-17 04:38:39 +09:00
|
|
|
|
|
|
|
|
const ctx = await createBaseContext();
|
|
|
|
|
// oxlint-disable-next-line typescript/no-explicit-any
|
|
|
|
|
const runPromise = processDiscordMessage(ctx as any);
|
|
|
|
|
|
2026-02-16 23:47:27 +00:00
|
|
|
let settled = false;
|
|
|
|
|
void runPromise.finally(() => {
|
|
|
|
|
settled = true;
|
|
|
|
|
});
|
|
|
|
|
for (let i = 0; i < 120 && !settled; i++) {
|
|
|
|
|
await vi.advanceTimersByTimeAsync(1_000);
|
|
|
|
|
}
|
2026-02-17 04:38:39 +09:00
|
|
|
|
|
|
|
|
await runPromise;
|
2026-02-17 14:32:57 +09:00
|
|
|
const emojis = (
|
|
|
|
|
reactMessageDiscord.mock.calls as unknown as Array<[unknown, unknown, string]>
|
|
|
|
|
).map((call) => call[2]);
|
2026-02-16 23:47:27 +00:00
|
|
|
expect(emojis).toContain("⏳");
|
|
|
|
|
expect(emojis).toContain("⚠️");
|
|
|
|
|
expect(emojis).toContain("✅");
|
2026-02-16 02:30:17 +00:00
|
|
|
});
|
2026-01-23 21:01:15 +01:00
|
|
|
});
|