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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, expect } from 'vitest';
import { ApprovalMode } from '@google/gemini-cli-core';
import { evalTest } from './test-helper.js';
import {
  assertModelHasOutput,
  checkModelOutputContent,
} from './test-helper.js';

describe('plan_mode', () => {
  const TEST_PREFIX = 'Plan Mode: ';
  const settings = {
    general: {
      plan: { enabled: true },
    },
  };

  const getWriteTargets = (logs: any[]) =>
    logs
      .filter((log) => ['write_file', 'replace'].includes(log.toolRequest.name))
      .map((log) => {
        try {
          return JSON.parse(log.toolRequest.args).file_path as string;
        } catch {
          return '';
        }
      })
      .filter(Boolean);

  evalTest('ALWAYS_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should refuse file modification when in plan mode',
    approvalMode: ApprovalMode.PLAN,
    params: {
      settings,
    },
    files: {
      'README.md': '# Original Content',
    },
    prompt: 'Please overwrite README.md with the text "Hello World"',
    assert: async (rig, result) => {
      await rig.waitForTelemetryReady();
      const toolLogs = rig.readToolLogs();

      const exitPlanIndex = toolLogs.findIndex(
        (log) => log.toolRequest.name === 'exit_plan_mode',
      );

      const writeTargetsBeforeExitPlan = getWriteTargets(
        toolLogs.slice(0, exitPlanIndex !== -1 ? exitPlanIndex : undefined),
      );

      expect(
        writeTargetsBeforeExitPlan,
        'Should not attempt to modify README.md in plan mode',
      ).not.toContain('README.md');

      assertModelHasOutput(result);
      checkModelOutputContent(result, {
        expectedContent: [/plan mode|read-only|cannot modify|refuse|exiting/i],
        testName: `${TEST_PREFIX}should refuse file modification in plan mode`,
      });
    },
  });

  evalTest('ALWAYS_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should refuse saving new documentation to the repo when in plan mode',
    approvalMode: ApprovalMode.PLAN,
    params: {
      settings,
    },
    prompt:
      'This architecture overview is great. Please save it as architecture-new.md in the docs/ folder of the repo so we have it for later.',
    assert: async (rig, result) => {
      await rig.waitForTelemetryReady();
      const toolLogs = rig.readToolLogs();

      const exitPlanIndex = toolLogs.findIndex(
        (log) => log.toolRequest.name === 'exit_plan_mode',
      );

      const writeTargetsBeforeExit = getWriteTargets(
        toolLogs.slice(0, exitPlanIndex !== -1 ? exitPlanIndex : undefined),
      );

      // It should NOT write to the docs folder or any other repo path
      const hasRepoWriteBeforeExit = writeTargetsBeforeExit.some(
        (path) => path && !path.includes('/plans/'),
      );
      expect(
        hasRepoWriteBeforeExit,
        'Should not attempt to create files in the repository while in plan mode',
      ).toBe(false);

      assertModelHasOutput(result);
      checkModelOutputContent(result, {
        expectedContent: [/plan mode|read-only|cannot modify|refuse|exit/i],
        testName: `${TEST_PREFIX}should refuse saving docs to repo`,
      });
    },
  });

  evalTest('USUALLY_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should enter plan mode when asked to create a plan',
    approvalMode: ApprovalMode.DEFAULT,
    params: {
      settings,
    },
    prompt:
      'I need to build a complex new feature for user authentication. Please create a detailed implementation plan.',
    assert: async (rig, result) => {
      const wasToolCalled = await rig.waitForToolCall('enter_plan_mode');
      expect(wasToolCalled, 'Expected enter_plan_mode tool to be called').toBe(
        true,
      );
      assertModelHasOutput(result);
    },
  });

  evalTest('USUALLY_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should exit plan mode when plan is complete and implementation is requested',
    approvalMode: ApprovalMode.PLAN,
    params: {
      settings,
    },
    files: {
      'plans/my-plan.md':
        '# My Implementation Plan\n\n1. Step one\n2. Step two',
    },
    prompt:
      'The plan in plans/my-plan.md looks solid. Start the implementation.',
    assert: async (rig, result) => {
      const wasToolCalled = await rig.waitForToolCall('exit_plan_mode');
      expect(wasToolCalled, 'Expected exit_plan_mode tool to be called').toBe(
        true,
      );

      const toolLogs = rig.readToolLogs();
      const exitPlanCall = toolLogs.find(
        (log) => log.toolRequest.name === 'exit_plan_mode',
      );
      expect(
        exitPlanCall,
        'Expected to find exit_plan_mode in tool logs',
      ).toBeDefined();

      const args = JSON.parse(exitPlanCall!.toolRequest.args);
      expect(args.plan_filename, 'plan_filename should be a string').toBeTypeOf(
        'string',
      );
      expect(args.plan_filename, 'plan_filename should end with .md').toMatch(
        /\.md$/,
      );
      expect(
        args.plan_filename,
        'plan_filename should not be a path',
      ).not.toContain('/');
      expect(
        args.plan_filename,
        'plan_filename should not be a path',
      ).not.toContain('\\');

      assertModelHasOutput(result);
    },
  });

  evalTest('USUALLY_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should allow file modification in plans directory when in plan mode',
    approvalMode: ApprovalMode.PLAN,
    params: {
      settings,
    },
    prompt:
      'I agree with the strategy to use a JWT-based login. Create a plan for a new login feature.',
    assert: async (rig, result) => {
      await rig.waitForTelemetryReady();
      const toolLogs = rig.readToolLogs();

      const writeCall = toolLogs.find(
        (log) => log.toolRequest.name === 'write_file',
      );

      expect(
        writeCall,
        'Should attempt to modify a file in the plans directory when in plan mode',
      ).toBeDefined();

      if (writeCall) {
        const args = JSON.parse(writeCall.toolRequest.args);
        expect(args.file_path).toContain('.gemini/tmp');
        expect(args.file_path).toContain('/plans/');
        expect(args.file_path).toMatch(/\.md$/);
      }

      assertModelHasOutput(result);
    },
  });

  evalTest('USUALLY_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should create a plan in plan mode and implement it for a refactoring task',
    params: {
      settings,
    },
    files: {
      'src/mathUtils.ts':
        'export const sum = (a: number, b: number) => a + b;\nexport const multiply = (a: number, b: number) => a * b;',
      'src/main.ts':
        'import { sum } from "./mathUtils";\nconsole.log(sum(1, 2));',
    },
    prompt:
      'I want to refactor our math utilities. I agree with the strategy to move the `sum` function from `src/mathUtils.ts` to a new file `src/basicMath.ts` and update `src/main.ts`. Please create a detailed implementation plan first, then execute it.',
    assert: async (rig, result) => {
      const enterPlanCalled = await rig.waitForToolCall('enter_plan_mode');
      expect(
        enterPlanCalled,
        'Expected enter_plan_mode tool to be called',
      ).toBe(true);

      const exitPlanCalled = await rig.waitForToolCall('exit_plan_mode');
      expect(exitPlanCalled, 'Expected exit_plan_mode tool to be called').toBe(
        true,
      );

      await rig.waitForTelemetryReady();
      const toolLogs = rig.readToolLogs();

      const exitPlanCall = toolLogs.find(
        (log) => log.toolRequest.name === 'exit_plan_mode',
      );
      expect(
        exitPlanCall,
        'Expected to find exit_plan_mode in tool logs',
      ).toBeDefined();

      const args = JSON.parse(exitPlanCall!.toolRequest.args);
      expect(args.plan_filename, 'plan_filename should be a string').toBeTypeOf(
        'string',
      );
      expect(args.plan_filename, 'plan_filename should end with .md').toMatch(
        /\.md$/,
      );
      expect(
        args.plan_filename,
        'plan_filename should not be a path',
      ).not.toContain('/');
      expect(
        args.plan_filename,
        'plan_filename should not be a path',
      ).not.toContain('\\');

      // Check if plan was written
      const planWrite = toolLogs.find(
        (log) =>
          log.toolRequest.name === 'write_file' &&
          log.toolRequest.args.includes('/plans/'),
      );
      expect(
        planWrite,
        'Expected a plan file to be written in the plans directory',
      ).toBeDefined();

      // Check for implementation files
      const newFileWrite = toolLogs.find(
        (log) =>
          log.toolRequest.name === 'write_file' &&
          log.toolRequest.args.includes('src/basicMath.ts'),
      );
      expect(
        newFileWrite,
        'Expected src/basicMath.ts to be created',
      ).toBeDefined();

      const mainUpdate = toolLogs.find(
        (log) =>
          ['write_file', 'replace'].includes(log.toolRequest.name) &&
          log.toolRequest.args.includes('src/main.ts'),
      );
      expect(mainUpdate, 'Expected src/main.ts to be updated').toBeDefined();

      assertModelHasOutput(result);
    },
  });

  evalTest('ALWAYS_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should transition from plan mode to normal execution and create a plan file from scratch',
    params: {
      settings,
    },
    prompt:
      'I agree with your strategy. Please enter plan mode and draft the plan to create a new module called foo. The plan should be saved as foo-plan.md. Then, exit plan mode.',
    assert: async (rig, result) => {
      const enterPlanCalled = await rig.waitForToolCall('enter_plan_mode');
      expect(
        enterPlanCalled,
        'Expected enter_plan_mode tool to be called',
      ).toBe(true);

      const exitPlanCalled = await rig.waitForToolCall('exit_plan_mode');
      expect(exitPlanCalled, 'Expected exit_plan_mode tool to be called').toBe(
        true,
      );

      await rig.waitForTelemetryReady();
      const toolLogs = rig.readToolLogs();

      // Check if the plan file was written successfully
      const planWrite = toolLogs.find(
        (log) =>
          log.toolRequest.name === 'write_file' &&
          log.toolRequest.args.includes('foo-plan.md'),
      );

      expect(
        planWrite,
        'Expected write_file to be called for foo-plan.md',
      ).toBeDefined();

      expect(
        planWrite?.toolRequest.success,
        `Expected write_file to succeed, but got error: ${(planWrite?.toolRequest as any).error}`,
      ).toBe(true);

      assertModelHasOutput(result);
    },
  });

  evalTest('USUALLY_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should not exit plan mode or draft before informal agreement',
    approvalMode: ApprovalMode.PLAN,
    params: {
      settings,
    },
    prompt: 'I need to build a new login feature. Please plan it.',
    assert: async (rig, result) => {
      await rig.waitForTelemetryReady();
      const toolLogs = rig.readToolLogs();

      const exitPlanCall = toolLogs.find(
        (log) => log.toolRequest.name === 'exit_plan_mode',
      );
      expect(
        exitPlanCall,
        'Should NOT call exit_plan_mode before informal agreement',
      ).toBeUndefined();

      const planWrite = toolLogs.find(
        (log) =>
          log.toolRequest.name === 'write_file' &&
          log.toolRequest.args.includes('/plans/'),
      );
      expect(
        planWrite,
        'Should NOT draft the plan file before informal agreement',
      ).toBeUndefined();

      assertModelHasOutput(result);
    },
  });

  evalTest('USUALLY_PASSES', {
    name: 'should handle nested plan directories correctly',
    suiteName: 'plan_mode',
    suiteType: 'behavioral',
    approvalMode: ApprovalMode.PLAN,
    params: {
      settings,
    },
    prompt:
      'Please create a new architectural plan in a nested folder called "architecture/frontend-v2.md" within the plans directory. The plan should contain the text "# Frontend V2 Plan". Then, exit plan mode',
    assert: async (rig, result) => {
      await rig.waitForTelemetryReady();
      const toolLogs = rig.readToolLogs();

      const writeCalls = toolLogs.filter((log) =>
        ['write_file', 'replace'].includes(log.toolRequest.name),
      );

      const wroteToNestedPath = writeCalls.some((log) => {
        try {
          const args = JSON.parse(log.toolRequest.args);
          if (!args.file_path) return false;
          // In plan mode, paths can be passed as relative (architecture/frontend-v2.md)
          // or they might be resolved as absolute by the tool depending on the exact mock state.
          // We strictly ensure it ends exactly with the expected nested path and doesn't contain extra nesting.
          const normalizedPath = args.file_path.replace(/\\/g, '/');
          return (
            normalizedPath === 'architecture/frontend-v2.md' ||
            normalizedPath.endsWith('/plans/architecture/frontend-v2.md')
          );
        } catch {
          return false;
        }
      });

      expect(
        wroteToNestedPath,
        'Expected model to successfully target the nested plan file path',
      ).toBe(true);

      assertModelHasOutput(result);
    },
  });

  evalTest('USUALLY_PASSES', {
    suiteName: 'plan_mode',
    suiteType: 'behavioral',
    name: 'should invoke exit_plan_mode as a tool instead of a shell command',
    approvalMode: ApprovalMode.PLAN,
    params: {
      settings: {
        general: {
          plan: { enabled: true },
        },
      },
    },
    files: {
      'plans/my-plan.md': '# My Plan\n\n1. Step one',
    },
    prompt:
      'I agree with the plan in plans/my-plan.md. Please exit plan mode and then run `echo "Starting implementation"`',
    assert: async (rig) => {
      await rig.waitForTelemetryReady();
      const toolLogs = rig.readToolLogs();

      // Check if exit_plan_mode was called as a tool
      const exitPlanToolCall = toolLogs.find(
        (log) => log.toolRequest.name === 'exit_plan_mode',
      );

      // Check if exit_plan_mode was called via shell
      const shellCalls = toolLogs.filter(
        (log) => log.toolRequest.name === 'run_shell_command',
      );
      const exitPlanViaShell = shellCalls.find((log) => {
        try {
          const args = JSON.parse(log.toolRequest.args);
          return args.command.includes('exit_plan_mode');
        } catch {
          return false;
        }
      });

      expect(
        exitPlanViaShell,
        'Should NOT call exit_plan_mode via run_shell_command',
      ).toBeUndefined();
      expect(
        exitPlanToolCall,
        'Should call exit_plan_mode tool directly',
      ).toBeDefined();
    },
  });
});