Files
openclaw/extensions/irc/src/inbound.behavior.test.ts
Peter Steinberger f9c0dc2d2b fix(feishu): fall back from missing thread replies (#80306)
Summary:
- The branch adds an opt-in Feishu top-level group-send fallback for withdrawn or missing normal quoted thread replies, plus regression coverage, a changelog entry, and CI/lint typing and baseline refreshes.
- Reproducibility: yes. at source level. Current main hard-errors withdrawn/not-found Feishu reply targets when `replyInThread` is true, and the existing regression test asserts that no top-level create fallback occurs.

Automerge notes:
- PR branch already contained follow-up commit before automerge: fix(feishu): fall back from missing thread replies
- PR branch already contained follow-up commit before automerge: fix(clawsweeper): address review for automerge-openclaw-openclaw-8030…
- PR branch already contained follow-up commit before automerge: fix(clawsweeper): reconcile automerge-openclaw-openclaw-80306 with ma…
- PR branch already contained follow-up commit before automerge: fix(ci): satisfy stricter lint and test types
- PR branch already contained follow-up commit before automerge: fix(ci): align Node 24 test typing

Validation:
- ClawSweeper review passed for head 93146f9d13.
- Required merge gates passed before the squash merge.

Prepared head SHA: 93146f9d13
Review: https://github.com/openclaw/openclaw/pull/80306#issuecomment-4415604729

Co-authored-by: Peter Steinberger <steipete@gmail.com>
Co-authored-by: clawsweeper <274271284+clawsweeper[bot]@users.noreply.github.com>
2026-05-10 16:41:51 +00:00

196 lines
5.7 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",
expect.stringContaining("OpenClaw: access not configured."),
undefined,
);
expect(sendReply).toHaveBeenCalledWith(
"alice",
expect.stringContaining("Your IRC id: alice!ident@example.com"),
undefined,
);
const replyMessages = sendReply.mock.calls.map((call) => call[1]);
expect(replyMessages.some((message) => message.includes("CODE"))).toBe(true);
});
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.turn.runAssembled as unknown as { mock: { calls: unknown[][] } }
).mock.calls[0]?.[0] as { replyPipeline?: unknown } | undefined;
expect(assembledRequest?.replyPipeline).toEqual({});
});
});