CLI: set local gateway mode in setup

This commit is contained in:
Vincent Koc 2026-03-08 11:17:15 -07:00
parent 4c71176c9f
commit 0b452a5665
2 changed files with 78 additions and 2 deletions

View File

@ -0,0 +1,60 @@
import fs from "node:fs/promises";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { withTempHome } from "../../test/helpers/temp-home.js";
import { setupCommand } from "./setup.js";
describe("setupCommand", () => {
it("writes gateway.mode=local on first run", async () => {
await withTempHome(async (home) => {
const runtime = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
await setupCommand(undefined, runtime);
const configPath = path.join(home, ".openclaw", "openclaw.json");
const raw = await fs.readFile(configPath, "utf-8");
expect(raw).toContain('"mode": "local"');
expect(raw).toContain('"workspace"');
});
});
it("adds gateway.mode=local to an existing config without overwriting workspace", async () => {
await withTempHome(async (home) => {
const runtime = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
const configDir = path.join(home, ".openclaw");
const configPath = path.join(configDir, "openclaw.json");
const workspace = path.join(home, "custom-workspace");
await fs.mkdir(configDir, { recursive: true });
await fs.writeFile(
configPath,
JSON.stringify({
agents: {
defaults: {
workspace,
},
},
}),
);
await setupCommand(undefined, runtime);
const raw = JSON.parse(await fs.readFile(configPath, "utf-8")) as {
agents?: { defaults?: { workspace?: string } };
gateway?: { mode?: string };
};
expect(raw.agents?.defaults?.workspace).toBe(workspace);
expect(raw.gateway?.mode).toBe("local");
});
});
});

View File

@ -50,14 +50,30 @@ export async function setupCommand(
workspace,
},
},
gateway: {
...cfg.gateway,
mode: cfg.gateway?.mode ?? "local",
},
};
if (!existingRaw.exists || defaults.workspace !== workspace) {
if (
!existingRaw.exists ||
defaults.workspace !== workspace ||
cfg.gateway?.mode !== next.gateway?.mode
) {
await writeConfigFile(next);
if (!existingRaw.exists) {
runtime.log(`Wrote ${formatConfigPath(configPath)}`);
} else {
logConfigUpdated(runtime, { path: configPath, suffix: "(set agents.defaults.workspace)" });
const updates: string[] = [];
if (defaults.workspace !== workspace) {
updates.push("set agents.defaults.workspace");
}
if (cfg.gateway?.mode !== next.gateway?.mode) {
updates.push("set gateway.mode");
}
const suffix = updates.length > 0 ? `(${updates.join(", ")})` : undefined;
logConfigUpdated(runtime, { path: configPath, suffix });
}
} else {
runtime.log(`Config OK: ${formatConfigPath(configPath)}`);