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

6.1 KB
Raw Blame
/**
 * tests/unit/quota-store-factory.test.ts
 *
 * Coverage for src/lib/quota/storeFactory.ts:
 *   - Default driver = sqlite
 *   - Env override QUOTA_STORE_DRIVER=redis + URL → redis store (if ioredis available)
 *   - Driver redis + URL absent → fallback sqlite
 *   - Singleton: multiple calls return same instance
 *   - resetQuotaStoreSingleton() resets
 */

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-store-factory-"));
process.env.DATA_DIR = TEST_DATA_DIR;

const core = await import("../../src/lib/db/core.ts");

async function resetStorage() {
  core.resetDbInstance();
  for (let attempt = 0; attempt < 10; attempt++) {
    try {
      if (fs.existsSync(TEST_DATA_DIR)) {
        fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
      }
      break;
    } catch (err: unknown) {
      const e = err as { code?: string };
      if ((e?.code === "EBUSY" || e?.code === "EPERM") && attempt < 9) {
        await new Promise((resolve) => setTimeout(resolve, 50 * (attempt + 1)));
      } else {
        throw err;
      }
    }
  }
  fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}

const origDriver = process.env.QUOTA_STORE_DRIVER;
const origRedisUrl = process.env.QUOTA_STORE_REDIS_URL;

test.beforeEach(async () => {
  await resetStorage();
  // Reset env
  delete process.env.QUOTA_STORE_DRIVER;
  delete process.env.QUOTA_STORE_REDIS_URL;
  // Reset singleton
  const { resetQuotaStoreSingleton } = await import("../../src/lib/quota/storeFactory.ts");
  resetQuotaStoreSingleton();
});

test.after(async () => {
  core.resetDbInstance();
  if (fs.existsSync(TEST_DATA_DIR)) {
    fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
  }
  // Restore env
  if (origDriver !== undefined) process.env.QUOTA_STORE_DRIVER = origDriver;
  else delete process.env.QUOTA_STORE_DRIVER;
  if (origRedisUrl !== undefined) process.env.QUOTA_STORE_REDIS_URL = origRedisUrl;
  else delete process.env.QUOTA_STORE_REDIS_URL;
});

// ─── Default driver ──────────────────────────────────────────────────────────

test("storeFactory: default driver is sqlite", async () => {
  const { getQuotaStore, resetQuotaStoreSingleton } =
    await import("../../src/lib/quota/storeFactory.ts");
  resetQuotaStoreSingleton();

  const store = await getQuotaStore();
  assert.ok(store, "Should return a store");
  // SQLite store has consume/peek/poolUsage/clear
  assert.ok(typeof store.consume === "function");
  assert.ok(typeof store.peek === "function");
  assert.ok(typeof store.poolUsage === "function");
  assert.ok(typeof store.clear === "function");
});

// ─── Singleton behaviour ─────────────────────────────────────────────────────

test("storeFactory: multiple calls return same singleton", async () => {
  const { getQuotaStore, resetQuotaStoreSingleton } =
    await import("../../src/lib/quota/storeFactory.ts");
  resetQuotaStoreSingleton();

  const store1 = await getQuotaStore();
  const store2 = await getQuotaStore();
  assert.strictEqual(store1, store2);
});

test("storeFactory: resetQuotaStoreSingleton() creates new instance on next call", async () => {
  const { getQuotaStore, resetQuotaStoreSingleton } =
    await import("../../src/lib/quota/storeFactory.ts");
  resetQuotaStoreSingleton();

  const store1 = await getQuotaStore();
  resetQuotaStoreSingleton();
  const store2 = await getQuotaStore();

  // After reset, a new instance is created (may or may not be the same object
  // since singleton is re-created — but the important thing is it doesn't throw)
  assert.ok(store2, "Should return a new store after reset");
});

// ─── Redis driver + no URL → fallback sqlite ─────────────────────────────────

test("storeFactory: QUOTA_STORE_DRIVER=redis without URL → fallback to sqlite", async () => {
  const { getQuotaStore, resetQuotaStoreSingleton } =
    await import("../../src/lib/quota/storeFactory.ts");
  resetQuotaStoreSingleton();

  process.env.QUOTA_STORE_DRIVER = "redis";
  delete process.env.QUOTA_STORE_REDIS_URL;

  // Should not throw — should fall back to sqlite
  const store = await getQuotaStore();
  assert.ok(store, "Should return a valid store (sqlite fallback)");
  assert.ok(typeof store.consume === "function");
});

// ─── Unknown driver → fallback sqlite ────────────────────────────────────────

test("storeFactory: unknown driver value → falls back to sqlite silently", async () => {
  const { getQuotaStore, resetQuotaStoreSingleton } =
    await import("../../src/lib/quota/storeFactory.ts");
  resetQuotaStoreSingleton();

  (process.env as Record<string, string>).QUOTA_STORE_DRIVER = "memcached";

  const store = await getQuotaStore();
  assert.ok(store, "Should return sqlite store as fallback");
  assert.ok(typeof store.consume === "function");
});

// ─── Redis driver + invalid URL (ioredis not installed) → fallback ────────────

test("storeFactory: QUOTA_STORE_DRIVER=redis with invalid URL → fallback or throws gracefully", async () => {
  const { getQuotaStore, resetQuotaStoreSingleton } =
    await import("../../src/lib/quota/storeFactory.ts");
  resetQuotaStoreSingleton();

  process.env.QUOTA_STORE_DRIVER = "redis";
  process.env.QUOTA_STORE_REDIS_URL = "redis://localhost:6380"; // likely not running

  // In test env, ioredis may or may not be installed.
  // If installed: store is created (Redis connection is lazy).
  // If not installed: factory falls back to sqlite.
  // Either way, no throw — returns a valid store.
  const store = await getQuotaStore();
  assert.ok(store, "Should always return a valid store");
  assert.ok(typeof store.consume === "function");
});