|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { OpenClawConfig } from "../config/config.js"; |
| 3 | +import type { ConfigFileSnapshot } from "../config/types.openclaw.js"; |
| 4 | + |
| 5 | +const loadConfigMock = vi.fn<() => OpenClawConfig>(); |
| 6 | +const readConfigFileSnapshotMock = vi.fn<() => Promise<ConfigFileSnapshot>>(); |
| 7 | +const cleanStaleMatrixPluginConfigMock = vi.fn(); |
| 8 | + |
| 9 | +vi.mock("../config/config.js", () => ({ |
| 10 | + loadConfig: () => loadConfigMock(), |
| 11 | + readConfigFileSnapshot: () => readConfigFileSnapshotMock(), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("../commands/doctor/providers/matrix.js", () => ({ |
| 15 | + cleanStaleMatrixPluginConfig: (cfg: OpenClawConfig) => cleanStaleMatrixPluginConfigMock(cfg), |
| 16 | +})); |
| 17 | + |
| 18 | +const { loadConfigForInstall } = await import("./plugins-install-command.js"); |
| 19 | + |
| 20 | +function makeSnapshot(overrides: Partial<ConfigFileSnapshot> = {}): ConfigFileSnapshot { |
| 21 | + return { |
| 22 | + path: "/tmp/config.json5", |
| 23 | + exists: true, |
| 24 | + raw: '{ "plugins": {} }', |
| 25 | + parsed: { plugins: {} }, |
| 26 | + resolved: { plugins: {} } as OpenClawConfig, |
| 27 | + valid: false, |
| 28 | + config: { plugins: {} } as OpenClawConfig, |
| 29 | + hash: "abc", |
| 30 | + issues: [{ path: "plugins.installs.matrix", message: "stale path" }], |
| 31 | + warnings: [], |
| 32 | + legacyIssues: [], |
| 33 | + ...overrides, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +describe("loadConfigForInstall", () => { |
| 38 | + beforeEach(() => { |
| 39 | + loadConfigMock.mockReset(); |
| 40 | + readConfigFileSnapshotMock.mockReset(); |
| 41 | + cleanStaleMatrixPluginConfigMock.mockReset(); |
| 42 | + |
| 43 | + cleanStaleMatrixPluginConfigMock.mockImplementation((cfg: OpenClawConfig) => ({ |
| 44 | + config: cfg, |
| 45 | + changes: [], |
| 46 | + })); |
| 47 | + }); |
| 48 | + |
| 49 | + it("returns the config directly when loadConfig succeeds", async () => { |
| 50 | + const cfg = { plugins: { entries: { matrix: { enabled: true } } } } as OpenClawConfig; |
| 51 | + loadConfigMock.mockReturnValue(cfg); |
| 52 | + |
| 53 | + const result = await loadConfigForInstall(); |
| 54 | + expect(result).toBe(cfg); |
| 55 | + expect(readConfigFileSnapshotMock).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("runs stale Matrix cleanup on the happy path", async () => { |
| 59 | + const cfg = { plugins: {} } as OpenClawConfig; |
| 60 | + const cleanedCfg = { plugins: { cleaned: true } } as unknown as OpenClawConfig; |
| 61 | + loadConfigMock.mockReturnValue(cfg); |
| 62 | + cleanStaleMatrixPluginConfigMock.mockReturnValue({ config: cleanedCfg, changes: ["cleaned"] }); |
| 63 | + |
| 64 | + const result = await loadConfigForInstall(); |
| 65 | + expect(cleanStaleMatrixPluginConfigMock).toHaveBeenCalledWith(cfg); |
| 66 | + expect(result).toBe(cleanedCfg); |
| 67 | + }); |
| 68 | + |
| 69 | + it("falls back to snapshot config when loadConfig throws INVALID_CONFIG and snapshot was parsed", async () => { |
| 70 | + const invalidConfigErr = new Error("config invalid"); |
| 71 | + (invalidConfigErr as { code?: string }).code = "INVALID_CONFIG"; |
| 72 | + loadConfigMock.mockImplementation(() => { |
| 73 | + throw invalidConfigErr; |
| 74 | + }); |
| 75 | + |
| 76 | + const snapshotCfg = { |
| 77 | + plugins: { installs: { matrix: { source: "path", installPath: "/gone" } } }, |
| 78 | + } as unknown as OpenClawConfig; |
| 79 | + readConfigFileSnapshotMock.mockResolvedValue( |
| 80 | + makeSnapshot({ |
| 81 | + parsed: { plugins: { installs: { matrix: {} } } }, |
| 82 | + config: snapshotCfg, |
| 83 | + }), |
| 84 | + ); |
| 85 | + |
| 86 | + const result = await loadConfigForInstall(); |
| 87 | + expect(readConfigFileSnapshotMock).toHaveBeenCalled(); |
| 88 | + expect(cleanStaleMatrixPluginConfigMock).toHaveBeenCalledWith(snapshotCfg); |
| 89 | + expect(result).toBe(snapshotCfg); |
| 90 | + }); |
| 91 | + |
| 92 | + it("throws when loadConfig fails with INVALID_CONFIG and snapshot parsed is empty (parse failure)", async () => { |
| 93 | + const invalidConfigErr = new Error("config invalid"); |
| 94 | + (invalidConfigErr as { code?: string }).code = "INVALID_CONFIG"; |
| 95 | + loadConfigMock.mockImplementation(() => { |
| 96 | + throw invalidConfigErr; |
| 97 | + }); |
| 98 | + |
| 99 | + readConfigFileSnapshotMock.mockResolvedValue( |
| 100 | + makeSnapshot({ |
| 101 | + parsed: {}, |
| 102 | + config: {} as OpenClawConfig, |
| 103 | + }), |
| 104 | + ); |
| 105 | + |
| 106 | + await expect(loadConfigForInstall()).rejects.toThrow( |
| 107 | + "Config file could not be parsed; run `openclaw doctor` to repair it.", |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it("throws when loadConfig fails with INVALID_CONFIG and config file does not exist", async () => { |
| 112 | + const invalidConfigErr = new Error("config invalid"); |
| 113 | + (invalidConfigErr as { code?: string }).code = "INVALID_CONFIG"; |
| 114 | + loadConfigMock.mockImplementation(() => { |
| 115 | + throw invalidConfigErr; |
| 116 | + }); |
| 117 | + |
| 118 | + readConfigFileSnapshotMock.mockResolvedValue(makeSnapshot({ exists: false, parsed: {} })); |
| 119 | + |
| 120 | + await expect(loadConfigForInstall()).rejects.toThrow( |
| 121 | + "Config file could not be parsed; run `openclaw doctor` to repair it.", |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + it("re-throws non-config errors from loadConfig", async () => { |
| 126 | + const fsErr = new Error("EACCES: permission denied"); |
| 127 | + (fsErr as { code?: string }).code = "EACCES"; |
| 128 | + loadConfigMock.mockImplementation(() => { |
| 129 | + throw fsErr; |
| 130 | + }); |
| 131 | + |
| 132 | + await expect(loadConfigForInstall()).rejects.toThrow("EACCES: permission denied"); |
| 133 | + expect(readConfigFileSnapshotMock).not.toHaveBeenCalled(); |
| 134 | + }); |
| 135 | +}); |
0 commit comments