|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; |
| 3 | +import { setCurrentWorkspace } from "../../platform/workspace-storage"; |
| 4 | +import { useAgentBulkEditPresetsStore } from "./bulk-edit-presets-store"; |
| 5 | + |
| 6 | +const flush = () => new Promise((resolve) => queueMicrotask(() => resolve(null))); |
| 7 | + |
| 8 | +beforeAll(() => { |
| 9 | + if (typeof globalThis.localStorage?.clear !== "function") { |
| 10 | + const values = new Map<string, string>(); |
| 11 | + const storage: Storage = { |
| 12 | + get length() { return values.size; }, |
| 13 | + clear: () => values.clear(), |
| 14 | + getItem: (k) => values.get(k) ?? null, |
| 15 | + key: (i) => Array.from(values.keys())[i] ?? null, |
| 16 | + removeItem: (k) => { values.delete(k); }, |
| 17 | + setItem: (k, v) => { values.set(k, v); }, |
| 18 | + }; |
| 19 | + Object.defineProperty(globalThis, "localStorage", { configurable: true, value: storage }); |
| 20 | + Object.defineProperty(window, "localStorage", { configurable: true, value: storage }); |
| 21 | + } |
| 22 | +}); |
| 23 | + |
| 24 | +beforeEach(() => { |
| 25 | + localStorage.clear(); |
| 26 | + setCurrentWorkspace(null, null); |
| 27 | + useAgentBulkEditPresetsStore.setState({ presets: [] }); |
| 28 | +}); |
| 29 | + |
| 30 | +afterEach(() => { |
| 31 | + setCurrentWorkspace(null, null); |
| 32 | +}); |
| 33 | + |
| 34 | +describe("useAgentBulkEditPresetsStore", () => { |
| 35 | + it("persists bulk edit presets under the workspace namespace", async () => { |
| 36 | + setCurrentWorkspace("acme", "ws_a"); |
| 37 | + await flush(); |
| 38 | + |
| 39 | + useAgentBulkEditPresetsStore.getState().savePreset("Claude local", { |
| 40 | + model: "claude-sonnet-4", |
| 41 | + customArgsPatch: [ |
| 42 | + { action: "add", value: "--permission-mode" }, |
| 43 | + { action: "add", value: "acceptEdits" }, |
| 44 | + ], |
| 45 | + env: [{ action: "remove", key: "OLD_KEY" }], |
| 46 | + }); |
| 47 | + |
| 48 | + const raw = localStorage.getItem("multica_agent_bulk_edit_presets:acme"); |
| 49 | + expect(raw).not.toBeNull(); |
| 50 | + expect(JSON.parse(raw as string).state.presets).toHaveLength(1); |
| 51 | + }); |
| 52 | + |
| 53 | + it("does not persist env values in local presets", async () => { |
| 54 | + setCurrentWorkspace("acme", "ws_a"); |
| 55 | + await flush(); |
| 56 | + |
| 57 | + useAgentBulkEditPresetsStore.getState().savePreset("Secret skeleton", { |
| 58 | + env: [ |
| 59 | + { action: "set", key: "ANTHROPIC_API_KEY", value: "sk-secret" }, |
| 60 | + { action: "remove", key: "OLD_KEY" }, |
| 61 | + ] as never, |
| 62 | + }); |
| 63 | + |
| 64 | + const preset = useAgentBulkEditPresetsStore.getState().presets[0]!; |
| 65 | + expect(preset.patch.env).toEqual([ |
| 66 | + { action: "set", key: "ANTHROPIC_API_KEY" }, |
| 67 | + { action: "remove", key: "OLD_KEY" }, |
| 68 | + ]); |
| 69 | + expect(localStorage.getItem("multica_agent_bulk_edit_presets:acme")).not.toContain("sk-secret"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("sanitizes custom arg and env operations before persisting a preset", async () => { |
| 73 | + setCurrentWorkspace("acme", "ws_a"); |
| 74 | + await flush(); |
| 75 | + |
| 76 | + useAgentBulkEditPresetsStore.getState().savePreset(" Clean preset ", { |
| 77 | + customArgsPatch: [ |
| 78 | + { action: "add", value: " --verbose " }, |
| 79 | + { action: "replace", value: " --old ", replacement: " --new " }, |
| 80 | + { action: "replace", value: "--missing-replacement", replacement: " " }, |
| 81 | + { action: "remove", value: " " }, |
| 82 | + ], |
| 83 | + env: [ |
| 84 | + { action: "set", key: " API_KEY " }, |
| 85 | + { action: "remove", key: " OLD_KEY " }, |
| 86 | + { action: "set", key: " " }, |
| 87 | + ], |
| 88 | + } as never); |
| 89 | + |
| 90 | + const preset = useAgentBulkEditPresetsStore.getState().presets[0]!; |
| 91 | + expect(preset.name).toBe("Clean preset"); |
| 92 | + expect(preset.patch.customArgsPatch).toEqual([ |
| 93 | + { action: "add", value: "--verbose", replacement: undefined }, |
| 94 | + { action: "replace", value: "--old", replacement: "--new" }, |
| 95 | + ]); |
| 96 | + expect(preset.patch.env).toEqual([ |
| 97 | + { action: "set", key: "API_KEY" }, |
| 98 | + { action: "remove", key: "OLD_KEY" }, |
| 99 | + ]); |
| 100 | + }); |
| 101 | +}); |
0 commit comments