refactor(channels): dedupe monitor message test flows

This commit is contained in:
Peter Steinberger
2026-03-03 01:05:52 +00:00
parent 57e1534df8
commit ef920f2f39
4 changed files with 318 additions and 376 deletions

View File

@@ -119,6 +119,13 @@ vi.mock("../../config/sessions.js", () => ({
const { processDiscordMessage } = await import("./message-handler.process.js");
const createBaseContext = createBaseDiscordMessageContext;
const BASE_CHANNEL_ROUTE = {
agentId: "main",
channel: "discord",
accountId: "default",
sessionKey: "agent:main:discord:channel:c1",
mainSessionKey: "agent:main:main",
} as const;
function mockDispatchSingleBlockReply(payload: { text: string; isReasoning?: boolean }) {
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
@@ -127,6 +134,10 @@ function mockDispatchSingleBlockReply(payload: { text: string; isReasoning?: boo
});
}
function createNoQueuedDispatchResult() {
return { queuedFinal: false, counts: { final: 0, tool: 0, block: 0 } };
}
async function processStreamOffDiscordMessage() {
const ctx = await createBaseContext({ discordConfig: { streamMode: "off" } });
// oxlint-disable-next-line typescript/no-explicit-any
@@ -144,10 +155,7 @@ beforeEach(() => {
recordInboundSession.mockClear();
readSessionUpdatedAt.mockClear();
resolveStorePath.mockClear();
dispatchInboundMessage.mockResolvedValue({
queuedFinal: false,
counts: { final: 0, tool: 0, block: 0 },
});
dispatchInboundMessage.mockResolvedValue(createNoQueuedDispatchResult());
recordInboundSession.mockResolvedValue(undefined);
readSessionUpdatedAt.mockReturnValue(undefined);
resolveStorePath.mockReturnValue("/tmp/openclaw-discord-process-test-sessions.json");
@@ -193,6 +201,28 @@ async function runInPartialStreamMode(): Promise<void> {
await runProcessDiscordMessage(ctx);
}
function getReactionEmojis(): string[] {
return (
sendMocks.reactMessageDiscord.mock.calls as unknown as Array<[unknown, unknown, string]>
).map((call) => call[2]);
}
function createMockDraftStreamForTest() {
const draftStream = createMockDraftStream();
createDiscordDraftStream.mockReturnValueOnce(draftStream);
return draftStream;
}
function expectSinglePreviewEdit() {
expect(editMessageDiscord).toHaveBeenCalledWith(
"c1",
"preview-1",
{ content: "Hello\nWorld" },
{ rest: {} },
);
expect(deliverDiscordReply).not.toHaveBeenCalled();
}
describe("processDiscordMessage ack reactions", () => {
it("skips ack reactions for group-mentions when mentions are not required", async () => {
const ctx = await createBaseContext({
@@ -245,7 +275,7 @@ describe("processDiscordMessage ack reactions", () => {
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
await params?.replyOptions?.onReasoningStream?.();
await params?.replyOptions?.onToolStart?.({ name: "exec" });
return { queuedFinal: false, counts: { final: 0, tool: 0, block: 0 } };
return createNoQueuedDispatchResult();
});
const ctx = await createBaseContext();
@@ -253,9 +283,7 @@ describe("processDiscordMessage ack reactions", () => {
// oxlint-disable-next-line typescript/no-explicit-any
await processDiscordMessage(ctx as any);
const emojis = (
sendMocks.reactMessageDiscord.mock.calls as unknown as Array<[unknown, unknown, string]>
).map((call) => call[2]);
const emojis = getReactionEmojis();
expect(emojis).toContain("👀");
expect(emojis).toContain(DEFAULT_EMOJIS.done);
expect(emojis).not.toContain(DEFAULT_EMOJIS.thinking);
@@ -270,7 +298,7 @@ describe("processDiscordMessage ack reactions", () => {
});
dispatchInboundMessage.mockImplementationOnce(async () => {
await dispatchGate;
return { queuedFinal: false, counts: { final: 0, tool: 0, block: 0 } };
return createNoQueuedDispatchResult();
});
const ctx = await createBaseContext();
@@ -293,7 +321,7 @@ describe("processDiscordMessage ack reactions", () => {
it("applies status reaction emoji/timing overrides from config", async () => {
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
await params?.replyOptions?.onReasoningStream?.();
return { queuedFinal: false, counts: { final: 0, tool: 0, block: 0 } };
return createNoQueuedDispatchResult();
});
const ctx = await createBaseContext({
@@ -312,9 +340,7 @@ describe("processDiscordMessage ack reactions", () => {
// oxlint-disable-next-line typescript/no-explicit-any
await processDiscordMessage(ctx as any);
const emojis = (
sendMocks.reactMessageDiscord.mock.calls as unknown as Array<[unknown, unknown, string]>
).map((call) => call[2]);
const emojis = getReactionEmojis();
expect(emojis).toContain("🟦");
expect(emojis).toContain("🏁");
});
@@ -347,13 +373,7 @@ describe("processDiscordMessage session routing", () => {
it("stores group lastRoute with channel target", async () => {
const ctx = await createBaseContext({
baseSessionKey: "agent:main:discord:channel:c1",
route: {
agentId: "main",
channel: "discord",
accountId: "default",
sessionKey: "agent:main:discord:channel:c1",
mainSessionKey: "agent:main:main",
},
route: BASE_CHANNEL_ROUTE,
});
// oxlint-disable-next-line typescript/no-explicit-any
@@ -389,13 +409,7 @@ describe("processDiscordMessage session routing", () => {
threadChannel: { id: "thread-1", name: "subagent-thread" },
boundSessionKey: "agent:main:subagent:child",
threadBindings,
route: {
agentId: "main",
channel: "discord",
accountId: "default",
sessionKey: "agent:main:discord:channel:c1",
mainSessionKey: "agent:main:main",
},
route: BASE_CHANNEL_ROUTE,
});
// oxlint-disable-next-line typescript/no-explicit-any
@@ -446,26 +460,12 @@ describe("processDiscordMessage draft streaming", () => {
it("finalizes via preview edit when final fits one chunk", async () => {
await runSingleChunkFinalScenario({ streamMode: "partial", maxLinesPerMessage: 5 });
expect(editMessageDiscord).toHaveBeenCalledWith(
"c1",
"preview-1",
{ content: "Hello\nWorld" },
{ rest: {} },
);
expect(deliverDiscordReply).not.toHaveBeenCalled();
expectSinglePreviewEdit();
});
it("accepts streaming=true alias for partial preview mode", async () => {
await runSingleChunkFinalScenario({ streaming: true, maxLinesPerMessage: 5 });
expect(editMessageDiscord).toHaveBeenCalledWith(
"c1",
"preview-1",
{ content: "Hello\nWorld" },
{ rest: {} },
);
expect(deliverDiscordReply).not.toHaveBeenCalled();
expectSinglePreviewEdit();
});
it("falls back to standard send when final needs multiple chunks", async () => {
@@ -508,12 +508,11 @@ describe("processDiscordMessage draft streaming", () => {
});
it("streams block previews using draft chunking", async () => {
const draftStream = createMockDraftStream();
createDiscordDraftStream.mockReturnValueOnce(draftStream);
const draftStream = createMockDraftStreamForTest();
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
await params?.replyOptions?.onPartialReply?.({ text: "HelloWorld" });
return { queuedFinal: false, counts: { final: 0, tool: 0, block: 0 } };
return createNoQueuedDispatchResult();
});
const ctx = await createBlockModeContext();
@@ -526,13 +525,12 @@ describe("processDiscordMessage draft streaming", () => {
});
it("forces new preview messages on assistant boundaries in block mode", async () => {
const draftStream = createMockDraftStream();
createDiscordDraftStream.mockReturnValueOnce(draftStream);
const draftStream = createMockDraftStreamForTest();
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
await params?.replyOptions?.onPartialReply?.({ text: "Hello" });
await params?.replyOptions?.onAssistantMessageStart?.();
return { queuedFinal: false, counts: { final: 0, tool: 0, block: 0 } };
return createNoQueuedDispatchResult();
});
const ctx = await createBlockModeContext();
@@ -544,14 +542,13 @@ describe("processDiscordMessage draft streaming", () => {
});
it("strips reasoning tags from partial stream updates", async () => {
const draftStream = createMockDraftStream();
createDiscordDraftStream.mockReturnValueOnce(draftStream);
const draftStream = createMockDraftStreamForTest();
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
await params?.replyOptions?.onPartialReply?.({
text: "<thinking>Let me think about this</thinking>\nThe answer is 42",
});
return { queuedFinal: false, counts: { final: 0, tool: 0, block: 0 } };
return createNoQueuedDispatchResult();
});
await runInPartialStreamMode();
@@ -563,14 +560,13 @@ describe("processDiscordMessage draft streaming", () => {
});
it("skips pure-reasoning partial updates without updating draft", async () => {
const draftStream = createMockDraftStream();
createDiscordDraftStream.mockReturnValueOnce(draftStream);
const draftStream = createMockDraftStreamForTest();
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
await params?.replyOptions?.onPartialReply?.({
text: "Reasoning:\nThe user asked about X so I need to consider Y",
});
return { queuedFinal: false, counts: { final: 0, tool: 0, block: 0 } };
return createNoQueuedDispatchResult();
});
await runInPartialStreamMode();