Files
openclaw/src/discord/audit.test.ts
Josh Avant 0e4245063f CLI: make read-only SecretRef status flows degrade safely (#37023)
* CLI: add read-only SecretRef inspection

* CLI: fix read-only SecretRef status regressions

* CLI: preserve read-only SecretRef status fallbacks

* Docs: document read-only channel inspection hook

* CLI: preserve audit coverage for read-only SecretRefs

* CLI: fix read-only status account selection

* CLI: fix targeted gateway fallback analysis

* CLI: fix Slack HTTP read-only inspection

* CLI: align audit credential status checks

* CLI: restore Telegram read-only fallback semantics
2026-03-05 23:07:13 -06:00

137 lines
3.9 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
vi.mock("./send.js", () => ({
fetchChannelPermissionsDiscord: vi.fn(),
}));
describe("discord audit", () => {
it("collects numeric channel ids and counts unresolved keys", async () => {
const { collectDiscordAuditChannelIds, auditDiscordChannelPermissions } =
await import("./audit.js");
const { fetchChannelPermissionsDiscord } = await import("./send.js");
const cfg = {
channels: {
discord: {
enabled: true,
token: "t",
groupPolicy: "allowlist",
guilds: {
"123": {
channels: {
"111": { allow: true },
general: { allow: true },
"222": { allow: false },
},
},
},
},
},
} as unknown as import("../config/config.js").OpenClawConfig;
const collected = collectDiscordAuditChannelIds({
cfg,
accountId: "default",
});
expect(collected.channelIds).toEqual(["111"]);
expect(collected.unresolvedChannels).toBe(1);
(fetchChannelPermissionsDiscord as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce({
channelId: "111",
permissions: ["ViewChannel"],
raw: "0",
isDm: false,
});
const audit = await auditDiscordChannelPermissions({
token: "t",
accountId: "default",
channelIds: collected.channelIds,
timeoutMs: 1000,
});
expect(audit.ok).toBe(false);
expect(audit.channels[0]?.channelId).toBe("111");
expect(audit.channels[0]?.missing).toContain("SendMessages");
});
it("does not count '*' wildcard key as unresolved channel", async () => {
const { collectDiscordAuditChannelIds } = await import("./audit.js");
const cfg = {
channels: {
discord: {
enabled: true,
token: "t",
groupPolicy: "allowlist",
guilds: {
"123": {
channels: {
"111": { allow: true },
"*": { allow: true },
},
},
},
},
},
} as unknown as import("../config/config.js").OpenClawConfig;
const collected = collectDiscordAuditChannelIds({ cfg, accountId: "default" });
expect(collected.channelIds).toEqual(["111"]);
expect(collected.unresolvedChannels).toBe(0);
});
it("handles guild with only '*' wildcard and no numeric channel ids", async () => {
const { collectDiscordAuditChannelIds } = await import("./audit.js");
const cfg = {
channels: {
discord: {
enabled: true,
token: "t",
groupPolicy: "allowlist",
guilds: {
"123": {
channels: {
"*": { allow: true },
},
},
},
},
},
} as unknown as import("../config/config.js").OpenClawConfig;
const collected = collectDiscordAuditChannelIds({ cfg, accountId: "default" });
expect(collected.channelIds).toEqual([]);
expect(collected.unresolvedChannels).toBe(0);
});
it("collects audit channel ids without resolving SecretRef-backed Discord tokens", async () => {
const { collectDiscordAuditChannelIds } = await import("./audit.js");
const cfg = {
channels: {
discord: {
enabled: true,
token: {
source: "env",
provider: "default",
id: "DISCORD_BOT_TOKEN",
},
guilds: {
"123": {
channels: {
"111": { allow: true },
general: { allow: true },
},
},
},
},
},
} as unknown as import("../config/config.js").OpenClawConfig;
const collected = collectDiscordAuditChannelIds({ cfg, accountId: "default" });
expect(collected.channelIds).toEqual(["111"]);
expect(collected.unresolvedChannels).toBe(1);
});
});