|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, expect, it, vi, beforeEach } from 'vitest'; |
| 8 | +import { ModelAvailabilityService } from './modelAvailabilityService.js'; |
| 9 | + |
| 10 | +describe('ModelAvailabilityService', () => { |
| 11 | + let service: ModelAvailabilityService; |
| 12 | + const model = 'test-model'; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + service = new ModelAvailabilityService(); |
| 16 | + vi.useRealTimers(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('returns available snapshot when no state recorded', () => { |
| 20 | + expect(service.snapshot(model)).toEqual({ available: true }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('tracks retry-once-per-turn failures', () => { |
| 24 | + service.markRetryOncePerTurn(model); |
| 25 | + expect(service.snapshot(model)).toEqual({ available: true }); |
| 26 | + |
| 27 | + service.consumeStickyAttempt(model); |
| 28 | + expect(service.snapshot(model)).toEqual({ |
| 29 | + available: false, |
| 30 | + reason: 'retry_once_per_turn', |
| 31 | + }); |
| 32 | + |
| 33 | + service.resetTurn(); |
| 34 | + expect(service.snapshot(model)).toEqual({ available: true }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('tracks terminal failures', () => { |
| 38 | + service.markTerminal(model, 'quota'); |
| 39 | + expect(service.snapshot(model)).toEqual({ |
| 40 | + available: false, |
| 41 | + reason: 'quota', |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('does not override terminal failure with sticky failure', () => { |
| 46 | + service.markTerminal(model, 'quota'); |
| 47 | + service.markRetryOncePerTurn(model); |
| 48 | + expect(service.snapshot(model)).toEqual({ |
| 49 | + available: false, |
| 50 | + reason: 'quota', |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('selects models respecting terminal and sticky states', () => { |
| 55 | + const stickyModel = 'stick-model'; |
| 56 | + const healthyModel = 'healthy-model'; |
| 57 | + |
| 58 | + service.markTerminal(model, 'capacity'); |
| 59 | + service.markRetryOncePerTurn(stickyModel); |
| 60 | + |
| 61 | + const first = service.selectFirstAvailable([ |
| 62 | + model, |
| 63 | + stickyModel, |
| 64 | + healthyModel, |
| 65 | + ]); |
| 66 | + expect(first).toEqual({ |
| 67 | + selected: stickyModel, |
| 68 | + attempts: 1, |
| 69 | + skipped: [ |
| 70 | + { |
| 71 | + model, |
| 72 | + reason: 'capacity', |
| 73 | + }, |
| 74 | + ], |
| 75 | + }); |
| 76 | + |
| 77 | + service.consumeStickyAttempt(stickyModel); |
| 78 | + const second = service.selectFirstAvailable([ |
| 79 | + model, |
| 80 | + stickyModel, |
| 81 | + healthyModel, |
| 82 | + ]); |
| 83 | + expect(second).toEqual({ |
| 84 | + selected: healthyModel, |
| 85 | + skipped: [ |
| 86 | + { |
| 87 | + model, |
| 88 | + reason: 'capacity', |
| 89 | + }, |
| 90 | + { |
| 91 | + model: stickyModel, |
| 92 | + reason: 'retry_once_per_turn', |
| 93 | + }, |
| 94 | + ], |
| 95 | + }); |
| 96 | + |
| 97 | + service.resetTurn(); |
| 98 | + const third = service.selectFirstAvailable([ |
| 99 | + model, |
| 100 | + stickyModel, |
| 101 | + healthyModel, |
| 102 | + ]); |
| 103 | + expect(third).toEqual({ |
| 104 | + selected: stickyModel, |
| 105 | + attempts: 1, |
| 106 | + skipped: [ |
| 107 | + { |
| 108 | + model, |
| 109 | + reason: 'capacity', |
| 110 | + }, |
| 111 | + ], |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('preserves consumed state when marking retry-once-per-turn again', () => { |
| 116 | + service.markRetryOncePerTurn(model); |
| 117 | + service.consumeStickyAttempt(model); |
| 118 | + |
| 119 | + // It is currently consumed |
| 120 | + expect(service.snapshot(model).available).toBe(false); |
| 121 | + |
| 122 | + // Marking it again should not reset the consumed flag |
| 123 | + service.markRetryOncePerTurn(model); |
| 124 | + expect(service.snapshot(model).available).toBe(false); |
| 125 | + }); |
| 126 | + |
| 127 | + it('clears consumed state when marked healthy', () => { |
| 128 | + service.markRetryOncePerTurn(model); |
| 129 | + service.consumeStickyAttempt(model); |
| 130 | + expect(service.snapshot(model).available).toBe(false); |
| 131 | + |
| 132 | + service.markHealthy(model); |
| 133 | + expect(service.snapshot(model).available).toBe(true); |
| 134 | + |
| 135 | + // If we mark it sticky again, it should be fresh (not consumed) |
| 136 | + service.markRetryOncePerTurn(model); |
| 137 | + expect(service.snapshot(model).available).toBe(true); |
| 138 | + }); |
| 139 | + |
| 140 | + it('resetTurn resets consumed state for multiple sticky models', () => { |
| 141 | + const model2 = 'model-2'; |
| 142 | + service.markRetryOncePerTurn(model); |
| 143 | + service.markRetryOncePerTurn(model2); |
| 144 | + |
| 145 | + service.consumeStickyAttempt(model); |
| 146 | + service.consumeStickyAttempt(model2); |
| 147 | + |
| 148 | + expect(service.snapshot(model).available).toBe(false); |
| 149 | + expect(service.snapshot(model2).available).toBe(false); |
| 150 | + |
| 151 | + service.resetTurn(); |
| 152 | + |
| 153 | + expect(service.snapshot(model).available).toBe(true); |
| 154 | + expect(service.snapshot(model2).available).toBe(true); |
| 155 | + }); |
| 156 | + |
| 157 | + it('resetTurn does not affect terminal models', () => { |
| 158 | + service.markTerminal(model, 'quota'); |
| 159 | + service.resetTurn(); |
| 160 | + expect(service.snapshot(model)).toEqual({ |
| 161 | + available: false, |
| 162 | + reason: 'quota', |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments