summaryrefslogtreecommitdiff
path: root/packages/cli/src/utils/modelCheck.test.ts
blob: 5c6f18081074ab83f6d19b0e735e73a421fc9ad3 (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
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { getEffectiveModel } from './modelCheck.js';
import {
  DEFAULT_GEMINI_MODEL,
  DEFAULT_GEMINI_FLASH_MODEL,
} from '../config/config.js';

// Mock global fetch
global.fetch = vi.fn();

// Mock AbortController
const mockAbort = vi.fn();
global.AbortController = vi.fn(() => ({
  signal: { aborted: false }, // Start with not aborted
  abort: mockAbort,
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
})) as any;

describe('getEffectiveModel', () => {
  const apiKey = 'test-api-key';

  beforeEach(() => {
    vi.useFakeTimers();
    vi.clearAllMocks();
    // Reset signal for each test if AbortController mock is more complex
    global.AbortController = vi.fn(() => ({
      signal: { aborted: false },
      abort: mockAbort,
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
    })) as any;
  });

  afterEach(() => {
    vi.restoreAllMocks();
    vi.useRealTimers();
  });

  describe('when currentConfiguredModel is not DEFAULT_GEMINI_MODEL', () => {
    it('should return the currentConfiguredModel without fetching', async () => {
      const customModel = 'custom-model-name';
      const result = await getEffectiveModel(apiKey, customModel);
      expect(result).toEqual(customModel);
      expect(fetch).not.toHaveBeenCalled();
    });
  });

  describe('when currentConfiguredModel is DEFAULT_GEMINI_MODEL', () => {
    it('should switch to DEFAULT_GEMINI_FLASH_MODEL if fetch returns 429', async () => {
      (fetch as vi.Mock).mockResolvedValueOnce({
        ok: false,
        status: 429,
      });
      const result = await getEffectiveModel(apiKey, DEFAULT_GEMINI_MODEL);
      expect(result).toEqual(DEFAULT_GEMINI_FLASH_MODEL);
      expect(fetch).toHaveBeenCalledTimes(1);
      expect(fetch).toHaveBeenCalledWith(
        `https://generativelanguage.googleapis.com/v1beta/models/${DEFAULT_GEMINI_MODEL}:generateContent?key=${apiKey}`,
        expect.any(Object),
      );
    });

    it('should return DEFAULT_GEMINI_MODEL if fetch returns 200', async () => {
      (fetch as vi.Mock).mockResolvedValueOnce({
        ok: true,
        status: 200,
      });
      const result = await getEffectiveModel(apiKey, DEFAULT_GEMINI_MODEL);
      expect(result).toEqual(DEFAULT_GEMINI_MODEL);
      expect(fetch).toHaveBeenCalledTimes(1);
    });

    it('should return DEFAULT_GEMINI_MODEL if fetch returns a non-429 error status (e.g., 500)', async () => {
      (fetch as vi.Mock).mockResolvedValueOnce({
        ok: false,
        status: 500,
      });
      const result = await getEffectiveModel(apiKey, DEFAULT_GEMINI_MODEL);
      expect(result).toEqual(DEFAULT_GEMINI_MODEL);
      expect(fetch).toHaveBeenCalledTimes(1);
    });

    it('should return DEFAULT_GEMINI_MODEL if fetch throws a network error', async () => {
      (fetch as vi.Mock).mockRejectedValueOnce(new Error('Network error'));
      const result = await getEffectiveModel(apiKey, DEFAULT_GEMINI_MODEL);
      expect(result).toEqual(DEFAULT_GEMINI_MODEL);
      expect(fetch).toHaveBeenCalledTimes(1);
    });

    it('should return DEFAULT_GEMINI_MODEL if fetch times out', async () => {
      // Simulate AbortController's signal changing and fetch throwing AbortError
      const abortControllerInstance = {
        signal: { aborted: false }, // mutable signal
        abort: vi.fn(() => {
          abortControllerInstance.signal.aborted = true; // Use abortControllerInstance
        }),
      };
      (global.AbortController as vi.Mock).mockImplementationOnce(
        () => abortControllerInstance,
      );

      (fetch as vi.Mock).mockImplementationOnce(
        async ({ signal }: { signal: AbortSignal }) => {
          // Simulate the timeout advancing and abort being called
          vi.advanceTimersByTime(2000);
          if (signal.aborted) {
            throw new DOMException('Aborted', 'AbortError');
          }
          // Should not reach here in a timeout scenario
          return { ok: true, status: 200 };
        },
      );

      const resultPromise = getEffectiveModel(apiKey, DEFAULT_GEMINI_MODEL);
      // Ensure timers are advanced to trigger the timeout within getEffectiveModel
      await vi.advanceTimersToNextTimerAsync(); // Or advanceTimersByTime(2000) if more precise control is needed

      const result = await resultPromise;

      expect(mockAbort).toHaveBeenCalledTimes(0); // setTimeout calls controller.abort(), not our direct mockAbort
      expect(abortControllerInstance.abort).toHaveBeenCalledTimes(1);
      expect(result).toEqual(DEFAULT_GEMINI_MODEL);
      expect(fetch).toHaveBeenCalledTimes(1);
    });

    it('should correctly pass API key and model in the fetch request', async () => {
      (fetch as vi.Mock).mockResolvedValueOnce({ ok: true, status: 200 });
      const specificApiKey = 'specific-key-for-this-test';
      await getEffectiveModel(specificApiKey, DEFAULT_GEMINI_MODEL);

      expect(fetch).toHaveBeenCalledWith(
        `https://generativelanguage.googleapis.com/v1beta/models/${DEFAULT_GEMINI_MODEL}:generateContent?key=${specificApiKey}`,
        expect.objectContaining({
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            contents: [{ parts: [{ text: 'test' }] }],
            generationConfig: {
              maxOutputTokens: 1,
              temperature: 0,
              topK: 1,
              thinkingConfig: { thinkingBudget: 0, includeThoughts: false },
            },
          }),
        }),
      );
    });
  });
});