test: stabilize lan auto-token onboarding
This commit is contained in:
@@ -7,32 +7,13 @@ import { describe, expect, it } from "vitest";
|
|||||||
import { WebSocket } from "ws";
|
import { WebSocket } from "ws";
|
||||||
|
|
||||||
import { PROTOCOL_VERSION } from "../gateway/protocol/index.js";
|
import { PROTOCOL_VERSION } from "../gateway/protocol/index.js";
|
||||||
|
import { getFreePort as getFreeTestPort } from "../gateway/test-helpers.js";
|
||||||
import { rawDataToString } from "../infra/ws.js";
|
import { rawDataToString } from "../infra/ws.js";
|
||||||
import {
|
import {
|
||||||
GATEWAY_CLIENT_MODES,
|
GATEWAY_CLIENT_MODES,
|
||||||
GATEWAY_CLIENT_NAMES,
|
GATEWAY_CLIENT_NAMES,
|
||||||
} from "../utils/message-provider.js";
|
} from "../utils/message-provider.js";
|
||||||
|
|
||||||
async function getFreePort(): Promise<number> {
|
|
||||||
return await new Promise((resolve, reject) => {
|
|
||||||
const srv = createServer();
|
|
||||||
srv.on("error", reject);
|
|
||||||
srv.listen(0, "127.0.0.1", () => {
|
|
||||||
const addr = srv.address();
|
|
||||||
if (!addr || typeof addr === "string") {
|
|
||||||
srv.close();
|
|
||||||
reject(new Error("failed to acquire free port"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const port = addr.port;
|
|
||||||
srv.close((err) => {
|
|
||||||
if (err) reject(err);
|
|
||||||
else resolve(port);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function isPortFree(port: number): Promise<boolean> {
|
async function isPortFree(port: number): Promise<boolean> {
|
||||||
if (!Number.isFinite(port) || port <= 0 || port > 65535) return false;
|
if (!Number.isFinite(port) || port <= 0 || port > 65535) return false;
|
||||||
return await new Promise((resolve) => {
|
return await new Promise((resolve) => {
|
||||||
@@ -48,7 +29,7 @@ async function getFreeGatewayPort(): Promise<number> {
|
|||||||
// Gateway uses derived ports (bridge/browser/canvas). Avoid flaky collisions by
|
// Gateway uses derived ports (bridge/browser/canvas). Avoid flaky collisions by
|
||||||
// ensuring the common derived offsets are free too.
|
// ensuring the common derived offsets are free too.
|
||||||
for (let attempt = 0; attempt < 25; attempt += 1) {
|
for (let attempt = 0; attempt < 25; attempt += 1) {
|
||||||
const port = await getFreePort();
|
const port = await getFreeTestPort();
|
||||||
const candidates = [port, port + 1, port + 2, port + 4];
|
const candidates = [port, port + 1, port + 2, port + 4];
|
||||||
const ok = (
|
const ok = (
|
||||||
await Promise.all(candidates.map((candidate) => isPortFree(candidate)))
|
await Promise.all(candidates.map((candidate) => isPortFree(candidate)))
|
||||||
@@ -142,8 +123,9 @@ describe("onboard (non-interactive): lan bind auto-token", () => {
|
|||||||
path.join(os.tmpdir(), "clawdbot-onboard-lan-"),
|
path.join(os.tmpdir(), "clawdbot-onboard-lan-"),
|
||||||
);
|
);
|
||||||
process.env.HOME = tempHome;
|
process.env.HOME = tempHome;
|
||||||
delete process.env.CLAWDBOT_STATE_DIR;
|
const stateDir = path.join(tempHome, ".clawdbot");
|
||||||
delete process.env.CLAWDBOT_CONFIG_PATH;
|
process.env.CLAWDBOT_STATE_DIR = stateDir;
|
||||||
|
process.env.CLAWDBOT_CONFIG_PATH = path.join(stateDir, "clawdbot.json");
|
||||||
|
|
||||||
const port = await getFreeGatewayPort();
|
const port = await getFreeGatewayPort();
|
||||||
const workspace = path.join(tempHome, "clawd");
|
const workspace = path.join(tempHome, "clawd");
|
||||||
@@ -177,8 +159,9 @@ describe("onboard (non-interactive): lan bind auto-token", () => {
|
|||||||
runtime,
|
runtime,
|
||||||
);
|
);
|
||||||
|
|
||||||
const { CONFIG_PATH_CLAWDBOT } = await import("../config/config.js");
|
const { resolveConfigPath } = await import("../config/paths.js");
|
||||||
const cfg = JSON.parse(await fs.readFile(CONFIG_PATH_CLAWDBOT, "utf8")) as {
|
const configPath = resolveConfigPath(process.env, stateDir);
|
||||||
|
const cfg = JSON.parse(await fs.readFile(configPath, "utf8")) as {
|
||||||
gateway?: {
|
gateway?: {
|
||||||
bind?: string;
|
bind?: string;
|
||||||
port?: number;
|
port?: number;
|
||||||
@@ -193,7 +176,13 @@ describe("onboard (non-interactive): lan bind auto-token", () => {
|
|||||||
expect(token.length).toBeGreaterThan(8);
|
expect(token.length).toBeGreaterThan(8);
|
||||||
|
|
||||||
const { startGatewayServer } = await import("../gateway/server.js");
|
const { startGatewayServer } = await import("../gateway/server.js");
|
||||||
const server = await startGatewayServer(port, { controlUiEnabled: false });
|
const server = await startGatewayServer(port, {
|
||||||
|
controlUiEnabled: false,
|
||||||
|
auth: {
|
||||||
|
mode: "token",
|
||||||
|
token,
|
||||||
|
},
|
||||||
|
});
|
||||||
try {
|
try {
|
||||||
const resNoToken = await connectReq({ url: `ws://127.0.0.1:${port}` });
|
const resNoToken = await connectReq({ url: `ws://127.0.0.1:${port}` });
|
||||||
expect(resNoToken.ok).toBe(false);
|
expect(resNoToken.ok).toBe(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user