Files
openclaw/extensions/irc/src/inbound.behavior.test.ts
Peter Steinberger 1507a9701b refactor: centralize inbound supplemental context
* refactor: centralize inbound supplemental context

* refactor: trim supplemental finalizer typing

* docs: clarify supplemental context projection

* refactor: move inbound finalization into core

* refactor: simplify channel inbound facts

* refactor: fold supplemental media into inbound finalizer

* refactor: migrate channel inbound callers to builder

* docs: mark inbound finalizer compat types deprecated

* refactor: wire runtime turn context builder

* refactor: replace channel turn runtime API

* fix: respect discord quote visibility

* fix: avoid deprecated line dispatch helper

* refactor: deprecate channel message SDK seams

* docs: trim channel outbound SDK page

* test: migrate irc inbound assertion

* refactor: deprecate outbound SDK facades

* refactor: deprecate channel helper SDK facades

* refactor: deprecate channel streaming SDK facade

* refactor: move direct dm helpers into inbound SDK

* chore: mark legacy test-utils SDK alias deprecated

* refactor: remove unused allow-from read helper

* refactor: route remaining channel dispatch through core

* refactor: enforce modern extension SDK imports

* test: give slow image root tests more time

* ci: support node fallback on windows

* fix: add transcripts tool display metadata

* refactor: trim legacy channel test seams

* fix: preserve channel compat after rebase

* fix: keep deprecated channel inbound aliases

* fix: preserve discord thread context visibility

* fix: clean final rebase conflicts

* fix: preserve channel message dispatch aliases

* fix: sync channel refactor after rebase

* fix: sync channel refactor after latest main

* fix: dedupe memory-core subagent mock

* test: align clickclack inbound dispatch assertions

* fix: sync plugin sdk api hash after rebase

* fix: sync channel refactor after latest main

* fix: sync plugin sdk api hash after rebase

* fix: sync plugin sdk api hash after latest main

* test: remove stale inbound context awaits
2026-05-27 09:26:06 +01:00

248 lines
7.1 KiB
TypeScript

import { createPluginRuntimeMock } from "openclaw/plugin-sdk/channel-test-helpers";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { ResolvedIrcAccount } from "./accounts.js";
import { handleIrcInbound } from "./inbound.js";
import type { RuntimeEnv } from "./runtime-api.js";
import { clearIrcRuntime, 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();
});
afterEach(() => {
clearIrcRuntime();
});
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",
"```",
"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("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 ctx = (
coreRuntime.channel.reply.finalizeInboundContext as unknown as {
mock: { calls: unknown[][] };
}
).mock.calls[0]?.[0] as Record<string, unknown> | undefined;
expect(
(coreRuntime.channel.inbound.dispatchReply as unknown as { mock: { calls: unknown[][] } })
.mock.calls.length,
).toBe(1);
expect(runtime.log).not.toHaveBeenCalled();
expect(ctx?.From).toBe("channel:#ops");
expect(ctx?.To).toBe("channel:#ops");
expect(ctx?.OriginatingTo).toBe("channel:#ops");
});
});