Files
OmniRoute/tests/unit/save-call-log-persistence.test.ts
Diego Rodrigues de Sa e Souza 7c23dab64d Release v3.8.40
v3.8.40 cycle integration → main. All test gates green (Unit/Integration/Coverage/Node-compat/Quality-Ratchet). The only red check, 'PR Test Policy', is the test-masking heuristic firing on the cumulative ~57-commit release diff (legitimate assert consolidations already reviewed per-PR — Gemini CLI removal #5246, retired GPT models #5280, provider catalog refreshes); overridden with --admin per the documented release-PR convention. CodeQL/SonarQube advisory scans non-blocking; #5278's code already passed CodeQL on main. Homologated on VPS 192.168.0.15 (v3.8.40 healthy).
2026-06-29 08:40:06 -03:00

97 lines
3.0 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { getDbInstance } from "../../src/lib/db/core.ts";
import { saveCallLog, getCallLogs } from "../../src/lib/usage/callLogs.ts";
test("saveCallLog persists to DB with correlationId", async () => {
const db = getDbInstance();
const testId = `test-corr-${Date.now()}`;
await saveCallLog({
id: testId,
method: "POST",
path: "/v1/chat/completions",
status: 200,
model: "test-model",
provider: "test-provider",
duration: 1234,
tokens: { in: 10, out: 5 },
correlationId: "test-correlation-id-123",
sourceFormat: "openai",
targetFormat: "openai",
});
const row = db
.prepare("SELECT id, correlation_id, status, model FROM call_logs WHERE id = ?")
.get(testId) as any;
assert.ok(row, "row should exist in call_logs");
assert.equal(row.id, testId);
assert.equal(row.correlation_id, "test-correlation-id-123");
assert.equal(row.status, 200);
assert.equal(row.model, "test-model");
db.prepare("DELETE FROM call_logs WHERE id = ?").run(testId);
});
test("saveCallLog persists null correlationId when not provided", async () => {
const db = getDbInstance();
const testId = `test-nocorr-${Date.now()}`;
await saveCallLog({
id: testId,
method: "POST",
path: "/v1/chat/completions",
status: 404,
model: "test-model-2",
provider: "test-provider",
duration: 500,
tokens: {},
});
const row = db
.prepare("SELECT id, correlation_id FROM call_logs WHERE id = ?")
.get(testId) as any;
assert.ok(row, "row should exist");
assert.equal(row.correlation_id, null, "correlation_id should be null when not provided");
db.prepare("DELETE FROM call_logs WHERE id = ?").run(testId);
});
test("getCallLogs returns correlationId", async () => {
const db = getDbInstance();
const testId = `test-getcid-${Date.now()}`;
await saveCallLog({
id: testId,
method: "POST",
path: "/v1/chat/completions",
status: 200,
model: "test-model-3",
provider: "test-provider",
duration: 100,
tokens: { in: 20, out: 10 },
correlationId: "cid-roundtrip-test",
});
const logs = await getCallLogs({ limit: 100 });
const found = logs.find((l: any) => l.id === testId);
assert.ok(found, "log entry should be found via getCallLogs");
assert.equal(found.correlationId, "cid-roundtrip-test");
db.prepare("DELETE FROM call_logs WHERE id = ?").run(testId);
});
test("call_logs table has correlation_id column", () => {
const db = getDbInstance();
const columns = db.prepare("PRAGMA table_info(call_logs)").all() as any[];
const colNames = columns.map((c: any) => c.name);
assert.ok(colNames.includes("correlation_id"), "call_logs should have correlation_id column");
const indexes = db.prepare("PRAGMA index_list(call_logs)").all() as any[];
const idxNames = indexes.map((i: any) => i.name);
assert.ok(
idxNames.includes("idx_cl_correlation_id"),
"call_logs should have idx_cl_correlation_id index"
);
});