mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 20:34:31 +00:00
86 lines
2.8 KiB
TypeScript
86 lines
2.8 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { buildDispatchInboundCaptureMock } from "../../../test/helpers/dispatch-inbound-capture.js";
|
|
import { expectInboundContextContract } from "../../../test/helpers/inbound-contract.js";
|
|
import type { MsgContext } from "../../auto-reply/templating.js";
|
|
|
|
let capturedCtx: MsgContext | undefined;
|
|
|
|
vi.mock("../../auto-reply/dispatch.js", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("../../auto-reply/dispatch.js")>();
|
|
return buildDispatchInboundCaptureMock(actual, (ctx) => {
|
|
capturedCtx = ctx as MsgContext;
|
|
});
|
|
});
|
|
|
|
import { createSignalEventHandler } from "./event-handler.js";
|
|
import {
|
|
createBaseSignalEventHandlerDeps,
|
|
createSignalReceiveEvent,
|
|
} from "./event-handler.test-harness.js";
|
|
|
|
describe("signal createSignalEventHandler inbound contract", () => {
|
|
it("passes a finalized MsgContext to dispatchInboundMessage", async () => {
|
|
capturedCtx = undefined;
|
|
|
|
const handler = createSignalEventHandler(
|
|
createBaseSignalEventHandlerDeps({
|
|
// oxlint-disable-next-line typescript/no-explicit-any
|
|
cfg: { messages: { inbound: { debounceMs: 0 } } } as any,
|
|
historyLimit: 0,
|
|
}),
|
|
);
|
|
|
|
await handler(
|
|
createSignalReceiveEvent({
|
|
dataMessage: {
|
|
message: "hi",
|
|
attachments: [],
|
|
groupInfo: { groupId: "g1", groupName: "Test Group" },
|
|
},
|
|
}),
|
|
);
|
|
|
|
expect(capturedCtx).toBeTruthy();
|
|
expectInboundContextContract(capturedCtx!);
|
|
const contextWithBody = capturedCtx as unknown as { Body?: string };
|
|
// Sender should appear as prefix in group messages (no redundant [from:] suffix)
|
|
expect(String(contextWithBody.Body ?? "")).toContain("Alice");
|
|
expect(String(contextWithBody.Body ?? "")).toMatch(/Alice.*:/);
|
|
expect(String(contextWithBody.Body ?? "")).not.toContain("[from:");
|
|
});
|
|
|
|
it("normalizes direct chat To/OriginatingTo targets to canonical Signal ids", async () => {
|
|
capturedCtx = undefined;
|
|
|
|
const handler = createSignalEventHandler(
|
|
createBaseSignalEventHandlerDeps({
|
|
// oxlint-disable-next-line typescript/no-explicit-any
|
|
cfg: { messages: { inbound: { debounceMs: 0 } } } as any,
|
|
historyLimit: 0,
|
|
}),
|
|
);
|
|
|
|
await handler(
|
|
createSignalReceiveEvent({
|
|
sourceNumber: "+15550002222",
|
|
sourceName: "Bob",
|
|
timestamp: 1700000000001,
|
|
dataMessage: {
|
|
message: "hello",
|
|
attachments: [],
|
|
},
|
|
}),
|
|
);
|
|
|
|
expect(capturedCtx).toBeTruthy();
|
|
const context = capturedCtx as unknown as {
|
|
ChatType?: string;
|
|
To?: string;
|
|
OriginatingTo?: string;
|
|
};
|
|
expect(context.ChatType).toBe("direct");
|
|
expect(context.To).toBe("+15550002222");
|
|
expect(context.OriginatingTo).toBe("+15550002222");
|
|
});
|
|
});
|