summaryrefslogtreecommitdiff
path: root/packages/core/src/test-utils/tools.ts
blob: 7d917b6c49988447cba31b4340d84f19f7069921 (plain)
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { vi } from 'vitest';
import {
  BaseTool,
  ToolCallConfirmationDetails,
  ToolResult,
  Kind,
} from '../tools/tools.js';
import { Schema, Type } from '@google/genai';

/**
 * A highly configurable mock tool for testing purposes.
 */
export class MockTool extends BaseTool<{ [key: string]: unknown }, ToolResult> {
  executeFn = vi.fn();
  shouldConfirm = false;

  constructor(
    name = 'mock-tool',
    displayName?: string,
    description = 'A mock tool for testing.',
    params: Schema = {
      type: Type.OBJECT,
      properties: { param: { type: Type.STRING } },
    },
  ) {
    super(name, displayName ?? name, description, Kind.Other, params);
  }

  async execute(
    params: { [key: string]: unknown },
    _abortSignal: AbortSignal,
  ): Promise<ToolResult> {
    const result = this.executeFn(params);
    return (
      result ?? {
        llmContent: `Tool ${this.name} executed successfully.`,
        returnDisplay: `Tool ${this.name} executed successfully.`,
      }
    );
  }

  async shouldConfirmExecute(
    _params: { [key: string]: unknown },
    _abortSignal: AbortSignal,
  ): Promise<ToolCallConfirmationDetails | false> {
    if (this.shouldConfirm) {
      return {
        type: 'exec' as const,
        title: `Confirm ${this.displayName}`,
        command: this.name,
        rootCommand: this.name,
        onConfirm: async () => {},
      };
    }
    return false;
  }
}