mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 12:11:23 +00:00
refactor(test): dedupe agent and discord test fixtures
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user