refactor(test): dedupe agent and discord test fixtures

This commit is contained in:
Peter Steinberger
2026-02-22 20:01:43 +00:00
parent 5547a2275c
commit 3c75bc0e41
26 changed files with 632 additions and 737 deletions

View File

@@ -1,17 +1,7 @@
import { describe, expect, it } from "vitest";
import { withFetchPreconnect } from "../test-utils/fetch-mock.js";
import { resolveDiscordUserAllowlist } from "./resolve-users.js";
function jsonResponse(body: unknown) {
return new Response(JSON.stringify(body), { status: 200 });
}
const urlToString = (url: Request | URL | string): string => {
if (typeof url === "string") {
return url;
}
return "url" in url ? url.url : String(url);
};
import { jsonResponse, urlToString } from "./test-http-helpers.js";
function createGuildListProbeFetcher() {
let guildsCalled = false;
@@ -29,6 +19,16 @@ function createGuildListProbeFetcher() {
};
}
function createGuildsForbiddenFetcher() {
return withFetchPreconnect(async (input: RequestInfo | URL) => {
const url = urlToString(input);
if (url.endsWith("/users/@me/guilds")) {
throw new Error("Forbidden: Missing Access");
}
return new Response("not found", { status: 404 });
});
}
describe("resolveDiscordUserAllowlist", () => {
it("resolves plain user ids without calling listGuilds", async () => {
const { fetcher, wasGuildsCalled } = createGuildListProbeFetcher();
@@ -84,13 +84,7 @@ describe("resolveDiscordUserAllowlist", () => {
});
it("resolves user ids even when listGuilds would fail", async () => {
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
const url = urlToString(input);
if (url.endsWith("/users/@me/guilds")) {
throw new Error("Forbidden: Missing Access");
}
return new Response("not found", { status: 404 });
});
const fetcher = createGuildsForbiddenFetcher();
// Before the fix, this would throw because listGuilds() was called eagerly
const results = await resolveDiscordUserAllowlist({
@@ -177,13 +171,7 @@ describe("resolveDiscordUserAllowlist", () => {
});
it("handles mixed ids and usernames — ids resolve even if guilds fail", async () => {
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
const url = urlToString(input);
if (url.endsWith("/users/@me/guilds")) {
throw new Error("Forbidden: Missing Access");
}
return new Response("not found", { status: 404 });
});
const fetcher = createGuildsForbiddenFetcher();
// IDs should succeed, username should fail (listGuilds throws)
await expect(