File size: 23,275 Bytes
84aa3bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { vi } from 'vitest';
import { act } from 'react';
import stripAnsi from 'strip-ansi';
import os from 'node:os';
import path from 'node:path';
import fs from 'node:fs';
import { AppContainer } from '../ui/AppContainer.js';
import {
  renderWithProviders,
  type RenderInstance,
  persistentStateMock,
} from './render.js';
import {
  makeFakeConfig,
  type Config,
  type ConfigParameters,
  ExtensionLoader,
  AuthType,
  ApprovalMode,
  createPolicyEngineConfig,
  PolicyDecision,
  ToolConfirmationOutcome,
  MessageBusType,
  type ToolCallsUpdateMessage,
  coreEvents,
  ideContextStore,
  createContentGenerator,
  IdeClient,
  debugLogger,
  CoreToolCallStatus,
  IntegrityDataStatus,
} from '@google/gemini-cli-core';
import {
  type MockShellCommand,
  MockShellExecutionService,
} from './MockShellExecutionService.js';
import { createMockSettings } from './settings.js';
import {
  type LoadedSettings,
  resetSettingsCacheForTesting,
} from '../config/settings.js';
import { AuthState, StreamingState } from '../ui/types.js';
import { randomUUID } from 'node:crypto';
import type {
  TrackedCancelledToolCall,
  TrackedCompletedToolCall,
  TrackedToolCall,
} from '../ui/hooks/useToolScheduler.js';

// Global state observer for React-based signals
const sessionStateMap = new Map<string, StreamingState>();
const activeRigs = new Map<string, AppRig>();

// Mock StreamingContext to report state changes back to the observer
vi.mock('../ui/contexts/StreamingContext.js', async (importOriginal) => {
  const original =
    await importOriginal<typeof import('../ui/contexts/StreamingContext.js')>();
  const { useConfig } = await import('../ui/contexts/ConfigContext.js');
  const React = await import('react');

  return {
    ...original,
    useStreamingContext: () => {
      const state = original.useStreamingContext();
      const config = useConfig();
      const sessionId = config.getSessionId();

      React.useEffect(() => {
        sessionStateMap.set(sessionId, state);
        // If we see activity, we are no longer "awaiting" the start of a response
        if (state !== StreamingState.Idle) {
          const rig = activeRigs.get(sessionId);
          if (rig) {
            rig.awaitingResponse = false;
          }
        }
      }, [sessionId, state]);

      return state;
    },
  };
});

// Mock core functions globally for tests using AppRig.
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
  const original =
    await importOriginal<typeof import('@google/gemini-cli-core')>();
  const { MockShellExecutionService: MockService } = await import(
    './MockShellExecutionService.js'
  );
  // Register the real execution logic so MockShellExecutionService can fall back to it
  MockService.setOriginalImplementation(original.ShellExecutionService.execute);

  return {
    ...original,
    ShellExecutionService: MockService,
  };
});

// Mock useAuthCommand to bypass authentication flows in tests
vi.mock('../ui/auth/useAuth.js', () => ({
  useAuthCommand: () => ({
    authState: AuthState.Authenticated,
    setAuthState: vi.fn(),
    authError: null,
    onAuthError: vi.fn(),
    apiKeyDefaultValue: 'test-api-key',
    reloadApiKey: vi.fn().mockResolvedValue('test-api-key'),
    accountSuspensionInfo: null,
    setAccountSuspensionInfo: vi.fn(),
  }),
  validateAuthMethodWithSettings: () => null,
}));

// A minimal mock ExtensionManager to satisfy AppContainer's forceful cast
class MockExtensionManager extends ExtensionLoader {
  getExtensions = vi.fn().mockReturnValue([]);
  setRequestConsent = vi.fn();
  setRequestSetting = vi.fn();
  integrityManager = {
    verifyExtensionIntegrity: vi
      .fn()
      .mockResolvedValue(IntegrityDataStatus.VERIFIED),
    storeExtensionIntegrity: vi.fn().mockResolvedValue(undefined),
  };
}

// Mock GeminiRespondingSpinner to disable animations (avoiding 'act()' warnings) without triggering screen reader mode.
vi.mock('../ui/components/GeminiRespondingSpinner.js', async () => {
  const React = await import('react');
  const { Text } = await import('ink');
  return {
    GeminiSpinner: () => React.createElement(Text, null, '...'),
    GeminiRespondingSpinner: ({
      nonRespondingDisplay,
    }: {
      nonRespondingDisplay: string;
    }) => React.createElement(Text, null, nonRespondingDisplay || '...'),
  };
});

