test: merge signal sender-prefix coverage into typing suite

This commit is contained in:
Peter Steinberger
2026-02-22 13:12:57 +00:00
parent 83597572df
commit a395479d8b
2 changed files with 44 additions and 91 deletions

View File

@@ -1,90 +0,0 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { SignalReactionMessage } from "./monitor/event-handler.types.js";
const dispatchMock = vi.fn();
const readAllowFromMock = vi.fn();
vi.mock("../auto-reply/dispatch.js", () => ({
dispatchInboundMessage: (...args: unknown[]) => dispatchMock(...args),
dispatchInboundMessageWithDispatcher: (...args: unknown[]) => dispatchMock(...args),
dispatchInboundMessageWithBufferedDispatcher: (...args: unknown[]) => dispatchMock(...args),
}));
vi.mock("../pairing/pairing-store.js", () => ({
readChannelAllowFromStore: (...args: unknown[]) => readAllowFromMock(...args),
upsertChannelPairingRequest: vi.fn(),
}));
describe("signal event handler sender prefix", () => {
beforeEach(() => {
dispatchMock.mockClear().mockImplementation(async ({ dispatcher, ctx }) => {
dispatcher.sendFinalReply({ text: "ok" });
return { queuedFinal: true, counts: { tool: 0, block: 0, final: 1 }, ctx };
});
readAllowFromMock.mockClear().mockResolvedValue([]);
});
it("prefixes group bodies with sender label", async () => {
let capturedBody = "";
dispatchMock.mockImplementationOnce(async ({ dispatcher, ctx }) => {
capturedBody = ctx.Body ?? "";
dispatcher.sendFinalReply({ text: "ok" });
return { queuedFinal: true, counts: { tool: 0, block: 0, final: 1 } };
});
const { createSignalEventHandler } = await import("./monitor/event-handler.js");
const handler = createSignalEventHandler({
runtime: {
log: vi.fn(),
error: vi.fn(),
exit: (code: number): never => {
throw new Error(`exit ${code}`);
},
},
cfg: {
agents: { defaults: { model: "anthropic/claude-opus-4-5", workspace: "/tmp/openclaw" } },
channels: { signal: {} },
} as never,
baseUrl: "http://localhost",
account: "+15550009999",
accountId: "default",
blockStreaming: false,
historyLimit: 0,
groupHistories: new Map(),
textLimit: 4000,
dmPolicy: "open",
allowFrom: [],
groupAllowFrom: [],
groupPolicy: "open",
reactionMode: "off",
reactionAllowlist: [],
mediaMaxBytes: 1000,
ignoreAttachments: true,
sendReadReceipts: false,
readReceiptsViaDaemon: false,
fetchAttachment: async () => null,
deliverReplies: async () => undefined,
resolveSignalReactionTargets: () => [],
isSignalReactionMessage: (_reaction): _reaction is SignalReactionMessage => false,
shouldEmitSignalReactionNotification: () => false,
buildSignalReactionSystemEventText: () => "",
});
const payload = {
envelope: {
sourceNumber: "+15550002222",
sourceName: "Alice",
timestamp: 1700000000000,
dataMessage: {
message: "hello",
groupInfo: { groupId: "group-1", groupName: "Test Group" },
},
},
};
await handler({ event: "receive", data: JSON.stringify(payload) });
expect(dispatchMock).toHaveBeenCalled();
expect(capturedBody).toContain("Alice (+15550002222): hello");
});
});

View File

@@ -7,7 +7,10 @@ import {
const sendTypingMock = vi.fn();
const sendReadReceiptMock = vi.fn();
const dispatchInboundMessageMock = vi.fn(
async (params: { replyOptions?: { onReplyStart?: () => void } }) => {
async (params: {
replyOptions?: { onReplyStart?: () => void };
dispatcher?: { sendFinalReply?: (payload: { text: string }) => void };
}) => {
await Promise.resolve(params.replyOptions?.onReplyStart?.());
return { queuedFinal: false, counts: { tool: 0, block: 0, final: 0 } };
},
@@ -69,4 +72,44 @@ describe("signal event handler typing + read receipts", () => {
expect.any(Object),
);
});
it("prefixes group bodies with sender label", async () => {
let capturedBody = "";
dispatchInboundMessageMock.mockImplementationOnce(
async (params: { dispatcher?: { sendFinalReply?: (payload: { text: string }) => void } }) => {
const ctx = params as { ctx?: { Body?: string } };
capturedBody = ctx.ctx?.Body ?? "";
params.dispatcher?.sendFinalReply?.({ text: "ok" });
return { queuedFinal: true, counts: { tool: 0, block: 0, final: 1 } };
},
);
const { createSignalEventHandler } = await import("./monitor/event-handler.js");
const handler = createSignalEventHandler(
createBaseSignalEventHandlerDeps({
cfg: {
channels: { signal: {} },
} as never,
account: "+15550009999",
blockStreaming: false,
historyLimit: 0,
groupHistories: new Map(),
allowFrom: [],
groupAllowFrom: [],
sendReadReceipts: false,
}),
);
await handler(
createSignalReceiveEvent({
dataMessage: {
message: "hello",
groupInfo: { groupId: "group-1", groupName: "Test Group" },
},
}),
);
expect(dispatchInboundMessageMock).toHaveBeenCalled();
expect(capturedBody).toContain("Alice (+15550001111): hello");
});
});