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

1.3 KB
Raw Blame
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import test from "node:test";

const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-gam-history-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET ?? "gam-test-secret";
delete process.env.INITIAL_PASSWORD;

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

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

test("transfer history lists entries recorded in the same second newest first", async () => {
  const db = core.getDbInstance();
  const insert = db.prepare(
    `INSERT INTO token_ledger (from_api_key_id, to_api_key_id, amount, reason, idempotency_key, created_at)
     VALUES (?, ?, ?, ?, ?, '2026-01-01 00:00:00')`
  );
  insert.run("a", "b", 1, "first", "i1");
  insert.run("a", "b", 2, "second", "i2");
  insert.run("a", "b", 3, "third", "i3");
  const history = await sharing.getHistory("b");
  assert.deepEqual(
    history.map((entry: { reason: string }) => entry.reason),
    ["third", "second", "first"]
  );
});