mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 10:17:39 +00:00
refactor: centralize target errors and cache lookups
This commit is contained in:
78
src/infra/outbound/target-resolver.test.ts
Normal file
78
src/infra/outbound/target-resolver.test.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import type { ChannelDirectoryEntry } from "../../channels/plugins/types.js";
|
||||
import type { ClawdbotConfig } from "../../config/config.js";
|
||||
import { resetDirectoryCache, resolveMessagingTarget } from "./target-resolver.js";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
listGroups: vi.fn(),
|
||||
listGroupsLive: vi.fn(),
|
||||
getChannelPlugin: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../../channels/plugins/index.js", () => ({
|
||||
getChannelPlugin: (...args: unknown[]) => mocks.getChannelPlugin(...args),
|
||||
}));
|
||||
|
||||
describe("resolveMessagingTarget (directory fallback)", () => {
|
||||
const cfg = {} as ClawdbotConfig;
|
||||
|
||||
beforeEach(() => {
|
||||
mocks.listGroups.mockReset();
|
||||
mocks.listGroupsLive.mockReset();
|
||||
mocks.getChannelPlugin.mockReset();
|
||||
resetDirectoryCache();
|
||||
mocks.getChannelPlugin.mockReturnValue({
|
||||
directory: {
|
||||
listGroups: mocks.listGroups,
|
||||
listGroupsLive: mocks.listGroupsLive,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("uses live directory fallback and caches the result", async () => {
|
||||
const entry: ChannelDirectoryEntry = { id: "123456789", name: "support" };
|
||||
mocks.listGroups.mockResolvedValue([]);
|
||||
mocks.listGroupsLive.mockResolvedValue([entry]);
|
||||
|
||||
const first = await resolveMessagingTarget({
|
||||
cfg,
|
||||
channel: "discord",
|
||||
input: "support",
|
||||
});
|
||||
|
||||
expect(first.ok).toBe(true);
|
||||
if (first.ok) {
|
||||
expect(first.target.source).toBe("directory");
|
||||
expect(first.target.to).toBe("123456789");
|
||||
}
|
||||
expect(mocks.listGroups).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.listGroupsLive).toHaveBeenCalledTimes(1);
|
||||
|
||||
const second = await resolveMessagingTarget({
|
||||
cfg,
|
||||
channel: "discord",
|
||||
input: "support",
|
||||
});
|
||||
|
||||
expect(second.ok).toBe(true);
|
||||
expect(mocks.listGroups).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.listGroupsLive).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("skips directory lookup for direct ids", async () => {
|
||||
const result = await resolveMessagingTarget({
|
||||
cfg,
|
||||
channel: "discord",
|
||||
input: "123456789",
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) {
|
||||
expect(result.target.source).toBe("normalized");
|
||||
expect(result.target.to).toBe("123456789");
|
||||
}
|
||||
expect(mocks.listGroups).not.toHaveBeenCalled();
|
||||
expect(mocks.listGroupsLive).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user