|
| 1 | +import path from "node:path"; |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { readConfigFileSnapshot } from "../config/config.js"; |
| 4 | +import { withEnvOverride, withTempHome, writeOpenClawConfig } from "../config/test-helpers.js"; |
| 5 | +import { |
| 6 | + runInitialConfigWriteHealth, |
| 7 | + runWriteConfigHealth, |
| 8 | +} from "../flows/doctor-health-contribution-runners.config.js"; |
| 9 | +import type { DoctorHealthFlowContext } from "../flows/doctor-health-contribution-types.js"; |
| 10 | +import type { RuntimeEnv } from "../runtime.js"; |
| 11 | +import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js"; |
| 12 | +import { loadAndMaybeMigrateDoctorConfig } from "./doctor-config-flow.js"; |
| 13 | +import { createDoctorPrompter, type DoctorOptions } from "./doctor-prompter.js"; |
| 14 | + |
| 15 | +describe("Doctor workspace persistence", () => { |
| 16 | + afterEach(() => { |
| 17 | + closeOpenClawStateDatabaseForTest(); |
| 18 | + }); |
| 19 | + |
| 20 | + it("keeps the legacy owner on the shared workspace across later health writes", async () => { |
| 21 | + await withTempHome(async (home) => { |
| 22 | + await withEnvOverride({ OPENCLAW_DISABLE_BUNDLED_PLUGINS: "1" }, async () => { |
| 23 | + const workspace = path.join(home, "shared-workspace"); |
| 24 | + const configPath = await writeOpenClawConfig(home, { |
| 25 | + agents: { |
| 26 | + defaults: { workspace }, |
| 27 | + entries: { |
| 28 | + main: { default: true }, |
| 29 | + cursor: { workspace }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + gateway: { mode: "local" }, |
| 33 | + }); |
| 34 | + const runtime: RuntimeEnv = { |
| 35 | + error: vi.fn(), |
| 36 | + exit: vi.fn(), |
| 37 | + log: vi.fn(), |
| 38 | + }; |
| 39 | + const options: DoctorOptions = { nonInteractive: true, repair: true }; |
| 40 | + const prompter = createDoctorPrompter({ runtime, options }); |
| 41 | + const configResult = await loadAndMaybeMigrateDoctorConfig({ |
| 42 | + options, |
| 43 | + confirm: (params) => prompter.confirm(params), |
| 44 | + runtime, |
| 45 | + prompter, |
| 46 | + }); |
| 47 | + const ctx: DoctorHealthFlowContext = { |
| 48 | + runtime, |
| 49 | + options, |
| 50 | + prompter, |
| 51 | + configResult, |
| 52 | + cfg: configResult.cfg, |
| 53 | + cfgForPersistence: structuredClone(configResult.cfg), |
| 54 | + sourceConfigValid: configResult.sourceConfigValid ?? true, |
| 55 | + configPath, |
| 56 | + stateDirExistedAtStart: true, |
| 57 | + ...(configResult.runWithPluginMetadataSnapshot |
| 58 | + ? { runWithPluginMetadataSnapshot: configResult.runWithPluginMetadataSnapshot } |
| 59 | + : {}), |
| 60 | + ...(configResult.invalidatePluginMetadataSnapshot |
| 61 | + ? { invalidatePluginMetadataSnapshot: configResult.invalidatePluginMetadataSnapshot } |
| 62 | + : {}), |
| 63 | + }; |
| 64 | + |
| 65 | + await runInitialConfigWriteHealth(ctx); |
| 66 | + expect((await readConfigFileSnapshot()).config.agents?.entries?.main?.workspace).toBe( |
| 67 | + workspace, |
| 68 | + ); |
| 69 | + |
| 70 | + ctx.cfg = { |
| 71 | + ...ctx.cfg, |
| 72 | + gateway: { ...ctx.cfg.gateway, bind: "lan" }, |
| 73 | + }; |
| 74 | + await runWriteConfigHealth(ctx); |
| 75 | + |
| 76 | + const snapshot = await readConfigFileSnapshot(); |
| 77 | + expect(snapshot.valid).toBe(true); |
| 78 | + expect(snapshot.config.agents?.ownership).toBe("explicit"); |
| 79 | + expect(snapshot.config.agents?.entries?.main?.workspace).toBe(workspace); |
| 80 | + }); |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments