File size: 7,723 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
import { ModelAvailabilityService } from './modelAvailabilityService.js';

describe('ModelAvailabilityService', () => {
  let service: ModelAvailabilityService;
  const model = 'test-model';

  beforeEach(() => {
    service = new ModelAvailabilityService();
    vi.useRealTimers();
  });

  it('returns available snapshot when no state recorded', () => {
    expect(service.snapshot(model)).toEqual({ available: true });
  });

  it('tracks retry-once-per-turn failures', () => {
    service.markRetryOncePerTurn(model);
    expect(service.snapshot(model)).toEqual({ available: true });

    service.consumeStickyAttempt(model);
    expect(service.snapshot(model)).toEqual({
      available: false,
      reason: 'retry_once_per_turn',
    });

    service.resetTurn();
    expect(service.snapshot(model)).toEqual({ available: true });
  });

  it('tracks retry with custom attempts', () => {
    service.markRetryOncePerTurn(model, 3);
    const selection = service.selectFirstAvailable([model]);
    expect(selection.attempts).toBe(3);
  });

  it('tracks terminal failures', () => {
    service.markTerminal(model, 'quota');
    expect(service.snapshot(model)).toEqual({
      available: false,
      reason: 'quota',
    });
  });

  it('does not override terminal failure with sticky failure', () => {
    service.markTerminal(model, 'quota');
    service.markRetryOncePerTurn(model);
    expect(service.snapshot(model)).toEqual({
      available: false,
      reason: 'quota',
    });
  });

  it('selects models respecting terminal and sticky states', () => {
    const stickyModel = 'stick-model';
    const healthyModel = 'healthy-model';

    service.markTerminal(model, 'capacity');
    service.markRetryOncePerTurn(stickyModel);

    const first = service.selectFirstAvailable([
      model,
      stickyModel,
      healthyModel,
    ]);
    expect(first).toEqual({
      selectedModel: stickyModel,
      attempts: 1,
      skipped: [
        {
          model,
          reason: 'capacity',
        },
      ],
    });

    service.consumeStickyAttempt(stickyModel);
    const second = service.selectFirstAvailable([
      model,
      stickyModel,
      healthyModel,
    ]);
    expect(second).toEqual({
      selectedModel: healthyModel,
      skipped: [
        {
          model,
          reason: 'capacity',
        },
        {
          model: stickyModel,
          reason: 'retry_once_per_turn',
        },
      ],
    });

    service.resetTurn();
    const third = service.selectFirstAvailable([
      model,
      stickyModel,
      healthyModel,
    ]);
    expect(third).toEqual({
      selectedModel: stickyModel,
      attempts: 1,
      skipped: [
        {
          model,
          reason: 'capacity',
        },
      ],
    });
  });

  it('preserves consumed state when marking retry-once-per-turn again', () => {
    service.markRetryOncePerTurn(model);
    service.consumeStickyAttempt(model);

    // It is currently consumed
    expect(service.snapshot(model).available).toBe(false);

    // Marking it again should not reset the consumed flag
    service.markRetryOncePerTurn(model);
    expect(service.snapshot(model).available).toBe(false);
  });

  it('clears consumed state when marked healthy', () => {
    service.markRetryOncePerTurn(model);
    service.consumeStickyAttempt(model);
    expect(service.snapshot(model).available).toBe(false);

    service.markHealthy(model);
    expect(service.snapshot(model).available).toBe(true);

    // If we mark it sticky again, it should be fresh (not consumed)
    service.markRetryOncePerTurn(model);
    expect(service.snapshot(model).available).toBe(true);
  });

  it('resetTurn resets consumed state for multiple sticky models', () => {
    const model2 = 'model-2';
    service.markRetryOncePerTurn(model);
    service.markRetryOncePerTurn(model2);

    service.consumeStickyAttempt(model);
    service.consumeStickyAttempt(model2);

    expect(service.snapshot(model).available).toBe(false);
    expect(service.snapshot(model2).available).toBe(false);

    service.resetTurn();

    expect(service.snapshot(model).available).toBe(true);
    expect(service.snapshot(model2).available).toBe(true);
  });

  it('resetTurn does not affect terminal models', () => {
    service.markTerminal(model, 'quota');
    service.resetTurn();
    expect(service.snapshot(model)).toEqual({
      available: false,
      reason: 'quota',
    });
  });

  describe('prefix normalization', () => {
    it('treats prefixed and non-prefixed models as identical when marking terminal', () => {
      service.markTerminal('models/gemini-3.1-pro-preview', 'quota');

      // Checking the non-prefixed version should show it as unavailable
      expect(service.snapshot('gemini-3.1-pro-preview')).toEqual({
        available: false,
        reason: 'quota',
      });

      // Checking the prefixed version should also show it as unavailable
      expect(service.snapshot('models/gemini-3.1-pro-preview')).toEqual({
        available: false,
        reason: 'quota',
      });
    });

    it('treats prefixed and non-prefixed models as identical when selecting', () => {
      service.markTerminal('gemini-3-flash-preview', 'quota');

      // Attempting to select the prefixed version should skip it because the base is exhausted
      const result = service.selectFirstAvailable([
        'models/gemini-3-flash-preview',
        'gemini-3.1-pro-preview',
      ]);

      expect(result.selectedModel).toBe('gemini-3.1-pro-preview');
      expect(result.skipped).toEqual([
        { model: 'gemini-3-flash-preview', reason: 'quota' },
      ]);
    });

    it('treats prefixed and non-prefixed models as identical when marking healthy', () => {
      service.markTerminal('gemini-3-flash-preview', 'quota');
      service.markHealthy('models/gemini-3-flash-preview');

      expect(service.snapshot('gemini-3-flash-preview')).toEqual({
        available: true,
      });
    });
  });

  describe('Capacity TTL expiration', () => {
    beforeEach(() => {
      vi.useFakeTimers();
    });

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

    it('recovers terminal status for capacity after the TTL expires', () => {
      service.markTerminal(model, 'capacity');

      // Before TTL expiration, model is unavailable
      expect(service.snapshot(model)).toEqual({
        available: false,
        reason: 'capacity',
      });

      // Fast forward 29 seconds (default is 30s)
      vi.advanceTimersByTime(29000);
      expect(service.snapshot(model)).toEqual({
        available: false,
        reason: 'capacity',
      });

      // Fast forward past 30 seconds
      vi.advanceTimersByTime(1001);
      expect(service.snapshot(model)).toEqual({
        available: true,
      });
    });

    it('allows custom TTL parameter in snapshot', () => {
      service.markTerminal(model, 'capacity');

      // Check with a custom 5s TTL
      expect(service.snapshot(model, 5000)).toEqual({
        available: false,
        reason: 'capacity',
      });

      // Advance 6 seconds
      vi.advanceTimersByTime(6000);
      expect(service.snapshot(model, 5000)).toEqual({
        available: true,
      });
    });

    it('does not expire terminal status for non-capacity reasons (e.g. quota)', () => {
      service.markTerminal(model, 'quota');

      // Even after advance, quota remains terminal
      vi.advanceTimersByTime(60000);
      expect(service.snapshot(model)).toEqual({
        available: false,
        reason: 'quota',
      });
    });
  });
});