export interface AppRigOptions {
  fakeResponsesPath?: string;
  terminalWidth?: number;
  terminalHeight?: number;
  configOverrides?: Partial<ConfigParameters>;
}

export interface PendingConfirmation {
  toolName: string;
  toolDisplayName?: string;
  correlationId: string;
}

export class AppRig {
  private renderResult: RenderInstance | undefined;
  private config: Config | undefined;
  private settings: LoadedSettings | undefined;
  private testDir: string;
  private sessionId: string;

  private pendingConfirmations = new Map<string, PendingConfirmation>();
  private breakpointTools = new Set<string>();
  private lastAwaitedConfirmation: PendingConfirmation | undefined;

  /**
   * True if a message was just sent but React hasn't yet reported a non-idle state.
   */
  awaitingResponse = false;

  constructor(private options: AppRigOptions = {}) {
    const uniqueId = randomUUID();
    this.testDir = fs.mkdtempSync(
      path.join(os.tmpdir(), `gemini-app-rig-${uniqueId.slice(0, 8)}-`),
    );
    this.sessionId = `test-session-${uniqueId}`;
    activeRigs.set(this.sessionId, this);

    // Pre-create the persistent state file to bypass the terminal setup prompt
    const geminiDir = path.join(this.testDir, '.gemini');
    if (!fs.existsSync(geminiDir)) {
      fs.mkdirSync(geminiDir, { recursive: true });
    }
    fs.writeFileSync(
      path.join(geminiDir, 'state.json'),
      JSON.stringify({ terminalSetupPromptShown: true }),
    );
  }

  async initialize() {
    persistentStateMock.setData({
      terminalSetupPromptShown: true,
      tipsShown: 10,
    });

    this.setupEnvironment();
    resetSettingsCacheForTesting();
    this.settings = this.createRigSettings();

    const approvalMode =
      this.options.configOverrides?.approvalMode ?? ApprovalMode.DEFAULT;
    const policyEngineConfig = await createPolicyEngineConfig(
      this.settings.merged,
      approvalMode,
    );

    const configParams: ConfigParameters = {
      sessionId: this.sessionId,
      targetDir: this.testDir,
      cwd: this.testDir,
      debugMode: false,
      model: 'test-model',
      fakeResponses: this.options.fakeResponsesPath,
      interactive: true,
      approvalMode,
      policyEngineConfig,
      enableEventDrivenScheduler: true,
      extensionLoader: new MockExtensionManager(),
      excludeTools: this.options.configOverrides?.excludeTools,
      useAlternateBuffer: false,
      ...this.options.configOverrides,
    };
    this.config = makeFakeConfig(configParams);

    if (this.options.fakeResponsesPath) {
      this.stubRefreshAuth();
    }

    this.setupMessageBusListeners();

    await act(async () => {
      await this.config!.initialize();
      // Since we mocked useAuthCommand, we must manually trigger the first
      // refreshAuth to ensure contentGenerator is initialized.
      await this.config!.refreshAuth(AuthType.USE_GEMINI);
    });
  }

  private setupEnvironment() {
    // Stub environment variables to avoid interference from developer's machine
    vi.stubEnv('GEMINI_CLI_HOME', this.testDir);
    vi.stubEnv('TERM_PROGRAM', 'other');
    vi.stubEnv('VSCODE_GIT_IPC_HANDLE', '');
    if (this.options.fakeResponsesPath) {
      vi.stubEnv('GEMINI_API_KEY', 'test-api-key');
      MockShellExecutionService.setPassthrough(false);
    } else {
      if (!process.env['GEMINI_API_KEY']) {
        throw new Error(
          'GEMINI_API_KEY must be set in the environment for live model tests.',
        );
      }
      // For live tests, we allow falling through to the real shell service if no mock matches
      MockShellExecutionService.setPassthrough(true);
    }
    vi.stubEnv('GEMINI_DEFAULT_AUTH_TYPE', AuthType.USE_GEMINI);
  }

  private createRigSettings(): LoadedSettings {
    return createMockSettings({
      user: {
        path: path.join(this.testDir, '.gemini', 'user_settings.json'),
        settings: {
          security: {
            auth: {
              selectedType: AuthType.USE_GEMINI,
              useExternal: true,
            },
            folderTrust: {
              enabled: true,
            },
          },
          ide: {
            enabled: false,
            hasSeenNudge: true,
          },
        },
        originalSettings: {},
      },
      merged: {
        security: {
          auth: {
            selectedType: AuthType.USE_GEMINI,
            useExternal: true,
          },
          folderTrust: {
            enabled: true,
          },
        },
        ide: {
          enabled: false,
          hasSeenNudge: true,
        },
        ui: {
          useAlternateBuffer: false,
        },
      },
    });
  }

