Files
OmniRoute/tests/unit/idempotency.test.mjs
T
diegosouzapw 71d14209a4 feat: OmniRoute v1.0.0 — Intelligent AI Gateway & Universal LLM Proxy
OmniRoute is an intelligent API gateway that unifies 20+ AI providers behind a single
OpenAI-compatible endpoint. Features include intelligent routing with 6 strategies,
multi-format translation (OpenAI/Claude/Gemini/Responses API), circuit breakers,
semantic caching, combo fallback chains, real-time health monitoring, and a full
dashboard with provider management, analytics, and CLI tool integration.

Key highlights:
- 20+ providers (Claude Code, Codex, Gemini CLI, GitHub Copilot, iFlow, Qwen, Kiro, etc.)
- 6 routing strategies (Fill First, Round Robin, P2C, Random, Least Used, Cost Optimized)
- Export/Import database backup with full archive support
- Translator Playground with 4 modes (Playground, Chat Tester, Test Bench, Live Monitor)
- 100% TypeScript across src/ and open-sse/
- Docker support with multi-stage builds
- Comprehensive documentation and 9 dashboard screenshots
2026-02-18 00:02:15 -03:00

84 lines
2.5 KiB
JavaScript

import { describe, it, beforeEach } from "node:test";
import assert from "node:assert/strict";
import {
getIdempotencyKey,
checkIdempotency,
saveIdempotency,
clearIdempotency,
getIdempotencyStats,
} from "../../src/lib/idempotencyLayer.ts";
describe("Idempotency Layer", () => {
beforeEach(() => {
clearIdempotency();
});
describe("getIdempotencyKey", () => {
it("returns null for null headers", () => {
assert.equal(getIdempotencyKey(null), null);
});
it("returns Idempotency-Key header", () => {
const headers = new Headers({ "Idempotency-Key": "abc-123" });
assert.equal(getIdempotencyKey(headers), "abc-123");
});
it("returns X-Request-Id header", () => {
const headers = new Headers({ "X-Request-Id": "req-456" });
assert.equal(getIdempotencyKey(headers), "req-456");
});
it("prefers Idempotency-Key over X-Request-Id", () => {
const headers = new Headers({
"Idempotency-Key": "idemp-1",
"X-Request-Id": "req-2",
});
assert.equal(getIdempotencyKey(headers), "idemp-1");
});
it("supports plain object headers", () => {
const headers = { "idempotency-key": "obj-key" };
assert.equal(getIdempotencyKey(headers), "obj-key");
});
});
describe("checkIdempotency / saveIdempotency", () => {
it("returns null for unknown key", () => {
assert.equal(checkIdempotency("unknown"), null);
});
it("returns null for null key", () => {
assert.equal(checkIdempotency(null), null);
});
it("returns cached response within window", () => {
const response = { choices: [{ message: { content: "hello" } }] };
saveIdempotency("key-1", response, 200);
const result = checkIdempotency("key-1");
assert.deepEqual(result, { response, status: 200 });
});
it("returns null after expiry", async () => {
const response = { choices: [] };
saveIdempotency("key-2", response, 200, 50); // 50ms window
await new Promise((r) => setTimeout(r, 100));
assert.equal(checkIdempotency("key-2"), null);
});
it("does nothing for null key", () => {
saveIdempotency(null, { data: 1 }, 200);
assert.equal(getIdempotencyStats().activeKeys, 0);
});
});
describe("getIdempotencyStats", () => {
it("reports active keys", () => {
saveIdempotency("a", {}, 200);
saveIdempotency("b", {}, 200);
const stats = getIdempotencyStats();
assert.equal(stats.activeKeys, 2);
assert.equal(stats.windowMs, 5000);
});
});
});