mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-18 13:54:47 +00:00
test: guard discord reaction monitor mock calls
This commit is contained in:
@@ -944,6 +944,27 @@ vi.spyOn(routingModule, "resolveAgentRoute").mockImplementation(resolveAgentRout
|
||||
const { DiscordMessageListener, DiscordReactionListener, registerDiscordListener } =
|
||||
await import("./monitor/listeners.js");
|
||||
|
||||
type MockWithCalls = { mock: { calls: unknown[][] } };
|
||||
|
||||
function firstMockCall(mock: MockWithCalls, label: string): unknown[] {
|
||||
const call = mock.mock.calls.at(0);
|
||||
if (!call) {
|
||||
throw new Error(`expected ${label} call`);
|
||||
}
|
||||
return call;
|
||||
}
|
||||
|
||||
function firstMockArg(mock: MockWithCalls, label: string) {
|
||||
return firstMockCall(mock, label)[0];
|
||||
}
|
||||
|
||||
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
||||
if (typeof value !== "object" || value === null) {
|
||||
throw new Error(`expected ${label} to be an object`);
|
||||
}
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function makeReactionEvent(overrides?: {
|
||||
guildId?: string;
|
||||
channelId?: string;
|
||||
@@ -1079,12 +1100,14 @@ describe("discord DM reaction handling", () => {
|
||||
await listener.handle(data, client);
|
||||
|
||||
expect(enqueueSystemEventSpy, testCase.name).toHaveBeenCalledOnce();
|
||||
const [text, opts] = enqueueSystemEventSpy.mock.calls[0];
|
||||
const [text, opts] = firstMockCall(enqueueSystemEventSpy, "enqueueSystemEvent");
|
||||
expect(text, testCase.name).toContain("Discord reaction added");
|
||||
expect(text, testCase.name).toContain("👍");
|
||||
expect(text, testCase.name).toContain("dm");
|
||||
expect(text, testCase.name).not.toContain("undefined");
|
||||
expect(opts.sessionKey, testCase.name).toBe("discord:acc-1:dm:user-1");
|
||||
expect(requireRecord(opts, "system event options").sessionKey, testCase.name).toBe(
|
||||
"discord:acc-1:dm:user-1",
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1222,7 +1245,7 @@ describe("discord DM reaction handling", () => {
|
||||
|
||||
expect(getReactionClientFetchChannelMock(client)).toHaveBeenCalled();
|
||||
expect(enqueueSystemEventSpy).toHaveBeenCalledOnce();
|
||||
const [text] = enqueueSystemEventSpy.mock.calls[0];
|
||||
const text = firstMockArg(enqueueSystemEventSpy, "enqueueSystemEvent");
|
||||
expect(text).toContain("Discord reaction added");
|
||||
});
|
||||
|
||||
@@ -1237,12 +1260,9 @@ describe("discord DM reaction handling", () => {
|
||||
await listener.handle(data, client);
|
||||
|
||||
expect(resolveAgentRouteMock).toHaveBeenCalledOnce();
|
||||
const routeArgs = (resolveAgentRouteMock.mock.calls[0]?.[0] ?? {}) as {
|
||||
const routeArgs = firstMockArg(resolveAgentRouteMock, "resolveAgentRoute") as {
|
||||
peer?: unknown;
|
||||
};
|
||||
if (!routeArgs) {
|
||||
throw new Error("expected route arguments");
|
||||
}
|
||||
expect(routeArgs.peer).toEqual({ kind: "direct", id: "user-42" });
|
||||
});
|
||||
|
||||
@@ -1257,12 +1277,9 @@ describe("discord DM reaction handling", () => {
|
||||
await listener.handle(data, client);
|
||||
|
||||
expect(resolveAgentRouteMock).toHaveBeenCalledOnce();
|
||||
const routeArgs = (resolveAgentRouteMock.mock.calls[0]?.[0] ?? {}) as {
|
||||
const routeArgs = firstMockArg(resolveAgentRouteMock, "resolveAgentRoute") as {
|
||||
peer?: unknown;
|
||||
};
|
||||
if (!routeArgs) {
|
||||
throw new Error("expected route arguments");
|
||||
}
|
||||
expect(routeArgs.peer).toEqual({ kind: "group", id: "channel-1" });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user