  private stubRefreshAuth() {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const gcConfig = this.config as any;
    gcConfig.refreshAuth = async (authMethod: AuthType) => {
      gcConfig.modelAvailabilityService.reset();

      const newContentGeneratorConfig = {
        authType: authMethod,
        proxy: gcConfig.getProxy(),
        apiKey: process.env['GEMINI_API_KEY'] || 'test-api-key',
      };

      gcConfig.contentGenerator = await createContentGenerator(
        newContentGeneratorConfig,
        this.config!,
        gcConfig.getSessionId(),
      );
      gcConfig.contentGeneratorConfig = newContentGeneratorConfig;

      // Initialize BaseLlmClient now that the ContentGenerator is available
      const { BaseLlmClient } = await import('@google/gemini-cli-core');
      gcConfig.baseLlmClient = new BaseLlmClient(
        gcConfig.contentGenerator,
        this.config!,
      );
    };
  }

  private toolCalls: TrackedToolCall[] = [];

  private setupMessageBusListeners() {
    if (!this.config) return;
    const messageBus = this.config.getMessageBus();

    messageBus.subscribe(
      MessageBusType.TOOL_CALLS_UPDATE,
      (message: ToolCallsUpdateMessage) => {
        this.toolCalls = message.toolCalls;
        for (const call of message.toolCalls) {
          if (call.status === 'awaiting_approval' && call.correlationId) {
            const details = call.confirmationDetails;
            const title = 'title' in details ? details.title : '';
            const toolDisplayName =
              call.tool?.displayName || title.replace(/^Confirm:\s*/, '');
            if (!this.pendingConfirmations.has(call.correlationId)) {
              this.pendingConfirmations.set(call.correlationId, {
                toolName: call.request.name,
                toolDisplayName,
                correlationId: call.correlationId,
              });
            }
          } else if (call.status !== 'awaiting_approval') {
            for (const [
              correlationId,
              pending,
            ] of this.pendingConfirmations.entries()) {
              if (pending.toolName === call.request.name) {
                this.pendingConfirmations.delete(correlationId);
                break;
              }
            }
          }
        }
      },
    );
  }

  /**
   * Returns true if the agent is currently busy (responding or executing tools).
   */
  isBusy(): boolean {
    if (this.awaitingResponse) {
      return true;
    }

    const reactState = sessionStateMap.get(this.sessionId);
    // If we have a React-based state, use it as the definitive signal.
    // 'responding' and 'waiting-for-confirmation' both count as busy for the overall task.
    if (reactState !== undefined) {
      return reactState !== StreamingState.Idle;
    }

    // Fallback to tool tracking if React hasn't reported yet
    const isAnyToolActive = this.toolCalls.some((tc) => {
      if (
        tc.status === CoreToolCallStatus.Executing ||
        tc.status === CoreToolCallStatus.Scheduled ||
        tc.status === CoreToolCallStatus.Validating
      ) {
        return true;
      }
      if (
        tc.status === CoreToolCallStatus.Success ||
        tc.status === CoreToolCallStatus.Error ||
        tc.status === CoreToolCallStatus.Cancelled
      ) {
        return !(tc as TrackedCompletedToolCall | TrackedCancelledToolCall)
          .responseSubmittedToGemini;
      }
      return false;
    });

    const isAwaitingConfirmation = this.toolCalls.some(
      (tc) => tc.status === CoreToolCallStatus.AwaitingApproval,
    );

    return isAnyToolActive || isAwaitingConfirmation;
  }

  async render() {
    if (!this.config || !this.settings)
      throw new Error('AppRig not initialized');

    await act(async () => {
      this.renderResult = await renderWithProviders(
        <AppContainer
          config={this.config!}
          version="test-version"
          initializationResult={{
            authError: null,
            accountSuspensionInfo: null,
            themeError: null,
            shouldOpenAuthDialog: false,
            geminiMdFileCount: 0,
          }}
        />,
        {
          config: this.config!,
          settings: this.settings!,
          width: this.options.terminalWidth ?? 120,
          uiState: {
            terminalHeight: this.options.terminalHeight ?? 40,
          },
        },
      );
    });
  }

  setMockCommands(commands: MockShellCommand[]) {
    MockShellExecutionService.setMockCommands(commands);
  }

  setToolPolicy(toolName: string, decision: PolicyDecision, priority = 10) {
    if (!this.config) throw new Error('AppRig not initialized');
    this.config.getPolicyEngine().addRule({
      toolName,
      decision,
      priority,
      source: 'AppRig Override',
    });
  }

