|
| 1 | +/** |
| 2 | + * Antigravity image generation uses a non-chat model, but Google reports a |
| 3 | + * model-specific quota bucket for it. Provider Limits must keep that bucket |
| 4 | + * without adding image/TTS/tab models to chat discovery. |
| 5 | + */ |
| 6 | +import test from "node:test"; |
| 7 | +import assert from "node:assert/strict"; |
| 8 | +import fs from "node:fs"; |
| 9 | +import os from "node:os"; |
| 10 | +import path from "node:path"; |
| 11 | + |
| 12 | +const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-ag-image-quota-")); |
| 13 | +process.env.DATA_DIR = TEST_DATA_DIR; |
| 14 | +process.env.API_KEY_SECRET = "test-ag-image-quota-secret"; |
| 15 | + |
| 16 | +const core = await import("../../src/lib/db/core.ts"); |
| 17 | +const { getUsageForProvider } = await import("../../open-sse/services/usage.ts"); |
| 18 | +const { |
| 19 | + isDiscoverableAntigravityModelId, |
| 20 | + isUserVisibleAntigravityQuotaModelId, |
| 21 | +} = await import("../../open-sse/config/antigravityModelAliases.ts"); |
| 22 | +const originalFetch = globalThis.fetch; |
| 23 | + |
| 24 | +const RESET_IN_2_HOURS = new Date(Date.now() + 2 * 60 * 60 * 1000).toISOString(); |
| 25 | + |
| 26 | +test.after(() => { |
| 27 | + globalThis.fetch = originalFetch; |
| 28 | + core.resetDbInstance(); |
| 29 | + fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }); |
| 30 | +}); |
| 31 | + |
| 32 | +test("image quota visibility stays separate from chat discovery", () => { |
| 33 | + assert.equal(isDiscoverableAntigravityModelId("gemini-3.1-flash-image"), false); |
| 34 | + assert.equal(isUserVisibleAntigravityQuotaModelId("gemini-3.1-flash-image"), true); |
| 35 | + assert.equal(isUserVisibleAntigravityQuotaModelId("gemini-3-pro-image-preview"), true); |
| 36 | + |
| 37 | + assert.equal(isUserVisibleAntigravityQuotaModelId("gemini-3.8-flash-high"), true); |
| 38 | + assert.equal(isUserVisibleAntigravityQuotaModelId("gemini-3.1-flash-tts-preview"), false); |
| 39 | + assert.equal(isUserVisibleAntigravityQuotaModelId("tab_flash_lite_preview"), false); |
| 40 | + assert.equal(isUserVisibleAntigravityQuotaModelId("gemini-3.5-flash-preview"), false); |
| 41 | + assert.equal(isUserVisibleAntigravityQuotaModelId(""), false); |
| 42 | +}); |
| 43 | + |
| 44 | +test("Provider Limits surfaces gemini-3.1-flash-image quota but hides unrelated non-chat buckets", async () => { |
| 45 | + core.resetDbInstance(); |
| 46 | + |
| 47 | + globalThis.fetch = (async (input: RequestInfo | URL) => { |
| 48 | + const url = |
| 49 | + typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; |
| 50 | + |
| 51 | + if (url.includes("retrieveUserQuotaSummary")) { |
| 52 | + return { ok: false, status: 404, json: async () => ({}) } as Response; |
| 53 | + } |
| 54 | + |
| 55 | + if (url.includes("retrieveUserQuota")) { |
| 56 | + return { |
| 57 | + ok: true, |
| 58 | + json: async () => ({ |
| 59 | + buckets: [ |
| 60 | + { |
| 61 | + modelId: "gemini-3.1-flash-image", |
| 62 | + remainingFraction: 0.25, |
| 63 | + resetTime: RESET_IN_2_HOURS, |
| 64 | + }, |
| 65 | + { |
| 66 | + modelId: "gemini-3.1-flash-tts-preview", |
| 67 | + remainingFraction: 0.5, |
| 68 | + resetTime: RESET_IN_2_HOURS, |
| 69 | + }, |
| 70 | + ], |
| 71 | + }), |
| 72 | + } as Response; |
| 73 | + } |
| 74 | + |
| 75 | + if (url.includes("fetchAvailableModels")) { |
| 76 | + return { |
| 77 | + ok: true, |
| 78 | + json: async () => ({ |
| 79 | + models: { |
| 80 | + "gemini-3.1-flash-image": { |
| 81 | + quotaInfo: { remainingFraction: 1, resetTime: RESET_IN_2_HOURS }, |
| 82 | + }, |
| 83 | + "gemini-3.1-flash-tts-preview": { |
| 84 | + quotaInfo: { remainingFraction: 1, resetTime: RESET_IN_2_HOURS }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }), |
| 88 | + } as Response; |
| 89 | + } |
| 90 | + |
| 91 | + return { |
| 92 | + ok: true, |
| 93 | + json: async () => ({ |
| 94 | + cloudaicompanionProject: { id: "test-project" }, |
| 95 | + tierId: "FREE", |
| 96 | + subscriptionType: "free", |
| 97 | + }), |
| 98 | + } as Response; |
| 99 | + }) as typeof fetch; |
| 100 | + |
| 101 | + const result = await getUsageForProvider( |
| 102 | + { |
| 103 | + id: "conn-image-quota", |
| 104 | + provider: "antigravity", |
| 105 | + accessToken: "fake-token-image-quota", |
| 106 | + providerSpecificData: { clientProfile: "cli" }, |
| 107 | + projectId: "test-project", |
| 108 | + }, |
| 109 | + { forceRefresh: true } |
| 110 | + ); |
| 111 | + |
| 112 | + assert.ok(result && "quotas" in result, "should return quotas"); |
| 113 | + const quotas = (result as any).quotas as Record<string, any>; |
| 114 | + |
| 115 | + assert.ok(quotas["gemini-3.1-flash-image"], "image quota should be visible"); |
| 116 | + assert.equal(quotas["gemini-3.1-flash-image"].remainingPercentage, 25); |
| 117 | + assert.equal(quotas["gemini-3.1-flash-image"].quotaSource, "retrieveUserQuota"); |
| 118 | + assert.equal(quotas["gemini-3.1-flash-tts-preview"], undefined); |
| 119 | +}); |
0 commit comments