|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { |
| 3 | + parseAntigravityQuotaSummary, |
| 4 | + parseAntigravityUserPlan, |
| 5 | + probeLocalAntigravityLanguageServer |
| 6 | +} from './antigravity-local-probe' |
| 7 | + |
| 8 | +const sampleQuotaSummaryResponse = { |
| 9 | + response: { |
| 10 | + groups: [ |
| 11 | + { |
| 12 | + displayName: 'Gemini Models', |
| 13 | + description: 'Models within this group: Gemini Flash, Gemini Pro', |
| 14 | + buckets: [ |
| 15 | + { |
| 16 | + bucketId: 'gemini-weekly', |
| 17 | + displayName: 'Weekly Limit Remaining', |
| 18 | + description: 'Weekly quota refresh', |
| 19 | + window: 'weekly', |
| 20 | + remainingFraction: 0.788, |
| 21 | + resetTime: '2026-09-29T23:37:07Z' |
| 22 | + }, |
| 23 | + { |
| 24 | + bucketId: 'gemini-5h', |
| 25 | + displayName: 'Five Hour Limit Remaining', |
| 26 | + description: 'Five hour quota refresh', |
| 27 | + window: '5h', |
| 28 | + remainingFraction: 0.927, |
| 29 | + resetTime: '2026-09-24T01:38:43Z' |
| 30 | + } |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + displayName: 'Claude and GPT models', |
| 35 | + description: 'Models within this group: Claude Opus, Claude Sonnet, GPT-OSS', |
| 36 | + buckets: [ |
| 37 | + { |
| 38 | + bucketId: '3p-weekly', |
| 39 | + displayName: 'Weekly Limit Remaining', |
| 40 | + window: 'weekly', |
| 41 | + remainingFraction: 0.95, |
| 42 | + resetTime: '2026-09-30T00:24:39Z' |
| 43 | + }, |
| 44 | + { |
| 45 | + bucketId: '3p-5h', |
| 46 | + displayName: 'Five Hour Limit Remaining', |
| 47 | + window: '5h', |
| 48 | + remainingFraction: 1, |
| 49 | + resetTime: '2026-09-24T02:50:24Z' |
| 50 | + } |
| 51 | + ] |
| 52 | + } |
| 53 | + ] |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +describe('parseAntigravityQuotaSummary', () => { |
| 58 | + it('extracts both 5h session and weekly windows separately', () => { |
| 59 | + const parsed = parseAntigravityQuotaSummary(sampleQuotaSummaryResponse) |
| 60 | + |
| 61 | + expect(parsed.session).toEqual({ |
| 62 | + usedPercent: 7, // 1 - 0.927 = 0.073 => 7% |
| 63 | + windowMinutes: 300, |
| 64 | + resetsAt: new Date('2026-09-24T01:38:43Z').getTime(), |
| 65 | + resetDescription: null |
| 66 | + }) |
| 67 | + |
| 68 | + expect(parsed.weekly).toEqual({ |
| 69 | + usedPercent: 21, // 1 - 0.788 = 0.212 => 21% |
| 70 | + windowMinutes: 10080, |
| 71 | + resetsAt: new Date('2026-09-29T23:37:07Z').getTime(), |
| 72 | + resetDescription: null |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + it('selects the most constrained window across groups', () => { |
| 77 | + const responseWithHigh3pUsage = { |
| 78 | + response: { |
| 79 | + groups: [ |
| 80 | + { |
| 81 | + displayName: 'Gemini Models', |
| 82 | + buckets: [ |
| 83 | + { |
| 84 | + window: '5h', |
| 85 | + remainingFraction: 0.9, |
| 86 | + resetTime: '2026-09-24T01:00:00Z' |
| 87 | + }, |
| 88 | + { |
| 89 | + window: 'weekly', |
| 90 | + remainingFraction: 0.8, |
| 91 | + resetTime: '2026-09-30T00:00:00Z' |
| 92 | + } |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + displayName: 'Claude and GPT models', |
| 97 | + buckets: [ |
| 98 | + { |
| 99 | + window: '5h', |
| 100 | + remainingFraction: 0.3, // 70% used! |
| 101 | + resetTime: '2026-09-24T02:00:00Z' |
| 102 | + }, |
| 103 | + { |
| 104 | + window: 'weekly', |
| 105 | + remainingFraction: 0.5, // 50% used! |
| 106 | + resetTime: '2026-09-30T00:00:00Z' |
| 107 | + } |
| 108 | + ] |
| 109 | + } |
| 110 | + ] |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + const parsed = parseAntigravityQuotaSummary(responseWithHigh3pUsage) |
| 115 | + expect(parsed.session?.usedPercent).toBe(70) |
| 116 | + expect(parsed.weekly?.usedPercent).toBe(50) |
| 117 | + }) |
| 118 | + |
| 119 | + it('handles empty or malformed data safely', () => { |
| 120 | + expect(parseAntigravityQuotaSummary(null)).toEqual({ session: null, weekly: null }) |
| 121 | + expect(parseAntigravityQuotaSummary({})).toEqual({ session: null, weekly: null }) |
| 122 | + expect(parseAntigravityQuotaSummary({ groups: [] })).toEqual({ session: null, weekly: null }) |
| 123 | + }) |
| 124 | +}) |
| 125 | + |
| 126 | +describe('parseAntigravityUserPlan', () => { |
| 127 | + it('extracts plan name from user status structure', () => { |
| 128 | + const raw = { |
| 129 | + userStatus: { |
| 130 | + planStatus: { |
| 131 | + planInfo: { |
| 132 | + planName: 'Pro' |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + expect(parseAntigravityUserPlan(raw)).toBe('Pro') |
| 138 | + }) |
| 139 | + |
| 140 | + it('handles missing plan information gracefully', () => { |
| 141 | + expect(parseAntigravityUserPlan(null)).toBeNull() |
| 142 | + expect(parseAntigravityUserPlan({})).toBeNull() |
| 143 | + }) |
| 144 | +}) |
| 145 | + |
| 146 | +describe('probeLocalAntigravityLanguageServer', () => { |
| 147 | + it('returns null if the language server process is not found', async () => { |
| 148 | + const result = await probeLocalAntigravityLanguageServer({ |
| 149 | + deps: { |
| 150 | + findProcess: vi.fn().mockResolvedValue(null) |
| 151 | + } |
| 152 | + }) |
| 153 | + expect(result).toBeNull() |
| 154 | + }) |
| 155 | + |
| 156 | + it('returns null if no listening ports are found', async () => { |
| 157 | + const result = await probeLocalAntigravityLanguageServer({ |
| 158 | + deps: { |
| 159 | + findProcess: vi.fn().mockResolvedValue({ pid: 1234, csrfToken: 'test-token' }), |
| 160 | + findPorts: vi.fn().mockResolvedValue([]) |
| 161 | + } |
| 162 | + }) |
| 163 | + expect(result).toBeNull() |
| 164 | + }) |
| 165 | + |
| 166 | + it('probes the local port and returns ok ProviderRateLimits with separated 5h and weekly', async () => { |
| 167 | + const requestJson = vi.fn().mockImplementation(async ({ path }: { path: string }) => { |
| 168 | + if (path.includes('RetrieveUserQuotaSummary')) { |
| 169 | + return { status: 200, body: sampleQuotaSummaryResponse } |
| 170 | + } |
| 171 | + if (path.includes('GetUserStatus')) { |
| 172 | + return { |
| 173 | + status: 200, |
| 174 | + body: { userStatus: { planStatus: { planInfo: { planName: 'Pro' } } } } |
| 175 | + } |
| 176 | + } |
| 177 | + return { status: 404, body: null } |
| 178 | + }) |
| 179 | + |
| 180 | + const result = await probeLocalAntigravityLanguageServer({ |
| 181 | + deps: { |
| 182 | + findProcess: vi.fn().mockResolvedValue({ pid: 1234, csrfToken: 'test-csrf-token' }), |
| 183 | + findPorts: vi.fn().mockResolvedValue([56498]), |
| 184 | + requestJson |
| 185 | + } |
| 186 | + }) |
| 187 | + |
| 188 | + expect(result).not.toBeNull() |
| 189 | + expect(result?.provider).toBe('antigravity') |
| 190 | + expect(result?.status).toBe('ok') |
| 191 | + expect(result?.planType).toBe('Pro') |
| 192 | + expect(result?.buckets).toBeUndefined() |
| 193 | + expect(result?.session).toEqual({ |
| 194 | + usedPercent: 7, |
| 195 | + windowMinutes: 300, |
| 196 | + resetsAt: new Date('2026-09-24T01:38:43Z').getTime(), |
| 197 | + resetDescription: null |
| 198 | + }) |
| 199 | + expect(result?.weekly).toEqual({ |
| 200 | + usedPercent: 21, |
| 201 | + windowMinutes: 10080, |
| 202 | + resetsAt: new Date('2026-09-29T23:37:07Z').getTime(), |
| 203 | + resetDescription: null |
| 204 | + }) |
| 205 | + }) |
| 206 | +}) |
0 commit comments