test: harden no-isolate timer and undici seams

This commit is contained in:
Peter Steinberger
2026-03-22 12:03:24 -07:00
parent 1b1167012f
commit 1822db9227
21 changed files with 317 additions and 142 deletions

View File

@@ -4,6 +4,7 @@ import {
shouldRetryTelegramTransportFallback,
} from "../../extensions/telegram/src/fetch.js";
import { makeProxyFetch } from "../../extensions/telegram/src/proxy.js";
import { TEST_UNDICI_RUNTIME_DEPS_KEY } from "../infra/net/undici-runtime.js";
import { fetchRemoteMedia } from "./fetch.js";
const undiciMocks = vi.hoisted(() => {
@@ -35,6 +36,11 @@ describe("fetchRemoteMedia telegram network policy", () => {
undiciMocks.agentCtor.mockClear();
undiciMocks.envHttpProxyAgentCtor.mockClear();
undiciMocks.proxyAgentCtor.mockClear();
(globalThis as Record<string, unknown>)[TEST_UNDICI_RUNTIME_DEPS_KEY] = {
Agent: undiciMocks.agentCtor,
EnvHttpProxyAgent: undiciMocks.envHttpProxyAgentCtor,
ProxyAgent: undiciMocks.proxyAgentCtor,
};
});
function createTelegramFetchFailedError(code: string): Error {
@@ -44,6 +50,7 @@ describe("fetchRemoteMedia telegram network policy", () => {
}
afterEach(() => {
Reflect.deleteProperty(globalThis as object, TEST_UNDICI_RUNTIME_DEPS_KEY);
vi.unstubAllEnvs();
});

View File

@@ -1,8 +1,9 @@
import fs from "node:fs/promises";
import { createRequire } from "node:module";
import type { AddressInfo } from "node:net";
import os from "node:os";
import path from "node:path";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
readFileWithinRoot: vi.fn(),
@@ -28,19 +29,32 @@ vi.mock("./store.js", async (importOriginal) => {
};
});
const { SafeOpenError } = await import("../infra/fs-safe.js");
const { startMediaServer } = await import("./server.js");
let SafeOpenError: typeof import("../infra/fs-safe.js").SafeOpenError;
let startMediaServer: typeof import("./server.js").startMediaServer;
let realFetch: typeof import("undici").fetch;
describe("media server outside-workspace mapping", () => {
let server: Awaited<ReturnType<typeof startMediaServer>>;
let port = 0;
beforeAll(async () => {
vi.useRealTimers();
vi.doUnmock("undici");
vi.resetModules();
const require = createRequire(import.meta.url);
({ SafeOpenError } = await import("../infra/fs-safe.js"));
({ startMediaServer } = await import("./server.js"));
({ fetch: realFetch } = require("undici") as typeof import("undici"));
mediaDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-media-outside-workspace-"));
server = await startMediaServer(0, 1_000);
port = (server.address() as AddressInfo).port;
});
beforeEach(() => {
mocks.readFileWithinRoot.mockReset();
mocks.cleanOldMedia.mockClear();
});
afterAll(async () => {
await new Promise((resolve) => server.close(resolve));
await fs.rm(mediaDir, { recursive: true, force: true });
@@ -52,7 +66,7 @@ describe("media server outside-workspace mapping", () => {
new SafeOpenError("outside-workspace", "file is outside workspace root"),
);
const response = await fetch(`http://127.0.0.1:${port}/media/ok-id`);
const response = await realFetch(`http://127.0.0.1:${port}/media/ok-id`);
expect(response.status).toBe(400);
expect(await response.text()).toBe("file is outside workspace root");
});

View File

@@ -1,4 +1,5 @@
import fs from "node:fs/promises";
import { createRequire } from "node:module";
import type { AddressInfo } from "node:net";
import os from "node:os";
import path from "node:path";
@@ -16,8 +17,9 @@ vi.mock("./store.js", async (importOriginal) => {
};
});
const { startMediaServer } = await import("./server.js");
const { MEDIA_MAX_BYTES } = await import("./store.js");
let startMediaServer: typeof import("./server.js").startMediaServer;
let MEDIA_MAX_BYTES: typeof import("./store.js").MEDIA_MAX_BYTES;
let realFetch: typeof import("undici").fetch;
async function waitForFileRemoval(filePath: string, maxTicks = 1000) {
for (let tick = 0; tick < maxTicks; tick += 1) {
@@ -46,6 +48,13 @@ describe("media server", () => {
}
beforeAll(async () => {
vi.useRealTimers();
vi.doUnmock("undici");
vi.resetModules();
const require = createRequire(import.meta.url);
({ startMediaServer } = await import("./server.js"));
({ MEDIA_MAX_BYTES } = await import("./store.js"));
({ fetch: realFetch } = require("undici") as typeof import("undici"));
MEDIA_DIR = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-media-test-"));
server = await startMediaServer(0, 1_000);
port = (server.address() as AddressInfo).port;
@@ -59,7 +68,7 @@ describe("media server", () => {
it("serves media and cleans up after send", async () => {
const file = await writeMediaFile("file1", "hello");
const res = await fetch(mediaUrl("file1"));
const res = await realFetch(mediaUrl("file1"));
expect(res.status).toBe(200);
expect(res.headers.get("x-content-type-options")).toBe("nosniff");
expect(await res.text()).toBe("hello");
@@ -70,7 +79,7 @@ describe("media server", () => {
const file = await writeMediaFile("old", "stale");
const past = Date.now() - 10_000;
await fs.utimes(file, past / 1000, past / 1000);
const res = await fetch(mediaUrl("old"));
const res = await realFetch(mediaUrl("old"));
expect(res.status).toBe(410);
await expect(fs.stat(file)).rejects.toThrow();
});
@@ -98,7 +107,7 @@ describe("media server", () => {
},
] as const)("$testName", async (testCase) => {
await testCase.setup?.();
const res = await fetch(mediaUrl(testCase.mediaPath));
const res = await realFetch(mediaUrl(testCase.mediaPath));
expect(res.status).toBe(400);
expect(await res.text()).toBe("invalid path");
});
@@ -106,25 +115,25 @@ describe("media server", () => {
it("rejects oversized media files", async () => {
const file = await writeMediaFile("big", "");
await fs.truncate(file, MEDIA_MAX_BYTES + 1);
const res = await fetch(mediaUrl("big"));
const res = await realFetch(mediaUrl("big"));
expect(res.status).toBe(413);
expect(await res.text()).toBe("too large");
});
it("returns not found for missing media IDs", async () => {
const res = await fetch(mediaUrl("missing-file"));
const res = await realFetch(mediaUrl("missing-file"));
expect(res.status).toBe(404);
expect(res.headers.get("x-content-type-options")).toBe("nosniff");
expect(await res.text()).toBe("not found");
});
it("returns 404 when route param is missing (dot path)", async () => {
const res = await fetch(mediaUrl("."));
const res = await realFetch(mediaUrl("."));
expect(res.status).toBe(404);
});
it("rejects overlong media id", async () => {
const res = await fetch(mediaUrl(`${"a".repeat(201)}.txt`));
const res = await realFetch(mediaUrl(`${"a".repeat(201)}.txt`));
expect(res.status).toBe(400);
expect(await res.text()).toBe("invalid path");
});