mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 04:57:40 +00:00
perf(test): streamline imessage monitor tests
This commit is contained in:
@@ -178,6 +178,14 @@ describe("monitorIMessageProvider", () => {
|
|||||||
expect(String(ctx.Body ?? "")).toContain("[Replying to +15559998888 id:9001]");
|
expect(String(ctx.Body ?? "")).toContain("[Replying to +15559998888 id:9001]");
|
||||||
expect(String(ctx.Body ?? "")).toContain("original message");
|
expect(String(ctx.Body ?? "")).toContain("original message");
|
||||||
}
|
}
|
||||||
|
expect(updateLastRouteMock).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
deliveryContext: expect.objectContaining({
|
||||||
|
channel: "imessage",
|
||||||
|
to: "+15550001111",
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
await closeMonitor();
|
await closeMonitor();
|
||||||
await run;
|
await run;
|
||||||
@@ -397,7 +405,7 @@ describe("monitorIMessageProvider", () => {
|
|||||||
expect(String(sendMock.mock.calls[0]?.[1] ?? "")).toContain("Pairing code: PAIRCODE");
|
expect(String(sendMock.mock.calls[0]?.[1] ?? "")).toContain("Pairing code: PAIRCODE");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("honors group allowlist when groupPolicy is allowlist", async () => {
|
it("honors group allowlist and ignores pairing-store senders in groups", async () => {
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
setConfigMock({
|
setConfigMock({
|
||||||
...config,
|
...config,
|
||||||
@@ -405,15 +413,20 @@ describe("monitorIMessageProvider", () => {
|
|||||||
...config.channels,
|
...config.channels,
|
||||||
imessage: {
|
imessage: {
|
||||||
...config.channels.imessage,
|
...config.channels.imessage,
|
||||||
|
dmPolicy: "pairing",
|
||||||
|
allowFrom: [],
|
||||||
groupPolicy: "allowlist",
|
groupPolicy: "allowlist",
|
||||||
groupAllowFrom: ["chat_id:101"],
|
groupAllowFrom: ["chat_id:101"],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
readAllowFromStoreMock.mockResolvedValue(["+15550003333"]);
|
||||||
|
|
||||||
const run = startMonitor();
|
const run = startMonitor();
|
||||||
await waitForSubscribe();
|
await waitForSubscribe();
|
||||||
|
|
||||||
|
replyMock.mockClear();
|
||||||
|
sendMock.mockClear();
|
||||||
notifyMessage({
|
notifyMessage({
|
||||||
id: 3,
|
id: 3,
|
||||||
chat_id: 202,
|
chat_id: 202,
|
||||||
@@ -424,122 +437,41 @@ describe("monitorIMessageProvider", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
await closeMonitor();
|
|
||||||
await run;
|
|
||||||
|
|
||||||
expect(replyMock).not.toHaveBeenCalled();
|
expect(replyMock).not.toHaveBeenCalled();
|
||||||
expect(sendMock).not.toHaveBeenCalled();
|
expect(sendMock).not.toHaveBeenCalled();
|
||||||
});
|
|
||||||
|
|
||||||
it("does not allow group sender from pairing store when groupPolicy is allowlist", async () => {
|
|
||||||
const config = getConfig();
|
|
||||||
setConfigMock({
|
|
||||||
...config,
|
|
||||||
channels: {
|
|
||||||
...config.channels,
|
|
||||||
imessage: {
|
|
||||||
...config.channels.imessage,
|
|
||||||
dmPolicy: "pairing",
|
|
||||||
allowFrom: [],
|
|
||||||
groupPolicy: "allowlist",
|
|
||||||
groupAllowFrom: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
readAllowFromStoreMock.mockResolvedValue(["+15550003333"]);
|
|
||||||
|
|
||||||
const run = startMonitor();
|
|
||||||
await waitForSubscribe();
|
|
||||||
|
|
||||||
notifyMessage({
|
|
||||||
id: 30,
|
|
||||||
chat_id: 909,
|
|
||||||
sender: "+15550003333",
|
|
||||||
is_from_me: false,
|
|
||||||
text: "@openclaw hi from paired sender",
|
|
||||||
is_group: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
await flush();
|
|
||||||
await closeMonitor();
|
|
||||||
await run;
|
|
||||||
|
|
||||||
expect(replyMock).not.toHaveBeenCalled();
|
|
||||||
expect(sendMock).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("does not allow sender from pairing store when groupAllowFrom is restricted to a different chat_id", async () => {
|
|
||||||
const config = getConfig();
|
|
||||||
setConfigMock({
|
|
||||||
...config,
|
|
||||||
channels: {
|
|
||||||
...config.channels,
|
|
||||||
imessage: {
|
|
||||||
...config.channels.imessage,
|
|
||||||
dmPolicy: "pairing",
|
|
||||||
allowFrom: [],
|
|
||||||
groupPolicy: "allowlist",
|
|
||||||
groupAllowFrom: ["chat_id:101"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
readAllowFromStoreMock.mockResolvedValue(["+15550003333"]);
|
|
||||||
|
|
||||||
const run = startMonitor();
|
|
||||||
await waitForSubscribe();
|
|
||||||
|
|
||||||
|
replyMock.mockClear();
|
||||||
|
sendMock.mockClear();
|
||||||
notifyMessage({
|
notifyMessage({
|
||||||
id: 31,
|
id: 31,
|
||||||
chat_id: 202,
|
chat_id: 202,
|
||||||
sender: "+15550003333",
|
sender: "+15550003333",
|
||||||
is_from_me: false,
|
is_from_me: false,
|
||||||
text: "@openclaw hi from paired sender",
|
|
||||||
is_group: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
await flush();
|
|
||||||
await closeMonitor();
|
|
||||||
await run;
|
|
||||||
|
|
||||||
expect(replyMock).not.toHaveBeenCalled();
|
|
||||||
expect(sendMock).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("does not authorize control command via pairing-store sender in non-allowlisted chat", async () => {
|
|
||||||
const config = getConfig();
|
|
||||||
setConfigMock({
|
|
||||||
...config,
|
|
||||||
channels: {
|
|
||||||
...config.channels,
|
|
||||||
imessage: {
|
|
||||||
...config.channels.imessage,
|
|
||||||
dmPolicy: "pairing",
|
|
||||||
allowFrom: [],
|
|
||||||
groupPolicy: "allowlist",
|
|
||||||
groupAllowFrom: ["chat_id:101"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
readAllowFromStoreMock.mockResolvedValue(["+15550003333"]);
|
|
||||||
|
|
||||||
const run = startMonitor();
|
|
||||||
await waitForSubscribe();
|
|
||||||
|
|
||||||
notifyMessage({
|
|
||||||
id: 32,
|
|
||||||
chat_id: 202,
|
|
||||||
sender: "+15550003333",
|
|
||||||
is_from_me: false,
|
|
||||||
text: "/status",
|
text: "/status",
|
||||||
is_group: true,
|
is_group: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await flush();
|
||||||
|
expect(replyMock).not.toHaveBeenCalled();
|
||||||
|
expect(sendMock).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
replyMock.mockClear();
|
||||||
|
sendMock.mockClear();
|
||||||
|
notifyMessage({
|
||||||
|
id: 33,
|
||||||
|
chat_id: 101,
|
||||||
|
sender: "+15550003333",
|
||||||
|
is_from_me: false,
|
||||||
|
text: "@openclaw ok",
|
||||||
|
is_group: true,
|
||||||
|
});
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
await closeMonitor();
|
await closeMonitor();
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(replyMock).not.toHaveBeenCalled();
|
expect(replyMock).toHaveBeenCalled();
|
||||||
expect(sendMock).not.toHaveBeenCalled();
|
expect(sendMock).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("blocks group messages when groupPolicy is disabled", async () => {
|
it("blocks group messages when groupPolicy is disabled", async () => {
|
||||||
@@ -574,39 +506,6 @@ describe("monitorIMessageProvider", () => {
|
|||||||
expect(replyMock).not.toHaveBeenCalled();
|
expect(replyMock).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("updates last route with sender handle for direct messages", async () => {
|
|
||||||
replyMock.mockResolvedValueOnce({ text: "ok" });
|
|
||||||
const run = startMonitor();
|
|
||||||
await waitForSubscribe();
|
|
||||||
|
|
||||||
getNotificationHandler()?.({
|
|
||||||
method: "message",
|
|
||||||
params: {
|
|
||||||
message: {
|
|
||||||
id: 4,
|
|
||||||
chat_id: 7,
|
|
||||||
sender: "+15550004444",
|
|
||||||
is_from_me: false,
|
|
||||||
text: "hey",
|
|
||||||
is_group: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
await flush();
|
|
||||||
await closeMonitor();
|
|
||||||
await run;
|
|
||||||
|
|
||||||
expect(updateLastRouteMock).toHaveBeenCalledWith(
|
|
||||||
expect.objectContaining({
|
|
||||||
deliveryContext: expect.objectContaining({
|
|
||||||
channel: "imessage",
|
|
||||||
to: "+15550004444",
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("does not trigger unhandledRejection when aborting during shutdown", async () => {
|
it("does not trigger unhandledRejection when aborting during shutdown", async () => {
|
||||||
requestMock.mockImplementation((method: string) => {
|
requestMock.mockImplementation((method: string) => {
|
||||||
if (method === "watch.subscribe") {
|
if (method === "watch.subscribe") {
|
||||||
|
|||||||
@@ -109,7 +109,14 @@ vi.mock("./probe.js", () => ({
|
|||||||
probeIMessage: vi.fn(async () => ({ ok: true })),
|
probeIMessage: vi.fn(async () => ({ ok: true })),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
export const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
|
export const flush = () =>
|
||||||
|
new Promise<void>((resolve) => {
|
||||||
|
if (typeof setImmediate === "function") {
|
||||||
|
setImmediate(resolve);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setTimeout(resolve, 0);
|
||||||
|
});
|
||||||
|
|
||||||
export async function waitForSubscribe() {
|
export async function waitForSubscribe() {
|
||||||
for (let i = 0; i < 25; i += 1) {
|
for (let i = 0; i < 25; i += 1) {
|
||||||
|
|||||||
Reference in New Issue
Block a user