test: add fetch mock helper and reaction coverage

This commit is contained in:
Sebastian
2026-02-17 09:01:30 -05:00
parent 0e023e300e
commit cc359d338e
28 changed files with 193 additions and 106 deletions

View File

@@ -1,4 +1,5 @@
import { describe, expect, it } from "vitest";
import { withFetchPreconnect } from "../test-utils/fetch-mock.js";
import { resolveDiscordChannelAllowlist } from "./resolve-channels.js";
function jsonResponse(body: unknown) {
@@ -14,7 +15,7 @@ const urlToString = (url: Request | URL | string): string => {
describe("resolveDiscordChannelAllowlist", () => {
it("resolves guild/channel by name", async () => {
const fetcher = async (input: RequestInfo | URL) => {
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
const url = urlToString(input);
if (url.endsWith("/users/@me/guilds")) {
return jsonResponse([{ id: "g1", name: "My Guild" }]);
@@ -26,7 +27,7 @@ describe("resolveDiscordChannelAllowlist", () => {
]);
}
return new Response("not found", { status: 404 });
};
});
const res = await resolveDiscordChannelAllowlist({
token: "test",
@@ -40,7 +41,7 @@ describe("resolveDiscordChannelAllowlist", () => {
});
it("resolves channel id to guild", async () => {
const fetcher = async (input: RequestInfo | URL) => {
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
const url = urlToString(input);
if (url.endsWith("/users/@me/guilds")) {
return jsonResponse([{ id: "g1", name: "Guild One" }]);
@@ -49,7 +50,7 @@ describe("resolveDiscordChannelAllowlist", () => {
return jsonResponse({ id: "123", name: "general", guild_id: "g1", type: 0 });
}
return new Response("not found", { status: 404 });
};
});
const res = await resolveDiscordChannelAllowlist({
token: "test",
@@ -63,7 +64,7 @@ describe("resolveDiscordChannelAllowlist", () => {
});
it("resolves guild: prefixed id as guild (not channel)", async () => {
const fetcher = async (input: RequestInfo | URL) => {
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
const url = urlToString(input);
if (url.endsWith("/users/@me/guilds")) {
return jsonResponse([{ id: "111222333444555666", name: "Guild One" }]);
@@ -73,7 +74,7 @@ describe("resolveDiscordChannelAllowlist", () => {
throw new Error("guild id was incorrectly routed to /channels/");
}
return new Response("not found", { status: 404 });
};
});
const res = await resolveDiscordChannelAllowlist({
token: "test",
@@ -90,7 +91,7 @@ describe("resolveDiscordChannelAllowlist", () => {
// Demonstrates why provider.ts must prefix guild-only entries with "guild:"
// In reality, Discord returns 404 when a guild ID is sent to /channels/<guildId>,
// which causes fetchDiscord to throw and the entire resolver to crash.
const fetcher = async (input: RequestInfo | URL) => {
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
const url = urlToString(input);
if (url.endsWith("/users/@me/guilds")) {
return jsonResponse([{ id: "999", name: "My Server" }]);
@@ -100,7 +101,7 @@ describe("resolveDiscordChannelAllowlist", () => {
return new Response(JSON.stringify({ message: "Unknown Channel" }), { status: 404 });
}
return new Response("not found", { status: 404 });
};
});
// Without the guild: prefix, a bare numeric string hits /channels/999 → 404 → throws
await expect(