mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-29 09:02:02 +03:00
refactor(test): share iMessage monitor test harness
This commit is contained in:
@@ -1,111 +1,41 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { monitorIMessageProvider } from "./monitor.js";
|
import { monitorIMessageProvider } from "./monitor.js";
|
||||||
|
import {
|
||||||
|
flush,
|
||||||
|
getCloseResolve,
|
||||||
|
getConfigMock,
|
||||||
|
getNotificationHandler,
|
||||||
|
getReplyMock,
|
||||||
|
getSendMock,
|
||||||
|
getUpsertPairingRequestMock,
|
||||||
|
installMonitorIMessageProviderTestHooks,
|
||||||
|
setConfigMock,
|
||||||
|
waitForSubscribe,
|
||||||
|
} from "./monitor.test-harness.js";
|
||||||
|
|
||||||
const requestMock = vi.fn();
|
installMonitorIMessageProviderTestHooks();
|
||||||
const stopMock = vi.fn();
|
|
||||||
const sendMock = vi.fn();
|
|
||||||
const replyMock = vi.fn();
|
|
||||||
const updateLastRouteMock = vi.fn();
|
|
||||||
const readAllowFromStoreMock = vi.fn();
|
|
||||||
const upsertPairingRequestMock = vi.fn();
|
|
||||||
|
|
||||||
let config: Record<string, unknown> = {};
|
const replyMock = getReplyMock();
|
||||||
let notificationHandler: ((msg: { method: string; params?: unknown }) => void) | undefined;
|
const sendMock = getSendMock();
|
||||||
let closeResolve: (() => void) | undefined;
|
const upsertPairingRequestMock = getUpsertPairingRequestMock();
|
||||||
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
type TestConfig = {
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
channels: Record<string, unknown> & { imessage: Record<string, unknown> };
|
||||||
return {
|
messages: Record<string, unknown>;
|
||||||
...actual,
|
session: Record<string, unknown>;
|
||||||
loadConfig: () => config,
|
[k: string]: unknown;
|
||||||
};
|
};
|
||||||
});
|
|
||||||
|
|
||||||
vi.mock("../auto-reply/reply.js", () => ({
|
function getConfig(): TestConfig {
|
||||||
getReplyFromConfig: (...args: unknown[]) => replyMock(...args),
|
return getConfigMock() as unknown as TestConfig;
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("./send.js", () => ({
|
|
||||||
sendMessageIMessage: (...args: unknown[]) => sendMock(...args),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
|
||||||
readChannelAllowFromStore: (...args: unknown[]) => readAllowFromStoreMock(...args),
|
|
||||||
upsertChannelPairingRequest: (...args: unknown[]) => upsertPairingRequestMock(...args),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../config/sessions.js", () => ({
|
|
||||||
resolveStorePath: vi.fn(() => "/tmp/openclaw-sessions.json"),
|
|
||||||
updateLastRoute: (...args: unknown[]) => updateLastRouteMock(...args),
|
|
||||||
readSessionUpdatedAt: vi.fn(() => undefined),
|
|
||||||
recordSessionMetaFromInbound: vi.fn().mockResolvedValue(undefined),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("./client.js", () => ({
|
|
||||||
createIMessageRpcClient: vi.fn(async (opts: { onNotification?: typeof notificationHandler }) => {
|
|
||||||
notificationHandler = opts.onNotification;
|
|
||||||
return {
|
|
||||||
request: (...args: unknown[]) => requestMock(...args),
|
|
||||||
waitForClose: () =>
|
|
||||||
new Promise<void>((resolve) => {
|
|
||||||
closeResolve = resolve;
|
|
||||||
}),
|
|
||||||
stop: (...args: unknown[]) => stopMock(...args),
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("./probe.js", () => ({
|
|
||||||
probeIMessage: vi.fn(async () => ({ ok: true })),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
||||||
|
|
||||||
async function waitForSubscribe() {
|
|
||||||
for (let i = 0; i < 5; i += 1) {
|
|
||||||
if (requestMock.mock.calls.some((call) => call[0] === "watch.subscribe")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await flush();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
config = {
|
|
||||||
channels: {
|
|
||||||
imessage: {
|
|
||||||
dmPolicy: "open",
|
|
||||||
allowFrom: ["*"],
|
|
||||||
groups: { "*": { requireMention: true } },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
session: { mainKey: "main" },
|
|
||||||
messages: {
|
|
||||||
groupChat: { mentionPatterns: ["@openclaw"] },
|
|
||||||
},
|
|
||||||
};
|
|
||||||
requestMock.mockReset().mockImplementation((method: string) => {
|
|
||||||
if (method === "watch.subscribe") {
|
|
||||||
return Promise.resolve({ subscription: 1 });
|
|
||||||
}
|
|
||||||
return Promise.resolve({});
|
|
||||||
});
|
|
||||||
stopMock.mockReset().mockResolvedValue(undefined);
|
|
||||||
sendMock.mockReset().mockResolvedValue({ messageId: "ok" });
|
|
||||||
replyMock.mockReset().mockResolvedValue({ text: "ok" });
|
|
||||||
updateLastRouteMock.mockReset();
|
|
||||||
readAllowFromStoreMock.mockReset().mockResolvedValue([]);
|
|
||||||
upsertPairingRequestMock.mockReset().mockResolvedValue({ code: "PAIRCODE", created: true });
|
|
||||||
notificationHandler = undefined;
|
|
||||||
closeResolve = undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("monitorIMessageProvider", () => {
|
describe("monitorIMessageProvider", () => {
|
||||||
it("skips group messages without a mention by default", async () => {
|
it("skips group messages without a mention by default", async () => {
|
||||||
const run = monitorIMessageProvider();
|
const run = monitorIMessageProvider();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
notificationHandler?.({
|
getNotificationHandler()?.({
|
||||||
method: "message",
|
method: "message",
|
||||||
params: {
|
params: {
|
||||||
message: {
|
message: {
|
||||||
@@ -120,7 +50,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(replyMock).not.toHaveBeenCalled();
|
expect(replyMock).not.toHaveBeenCalled();
|
||||||
@@ -128,21 +58,22 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("allows group messages when imessage groups default disables mention gating", async () => {
|
it("allows group messages when imessage groups default disables mention gating", async () => {
|
||||||
config = {
|
const config = getConfig();
|
||||||
|
setConfigMock({
|
||||||
...config,
|
...config,
|
||||||
channels: {
|
channels: {
|
||||||
...config.channels,
|
...config.channels,
|
||||||
imessage: {
|
imessage: {
|
||||||
...config.channels?.imessage,
|
...config.channels.imessage,
|
||||||
groupPolicy: "open",
|
groupPolicy: "open",
|
||||||
groups: { "*": { requireMention: false } },
|
groups: { "*": { requireMention: false } },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
const run = monitorIMessageProvider();
|
const run = monitorIMessageProvider();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
notificationHandler?.({
|
getNotificationHandler()?.({
|
||||||
method: "message",
|
method: "message",
|
||||||
params: {
|
params: {
|
||||||
message: {
|
message: {
|
||||||
@@ -157,29 +88,30 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(replyMock).toHaveBeenCalled();
|
expect(replyMock).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("allows group messages when requireMention is true but no mentionPatterns exist", async () => {
|
it("allows group messages when requireMention is true but no mentionPatterns exist", async () => {
|
||||||
config = {
|
const config = getConfig();
|
||||||
|
setConfigMock({
|
||||||
...config,
|
...config,
|
||||||
messages: { groupChat: { mentionPatterns: [] } },
|
messages: { groupChat: { mentionPatterns: [] } },
|
||||||
channels: {
|
channels: {
|
||||||
...config.channels,
|
...config.channels,
|
||||||
imessage: {
|
imessage: {
|
||||||
...config.channels?.imessage,
|
...config.channels.imessage,
|
||||||
groupPolicy: "open",
|
groupPolicy: "open",
|
||||||
groups: { "*": { requireMention: true } },
|
groups: { "*": { requireMention: true } },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
const run = monitorIMessageProvider();
|
const run = monitorIMessageProvider();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
notificationHandler?.({
|
getNotificationHandler()?.({
|
||||||
method: "message",
|
method: "message",
|
||||||
params: {
|
params: {
|
||||||
message: {
|
message: {
|
||||||
@@ -194,27 +126,28 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(replyMock).toHaveBeenCalled();
|
expect(replyMock).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("blocks group messages when imessage.groups is set without a wildcard", async () => {
|
it("blocks group messages when imessage.groups is set without a wildcard", async () => {
|
||||||
config = {
|
const config = getConfig();
|
||||||
|
setConfigMock({
|
||||||
...config,
|
...config,
|
||||||
channels: {
|
channels: {
|
||||||
...config.channels,
|
...config.channels,
|
||||||
imessage: {
|
imessage: {
|
||||||
...config.channels?.imessage,
|
...config.channels.imessage,
|
||||||
groups: { "99": { requireMention: false } },
|
groups: { "99": { requireMention: false } },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
const run = monitorIMessageProvider();
|
const run = monitorIMessageProvider();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
notificationHandler?.({
|
getNotificationHandler()?.({
|
||||||
method: "message",
|
method: "message",
|
||||||
params: {
|
params: {
|
||||||
message: {
|
message: {
|
||||||
@@ -229,7 +162,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(replyMock).not.toHaveBeenCalled();
|
expect(replyMock).not.toHaveBeenCalled();
|
||||||
@@ -237,23 +170,24 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("treats configured chat_id as a group session even when is_group is false", async () => {
|
it("treats configured chat_id as a group session even when is_group is false", async () => {
|
||||||
config = {
|
const config = getConfig();
|
||||||
|
setConfigMock({
|
||||||
...config,
|
...config,
|
||||||
channels: {
|
channels: {
|
||||||
...config.channels,
|
...config.channels,
|
||||||
imessage: {
|
imessage: {
|
||||||
...config.channels?.imessage,
|
...config.channels.imessage,
|
||||||
dmPolicy: "open",
|
dmPolicy: "open",
|
||||||
allowFrom: ["*"],
|
allowFrom: ["*"],
|
||||||
groups: { "2": { requireMention: false } },
|
groups: { "2": { requireMention: false } },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
const run = monitorIMessageProvider();
|
const run = monitorIMessageProvider();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
notificationHandler?.({
|
getNotificationHandler()?.({
|
||||||
method: "message",
|
method: "message",
|
||||||
params: {
|
params: {
|
||||||
message: {
|
message: {
|
||||||
@@ -268,7 +202,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(replyMock).toHaveBeenCalled();
|
expect(replyMock).toHaveBeenCalled();
|
||||||
@@ -281,15 +215,16 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("prefixes final replies with responsePrefix", async () => {
|
it("prefixes final replies with responsePrefix", async () => {
|
||||||
config = {
|
const config = getConfig();
|
||||||
|
setConfigMock({
|
||||||
...config,
|
...config,
|
||||||
messages: { responsePrefix: "PFX" },
|
messages: { responsePrefix: "PFX" },
|
||||||
};
|
});
|
||||||
replyMock.mockResolvedValue({ text: "final reply" });
|
replyMock.mockResolvedValue({ text: "final reply" });
|
||||||
const run = monitorIMessageProvider();
|
const run = monitorIMessageProvider();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
notificationHandler?.({
|
getNotificationHandler()?.({
|
||||||
method: "message",
|
method: "message",
|
||||||
params: {
|
params: {
|
||||||
message: {
|
message: {
|
||||||
@@ -304,7 +239,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(sendMock).toHaveBeenCalledTimes(1);
|
expect(sendMock).toHaveBeenCalledTimes(1);
|
||||||
@@ -312,22 +247,23 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("defaults to dmPolicy=pairing behavior when allowFrom is empty", async () => {
|
it("defaults to dmPolicy=pairing behavior when allowFrom is empty", async () => {
|
||||||
config = {
|
const config = getConfig();
|
||||||
|
setConfigMock({
|
||||||
...config,
|
...config,
|
||||||
channels: {
|
channels: {
|
||||||
...config.channels,
|
...config.channels,
|
||||||
imessage: {
|
imessage: {
|
||||||
...config.channels?.imessage,
|
...config.channels.imessage,
|
||||||
dmPolicy: "pairing",
|
dmPolicy: "pairing",
|
||||||
allowFrom: [],
|
allowFrom: [],
|
||||||
groups: { "*": { requireMention: true } },
|
groups: { "*": { requireMention: true } },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
const run = monitorIMessageProvider();
|
const run = monitorIMessageProvider();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
notificationHandler?.({
|
getNotificationHandler()?.({
|
||||||
method: "message",
|
method: "message",
|
||||||
params: {
|
params: {
|
||||||
message: {
|
message: {
|
||||||
@@ -342,7 +278,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(replyMock).not.toHaveBeenCalled();
|
expect(replyMock).not.toHaveBeenCalled();
|
||||||
@@ -359,7 +295,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
const run = monitorIMessageProvider();
|
const run = monitorIMessageProvider();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
notificationHandler?.({
|
getNotificationHandler()?.({
|
||||||
method: "message",
|
method: "message",
|
||||||
params: {
|
params: {
|
||||||
message: {
|
message: {
|
||||||
@@ -376,7 +312,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(replyMock).toHaveBeenCalledOnce();
|
expect(replyMock).toHaveBeenCalledOnce();
|
||||||
@@ -394,21 +330,22 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("honors group allowlist when groupPolicy is allowlist", async () => {
|
it("honors group allowlist when groupPolicy is allowlist", async () => {
|
||||||
config = {
|
const config = getConfig();
|
||||||
|
setConfigMock({
|
||||||
...config,
|
...config,
|
||||||
channels: {
|
channels: {
|
||||||
...config.channels,
|
...config.channels,
|
||||||
imessage: {
|
imessage: {
|
||||||
...config.channels?.imessage,
|
...config.channels.imessage,
|
||||||
groupPolicy: "allowlist",
|
groupPolicy: "allowlist",
|
||||||
groupAllowFrom: ["chat_id:101"],
|
groupAllowFrom: ["chat_id:101"],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
const run = monitorIMessageProvider();
|
const run = monitorIMessageProvider();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
notificationHandler?.({
|
getNotificationHandler()?.({
|
||||||
method: "message",
|
method: "message",
|
||||||
params: {
|
params: {
|
||||||
message: {
|
message: {
|
||||||
@@ -423,27 +360,28 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(replyMock).not.toHaveBeenCalled();
|
expect(replyMock).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("blocks group messages when groupPolicy is disabled", async () => {
|
it("blocks group messages when groupPolicy is disabled", async () => {
|
||||||
config = {
|
const config = getConfig();
|
||||||
|
setConfigMock({
|
||||||
...config,
|
...config,
|
||||||
channels: {
|
channels: {
|
||||||
...config.channels,
|
...config.channels,
|
||||||
imessage: {
|
imessage: {
|
||||||
...config.channels?.imessage,
|
...config.channels.imessage,
|
||||||
groupPolicy: "disabled",
|
groupPolicy: "disabled",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
const run = monitorIMessageProvider();
|
const run = monitorIMessageProvider();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
notificationHandler?.({
|
getNotificationHandler()?.({
|
||||||
method: "message",
|
method: "message",
|
||||||
params: {
|
params: {
|
||||||
message: {
|
message: {
|
||||||
@@ -458,7 +396,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(replyMock).not.toHaveBeenCalled();
|
expect(replyMock).not.toHaveBeenCalled();
|
||||||
@@ -468,7 +406,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
const run = monitorIMessageProvider();
|
const run = monitorIMessageProvider();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
notificationHandler?.({
|
getNotificationHandler()?.({
|
||||||
method: "message",
|
method: "message",
|
||||||
params: {
|
params: {
|
||||||
message: {
|
message: {
|
||||||
@@ -485,7 +423,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(replyMock).toHaveBeenCalled();
|
expect(replyMock).toHaveBeenCalled();
|
||||||
@@ -499,7 +437,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
const run = monitorIMessageProvider();
|
const run = monitorIMessageProvider();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
notificationHandler?.({
|
getNotificationHandler()?.({
|
||||||
method: "message",
|
method: "message",
|
||||||
params: {
|
params: {
|
||||||
message: {
|
message: {
|
||||||
@@ -517,7 +455,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(replyMock).toHaveBeenCalled();
|
expect(replyMock).toHaveBeenCalled();
|
||||||
|
|||||||
@@ -0,0 +1,151 @@
|
|||||||
|
import { beforeEach, vi } from "vitest";
|
||||||
|
|
||||||
|
type NotificationHandler = (msg: { method: string; params?: unknown }) => void;
|
||||||
|
|
||||||
|
const state = vi.hoisted(() => ({
|
||||||
|
requestMock: vi.fn(),
|
||||||
|
stopMock: vi.fn(),
|
||||||
|
sendMock: vi.fn(),
|
||||||
|
replyMock: vi.fn(),
|
||||||
|
updateLastRouteMock: vi.fn(),
|
||||||
|
readAllowFromStoreMock: vi.fn(),
|
||||||
|
upsertPairingRequestMock: vi.fn(),
|
||||||
|
config: {} as Record<string, unknown>,
|
||||||
|
notificationHandler: undefined as NotificationHandler | undefined,
|
||||||
|
closeResolve: undefined as (() => void) | undefined,
|
||||||
|
}));
|
||||||
|
|
||||||
|
export function getRequestMock() {
|
||||||
|
return state.requestMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getStopMock() {
|
||||||
|
return state.stopMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getSendMock() {
|
||||||
|
return state.sendMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getReplyMock() {
|
||||||
|
return state.replyMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getUpdateLastRouteMock() {
|
||||||
|
return state.updateLastRouteMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getReadAllowFromStoreMock() {
|
||||||
|
return state.readAllowFromStoreMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getUpsertPairingRequestMock() {
|
||||||
|
return state.upsertPairingRequestMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getNotificationHandler() {
|
||||||
|
return state.notificationHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getCloseResolve() {
|
||||||
|
return state.closeResolve;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setConfigMock(next: Record<string, unknown>) {
|
||||||
|
state.config = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getConfigMock() {
|
||||||
|
return state.config;
|
||||||
|
}
|
||||||
|
|
||||||
|
vi.mock("../config/config.js", async (importOriginal) => {
|
||||||
|
const actual = await importOriginal<typeof import("../config/config.js")>();
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
loadConfig: () => state.config,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
vi.mock("../auto-reply/reply.js", () => ({
|
||||||
|
getReplyFromConfig: (...args: unknown[]) => state.replyMock(...args),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("./send.js", () => ({
|
||||||
|
sendMessageIMessage: (...args: unknown[]) => state.sendMock(...args),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
|
readChannelAllowFromStore: (...args: unknown[]) => state.readAllowFromStoreMock(...args),
|
||||||
|
upsertChannelPairingRequest: (...args: unknown[]) => state.upsertPairingRequestMock(...args),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../config/sessions.js", () => ({
|
||||||
|
resolveStorePath: vi.fn(() => "/tmp/openclaw-sessions.json"),
|
||||||
|
updateLastRoute: (...args: unknown[]) => state.updateLastRouteMock(...args),
|
||||||
|
readSessionUpdatedAt: vi.fn(() => undefined),
|
||||||
|
recordSessionMetaFromInbound: vi.fn().mockResolvedValue(undefined),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("./client.js", () => ({
|
||||||
|
createIMessageRpcClient: vi.fn(async (opts: { onNotification?: NotificationHandler }) => {
|
||||||
|
state.notificationHandler = opts.onNotification;
|
||||||
|
return {
|
||||||
|
request: (...args: unknown[]) => state.requestMock(...args),
|
||||||
|
waitForClose: () =>
|
||||||
|
new Promise<void>((resolve) => {
|
||||||
|
state.closeResolve = resolve;
|
||||||
|
}),
|
||||||
|
stop: (...args: unknown[]) => state.stopMock(...args),
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("./probe.js", () => ({
|
||||||
|
probeIMessage: vi.fn(async () => ({ ok: true })),
|
||||||
|
}));
|
||||||
|
|
||||||
|
export const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
|
||||||
|
|
||||||
|
export async function waitForSubscribe() {
|
||||||
|
for (let i = 0; i < 5; i += 1) {
|
||||||
|
if (state.requestMock.mock.calls.some((call) => call[0] === "watch.subscribe")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function installMonitorIMessageProviderTestHooks() {
|
||||||
|
beforeEach(() => {
|
||||||
|
state.config = {
|
||||||
|
channels: {
|
||||||
|
imessage: {
|
||||||
|
dmPolicy: "open",
|
||||||
|
allowFrom: ["*"],
|
||||||
|
groups: { "*": { requireMention: true } },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
session: { mainKey: "main" },
|
||||||
|
messages: {
|
||||||
|
groupChat: { mentionPatterns: ["@openclaw"] },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
state.requestMock.mockReset().mockImplementation((method: string) => {
|
||||||
|
if (method === "watch.subscribe") {
|
||||||
|
return Promise.resolve({ subscription: 1 });
|
||||||
|
}
|
||||||
|
return Promise.resolve({});
|
||||||
|
});
|
||||||
|
state.stopMock.mockReset().mockResolvedValue(undefined);
|
||||||
|
state.sendMock.mockReset().mockResolvedValue({ messageId: "ok" });
|
||||||
|
state.replyMock.mockReset().mockResolvedValue({ text: "ok" });
|
||||||
|
state.updateLastRouteMock.mockReset();
|
||||||
|
state.readAllowFromStoreMock.mockReset().mockResolvedValue([]);
|
||||||
|
state.upsertPairingRequestMock
|
||||||
|
.mockReset()
|
||||||
|
.mockResolvedValue({ code: "PAIRCODE", created: true });
|
||||||
|
state.notificationHandler = undefined;
|
||||||
|
state.closeResolve = undefined;
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,104 +1,23 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { monitorIMessageProvider } from "./monitor.js";
|
import { monitorIMessageProvider } from "./monitor.js";
|
||||||
|
import {
|
||||||
|
flush,
|
||||||
|
getCloseResolve,
|
||||||
|
getNotificationHandler,
|
||||||
|
getReplyMock,
|
||||||
|
getRequestMock,
|
||||||
|
getStopMock,
|
||||||
|
getUpdateLastRouteMock,
|
||||||
|
installMonitorIMessageProviderTestHooks,
|
||||||
|
waitForSubscribe,
|
||||||
|
} from "./monitor.test-harness.js";
|
||||||
|
|
||||||
const requestMock = vi.fn();
|
installMonitorIMessageProviderTestHooks();
|
||||||
const stopMock = vi.fn();
|
|
||||||
const sendMock = vi.fn();
|
|
||||||
const replyMock = vi.fn();
|
|
||||||
const updateLastRouteMock = vi.fn();
|
|
||||||
const readAllowFromStoreMock = vi.fn();
|
|
||||||
const upsertPairingRequestMock = vi.fn();
|
|
||||||
|
|
||||||
let config: Record<string, unknown> = {};
|
const replyMock = getReplyMock();
|
||||||
let notificationHandler: ((msg: { method: string; params?: unknown }) => void) | undefined;
|
const requestMock = getRequestMock();
|
||||||
let closeResolve: (() => void) | undefined;
|
const stopMock = getStopMock();
|
||||||
|
const updateLastRouteMock = getUpdateLastRouteMock();
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig: () => config,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
vi.mock("../auto-reply/reply.js", () => ({
|
|
||||||
getReplyFromConfig: (...args: unknown[]) => replyMock(...args),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("./send.js", () => ({
|
|
||||||
sendMessageIMessage: (...args: unknown[]) => sendMock(...args),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
|
||||||
readChannelAllowFromStore: (...args: unknown[]) => readAllowFromStoreMock(...args),
|
|
||||||
upsertChannelPairingRequest: (...args: unknown[]) => upsertPairingRequestMock(...args),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../config/sessions.js", () => ({
|
|
||||||
resolveStorePath: vi.fn(() => "/tmp/openclaw-sessions.json"),
|
|
||||||
updateLastRoute: (...args: unknown[]) => updateLastRouteMock(...args),
|
|
||||||
readSessionUpdatedAt: vi.fn(() => undefined),
|
|
||||||
recordSessionMetaFromInbound: vi.fn().mockResolvedValue(undefined),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("./client.js", () => ({
|
|
||||||
createIMessageRpcClient: vi.fn(async (opts: { onNotification?: typeof notificationHandler }) => {
|
|
||||||
notificationHandler = opts.onNotification;
|
|
||||||
return {
|
|
||||||
request: (...args: unknown[]) => requestMock(...args),
|
|
||||||
waitForClose: () =>
|
|
||||||
new Promise<void>((resolve) => {
|
|
||||||
closeResolve = resolve;
|
|
||||||
}),
|
|
||||||
stop: (...args: unknown[]) => stopMock(...args),
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("./probe.js", () => ({
|
|
||||||
probeIMessage: vi.fn(async () => ({ ok: true })),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
||||||
|
|
||||||
async function waitForSubscribe() {
|
|
||||||
for (let i = 0; i < 5; i += 1) {
|
|
||||||
if (requestMock.mock.calls.some((call) => call[0] === "watch.subscribe")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await flush();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
config = {
|
|
||||||
channels: {
|
|
||||||
imessage: {
|
|
||||||
dmPolicy: "open",
|
|
||||||
allowFrom: ["*"],
|
|
||||||
groups: { "*": { requireMention: true } },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
session: { mainKey: "main" },
|
|
||||||
messages: {
|
|
||||||
groupChat: { mentionPatterns: ["@openclaw"] },
|
|
||||||
},
|
|
||||||
};
|
|
||||||
requestMock.mockReset().mockImplementation((method: string) => {
|
|
||||||
if (method === "watch.subscribe") {
|
|
||||||
return Promise.resolve({ subscription: 1 });
|
|
||||||
}
|
|
||||||
return Promise.resolve({});
|
|
||||||
});
|
|
||||||
stopMock.mockReset().mockResolvedValue(undefined);
|
|
||||||
sendMock.mockReset().mockResolvedValue({ messageId: "ok" });
|
|
||||||
replyMock.mockReset().mockResolvedValue({ text: "ok" });
|
|
||||||
updateLastRouteMock.mockReset();
|
|
||||||
readAllowFromStoreMock.mockReset().mockResolvedValue([]);
|
|
||||||
upsertPairingRequestMock.mockReset().mockResolvedValue({ code: "PAIRCODE", created: true });
|
|
||||||
notificationHandler = undefined;
|
|
||||||
closeResolve = undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("monitorIMessageProvider", () => {
|
describe("monitorIMessageProvider", () => {
|
||||||
it("updates last route with sender handle for direct messages", async () => {
|
it("updates last route with sender handle for direct messages", async () => {
|
||||||
@@ -106,7 +25,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
const run = monitorIMessageProvider();
|
const run = monitorIMessageProvider();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
notificationHandler?.({
|
getNotificationHandler()?.({
|
||||||
method: "message",
|
method: "message",
|
||||||
params: {
|
params: {
|
||||||
message: {
|
message: {
|
||||||
@@ -121,7 +40,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(updateLastRouteMock).toHaveBeenCalledWith(
|
expect(updateLastRouteMock).toHaveBeenCalledWith(
|
||||||
@@ -162,7 +81,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
abortController.abort();
|
abortController.abort();
|
||||||
await flush();
|
await flush();
|
||||||
|
|
||||||
closeResolve?.();
|
getCloseResolve()?.();
|
||||||
await run;
|
await run;
|
||||||
} finally {
|
} finally {
|
||||||
process.off("unhandledRejection", onUnhandled);
|
process.off("unhandledRejection", onUnhandled);
|
||||||
|
|||||||
Reference in New Issue
Block a user