test(ci): harden proxy-sensitive and timeout unit tests

This commit is contained in:
Vincent Koc 2026-04-04 02:11:45 +09:00
parent b871707628
commit f575bc2bfe
3 changed files with 40 additions and 18 deletions

View File

@ -803,9 +803,22 @@ describe("update-cli", () => {
platformSpy.mockRestore();
expect(runGatewayUpdate).not.toHaveBeenCalled();
expect(runCommandWithTimeout).toHaveBeenCalledWith(
[brewNpm, "i", "-g", "openclaw@latest", "--no-fund", "--no-audit", "--loglevel=error"],
expect.any(Object),
const installCall = vi
.mocked(runCommandWithTimeout)
.mock.calls.find(
([argv]) =>
Array.isArray(argv) &&
path.normalize(String(argv[0] ?? "")) === path.normalize(brewNpm) &&
argv[1] === "i" &&
argv[2] === "-g" &&
argv[3] === "openclaw@latest",
);
expect(installCall).toBeDefined();
expect(installCall?.[1]).toEqual(
expect.objectContaining({
timeoutMs: expect.any(Number),
}),
);
});

View File

@ -4,6 +4,7 @@ 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 { withEnvAsync } from "../test-utils/env.js";
let MEDIA_DIR = "";
const cleanOldMedia = vi.fn().mockResolvedValue(undefined);
@ -20,6 +21,16 @@ vi.mock("./store.js", async (importOriginal) => {
let startMediaServer: typeof import("./server.js").startMediaServer;
let MEDIA_MAX_BYTES: typeof import("./store.js").MEDIA_MAX_BYTES;
let realFetch: typeof import("undici").fetch;
const LOOPBACK_FETCH_ENV = {
HTTP_PROXY: undefined,
HTTPS_PROXY: undefined,
ALL_PROXY: undefined,
http_proxy: undefined,
https_proxy: undefined,
all_proxy: undefined,
NO_PROXY: "127.0.0.1,localhost",
no_proxy: "127.0.0.1,localhost",
} as const;
async function waitForFileRemoval(filePath: string, maxTicks = 1000) {
for (let tick = 0; tick < maxTicks; tick += 1) {
@ -77,7 +88,7 @@ describe("media server", () => {
}) {
const file = await writeMediaFile(params.id, params.contents);
await params.mutateFile?.(file);
const res = await realFetch(mediaUrl(params.id));
const res = await withEnvAsync(LOOPBACK_FETCH_ENV, () => realFetch(mediaUrl(params.id)));
expectFetchedResponse(res, { status: params.expectedStatus });
if (params.expectedBody !== undefined) {
expect(await res.text()).toBe(params.expectedBody);
@ -93,7 +104,7 @@ describe("media server", () => {
setup?: () => Promise<void>;
}) {
await params.setup?.();
const res = await realFetch(mediaUrl(params.mediaPath));
const res = await withEnvAsync(LOOPBACK_FETCH_ENV, () => realFetch(mediaUrl(params.mediaPath)));
expectFetchedResponse(res, {
status: params.expectedStatus,
...(params.expectedNoSniff ? { noSniff: true } : {}),

View File

@ -12,6 +12,10 @@ import {
} from "./exec.js";
describe("runCommandWithTimeout", () => {
function createSilentIdleArgv(): string[] {
return [process.execPath, "-e", "setInterval(() => {}, 1_000)"];
}
beforeEach(() => {
vi.useRealTimers();
});
@ -89,13 +93,10 @@ describe("runCommandWithTimeout", () => {
"kills command when no output timeout elapses",
{ timeout: 15_000 },
async () => {
const result = await runCommandWithTimeout(
[process.execPath, "-e", "setTimeout(() => {}, 5_000)"],
{
timeoutMs: 2_000,
noOutputTimeoutMs: 200,
},
);
const result = await runCommandWithTimeout(createSilentIdleArgv(), {
timeoutMs: 2_000,
noOutputTimeoutMs: 200,
});
expect(result.termination).toBe("no-output-timeout");
expect(result.noOutputTimedOut).toBe(true);
@ -107,12 +108,9 @@ describe("runCommandWithTimeout", () => {
"reports global timeout termination when overall timeout elapses",
{ timeout: 15_000 },
async () => {
const result = await runCommandWithTimeout(
[process.execPath, "-e", "setTimeout(() => {}, 5_000)"],
{
timeoutMs: 200,
},
);
const result = await runCommandWithTimeout(createSilentIdleArgv(), {
timeoutMs: 200,
});
expect(result.termination).toBe("timeout");
expect(result.noOutputTimedOut).toBe(false);