mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 19:04:31 +00:00
refactor(discord): dedupe resolve channels fallback tests
This commit is contained in:
@@ -4,9 +4,11 @@ import { resolveDiscordChannelAllowlist } from "./resolve-channels.js";
|
|||||||
import { jsonResponse, urlToString } from "./test-http-helpers.js";
|
import { jsonResponse, urlToString } from "./test-http-helpers.js";
|
||||||
|
|
||||||
describe("resolveDiscordChannelAllowlist", () => {
|
describe("resolveDiscordChannelAllowlist", () => {
|
||||||
|
type DiscordChannel = { id: string; name: string; guild_id: string; type: number };
|
||||||
|
|
||||||
async function resolveWithChannelLookup(params: {
|
async function resolveWithChannelLookup(params: {
|
||||||
guilds: Array<{ id: string; name: string }>;
|
guilds: Array<{ id: string; name: string }>;
|
||||||
channel: { id: string; name: string; guild_id: string; type: number };
|
channel: DiscordChannel;
|
||||||
entry: string;
|
entry: string;
|
||||||
}) {
|
}) {
|
||||||
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
|
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
|
||||||
@@ -26,6 +28,44 @@ describe("resolveDiscordChannelAllowlist", () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function resolveGuild111Entry2024(params: {
|
||||||
|
channelLookup: () => Response;
|
||||||
|
guildChannels?: DiscordChannel[];
|
||||||
|
}) {
|
||||||
|
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
|
||||||
|
const url = urlToString(input);
|
||||||
|
if (url.endsWith("/users/@me/guilds")) {
|
||||||
|
return jsonResponse([{ id: "111", name: "Test Server" }]);
|
||||||
|
}
|
||||||
|
if (url.endsWith("/channels/2024")) {
|
||||||
|
return params.channelLookup();
|
||||||
|
}
|
||||||
|
if (url.endsWith("/guilds/111/channels")) {
|
||||||
|
return jsonResponse(
|
||||||
|
params.guildChannels ?? [
|
||||||
|
{ id: "c1", name: "2024", guild_id: "111", type: 0 },
|
||||||
|
{ id: "c2", name: "general", guild_id: "111", type: 0 },
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return new Response("not found", { status: 404 });
|
||||||
|
});
|
||||||
|
|
||||||
|
return resolveDiscordChannelAllowlist({
|
||||||
|
token: "test",
|
||||||
|
entries: ["111/2024"],
|
||||||
|
fetcher,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function expectUnresolved1112024(
|
||||||
|
res: Awaited<ReturnType<typeof resolveDiscordChannelAllowlist>>,
|
||||||
|
) {
|
||||||
|
expect(res[0]?.resolved).toBe(false);
|
||||||
|
expect(res[0]?.channelId).toBe("2024");
|
||||||
|
expect(res[0]?.guildId).toBe("111");
|
||||||
|
}
|
||||||
|
|
||||||
it("resolves guild/channel by name", async () => {
|
it("resolves guild/channel by name", async () => {
|
||||||
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
|
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
|
||||||
const url = urlToString(input);
|
const url = urlToString(input);
|
||||||
@@ -210,27 +250,8 @@ describe("resolveDiscordChannelAllowlist", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("falls back to name matching when numeric channel name is not a valid ID", async () => {
|
it("falls back to name matching when numeric channel name is not a valid ID", async () => {
|
||||||
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
|
const res = await resolveGuild111Entry2024({
|
||||||
const url = urlToString(input);
|
channelLookup: () => new Response("not found", { status: 404 }),
|
||||||
if (url.endsWith("/users/@me/guilds")) {
|
|
||||||
return jsonResponse([{ id: "111", name: "Test Server" }]);
|
|
||||||
}
|
|
||||||
if (url.endsWith("/channels/2024")) {
|
|
||||||
return new Response("not found", { status: 404 });
|
|
||||||
}
|
|
||||||
if (url.endsWith("/guilds/111/channels")) {
|
|
||||||
return jsonResponse([
|
|
||||||
{ id: "c1", name: "2024", guild_id: "111", type: 0 },
|
|
||||||
{ id: "c2", name: "general", guild_id: "111", type: 0 },
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
return new Response("not found", { status: 404 });
|
|
||||||
});
|
|
||||||
|
|
||||||
const res = await resolveDiscordChannelAllowlist({
|
|
||||||
token: "test",
|
|
||||||
entries: ["111/2024"],
|
|
||||||
fetcher,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(res[0]?.resolved).toBe(true);
|
expect(res[0]?.resolved).toBe(true);
|
||||||
@@ -240,58 +261,20 @@ describe("resolveDiscordChannelAllowlist", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("does not fall back to name matching when channel lookup returns 403", async () => {
|
it("does not fall back to name matching when channel lookup returns 403", async () => {
|
||||||
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
|
const res = await resolveGuild111Entry2024({
|
||||||
const url = urlToString(input);
|
channelLookup: () => new Response("Missing Access", { status: 403 }),
|
||||||
if (url.endsWith("/users/@me/guilds")) {
|
|
||||||
return jsonResponse([{ id: "111", name: "Test Server" }]);
|
|
||||||
}
|
|
||||||
if (url.endsWith("/channels/2024")) {
|
|
||||||
return new Response("Missing Access", { status: 403 });
|
|
||||||
}
|
|
||||||
if (url.endsWith("/guilds/111/channels")) {
|
|
||||||
return jsonResponse([
|
|
||||||
{ id: "c1", name: "2024", guild_id: "111", type: 0 },
|
|
||||||
{ id: "c2", name: "general", guild_id: "111", type: 0 },
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
return new Response("not found", { status: 404 });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const res = await resolveDiscordChannelAllowlist({
|
expectUnresolved1112024(res);
|
||||||
token: "test",
|
|
||||||
entries: ["111/2024"],
|
|
||||||
fetcher,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res[0]?.resolved).toBe(false);
|
|
||||||
expect(res[0]?.channelId).toBe("2024");
|
|
||||||
expect(res[0]?.guildId).toBe("111");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not fall back to name matching when channel payload is malformed", async () => {
|
it("does not fall back to name matching when channel payload is malformed", async () => {
|
||||||
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
|
const res = await resolveGuild111Entry2024({
|
||||||
const url = urlToString(input);
|
channelLookup: () => jsonResponse({ id: "2024", name: "unknown", type: 0 }),
|
||||||
if (url.endsWith("/users/@me/guilds")) {
|
guildChannels: [{ id: "c1", name: "2024", guild_id: "111", type: 0 }],
|
||||||
return jsonResponse([{ id: "111", name: "Test Server" }]);
|
|
||||||
}
|
|
||||||
if (url.endsWith("/channels/2024")) {
|
|
||||||
return jsonResponse({ id: "2024", name: "unknown", type: 0 });
|
|
||||||
}
|
|
||||||
if (url.endsWith("/guilds/111/channels")) {
|
|
||||||
return jsonResponse([{ id: "c1", name: "2024", guild_id: "111", type: 0 }]);
|
|
||||||
}
|
|
||||||
return new Response("not found", { status: 404 });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const res = await resolveDiscordChannelAllowlist({
|
expectUnresolved1112024(res);
|
||||||
token: "test",
|
|
||||||
entries: ["111/2024"],
|
|
||||||
fetcher,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res[0]?.resolved).toBe(false);
|
|
||||||
expect(res[0]?.channelId).toBe("2024");
|
|
||||||
expect(res[0]?.guildId).toBe("111");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("resolves guild: prefixed id as guild (not channel)", async () => {
|
it("resolves guild: prefixed id as guild (not channel)", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user