mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 02:02:45 +00:00
tests: cover Discord username resolution
This commit is contained in:
@@ -1,7 +1,13 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import type { ClawdbotConfig } from "../config/config.js";
|
||||||
import { normalizeDiscordMessagingTarget } from "../channels/plugins/normalize/discord.js";
|
import { normalizeDiscordMessagingTarget } from "../channels/plugins/normalize/discord.js";
|
||||||
import { parseDiscordTarget, resolveDiscordChannelId } from "./targets.js";
|
import { listDiscordDirectoryPeersLive } from "./directory-live.js";
|
||||||
|
import { parseDiscordTarget, resolveDiscordChannelId, resolveDiscordTarget } from "./targets.js";
|
||||||
|
|
||||||
|
vi.mock("./directory-live.js", () => ({
|
||||||
|
listDiscordDirectoryPeersLive: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
describe("parseDiscordTarget", () => {
|
describe("parseDiscordTarget", () => {
|
||||||
it("parses user mention and prefixes", () => {
|
it("parses user mention and prefixes", () => {
|
||||||
@@ -68,6 +74,38 @@ describe("resolveDiscordChannelId", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("resolveDiscordTarget", () => {
|
||||||
|
const cfg = { channels: { discord: {} } } as ClawdbotConfig;
|
||||||
|
const listPeers = vi.mocked(listDiscordDirectoryPeersLive);
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
listPeers.mockReset();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns a resolved user for usernames", async () => {
|
||||||
|
listPeers.mockResolvedValueOnce([{ kind: "user", id: "user:999", name: "Jane" } as const]);
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
resolveDiscordTarget("jane", { cfg, accountId: "default" }),
|
||||||
|
).resolves.toMatchObject({ kind: "user", id: "999", normalized: "user:999" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("falls back to parsing when lookup misses", async () => {
|
||||||
|
listPeers.mockResolvedValueOnce([]);
|
||||||
|
await expect(
|
||||||
|
resolveDiscordTarget("general", { cfg, accountId: "default" }),
|
||||||
|
).resolves.toMatchObject({ kind: "channel", id: "general" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not call directory lookup for explicit user ids", async () => {
|
||||||
|
listPeers.mockResolvedValueOnce([]);
|
||||||
|
await expect(
|
||||||
|
resolveDiscordTarget("user:123", { cfg, accountId: "default" }),
|
||||||
|
).resolves.toMatchObject({ kind: "user", id: "123" });
|
||||||
|
expect(listPeers).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("normalizeDiscordMessagingTarget", () => {
|
describe("normalizeDiscordMessagingTarget", () => {
|
||||||
it("defaults raw numeric ids to channels", () => {
|
it("defaults raw numeric ids to channels", () => {
|
||||||
expect(normalizeDiscordMessagingTarget("123")).toBe("channel:123");
|
expect(normalizeDiscordMessagingTarget("123")).toBe("channel:123");
|
||||||
|
|||||||
Reference in New Issue
Block a user