|
1 | | -import { describe, expect, it, vi } from "vitest"; |
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { |
3 | 3 | applyConfigSnapshot, |
4 | 4 | applyConfig, |
5 | 5 | ensureAgentConfigEntry, |
6 | 6 | findAgentConfigEntryIndex, |
7 | 7 | loadConfig, |
| 8 | + openConfigFile, |
8 | 9 | resetConfigPendingChanges, |
9 | 10 | runUpdate, |
10 | 11 | saveConfig, |
@@ -64,6 +65,10 @@ function requireRequestCall(request: ReturnType<typeof vi.fn>, index = 0): unkno |
64 | 65 | return call; |
65 | 66 | } |
66 | 67 |
|
| 68 | +afterEach(() => { |
| 69 | + vi.unstubAllGlobals(); |
| 70 | +}); |
| 71 | + |
67 | 72 | describe("applyConfigSnapshot", () => { |
68 | 73 | it("does not clobber form edits while dirty", () => { |
69 | 74 | const state = createState(); |
@@ -271,6 +276,57 @@ describe("loadConfig", () => { |
271 | 276 | }); |
272 | 277 | }); |
273 | 278 |
|
| 279 | +describe("openConfigFile", () => { |
| 280 | + it("surfaces failed open responses and copies the returned config path", async () => { |
| 281 | + const request = vi.fn().mockResolvedValue({ |
| 282 | + ok: false, |
| 283 | + path: "/tmp/openclaw.json", |
| 284 | + error: "Cannot open file in headless environment.", |
| 285 | + }); |
| 286 | + const writeText = vi.fn().mockResolvedValue(undefined); |
| 287 | + vi.stubGlobal("navigator", { clipboard: { writeText } }); |
| 288 | + |
| 289 | + const state = createState(); |
| 290 | + state.connected = true; |
| 291 | + state.client = { request } as unknown as ConfigState["client"]; |
| 292 | + state.lastError = "stale error"; |
| 293 | + |
| 294 | + await openConfigFile(state); |
| 295 | + |
| 296 | + expect(request).toHaveBeenCalledWith("config.openFile", {}); |
| 297 | + expect(writeText).toHaveBeenCalledWith("/tmp/openclaw.json"); |
| 298 | + expect(state.lastError).toBe( |
| 299 | + "Cannot open file in headless environment.\n\nFile path copied to clipboard: /tmp/openclaw.json", |
| 300 | + ); |
| 301 | + }); |
| 302 | + |
| 303 | + it("includes the config path in the visible error when clipboard fallback fails", async () => { |
| 304 | + const request = vi.fn().mockResolvedValue({ |
| 305 | + ok: false, |
| 306 | + error: "Failed to open config file", |
| 307 | + }); |
| 308 | + const writeText = vi.fn().mockRejectedValue(new Error("clipboard denied")); |
| 309 | + vi.stubGlobal("navigator", { clipboard: { writeText } }); |
| 310 | + |
| 311 | + const state = createState(); |
| 312 | + state.connected = true; |
| 313 | + state.client = { request } as unknown as ConfigState["client"]; |
| 314 | + state.configSnapshot = { |
| 315 | + config: {}, |
| 316 | + path: "/tmp/from-snapshot.json", |
| 317 | + valid: true, |
| 318 | + issues: [], |
| 319 | + }; |
| 320 | + |
| 321 | + await openConfigFile(state); |
| 322 | + |
| 323 | + expect(writeText).toHaveBeenCalledWith("/tmp/from-snapshot.json"); |
| 324 | + expect(state.lastError).toBe( |
| 325 | + "Failed to open config file\n\nFile path: /tmp/from-snapshot.json", |
| 326 | + ); |
| 327 | + }); |
| 328 | +}); |
| 329 | + |
274 | 330 | describe("updateConfigFormValue", () => { |
275 | 331 | it("seeds from snapshot when form is null", () => { |
276 | 332 | const state = createState(); |
|
0 commit comments