Skip to content

Commit 5dc4531

Browse files
committed
test(discord): isolate timer-sensitive request tests
1 parent 9518d1f commit 5dc4531

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

extensions/discord/src/probe.intents.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { withFetchPreconnect } from "openclaw/plugin-sdk/test-env";
2-
import { describe, expect, it } from "vitest";
2+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
33
import {
44
fetchDiscordApplicationId,
55
fetchDiscordApplicationSummary,
@@ -8,6 +8,14 @@ import {
88
import { jsonResponse } from "./test-http-helpers.js";
99

1010
describe("resolveDiscordPrivilegedIntentsFromFlags", () => {
11+
beforeEach(() => {
12+
vi.useRealTimers();
13+
});
14+
15+
afterEach(() => {
16+
vi.useRealTimers();
17+
});
18+
1119
it("reports disabled when no bits set", () => {
1220
expect(resolveDiscordPrivilegedIntentsFromFlags(0)).toEqual({
1321
presence: "disabled",

extensions/discord/src/proxy-request-client.test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it, vi } from "vitest";
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
22
import {
33
createAbortableFetchMock,
44
createJsonResponse,
@@ -21,6 +21,14 @@ async function expectAbortError(promise: Promise<unknown>) {
2121
}
2222

2323
describe("createDiscordRequestClient", () => {
24+
beforeEach(() => {
25+
vi.useRealTimers();
26+
});
27+
28+
afterEach(() => {
29+
vi.useRealTimers();
30+
});
31+
2432
it("preserves the REST client's abort signal for proxied fetch calls", async () => {
2533
const fetchSpy = vi.fn(async (_input: string | URL | Request, init?: RequestInit) => {
2634
if (!(init?.signal instanceof AbortSignal)) {
@@ -41,14 +49,20 @@ describe("createDiscordRequestClient", () => {
4149

4250
it("lets the REST client abort hanging proxied requests after its timeout", async () => {
4351
const { fetch: fetchSpy } = createAbortableFetchMock();
52+
vi.useFakeTimers();
4453

4554
const client = createDiscordRequestClient("Bot test-token", {
4655
fetch: fetchSpy as never,
4756
queueRequests: false,
4857
timeout: 20,
4958
});
5059

51-
await expectAbortError(client.get("/channels/123/messages"));
60+
const request = client.get("/channels/123/messages");
61+
const abortExpectation = expectAbortError(request);
62+
await Promise.resolve();
63+
expect(fetchSpy).toHaveBeenCalledTimes(1);
64+
await vi.advanceTimersByTimeAsync(20);
65+
await abortExpectation;
5266
}, 1_000);
5367

5468
it("lets abortAllRequests cancel active proxied fetches", async () => {

0 commit comments

Comments
 (0)