mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-03 09:04:04 +00:00
test(discord): isolate timer-sensitive request tests
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { withFetchPreconnect } from "openclaw/plugin-sdk/test-env";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
fetchDiscordApplicationId,
|
||||
fetchDiscordApplicationSummary,
|
||||
@@ -8,6 +8,14 @@ import {
|
||||
import { jsonResponse } from "./test-http-helpers.js";
|
||||
|
||||
describe("resolveDiscordPrivilegedIntentsFromFlags", () => {
|
||||
beforeEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("reports disabled when no bits set", () => {
|
||||
expect(resolveDiscordPrivilegedIntentsFromFlags(0)).toEqual({
|
||||
presence: "disabled",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
createAbortableFetchMock,
|
||||
createJsonResponse,
|
||||
@@ -21,6 +21,14 @@ async function expectAbortError(promise: Promise<unknown>) {
|
||||
}
|
||||
|
||||
describe("createDiscordRequestClient", () => {
|
||||
beforeEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("preserves the REST client's abort signal for proxied fetch calls", async () => {
|
||||
const fetchSpy = vi.fn(async (_input: string | URL | Request, init?: RequestInit) => {
|
||||
if (!(init?.signal instanceof AbortSignal)) {
|
||||
@@ -41,6 +49,7 @@ describe("createDiscordRequestClient", () => {
|
||||
|
||||
it("lets the REST client abort hanging proxied requests after its timeout", async () => {
|
||||
const { fetch: fetchSpy } = createAbortableFetchMock();
|
||||
vi.useFakeTimers();
|
||||
|
||||
const client = createDiscordRequestClient("Bot test-token", {
|
||||
fetch: fetchSpy as never,
|
||||
@@ -48,7 +57,12 @@ describe("createDiscordRequestClient", () => {
|
||||
timeout: 20,
|
||||
});
|
||||
|
||||
await expectAbortError(client.get("/channels/123/messages"));
|
||||
const request = client.get("/channels/123/messages");
|
||||
const abortExpectation = expectAbortError(request);
|
||||
await Promise.resolve();
|
||||
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
||||
await vi.advanceTimersByTimeAsync(20);
|
||||
await abortExpectation;
|
||||
}, 1_000);
|
||||
|
||||
it("lets abortAllRequests cancel active proxied fetches", async () => {
|
||||
|
||||
Reference in New Issue
Block a user