test: speed up non-interactive gateway onboarding suite

This commit is contained in:
Peter Steinberger
2026-02-22 13:08:34 +00:00
parent 9e868dcf5a
commit d236ded43f

View File

@@ -4,7 +4,6 @@ import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import type { GatewayAuthConfig } from "../config/config.js"; import type { GatewayAuthConfig } from "../config/config.js";
import { makeTempWorkspace } from "../test-helpers/workspace.js"; import { makeTempWorkspace } from "../test-helpers/workspace.js";
import { captureEnv } from "../test-utils/env.js"; import { captureEnv } from "../test-utils/env.js";
import { getFreePortBlockWithPermissionFallback } from "../test-utils/ports.js";
import { import {
createThrowingRuntime, createThrowingRuntime,
readJsonFile, readJsonFile,
@@ -18,6 +17,7 @@ const gatewayClientCalls: Array<{
onHelloOk?: () => void; onHelloOk?: () => void;
onClose?: (code: number, reason: string) => void; onClose?: (code: number, reason: string) => void;
}> = []; }> = [];
const ensureWorkspaceAndSessionsMock = vi.fn(async (..._args: unknown[]) => {});
vi.mock("../gateway/client.js", () => ({ vi.mock("../gateway/client.js", () => ({
GatewayClient: class { GatewayClient: class {
@@ -46,18 +46,16 @@ vi.mock("../gateway/client.js", () => ({
}, },
})); }));
async function getFreePort(): Promise<number> { vi.mock("./onboard-helpers.js", async (importOriginal) => {
return await getFreePortBlockWithPermissionFallback({ const actual = await importOriginal<typeof import("./onboard-helpers.js")>();
offsets: [0], return {
fallbackBase: 30_000, ...actual,
}); ensureWorkspaceAndSessions: ensureWorkspaceAndSessionsMock,
} };
});
async function getFreeGatewayPort(): Promise<number> { function getPseudoPort(base: number): number {
return await getFreePortBlockWithPermissionFallback({ return base + (process.pid % 1000);
offsets: [0, 1, 2, 4],
fallbackBase: 40_000,
});
} }
const runtime = createThrowingRuntime(); const runtime = createThrowingRuntime();
@@ -173,7 +171,7 @@ describe("onboard (non-interactive): gateway and remote auth", () => {
it("writes gateway.remote url/token and callGateway uses them", async () => { it("writes gateway.remote url/token and callGateway uses them", async () => {
await withStateDir("state-remote-", async () => { await withStateDir("state-remote-", async () => {
const port = await getFreePort(); const port = getPseudoPort(30_000);
const token = "tok_remote_123"; const token = "tok_remote_123";
await runNonInteractiveOnboarding( await runNonInteractiveOnboarding(
{ {
@@ -215,7 +213,7 @@ describe("onboard (non-interactive): gateway and remote auth", () => {
process.env.OPENCLAW_STATE_DIR = stateDir; process.env.OPENCLAW_STATE_DIR = stateDir;
process.env.OPENCLAW_CONFIG_PATH = path.join(stateDir, "openclaw.json"); process.env.OPENCLAW_CONFIG_PATH = path.join(stateDir, "openclaw.json");
const port = await getFreeGatewayPort(); const port = getPseudoPort(40_000);
const workspace = path.join(stateDir, "openclaw"); const workspace = path.join(stateDir, "openclaw");
await runNonInteractiveOnboarding( await runNonInteractiveOnboarding(