diff --git a/src/commands/setup.test.ts b/src/commands/setup.test.ts new file mode 100644 index 00000000000..c72850d08b0 --- /dev/null +++ b/src/commands/setup.test.ts @@ -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"); + }); + }); +}); diff --git a/src/commands/setup.ts b/src/commands/setup.ts index 3045f748b19..007e83af339 100644 --- a/src/commands/setup.ts +++ b/src/commands/setup.ts @@ -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)}`);