|
| 1 | +import fs from "fs"; |
| 2 | +import os from "os"; |
| 3 | +import path from "path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { |
| 6 | + getWorkspaceTrustDecision, |
| 7 | + getWorkspaceTrustKey, |
| 8 | + getWorkspaceTrustPath, |
| 9 | + isShuruSandboxSupported, |
| 10 | + loadWorkspaceTrustStore, |
| 11 | + resolveWorkspaceTrustPromptAnswer, |
| 12 | + saveWorkspaceTrustDecision, |
| 13 | + WORKSPACE_TRUST_FILENAME, |
| 14 | +} from "./workspace-trust"; |
| 15 | + |
| 16 | +let tempDirs: string[] = []; |
| 17 | + |
| 18 | +afterEach(() => { |
| 19 | + for (const dir of tempDirs) fs.rmSync(dir, { recursive: true, force: true }); |
| 20 | + tempDirs = []; |
| 21 | +}); |
| 22 | + |
| 23 | +function createTempDir(prefix: string): string { |
| 24 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); |
| 25 | + tempDirs.push(dir); |
| 26 | + return dir; |
| 27 | +} |
| 28 | + |
| 29 | +describe("workspace trust settings", () => { |
| 30 | + it("detects Shuru support only on macOS Apple Silicon", () => { |
| 31 | + expect(isShuruSandboxSupported("darwin", "arm64")).toBe(true); |
| 32 | + expect(isShuruSandboxSupported("darwin", "x64")).toBe(false); |
| 33 | + expect(isShuruSandboxSupported("linux", "arm64")).toBe(false); |
| 34 | + expect(isShuruSandboxSupported("win32", "x64")).toBe(false); |
| 35 | + }); |
| 36 | + |
| 37 | + it("only disables supported sandbox mode on explicit no answers", () => { |
| 38 | + expect(resolveWorkspaceTrustPromptAnswer("", true)).toEqual({ sandboxMode: "shuru", remember: true }); |
| 39 | + expect(resolveWorkspaceTrustPromptAnswer("y", true)).toEqual({ sandboxMode: "shuru", remember: true }); |
| 40 | + expect(resolveWorkspaceTrustPromptAnswer("typo", true)).toEqual({ sandboxMode: "shuru", remember: true }); |
| 41 | + expect(resolveWorkspaceTrustPromptAnswer("n", true)).toEqual({ sandboxMode: "off", remember: true }); |
| 42 | + expect(resolveWorkspaceTrustPromptAnswer("no", true)).toEqual({ sandboxMode: "off", remember: true }); |
| 43 | + expect(resolveWorkspaceTrustPromptAnswer("s", true)).toEqual({ sandboxMode: "shuru", remember: false }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("keeps unsupported sandbox prompt decisions in host mode", () => { |
| 47 | + expect(resolveWorkspaceTrustPromptAnswer("", false)).toEqual({ sandboxMode: "off", remember: true }); |
| 48 | + expect(resolveWorkspaceTrustPromptAnswer("typo", false)).toEqual({ sandboxMode: "off", remember: true }); |
| 49 | + expect(resolveWorkspaceTrustPromptAnswer("s", false)).toEqual({ sandboxMode: "off", remember: false }); |
| 50 | + }); |
| 51 | + |
| 52 | + it("stores sandbox decisions by canonical workspace path", () => { |
| 53 | + const homeDir = createTempDir("grok-trust-home-"); |
| 54 | + const workspace = createTempDir("grok-trust-workspace-"); |
| 55 | + const trustPath = getWorkspaceTrustPath(homeDir); |
| 56 | + |
| 57 | + saveWorkspaceTrustDecision(workspace, "shuru", trustPath); |
| 58 | + |
| 59 | + expect(getWorkspaceTrustDecision(workspace, trustPath)).toBe("shuru"); |
| 60 | + expect(loadWorkspaceTrustStore(trustPath).workspaces[getWorkspaceTrustKey(workspace)]?.sandboxMode).toBe("shuru"); |
| 61 | + expect(path.basename(trustPath)).toBe(WORKSPACE_TRUST_FILENAME); |
| 62 | + }); |
| 63 | + |
| 64 | + it("preserves existing entries when saving another workspace", () => { |
| 65 | + const homeDir = createTempDir("grok-trust-home-"); |
| 66 | + const firstWorkspace = createTempDir("grok-trust-first-"); |
| 67 | + const secondWorkspace = createTempDir("grok-trust-second-"); |
| 68 | + const trustPath = getWorkspaceTrustPath(homeDir); |
| 69 | + |
| 70 | + saveWorkspaceTrustDecision(firstWorkspace, "shuru", trustPath); |
| 71 | + saveWorkspaceTrustDecision(secondWorkspace, "off", trustPath); |
| 72 | + |
| 73 | + expect(getWorkspaceTrustDecision(firstWorkspace, trustPath)).toBe("shuru"); |
| 74 | + expect(getWorkspaceTrustDecision(secondWorkspace, trustPath)).toBe("off"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("ignores malformed files and entries", () => { |
| 78 | + const homeDir = createTempDir("grok-trust-home-"); |
| 79 | + const workspace = createTempDir("grok-trust-workspace-"); |
| 80 | + const trustPath = getWorkspaceTrustPath(homeDir); |
| 81 | + fs.mkdirSync(path.dirname(trustPath), { recursive: true }); |
| 82 | + fs.writeFileSync( |
| 83 | + trustPath, |
| 84 | + JSON.stringify({ |
| 85 | + workspaces: { |
| 86 | + [getWorkspaceTrustKey(workspace)]: { sandboxMode: "invalid" }, |
| 87 | + }, |
| 88 | + }), |
| 89 | + ); |
| 90 | + |
| 91 | + expect(getWorkspaceTrustDecision(workspace, trustPath)).toBeNull(); |
| 92 | + expect(loadWorkspaceTrustStore(path.join(homeDir, ".grok", "broken.json"))).toEqual({ |
| 93 | + version: 1, |
| 94 | + workspaces: {}, |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments