mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 08:41:23 +00:00
test: add fetch mock helper and reaction coverage
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { withFetchPreconnect } from "../test-utils/fetch-mock.js";
|
||||
import { fetchDiscord } from "./api.js";
|
||||
|
||||
function jsonResponse(body: unknown, status = 200) {
|
||||
@@ -7,7 +8,7 @@ function jsonResponse(body: unknown, status = 200) {
|
||||
|
||||
describe("fetchDiscord", () => {
|
||||
it("formats rate limit payloads without raw JSON", async () => {
|
||||
const fetcher = async () =>
|
||||
const fetcher = withFetchPreconnect(async () =>
|
||||
jsonResponse(
|
||||
{
|
||||
message: "You are being rate limited.",
|
||||
@@ -15,11 +16,12 @@ describe("fetchDiscord", () => {
|
||||
global: false,
|
||||
},
|
||||
429,
|
||||
);
|
||||
),
|
||||
);
|
||||
|
||||
let error: unknown;
|
||||
try {
|
||||
await fetchDiscord("/users/@me/guilds", "test", fetcher as typeof fetch, {
|
||||
await fetchDiscord("/users/@me/guilds", "test", fetcher, {
|
||||
retry: { attempts: 1 },
|
||||
});
|
||||
} catch (err) {
|
||||
@@ -35,9 +37,9 @@ describe("fetchDiscord", () => {
|
||||
});
|
||||
|
||||
it("preserves non-JSON error text", async () => {
|
||||
const fetcher = async () => new Response("Not Found", { status: 404 });
|
||||
const fetcher = withFetchPreconnect(async () => new Response("Not Found", { status: 404 }));
|
||||
await expect(
|
||||
fetchDiscord("/users/@me/guilds", "test", fetcher as typeof fetch, {
|
||||
fetchDiscord("/users/@me/guilds", "test", fetcher, {
|
||||
retry: { attempts: 1 },
|
||||
}),
|
||||
).rejects.toThrow("Discord API /users/@me/guilds failed (404): Not Found");
|
||||
@@ -45,7 +47,7 @@ describe("fetchDiscord", () => {
|
||||
|
||||
it("retries rate limits before succeeding", async () => {
|
||||
let calls = 0;
|
||||
const fetcher = async () => {
|
||||
const fetcher = withFetchPreconnect(async () => {
|
||||
calls += 1;
|
||||
if (calls === 1) {
|
||||
return jsonResponse(
|
||||
@@ -58,12 +60,12 @@ describe("fetchDiscord", () => {
|
||||
);
|
||||
}
|
||||
return jsonResponse([{ id: "1", name: "Guild" }], 200);
|
||||
};
|
||||
});
|
||||
|
||||
const result = await fetchDiscord<Array<{ id: string; name: string }>>(
|
||||
"/users/@me/guilds",
|
||||
"test",
|
||||
fetcher as typeof fetch,
|
||||
fetcher,
|
||||
{ retry: { attempts: 2, minDelayMs: 0, maxDelayMs: 0 } },
|
||||
);
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user