refactor(test): share gateway onboarding state-dir lifecycle

This commit is contained in:
Peter Steinberger
2026-02-16 15:40:48 +00:00
parent a0e8f00b20
commit a948a3bd00

View File

@@ -100,6 +100,21 @@ describe("onboard (non-interactive): gateway and remote auth", () => {
delete process.env.OPENCLAW_CONFIG_PATH; delete process.env.OPENCLAW_CONFIG_PATH;
return stateDir; return stateDir;
}; };
const withStateDir = async (
prefix: string,
run: (stateDir: string) => Promise<void>,
): Promise<void> => {
const stateDir = await initStateDir(prefix);
try {
await run(stateDir);
} finally {
await fs.rm(stateDir, { recursive: true, force: true });
}
};
const runOnboarding = async (options: Record<string, unknown>) => {
const { runNonInteractiveOnboarding } = await import("./onboard-non-interactive.js");
await runNonInteractiveOnboarding(options, runtime);
};
beforeAll(async () => { beforeAll(async () => {
process.env.OPENCLAW_SKIP_CHANNELS = "1"; process.env.OPENCLAW_SKIP_CHANNELS = "1";
@@ -131,13 +146,11 @@ describe("onboard (non-interactive): gateway and remote auth", () => {
}); });
it("writes gateway token auth into config and gateway enforces it", async () => { it("writes gateway token auth into config and gateway enforces it", async () => {
const stateDir = await initStateDir("state-noninteractive-"); await withStateDir("state-noninteractive-", async (stateDir) => {
const token = "tok_test_123"; const token = "tok_test_123";
const workspace = path.join(stateDir, "openclaw"); const workspace = path.join(stateDir, "openclaw");
const { runNonInteractiveOnboarding } = await import("./onboard-non-interactive.js"); await runOnboarding({
await runNonInteractiveOnboarding(
{
nonInteractive: true, nonInteractive: true,
mode: "local", mode: "local",
workspace, workspace,
@@ -148,9 +161,7 @@ describe("onboard (non-interactive): gateway and remote auth", () => {
gatewayBind: "loopback", gatewayBind: "loopback",
gatewayAuth: "token", gatewayAuth: "token",
gatewayToken: token, gatewayToken: token,
}, });
runtime,
);
const { resolveConfigPath } = await import("../config/paths.js"); const { resolveConfigPath } = await import("../config/paths.js");
const configPath = resolveConfigPath(process.env, stateDir); const configPath = resolveConfigPath(process.env, stateDir);
@@ -168,26 +179,21 @@ describe("onboard (non-interactive): gateway and remote auth", () => {
token, token,
env: process.env, env: process.env,
}); });
});
await fs.rm(stateDir, { recursive: true, force: true });
}, 60_000); }, 60_000);
it("writes gateway.remote url/token and callGateway uses them", async () => { it("writes gateway.remote url/token and callGateway uses them", async () => {
const stateDir = await initStateDir("state-remote-"); await withStateDir("state-remote-", async () => {
const port = await getFreePort(); const port = await getFreePort();
const token = "tok_remote_123"; const token = "tok_remote_123";
const { runNonInteractiveOnboarding } = await import("./onboard-non-interactive.js"); await runOnboarding({
await runNonInteractiveOnboarding(
{
nonInteractive: true, nonInteractive: true,
mode: "remote", mode: "remote",
remoteUrl: `ws://127.0.0.1:${port}`, remoteUrl: `ws://127.0.0.1:${port}`,
remoteToken: token, remoteToken: token,
authChoice: "skip", authChoice: "skip",
json: true, json: true,
}, });
runtime,
);
const { resolveConfigPath } = await import("../config/config.js"); const { resolveConfigPath } = await import("../config/config.js");
const cfg = JSON.parse(await fs.readFile(resolveConfigPath(), "utf8")) as { const cfg = JSON.parse(await fs.readFile(resolveConfigPath(), "utf8")) as {
@@ -205,8 +211,7 @@ describe("onboard (non-interactive): gateway and remote auth", () => {
const lastCall = gatewayClientCalls[gatewayClientCalls.length - 1]; const lastCall = gatewayClientCalls[gatewayClientCalls.length - 1];
expect(lastCall?.url).toBe(`ws://127.0.0.1:${port}`); expect(lastCall?.url).toBe(`ws://127.0.0.1:${port}`);
expect(lastCall?.token).toBe(token); expect(lastCall?.token).toBe(token);
});
await fs.rm(stateDir, { recursive: true, force: true });
}, 60_000); }, 60_000);
it("auto-generates token auth when binding LAN and persists the token", async () => { it("auto-generates token auth when binding LAN and persists the token", async () => {
@@ -214,16 +219,14 @@ describe("onboard (non-interactive): gateway and remote auth", () => {
// Windows runner occasionally drops the temp config write in this flow; skip to keep CI green. // Windows runner occasionally drops the temp config write in this flow; skip to keep CI green.
return; return;
} }
const stateDir = await initStateDir("state-lan-"); await withStateDir("state-lan-", async (stateDir) => {
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 = await getFreeGatewayPort();
const workspace = path.join(stateDir, "openclaw"); const workspace = path.join(stateDir, "openclaw");
const { runNonInteractiveOnboarding } = await import("./onboard-non-interactive.js"); await runOnboarding({
await runNonInteractiveOnboarding(
{
nonInteractive: true, nonInteractive: true,
mode: "local", mode: "local",
workspace, workspace,
@@ -233,9 +236,7 @@ describe("onboard (non-interactive): gateway and remote auth", () => {
installDaemon: false, installDaemon: false,
gatewayPort: port, gatewayPort: port,
gatewayBind: "lan", gatewayBind: "lan",
}, });
runtime,
);
const { resolveConfigPath } = await import("../config/paths.js"); const { resolveConfigPath } = await import("../config/paths.js");
const configPath = resolveConfigPath(process.env, stateDir); const configPath = resolveConfigPath(process.env, stateDir);
@@ -258,7 +259,6 @@ describe("onboard (non-interactive): gateway and remote auth", () => {
token, token,
env: process.env, env: process.env,
}); });
});
await fs.rm(stateDir, { recursive: true, force: true });
}, 60_000); }, 60_000);
}); });