|
| 1 | +import { access, mkdir, readFile, writeFile } from "node:fs/promises"; |
| 2 | +import { dirname, join } from "node:path"; |
| 3 | +import { expect } from "vitest"; |
| 4 | + |
| 5 | +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; |
| 6 | +import { cliTest, type TestCli } from "../../support/cli/cliFixture.js"; |
| 7 | + |
| 8 | +const setupArguments = ["setup", "--client", "gemini_cli", "--yes", "--json"]; |
| 9 | +const doctorArguments = ["doctor", "--client", "gemini_cli", "--json"]; |
| 10 | + |
| 11 | +const geminiWorkspace = async (cli: TestCli) => { |
| 12 | + const home = await createTestTempDirectory("rea-gemini-workspace-trust-"); |
| 13 | + const project = join(home, "project"); |
| 14 | + const workspace = join(project, ".gemini/settings.json"); |
| 15 | + const user = join(home, ".gemini/settings.json"); |
| 16 | + await mkdir(dirname(workspace), { recursive: true }); |
| 17 | + const environment: NodeJS.ProcessEnv = { |
| 18 | + HOME: home, |
| 19 | + USERPROFILE: home, |
| 20 | + GEMINI_CLI_HOME: home, |
| 21 | + GEMINI_CLI_SYSTEM_SETTINGS_PATH: join(home, "system.json"), |
| 22 | + GEMINI_CLI_SYSTEM_DEFAULTS_PATH: join(home, "defaults.json"), |
| 23 | + GEMINI_CLI_TRUSTED_FOLDERS_PATH: join(home, "trust.json"), |
| 24 | + }; |
| 25 | + const initial = await cli.run({ |
| 26 | + arguments: setupArguments, |
| 27 | + cwd: home, |
| 28 | + environment, |
| 29 | + }); |
| 30 | + expect(initial.exitCode, initial.stdout).toBe(0); |
| 31 | + const originalUser = await readFile(user); |
| 32 | + const run = (arguments_: readonly string[]) => |
| 33 | + cli.run({ arguments: arguments_, cwd: project, environment }); |
| 34 | + return { workspace, user, originalUser, environment, run }; |
| 35 | +}; |
| 36 | + |
| 37 | +cliTest( |
| 38 | + "Gemini does not require workspace trust until workspace settings exist", |
| 39 | + async ({ cli }) => { |
| 40 | + const fixture = await geminiWorkspace(cli); |
| 41 | + const trust = join(dirname(fixture.user), "trustedFolders.json"); |
| 42 | + fixture.environment.GEMINI_CLI_TRUSTED_FOLDERS_PATH = trust; |
| 43 | + for (const text of ["{}", "{ not json"]) { |
| 44 | + await writeFile(trust, text); |
| 45 | + const setup = await fixture.run(setupArguments); |
| 46 | + expect(setup.exitCode, setup.stdout).toBe(0); |
| 47 | + expect(setup.json).toMatchObject({ status: "ready", appliedActions: [] }); |
| 48 | + const doctor = await fixture.run(doctorArguments); |
| 49 | + expect(doctor.exitCode, doctor.stdout).toBe(0); |
| 50 | + expect(doctor.json).toMatchObject({ |
| 51 | + healthy: true, |
| 52 | + identity: { |
| 53 | + registrations: expect.arrayContaining([ |
| 54 | + expect.objectContaining({ |
| 55 | + client: "gemini_cli", |
| 56 | + state: "aligned", |
| 57 | + remediation: null, |
| 58 | + }), |
| 59 | + ]), |
| 60 | + }, |
| 61 | + }); |
| 62 | + await expect(access(fixture.workspace)).rejects.toMatchObject({ |
| 63 | + code: "ENOENT", |
| 64 | + }); |
| 65 | + expect(await readFile(trust, "utf8")).toBe(text); |
| 66 | + expect(await readFile(fixture.user)).toEqual(fixture.originalUser); |
| 67 | + } |
| 68 | + |
| 69 | + const workspaceText = "{ not json"; |
| 70 | + await writeFile(fixture.workspace, workspaceText); |
| 71 | + const setup = await fixture.run(setupArguments); |
| 72 | + expect(setup.exitCode, setup.stdout).toBe(1); |
| 73 | + expect(setup.json).toMatchObject({ |
| 74 | + status: "needs_human", |
| 75 | + plannedActions: [], |
| 76 | + appliedActions: [], |
| 77 | + remediation: expect.stringContaining(trust), |
| 78 | + }); |
| 79 | + const doctor = await fixture.run(doctorArguments); |
| 80 | + expect(doctor.exitCode, doctor.stdout).toBe(1); |
| 81 | + expect(doctor.json).toMatchObject({ |
| 82 | + healthy: false, |
| 83 | + identity: { |
| 84 | + registrations: expect.arrayContaining([ |
| 85 | + expect.objectContaining({ |
| 86 | + client: "gemini_cli", |
| 87 | + state: "invalid", |
| 88 | + remediation: expect.stringContaining(trust), |
| 89 | + }), |
| 90 | + ]), |
| 91 | + }, |
| 92 | + }); |
| 93 | + expect(await readFile(fixture.workspace, "utf8")).toBe(workspaceText); |
| 94 | + expect(await readFile(trust, "utf8")).toBe("{ not json"); |
| 95 | + expect(await readFile(fixture.user)).toEqual(fixture.originalUser); |
| 96 | + }, |
| 97 | +); |
| 98 | + |
| 99 | +for (const scenario of [ |
| 100 | + { name: "an unlisted workspace", environment: {} }, |
| 101 | + { |
| 102 | + name: "an explicitly untrusted workspace", |
| 103 | + environment: { GEMINI_CLI_TRUST_WORKSPACE: "false" }, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "a restricted workspace with an explicit trust opt-in", |
| 107 | + environment: { |
| 108 | + GEMINI_CLI_TRUST_WORKSPACE: "true", |
| 109 | + GEMINI_RESTRICTED_MODE: "true", |
| 110 | + }, |
| 111 | + }, |
| 112 | +]) |
| 113 | + cliTest( |
| 114 | + `Gemini setup and doctor ignore malformed settings in ${scenario.name}`, |
| 115 | + async ({ cli }) => { |
| 116 | + const fixture = await geminiWorkspace(cli); |
| 117 | + Object.assign(fixture.environment, scenario.environment); |
| 118 | + const malformed = Buffer.from("{ not json"); |
| 119 | + await writeFile(fixture.workspace, malformed); |
| 120 | + |
| 121 | + const setup = await fixture.run(setupArguments); |
| 122 | + expect(setup.exitCode, setup.stdout).toBe(0); |
| 123 | + expect(setup.json).toMatchObject({ status: "ready", appliedActions: [] }); |
| 124 | + const doctor = await fixture.run(doctorArguments); |
| 125 | + expect(doctor.exitCode, doctor.stdout).toBe(0); |
| 126 | + expect(doctor.json).toMatchObject({ |
| 127 | + healthy: true, |
| 128 | + identity: { |
| 129 | + registrations: expect.arrayContaining([ |
| 130 | + expect.objectContaining({ |
| 131 | + client: "gemini_cli", |
| 132 | + state: "aligned", |
| 133 | + remediation: null, |
| 134 | + }), |
| 135 | + ]), |
| 136 | + }, |
| 137 | + }); |
| 138 | + expect(await readFile(fixture.workspace)).toEqual(malformed); |
| 139 | + expect(await readFile(fixture.user)).toEqual(fixture.originalUser); |
| 140 | + await expect( |
| 141 | + access(`${fixture.workspace}.rea.backup`), |
| 142 | + ).rejects.toMatchObject({ |
| 143 | + code: "ENOENT", |
| 144 | + }); |
| 145 | + |
| 146 | + if (scenario.name === "an explicitly untrusted workspace") { |
| 147 | + const excluded = Buffer.from( |
| 148 | + JSON.stringify({ mcp: { excluded: ["rea"] } }), |
| 149 | + ); |
| 150 | + await writeFile(fixture.workspace, excluded); |
| 151 | + const excludedSetup = await fixture.run(setupArguments); |
| 152 | + expect(excludedSetup.exitCode, excludedSetup.stdout).toBe(0); |
| 153 | + const excludedDoctor = await fixture.run(doctorArguments); |
| 154 | + expect(excludedDoctor.exitCode, excludedDoctor.stdout).toBe(0); |
| 155 | + expect(excludedDoctor.json).toMatchObject({ |
| 156 | + identity: { |
| 157 | + registrations: expect.arrayContaining([ |
| 158 | + expect.objectContaining({ |
| 159 | + client: "gemini_cli", |
| 160 | + state: "aligned", |
| 161 | + }), |
| 162 | + ]), |
| 163 | + }, |
| 164 | + }); |
| 165 | + expect(await readFile(fixture.workspace)).toEqual(excluded); |
| 166 | + } |
| 167 | + }, |
| 168 | + ); |
| 169 | + |
| 170 | +cliTest( |
| 171 | + "Gemini still rejects malformed settings and exclusions in a trusted workspace", |
| 172 | + async ({ cli }) => { |
| 173 | + const fixture = await geminiWorkspace(cli); |
| 174 | + fixture.environment.GEMINI_CLI_TRUST_WORKSPACE = "true"; |
| 175 | + for (const scenario of [ |
| 176 | + { |
| 177 | + text: "{ not json", |
| 178 | + state: "invalid", |
| 179 | + diagnostic: "Invalid JSON/JSONC", |
| 180 | + }, |
| 181 | + { |
| 182 | + text: JSON.stringify({ mcp: { excluded: ["rea"] } }), |
| 183 | + state: "stale", |
| 184 | + diagnostic: "mcp.excluded", |
| 185 | + }, |
| 186 | + ]) { |
| 187 | + const original = Buffer.from(scenario.text); |
| 188 | + await writeFile(fixture.workspace, original); |
| 189 | + const setup = await fixture.run(setupArguments); |
| 190 | + expect(setup.exitCode, setup.stdout).toBe(1); |
| 191 | + expect(setup.json).toMatchObject({ |
| 192 | + status: "needs_human", |
| 193 | + plannedActions: [], |
| 194 | + appliedActions: [], |
| 195 | + remediation: expect.stringContaining(scenario.diagnostic), |
| 196 | + }); |
| 197 | + const doctor = await fixture.run(doctorArguments); |
| 198 | + expect(doctor.exitCode, doctor.stdout).toBe(1); |
| 199 | + expect(doctor.json).toMatchObject({ |
| 200 | + healthy: false, |
| 201 | + identity: { |
| 202 | + registrations: expect.arrayContaining([ |
| 203 | + expect.objectContaining({ |
| 204 | + client: "gemini_cli", |
| 205 | + state: scenario.state, |
| 206 | + remediation: expect.stringContaining(scenario.diagnostic), |
| 207 | + }), |
| 208 | + ]), |
| 209 | + }, |
| 210 | + }); |
| 211 | + expect(await readFile(fixture.workspace)).toEqual(original); |
| 212 | + expect(await readFile(fixture.user)).toEqual(fixture.originalUser); |
| 213 | + await expect( |
| 214 | + access(`${fixture.workspace}.rea.backup`), |
| 215 | + ).rejects.toMatchObject({ |
| 216 | + code: "ENOENT", |
| 217 | + }); |
| 218 | + } |
| 219 | + }, |
| 220 | +); |
0 commit comments