  setBreakpoint(toolName: string | string[]) {
    if (Array.isArray(toolName)) {
      for (const name of toolName) {
        this.setBreakpoint(name);
      }
    } else {
      this.setToolPolicy(toolName, PolicyDecision.ASK_USER, 100);
      this.breakpointTools.add(toolName);
    }
  }

  removeToolPolicy(toolName: string, source = 'AppRig Override') {
    if (!this.config) throw new Error('AppRig not initialized');
    this.config.getPolicyEngine().removeRulesForTool(toolName, source);
    this.breakpointTools.delete(toolName);
  }

  getTestDir(): string {
    return this.testDir;
  }

  getPendingConfirmations() {
    return Array.from(this.pendingConfirmations.values());
  }

  private async waitUntil(
    predicate: () => boolean | Promise<boolean>,
    options: { timeout?: number; interval?: number; message?: string } = {},
  ) {
    const {
      timeout = 30000,
      interval = 100,
      message = 'Condition timed out',
    } = options;
    const start = Date.now();

    while (true) {
      if (await predicate()) return;

      if (Date.now() - start > timeout) {
        throw new Error(message);
      }

      await act(async () => {
        await new Promise((resolve) => setTimeout(resolve, interval));
      });
    }
  }

  async waitForPendingConfirmation(
    toolNameOrDisplayName?: string | RegExp | string[],
    timeout = 30000,
  ): Promise<PendingConfirmation> {
    const matches = (p: PendingConfirmation) => {
      if (!toolNameOrDisplayName) return true;
      if (typeof toolNameOrDisplayName === 'string') {
        return (
          p.toolName === toolNameOrDisplayName ||
          p.toolDisplayName === toolNameOrDisplayName
        );
      }
      if (Array.isArray(toolNameOrDisplayName)) {
        return (
          toolNameOrDisplayName.includes(p.toolName) ||
          toolNameOrDisplayName.includes(p.toolDisplayName || '')
        );
      }
      return (
        toolNameOrDisplayName.test(p.toolName) ||
        toolNameOrDisplayName.test(p.toolDisplayName || '')
      );
    };

    let matched: PendingConfirmation | undefined;
    await this.waitUntil(
      () => {
        matched = this.getPendingConfirmations().find(matches);
        return !!matched;
      },
      {
        timeout,
        message: `Timed out waiting for pending confirmation: ${toolNameOrDisplayName || 'any'}. Current pending: ${this.getPendingConfirmations()
          .map((p) => p.toolName)
          .join(', ')}`,
      },
    );

    this.lastAwaitedConfirmation = matched;
    return matched!;
  }

  /**
   * Waits for either a tool confirmation request OR for the agent to go idle.
   */
  async waitForNextEvent(
    timeout = 60000,
  ): Promise<
    | { type: 'confirmation'; confirmation: PendingConfirmation }
    | { type: 'idle' }
  > {
    let confirmation: PendingConfirmation | undefined;
    let isIdle = false;

    await this.waitUntil(
      async () => {
        await act(async () => {
          await new Promise((resolve) => setTimeout(resolve, 0));
        });
        confirmation = this.getPendingConfirmations()[0];
        // Now that we have a code-powered signal, this should be perfectly deterministic.
        isIdle = !this.isBusy();
        return !!confirmation || isIdle;
      },
      {
        timeout,
        message: 'Timed out waiting for next event (confirmation or idle).',
      },
    );

    if (confirmation) {
      this.lastAwaitedConfirmation = confirmation;
      return { type: 'confirmation', confirmation };
    }

    // Ensure all renders are flushed before returning 'idle'
    await this.renderResult?.waitUntilReady();
    return { type: 'idle' };
  }

  async resolveTool(
    toolNameOrDisplayName: string | RegExp | PendingConfirmation,
    outcome: ToolConfirmationOutcome = ToolConfirmationOutcome.ProceedOnce,
  ): Promise<void> {
    if (!this.config) throw new Error('AppRig not initialized');
    const messageBus = this.config.getMessageBus();

    let pending: PendingConfirmation;
    if (
      typeof toolNameOrDisplayName === 'object' &&
      'correlationId' in toolNameOrDisplayName
    ) {
      pending = toolNameOrDisplayName;
    } else {
      pending = await this.waitForPendingConfirmation(toolNameOrDisplayName);
    }

    await act(async () => {
      this.pendingConfirmations.delete(pending.correlationId);

      if (this.breakpointTools.has(pending.toolName)) {
        this.removeToolPolicy(pending.toolName);
      }

      // eslint-disable-next-line @typescript-eslint/no-floating-promises
      messageBus.publish({
        type: MessageBusType.TOOL_CONFIRMATION_RESPONSE,
        correlationId: pending.correlationId,
        confirmed: outcome !== ToolConfirmationOutcome.Cancel,
        outcome,
      });
    });

    await act(async () => {
      await new Promise((resolve) => setTimeout(resolve, 100));
    });
  }

