Files
openclaw/extensions/irc/src/inbound.behavior.test.ts
Peter Steinberger 873bcc2985 fix(irc): recognize punctuation in nickname mentions (#116758)
Co-authored-by: Peter Steinberger <steipete@macos.shared>
2026-07-31 03:21:31 -07:00

468 lines
15 KiB
TypeScript

// Irc tests cover inbound.behavior plugin behavior.
import { createPluginRuntimeMock } from "openclaw/plugin-sdk/channel-test-helpers";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { ResolvedIrcAccount } from "./accounts.js";
import { handleIrcInbound } from "./inbound.js";
import type { IrcIngressLifecycle } from "./irc-ingress.js";
import type { RuntimeEnv } from "./runtime-api.js";
import { setIrcRuntime } from "./runtime.js";
import type { CoreConfig, IrcInboundMessage } from "./types.js";
const {
buildMentionRegexesMock,
hasControlCommandMock,
matchesMentionPatternsMock,
readAllowFromStoreMock,
shouldHandleTextCommandsMock,
upsertPairingRequestMock,
} = vi.hoisted(() => {
return {
buildMentionRegexesMock: vi.fn(() => []),
hasControlCommandMock: vi.fn(() => false),
matchesMentionPatternsMock: vi.fn(() => false),
readAllowFromStoreMock: vi.fn(async () => []),
shouldHandleTextCommandsMock: vi.fn(() => false),
upsertPairingRequestMock: vi.fn(async () => ({ code: "CODE", created: true })),
};
});
function installIrcRuntime() {
setIrcRuntime({
channel: {
pairing: {
readAllowFromStore: readAllowFromStoreMock,
upsertPairingRequest: upsertPairingRequestMock,
},
commands: {
shouldHandleTextCommands: shouldHandleTextCommandsMock,
},
text: {
hasControlCommand: hasControlCommandMock,
},
mentions: {
buildMentionRegexes: buildMentionRegexesMock,
matchesMentionPatterns: matchesMentionPatternsMock,
},
},
} as never);
}
function createRuntimeEnv() {
return {
log: vi.fn(),
error: vi.fn(),
} as unknown as RuntimeEnv;
}
function createAccount(overrides?: Partial<ResolvedIrcAccount>): ResolvedIrcAccount {
return {
accountId: "default",
enabled: true,
server: "irc.example.com",
nick: "OpenClaw",
config: {
dmPolicy: "pairing",
allowFrom: [],
groupPolicy: "allowlist",
groupAllowFrom: [],
},
...overrides,
} as ResolvedIrcAccount;
}
function createMessage(overrides?: Partial<IrcInboundMessage>): IrcInboundMessage {
return {
messageId: "msg-1",
target: "alice",
senderNick: "alice",
senderUser: "ident",
senderHost: "example.com",
text: "hello",
timestamp: Date.now(),
isGroup: false,
...overrides,
};
}
function resetInboundMocks() {
buildMentionRegexesMock.mockReset().mockReturnValue([]);
hasControlCommandMock.mockReset().mockReturnValue(false);
matchesMentionPatternsMock.mockReset().mockReturnValue(false);
readAllowFromStoreMock.mockReset().mockResolvedValue([]);
shouldHandleTextCommandsMock.mockReset().mockReturnValue(false);
upsertPairingRequestMock.mockReset().mockResolvedValue({ code: "CODE", created: true });
}
describe("irc inbound behavior", () => {
beforeEach(() => {
resetInboundMocks();
installIrcRuntime();
});
it("issues a DM pairing challenge and sends the reply to the sender nick", async () => {
const sendReply = vi.fn<(target: string, text: string, replyToId?: string) => Promise<void>>(
async () => {},
);
await handleIrcInbound({
message: createMessage(),
account: createAccount(),
config: { channels: { irc: {} } } as CoreConfig,
runtime: createRuntimeEnv(),
sendReply,
});
expect(upsertPairingRequestMock).toHaveBeenCalledWith({
channel: "irc",
accountId: "default",
id: "alice!ident@example.com",
meta: { name: "alice" },
});
expect(sendReply).toHaveBeenCalledTimes(1);
expect(sendReply).toHaveBeenCalledWith(
"alice",
[
"OpenClaw: access not configured.",
"",
"Your IRC id: alice!ident@example.com",
"Pairing code:",
"```",
"CODE",
"```",
"",
"Ask the bot owner to approve with:",
"```",
"openclaw pairing approve irc CODE",
"```",
].join("\n"),
undefined,
);
});
it("drops unauthorized group control commands before dispatch", async () => {
const runtime = createRuntimeEnv();
shouldHandleTextCommandsMock.mockReturnValue(true);
hasControlCommandMock.mockReturnValue(true);
await handleIrcInbound({
message: createMessage({
target: "#ops",
isGroup: true,
text: "/admin",
}),
account: createAccount({
config: {
dmPolicy: "pairing",
allowFrom: [],
groupPolicy: "allowlist",
groupAllowFrom: ["bob!ident@example.com"],
groups: {
"#ops": {
allowFrom: ["alice!ident@example.com"],
},
},
},
}),
config: { channels: { irc: {} }, commands: { useAccessGroups: true } } as CoreConfig,
runtime,
});
expect(runtime.log).toHaveBeenCalledWith(
"irc: drop control command (unauthorized) target=alice!ident@example.com",
);
});
it("passes the shared reply pipeline for dispatched replies", async () => {
const coreRuntime = createPluginRuntimeMock();
setIrcRuntime(coreRuntime as never);
await handleIrcInbound({
message: createMessage(),
account: createAccount({
config: {
dmPolicy: "open",
allowFrom: ["*"],
groupPolicy: "allowlist",
groupAllowFrom: [],
},
}),
config: { channels: { irc: {} } } as CoreConfig,
runtime: createRuntimeEnv(),
sendReply: vi.fn(async () => {}),
});
const assembledRequest = (
coreRuntime.channel.inbound.dispatchReply as unknown as { mock: { calls: unknown[][] } }
).mock.calls[0]?.[0] as { replyPipeline?: unknown } | undefined;
expect(assembledRequest?.replyPipeline).toEqual({});
});
it("binds durable completion to reply-lane adoption", async () => {
const coreRuntime = createPluginRuntimeMock();
setIrcRuntime(coreRuntime as never);
const onAdopted = vi.fn(async () => undefined);
const turnAdoptionLifecycle: IrcIngressLifecycle = {
abortSignal: new AbortController().signal,
onAdopted,
onDeferred: vi.fn(),
onAdoptionFinalizing: vi.fn(),
onAbandoned: vi.fn(async () => undefined),
};
const result = await handleIrcInbound({
message: createMessage(),
account: createAccount({
config: {
dmPolicy: "open",
allowFrom: ["*"],
groupPolicy: "allowlist",
groupAllowFrom: [],
},
}),
config: { channels: { irc: {} } } as CoreConfig,
runtime: createRuntimeEnv(),
turnAdoptionLifecycle,
sendReply: vi.fn(async () => {}),
});
const dispatchReply = coreRuntime.channel.reply
.dispatchReplyWithBufferedBlockDispatcher as unknown as { mock: { calls: unknown[][] } };
const replyOptions = (
dispatchReply.mock.calls[0]?.[0] as
| { replyOptions?: { turnAdoptionLifecycle?: IrcIngressLifecycle } }
| undefined
)?.replyOptions;
expect(replyOptions?.turnAdoptionLifecycle).toEqual(
expect.objectContaining({ abortSignal: turnAdoptionLifecycle.abortSignal }),
);
expect(onAdopted).toHaveBeenCalledOnce();
expect(result).toEqual({ kind: "completed" });
});
it.each([
{
name: "mixed assistant text",
reply: "Done.\n⚠️ 🛠️ `search repos (agent)` failed",
expected: ["Done."],
},
{
name: "trace-only assistant text",
reply: "⚠️ 🛠️ `search repos (agent)` failed",
expected: [],
},
{
name: "ordinary assistant text",
reply: "The pipeline has 3 open deals.",
expected: ["The pipeline has 3 open deals."],
},
])("sanitizes $name on the inbound reply path", async ({ reply, expected }) => {
const coreRuntime = createPluginRuntimeMock();
const sendReply = vi.fn<(target: string, text: string, replyToId?: string) => Promise<void>>(
async () => {},
);
const dispatchReply = coreRuntime.channel.inbound.dispatchReply as unknown as ReturnType<
typeof vi.fn<
(params: {
delivery: { deliver: (payload: { text: string }) => Promise<void> };
}) => Promise<void>
>
>;
dispatchReply.mockImplementation(
async (params: { delivery: { deliver: (payload: { text: string }) => Promise<void> } }) => {
await params.delivery.deliver({ text: reply });
},
);
setIrcRuntime(coreRuntime as never);
await handleIrcInbound({
message: createMessage(),
account: createAccount({
config: {
dmPolicy: "open",
allowFrom: ["*"],
groupPolicy: "allowlist",
groupAllowFrom: [],
},
}),
config: { channels: { irc: {} } } as CoreConfig,
runtime: createRuntimeEnv(),
sendReply,
});
expect(sendReply.mock.calls.map((call) => call[1])).toEqual(expected);
});
it("uses channel:# prefix for group channel From and OriginatingTo fields", async () => {
const coreRuntime = createPluginRuntimeMock();
const runtime = createRuntimeEnv();
setIrcRuntime(coreRuntime as never);
await handleIrcInbound({
message: createMessage({
target: "#ops",
isGroup: true,
senderNick: "alice",
senderUser: "ident",
senderHost: "example.com",
text: "hello",
}),
account: createAccount({
config: {
dmPolicy: "open",
allowFrom: ["*"],
groupPolicy: "open",
groupAllowFrom: [],
groups: {
"#ops": { enabled: true, requireMention: false },
},
},
}),
config: { channels: { irc: {} } } as CoreConfig,
runtime,
sendReply: vi.fn(async () => {}),
});
const dispatch = coreRuntime.channel.inbound.dispatch as unknown as {
mock: { calls: unknown[][] };
};
expect(dispatch.mock.calls).toHaveLength(1);
const ctx = (
dispatch.mock.calls[0]?.[0] as { ctxPayload?: Record<string, unknown> } | undefined
)?.ctxPayload;
expect(runtime.log).not.toHaveBeenCalled();
expect(ctx?.From).toBe("channel:#ops");
expect(ctx?.To).toBe("channel:#ops");
expect(ctx?.OriginatingTo).toBe("channel:#ops");
});
it.each([
{ label: "ordinary nick", nick: "OpenClaw", text: "OpenClaw: hello", mentioned: true },
{ label: "ASCII case folding", nick: "OpenClaw", text: "openclaw: hello", mentioned: true },
{ label: "leading bracket", nick: "[Claw]", text: "[Claw]: hello", mentioned: true },
{ label: "trailing bracket", nick: "Claw]", text: "hello Claw],", mentioned: true },
{ label: "leading caret", nick: "^Claw", text: "^Claw, hello", mentioned: true },
{ label: "trailing hyphen", nick: "Claw-", text: "Claw-: hello", mentioned: true },
{ label: "escaped backslash", nick: "\\Claw", text: "\\Claw: hello", mentioned: true },
{ label: "embedded brackets", nick: "Claw[Ops]", text: "Claw[Ops]: hi", mentioned: true },
{ label: "RFC1459 opening bracket", nick: "[Claw", text: "{claw: hello", mentioned: true },
{ label: "RFC1459 opening brace", nick: "{Claw", text: "[claw: hello", mentioned: true },
{ label: "RFC1459 closing bracket", nick: "Claw]", text: "claw}: hello", mentioned: true },
{ label: "RFC1459 closing brace", nick: "Claw}", text: "claw]: hello", mentioned: true },
{ label: "RFC1459 backslash", nick: "\\Claw", text: "|claw: hello", mentioned: true },
{ label: "RFC1459 vertical bar", nick: "|Claw", text: "\\claw: hello", mentioned: true },
{ label: "RFC1459 caret", nick: "^Claw", text: "~claw: hello", mentioned: true },
{ label: "RFC1459 tilde", nick: "~Claw", text: "^claw: hello", mentioned: true },
{ label: "ordinary nick suffix", nick: "Claw", text: "Clawbot: hello", mentioned: false },
{ label: "ordinary nick prefix", nick: "Claw", text: "overClaw: hello", mentioned: false },
{ label: "IRC nick punctuation suffix", nick: "Claw", text: "Claw-bot: hi", mentioned: false },
{ label: "RFC1459 tilde nick suffix", nick: "Claw", text: "Claw~bot: hi", mentioned: false },
{
label: "punctuated nick inside a longer nick",
nick: "[Claw]",
text: "prefix[Claw]: hello",
mentioned: false,
},
])(
"recognizes only complete IRC nickname mentions: $label",
async ({ nick, text, mentioned }) => {
const coreRuntime = createPluginRuntimeMock();
const runtime = createRuntimeEnv();
setIrcRuntime(coreRuntime as never);
await handleIrcInbound({
message: createMessage({
target: "#ops",
isGroup: true,
text,
}),
account: createAccount({
nick,
config: {
dmPolicy: "open",
allowFrom: ["*"],
groupPolicy: "open",
groupAllowFrom: [],
groups: {
"#ops": { enabled: true, requireMention: true },
},
},
}),
config: { channels: { irc: {} } } as CoreConfig,
runtime,
sendReply: vi.fn(async () => {}),
});
expect(coreRuntime.channel.inbound.dispatch).toHaveBeenCalledTimes(mentioned ? 1 : 0);
if (!mentioned) {
expect(runtime.log).toHaveBeenCalledWith("irc: drop channel #ops (missing-mention)");
}
},
);
it("drops a spoofed sender for a host-less nick!user DM allowlist entry", async () => {
const coreRuntime = createPluginRuntimeMock();
const runtime = createRuntimeEnv();
setIrcRuntime(coreRuntime as never);
await handleIrcInbound({
message: createMessage({
target: "alice",
senderNick: "alice",
senderUser: "ident",
senderHost: "attacker.example",
text: "hello",
}),
account: createAccount({
config: {
dmPolicy: "allowlist",
allowFrom: ["alice!ident"],
groupPolicy: "allowlist",
groupAllowFrom: [],
},
}),
config: { channels: { irc: {} } } as CoreConfig,
runtime,
sendReply: vi.fn(async () => {}),
});
expect(
(coreRuntime.channel.inbound.dispatchReply as unknown as { mock: { calls: unknown[][] } })
.mock.calls.length,
).toBe(0);
expect(runtime.log).toHaveBeenCalledWith(
"irc: drop DM sender alice!ident@attacker.example (dmPolicy=allowlist)",
);
});
it("admits a sender matching a full nick!user@host DM allowlist entry", async () => {
const coreRuntime = createPluginRuntimeMock();
const runtime = createRuntimeEnv();
setIrcRuntime(coreRuntime as never);
await handleIrcInbound({
message: createMessage({
target: "alice",
senderNick: "alice",
senderUser: "ident",
senderHost: "example.com",
text: "hello",
}),
account: createAccount({
config: {
dmPolicy: "allowlist",
allowFrom: ["alice!ident@example.com"],
groupPolicy: "allowlist",
groupAllowFrom: [],
},
}),
config: { channels: { irc: {} } } as CoreConfig,
runtime,
sendReply: vi.fn(async () => {}),
});
expect(
(coreRuntime.channel.inbound.dispatchReply as unknown as { mock: { calls: unknown[][] } })
.mock.calls.length,
).toBe(1);
});
});