Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

5.0 KB
Raw Blame
/**
 * LEDGER-3 (#3821-review) — the Antigravity local-usage fallback (#3604) replaces a
 * stale full `fetchAvailableModels` bucket (used=0) with real consumption summed from
 * `usage_history`, flipping quotaSource to "localUsageHistory". Every prior #3604 test
 * mocks only the HTTP layer, so the model-id match against usage_history.model was never
 * exercised end-to-end. This seeds a real usage_history row keyed by the upstream/public id
 * the fallback queries and asserts the flip — the regression guard for the id contract.
 *
 * Contract note: the fallback queries `usage_history WHERE model = <public model id>`
 * (e.g. gemini-3.8-flash-high), so the executor MUST log usage under that same model id
 * for the fallback to fire. This test pins exactly that join.
 */
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-ag-local-usage-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = "test-ag-local-usage-secret";

const core = await import("../../src/lib/db/core.ts");
// Load usage.ts up-front (its index.ts proxyFetch patch runs at module eval) before mocks.
const usageModule = await import("../../open-sse/services/usage.ts");
const { getUsageForProvider } = usageModule;

const originalFetch = globalThis.fetch;

test.after(() => {
  globalThis.fetch = originalFetch;
  core.resetDbInstance();
  fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});

test("Antigravity fetchAvailableModels(used=0) → localUsageHistory when usage_history has rows", async () => {
  core.resetDbInstance();

  // resetTime an hour out → the 5h local-usage window is [now-4h, now+1h).
  const resetTime = new Date(Date.now() + 60 * 60 * 1000).toISOString();
  const seededTimestamp = new Date(Date.now() - 30 * 60 * 1000).toISOString(); // within window

  // Seed a usage_history row keyed by the public upstream id the fallback queries.
  const db = core.getDbInstance() as unknown as {
    prepare: (sql: string) => { run: (...a: unknown[]) => unknown };
  };
  db.prepare(
    `INSERT INTO usage_history (provider, model, connection_id, tokens_input, tokens_output, tokens_reasoning, success, timestamp)
     VALUES (?, ?, ?, ?, ?, ?, 1, ?)`
  ).run("antigravity", "gemini-3.8-flash-high", "conn-local-1", 1000, 1500, 500, seededTimestamp);
  // Total seeded tokens = 3000 → ceil(3000/1000) = 3 units used.

  globalThis.fetch = (async (input: any) => {
    const url = typeof input === "string" ? input : input?.url || "";
    // retrieveUserQuota (the live signal) is unavailable → falls back to fetchAvailableModels.
    if (url.includes("retrieveUserQuota")) {
      return { ok: false, status: 404, json: async () => ({}) } as Response;
    }
    // fetchAvailableModels returns a FULL (stale) bucket: remainingFraction 1.0 + resetTime.
    return {
      ok: true,
      json: async () => ({
        models: {
          "gemini-3.8-flash-high": {
            quotaInfo: { remainingFraction: 1.0, resetTime },
          },
        },
      }),
    } as Response;
  }) as typeof fetch;

  const connection = {
    id: "conn-local-1",
    provider: "antigravity",
    accessToken: "fake-token-local-usage-unique",
    providerSpecificData: {},
    projectId: undefined,
  };

  const result = await getUsageForProvider(connection, { forceRefresh: true });
  assert.ok(result && "quotas" in result, "should return quotas");
  const quota = (result as any).quotas["gemini-3.8-flash-high"];
  assert.ok(quota, "should have the gemini-3.8-flash-high quota");
  assert.equal(quota.quotaSource, "localUsageHistory", "stale full bucket replaced by local usage");
  assert.equal(quota.used, 3, "3000 seeded tokens → 3 units used");
});

test("Antigravity stays fetchAvailableModels when usage_history has no matching rows", async () => {
  core.resetDbInstance();

  const resetTime = new Date(Date.now() + 60 * 60 * 1000).toISOString();

  globalThis.fetch = (async (input: any) => {
    const url = typeof input === "string" ? input : input?.url || "";
    if (url.includes("retrieveUserQuota")) {
      return { ok: false, status: 404, json: async () => ({}) } as Response;
    }
    return {
      ok: true,
      json: async () => ({
        models: {
          "gemini-3.8-flash-high": { quotaInfo: { remainingFraction: 1.0, resetTime } },
        },
      }),
    } as Response;
  }) as typeof fetch;

  const connection = {
    id: "conn-local-2",
    provider: "antigravity",
    accessToken: "fake-token-local-usage-empty",
    providerSpecificData: {},
    projectId: undefined,
  };

  const result = await getUsageForProvider(connection, { forceRefresh: true });
  const quota = (result as any).quotas["gemini-3.8-flash-high"];
  assert.ok(quota, "should have the quota");
  assert.equal(quota.quotaSource, "fetchAvailableModels", "no local rows → keep the catalog view");
  assert.equal(quota.used, 0, "full bucket stays at 0 used");
});