  async resolveAwaitedTool(
    outcome: ToolConfirmationOutcome = ToolConfirmationOutcome.ProceedOnce,
  ): Promise<void> {
    if (!this.lastAwaitedConfirmation) {
      throw new Error('No tool has been awaited yet');
    }
    await this.resolveTool(this.lastAwaitedConfirmation, outcome);
    this.lastAwaitedConfirmation = undefined;
  }

  async addUserHint(hint: string) {
    if (!this.config) throw new Error('AppRig not initialized');
    await act(async () => {
      this.config!.injectionService.addInjection(hint, 'user_steering');
    });
  }

  /**
   * Drains all pending tool calls that hit a breakpoint until the agent is idle.
   * Useful for negative tests to ensure no unwanted tools (like generalist) are called.
   *
   * @param onConfirmation Optional callback to inspect each confirmation before resolving.
   *                       Return true to skip the default resolveTool call (e.g. if you handled it).
   */
  async drainBreakpointsUntilIdle(
    onConfirmation?: (confirmation: PendingConfirmation) => void | boolean,
    timeout = 60000,
  ) {
    while (true) {
      const event = await this.waitForNextEvent(timeout);
      if (event.type === 'idle') {
        break;
      }

      const confirmation = event.confirmation;
      const handled = onConfirmation?.(confirmation);

      if (!handled) {
        await this.resolveTool(confirmation);
      }
    }
  }

  getConfig(): Config {
    if (!this.config) throw new Error('AppRig not initialized');
    return this.config;
  }

  async type(text: string) {
    if (!this.renderResult) throw new Error('AppRig not initialized');
    await act(async () => {
      this.renderResult!.stdin.write(text);
    });
    await act(async () => {
      await new Promise((resolve) => setTimeout(resolve, 50));
    });
  }

  async pressEnter() {
    await this.type('\r');
  }

  async pressKey(key: string) {
    if (!this.renderResult) throw new Error('AppRig not initialized');
    await act(async () => {
      this.renderResult!.stdin.write(key);
    });
    await act(async () => {
      await new Promise((resolve) => setTimeout(resolve, 50));
    });
  }

  get lastFrame() {
    if (!this.renderResult) return '';
    return stripAnsi(this.renderResult.lastFrame({ allowEmpty: true }) || '');
  }

  getStaticOutput() {
    if (!this.renderResult) return '';
    return stripAnsi(this.renderResult.stdout.lastFrame() || '');
  }

  async waitForOutput(pattern: string | RegExp, timeout = 30000) {
    await this.waitUntil(
      () => {
        const frame = this.lastFrame;
        return typeof pattern === 'string'
          ? frame.includes(pattern)
          : pattern.test(frame);
      },
      {
        timeout,
        message: `Timed out waiting for output: ${pattern}\nLast frame:\n${this.lastFrame}`,
      },
    );
  }

  async waitForIdle(timeout = 20000) {
    await this.waitForOutput('Type your message', timeout);
  }

  async sendMessage(text: string) {
    this.awaitingResponse = true;
    await this.type(text);
    await this.pressEnter();
  }

  async unmount() {
    // Clean up global state for this session
    sessionStateMap.delete(this.sessionId);
    activeRigs.delete(this.sessionId);

    // Poison the chat recording service to prevent late writes to the test directory
    if (this.config) {
      const recordingService = this.config
        .getGeminiClient()
        ?.getChatRecordingService();
      if (recordingService) {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        (recordingService as any).conversationFile = null;
      }
    }

    if (this.renderResult) {
      this.renderResult.unmount();
    }

    await act(async () => {
      await new Promise((resolve) => setTimeout(resolve, 500));
    });

    vi.unstubAllEnvs();

    coreEvents.removeAllListeners();
    coreEvents.drainBacklogs();
    MockShellExecutionService.reset();
    ideContextStore.clear();
    // Forcefully clear IdeClient singleton promise
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (IdeClient as any).instancePromise = null;
    vi.clearAllMocks();

    this.config = undefined;
    this.renderResult = undefined;

    if (this.testDir && fs.existsSync(this.testDir)) {
      try {
        fs.rmSync(this.testDir, { recursive: true, force: true });
      } catch (e) {
        debugLogger.warn(
          `Failed to cleanup test directory ${this.testDir}:`,
          e,
        );
      }
    }
  }
}