refactor(test): reuse withGatewayServer in auth/http suites

This commit is contained in:
Peter Steinberger
2026-02-15 23:06:34 +00:00
parent 99909f7bc7
commit 8ba16a894f
2 changed files with 140 additions and 129 deletions

View File

@@ -2,7 +2,13 @@ import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { HISTORY_CONTEXT_MARKER } from "../auto-reply/reply/history.js"; import { HISTORY_CONTEXT_MARKER } from "../auto-reply/reply/history.js";
import { CURRENT_MESSAGE_MARKER } from "../auto-reply/reply/mentions.js"; import { CURRENT_MESSAGE_MARKER } from "../auto-reply/reply/mentions.js";
import { emitAgentEvent } from "../infra/agent-events.js"; import { emitAgentEvent } from "../infra/agent-events.js";
import { agentCommand, getFreePort, installGatewayTestHooks, testState } from "./test-helpers.js"; import {
agentCommand,
getFreePort,
installGatewayTestHooks,
testState,
withGatewayServer,
} from "./test-helpers.js";
installGatewayTestHooks({ scope: "suite" }); installGatewayTestHooks({ scope: "suite" });
@@ -345,20 +351,14 @@ describe("OpenAI-compatible HTTP API (e2e)", () => {
}); });
it("returns 429 for repeated failed auth when gateway.auth.rateLimit is configured", async () => { it("returns 429 for repeated failed auth when gateway.auth.rateLimit is configured", async () => {
const { startGatewayServer } = await import("./server.js");
testState.gatewayAuth = { testState.gatewayAuth = {
mode: "token", mode: "token",
token: "secret", token: "secret",
rateLimit: { maxAttempts: 1, windowMs: 60_000, lockoutMs: 60_000, exemptLoopback: false }, rateLimit: { maxAttempts: 1, windowMs: 60_000, lockoutMs: 60_000, exemptLoopback: false },
// oxlint-disable-next-line typescript/no-explicit-any // oxlint-disable-next-line typescript/no-explicit-any
} as any; } as any;
const port = await getFreePort(); await withGatewayServer(
const server = await startGatewayServer(port, { async ({ port }) => {
host: "127.0.0.1",
controlUiEnabled: false,
openAiChatCompletionsEnabled: true,
});
try {
const headers = { const headers = {
"content-type": "application/json", "content-type": "application/json",
authorization: "Bearer wrong", authorization: "Bearer wrong",
@@ -382,9 +382,15 @@ describe("OpenAI-compatible HTTP API (e2e)", () => {
}); });
expect(second.status).toBe(429); expect(second.status).toBe(429);
expect(second.headers.get("retry-after")).toBeTruthy(); expect(second.headers.get("retry-after")).toBeTruthy();
} finally { },
await server.close({ reason: "rate-limit auth test done" }); {
} serverOptions: {
host: "127.0.0.1",
controlUiEnabled: false,
openAiChatCompletionsEnabled: true,
},
},
);
}); });
it("streams SSE chunks when stream=true", async () => { it("streams SSE chunks when stream=true", async () => {

View File

@@ -14,6 +14,7 @@ import {
startServerWithClient, startServerWithClient,
testTailscaleWhois, testTailscaleWhois,
testState, testState,
withGatewayServer,
} from "./test-helpers.js"; } from "./test-helpers.js";
installGatewayTestHooks({ scope: "suite" }); installGatewayTestHooks({ scope: "suite" });
@@ -596,8 +597,8 @@ describe("gateway server auth/connect", () => {
} as any); } as any);
const prevToken = process.env.OPENCLAW_GATEWAY_TOKEN; const prevToken = process.env.OPENCLAW_GATEWAY_TOKEN;
process.env.OPENCLAW_GATEWAY_TOKEN = "secret"; process.env.OPENCLAW_GATEWAY_TOKEN = "secret";
const port = await getFreePort(); try {
const server = await startGatewayServer(port); await withGatewayServer(async ({ port }) => {
const ws = new WebSocket(`ws://127.0.0.1:${port}`, { const ws = new WebSocket(`ws://127.0.0.1:${port}`, {
headers: { headers: {
origin: "https://localhost", origin: "https://localhost",
@@ -647,12 +648,14 @@ describe("gateway server auth/connect", () => {
}); });
expect(res.ok).toBe(true); expect(res.ok).toBe(true);
ws.close(); ws.close();
await server.close(); });
} finally {
if (prevToken === undefined) { if (prevToken === undefined) {
delete process.env.OPENCLAW_GATEWAY_TOKEN; delete process.env.OPENCLAW_GATEWAY_TOKEN;
} else { } else {
process.env.OPENCLAW_GATEWAY_TOKEN = prevToken; process.env.OPENCLAW_GATEWAY_TOKEN = prevToken;
} }
}
}); });
test("allows control ui with stale device identity when device auth is disabled", async () => { test("allows control ui with stale device identity when device auth is disabled", async () => {
@@ -660,8 +663,8 @@ describe("gateway server auth/connect", () => {
testState.gatewayAuth = { mode: "token", token: "secret" }; testState.gatewayAuth = { mode: "token", token: "secret" };
const prevToken = process.env.OPENCLAW_GATEWAY_TOKEN; const prevToken = process.env.OPENCLAW_GATEWAY_TOKEN;
process.env.OPENCLAW_GATEWAY_TOKEN = "secret"; process.env.OPENCLAW_GATEWAY_TOKEN = "secret";
const port = await getFreePort(); try {
const server = await startGatewayServer(port); await withGatewayServer(async ({ port }) => {
const ws = await openWs(port, { origin: originForPort(port) }); const ws = await openWs(port, { origin: originForPort(port) });
const { loadOrCreateDeviceIdentity, publicKeyRawBase64UrlFromPem, signDevicePayload } = const { loadOrCreateDeviceIdentity, publicKeyRawBase64UrlFromPem, signDevicePayload } =
await import("../infra/device-identity.js"); await import("../infra/device-identity.js");
@@ -695,12 +698,14 @@ describe("gateway server auth/connect", () => {
expect(res.ok).toBe(true); expect(res.ok).toBe(true);
expect((res.payload as { auth?: unknown } | undefined)?.auth).toBeUndefined(); expect((res.payload as { auth?: unknown } | undefined)?.auth).toBeUndefined();
ws.close(); ws.close();
await server.close(); });
} finally {
if (prevToken === undefined) { if (prevToken === undefined) {
delete process.env.OPENCLAW_GATEWAY_TOKEN; delete process.env.OPENCLAW_GATEWAY_TOKEN;
} else { } else {
process.env.OPENCLAW_GATEWAY_TOKEN = prevToken; process.env.OPENCLAW_GATEWAY_TOKEN = prevToken;
} }
}
}); });
test("accepts device token auth for paired device", async () => { test("accepts device token auth for paired device", async () => {