Files
Moltbot/src/imessage/monitor/inbound-processing.test.ts
2026-02-26 19:49:36 +01:00

129 lines
3.4 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../../config/config.js";
import {
describeIMessageEchoDropLog,
resolveIMessageInboundDecision,
} from "./inbound-processing.js";
describe("resolveIMessageInboundDecision echo detection", () => {
const cfg = {} as OpenClawConfig;
it("drops inbound messages when outbound message id matches echo cache", () => {
const echoHas = vi.fn((_scope: string, lookup: { text?: string; messageId?: string }) => {
return lookup.messageId === "42";
});
const decision = resolveIMessageInboundDecision({
cfg,
accountId: "default",
message: {
id: 42,
sender: "+15555550123",
text: "Reasoning:\n_step_",
is_from_me: false,
is_group: false,
},
opts: undefined,
messageText: "Reasoning:\n_step_",
bodyText: "Reasoning:\n_step_",
allowFrom: [],
groupAllowFrom: [],
groupPolicy: "open",
dmPolicy: "open",
storeAllowFrom: [],
historyLimit: 0,
groupHistories: new Map(),
echoCache: { has: echoHas },
logVerbose: undefined,
});
expect(decision).toEqual({ kind: "drop", reason: "echo" });
expect(echoHas).toHaveBeenCalledWith(
"default:imessage:+15555550123",
expect.objectContaining({
text: "Reasoning:\n_step_",
messageId: "42",
}),
);
});
});
describe("describeIMessageEchoDropLog", () => {
it("includes message id when available", () => {
expect(
describeIMessageEchoDropLog({
messageText: "Reasoning:\n_step_",
messageId: "abc-123",
}),
).toContain("id=abc-123");
});
});
describe("resolveIMessageInboundDecision command auth", () => {
const cfg = {} as OpenClawConfig;
it("does not auto-authorize DM commands in open mode without allowlists", () => {
const decision = resolveIMessageInboundDecision({
cfg,
accountId: "default",
message: {
id: 100,
sender: "+15555550123",
text: "/status",
is_from_me: false,
is_group: false,
},
opts: undefined,
messageText: "/status",
bodyText: "/status",
allowFrom: [],
groupAllowFrom: [],
groupPolicy: "open",
dmPolicy: "open",
storeAllowFrom: [],
historyLimit: 0,
groupHistories: new Map(),
echoCache: undefined,
logVerbose: undefined,
});
expect(decision.kind).toBe("dispatch");
if (decision.kind !== "dispatch") {
return;
}
expect(decision.commandAuthorized).toBe(false);
});
it("authorizes DM commands for senders in pairing-store allowlist", () => {
const decision = resolveIMessageInboundDecision({
cfg,
accountId: "default",
message: {
id: 101,
sender: "+15555550123",
text: "/status",
is_from_me: false,
is_group: false,
},
opts: undefined,
messageText: "/status",
bodyText: "/status",
allowFrom: [],
groupAllowFrom: [],
groupPolicy: "open",
dmPolicy: "open",
storeAllowFrom: ["+15555550123"],
historyLimit: 0,
groupHistories: new Map(),
echoCache: undefined,
logVerbose: undefined,
});
expect(decision.kind).toBe("dispatch");
if (decision.kind !== "dispatch") {
return;
}
expect(decision.commandAuthorized).toBe(true);
});
});