Files
OmniRoute/tests/unit/db-settings-split.test.ts
Diego Rodrigues de Sa e Souza 2787449f57 refactor(db): extract pricing/lkgp/cache-metrics from db/settings.ts into leaf modules (#5709)
BLOCO E3 of the god-files campaign. db/settings.ts (1154 LOC) mixed five concerns; the
three cleanly-separable ones plus the shared toRecord/JsonRecord helper were lifted out
verbatim into a new src/lib/db/settings/ subdirectory, leaving the Settings-core + Proxy
config concerns in the host (host now 646 LOC). The host re-exports every moved public
symbol so the module's public API (consumed by ~93 test files + localDb) is unchanged.

- settings/shared.ts      — toRecord + JsonRecord (9 LOC)
- settings/pricing.ts     — pricing layers/sources/per-model + update/reset (254 LOC)
- settings/lkgp.ts        — Last-Known-Good-Provider get/set/clear (49 LOC)
- settings/cacheMetrics.ts — cache metrics + trend (235 LOC)

Settings-core + the Proxy-config concern stay in the host: proxy is the most tangled
(245-line resolveProxyForConnection, resolution cache, imports from ./proxies) and
getSettings is the most central function — leaving them is the correct coupled-core stop.
Pricing/LKGP/Cache have NO dependency on Settings/Proxy helpers (verified); the
dependency DAG is acyclic (check:cycles).

Adds tests/unit/db-settings-split.test.ts: characterization of the shared toRecord helper
+ a guard asserting the host preserves its full public export surface.

Validated: typecheck:core, check:cycles (no cycles), 149 existing+new db/settings consumer
tests green (db-settings-crud/extended, 8 pricing suites, cache-metrics, 2 proxy-resolution
suites + 29 new), ESLint, Prettier, check:file-size (host 646 < frozen 1155).
2026-06-30 16:37:07 -03:00

91 lines
2.7 KiB
TypeScript

/**
* Characterization test: settings.ts god-file decomposition.
* Verifies that:
* 1. toRecord in shared.ts has correct behavior (DB-free, pure function).
* 2. The host settings.ts still re-exports the full public API surface.
*/
import { describe, it } from "node:test";
import assert from "node:assert/strict";
// ── 1. shared.ts — toRecord ──────────────────────────────────────────────────
import { toRecord } from "../../src/lib/db/settings/shared.ts";
describe("toRecord", () => {
it("returns the object as-is when given a plain object", () => {
const obj = { a: 1, b: "two" };
assert.deepStrictEqual(toRecord(obj), obj);
});
it("returns {} for null", () => {
assert.deepStrictEqual(toRecord(null), {});
});
it("returns {} for undefined", () => {
assert.deepStrictEqual(toRecord(undefined), {});
});
it("returns {} for a string", () => {
assert.deepStrictEqual(toRecord("hello"), {});
});
it("returns {} for a number", () => {
assert.deepStrictEqual(toRecord(42), {});
});
it("returns {} for an array (arrays are objects but toRecord returns the array cast)", () => {
// toRecord casts arrays as JsonRecord — they ARE objects, so the cast succeeds.
const arr = [1, 2, 3];
assert.strictEqual(toRecord(arr), arr);
});
});
// ── 2. settings.ts — public API surface ─────────────────────────────────────
const settingsModule = await import("../../src/lib/db/settings.ts");
describe("settings.ts public API surface", () => {
const expectedFunctions = [
// Settings core
"getSettings",
"updateSettings",
"isCloudEnabled",
// Proxy helpers (exported)
"bumpProxyConfigGeneration",
// Proxy config
"getProxyConfig",
"getProxyForLevel",
"setProxyForLevel",
"deleteProxyForLevel",
"resolveProxyForConnection",
"setProxyConfig",
// Pricing (re-exported from ./settings/pricing)
"getPricing",
"getPricingWithSources",
"getPricingForModel",
"updatePricing",
"resetPricing",
"resetAllPricing",
// LKGP (re-exported from ./settings/lkgp)
"getLKGP",
"setLKGP",
"clearAllLKGP",
// Cache metrics (re-exported from ./settings/cacheMetrics)
"getCacheMetrics",
"updateCacheMetrics",
"getCacheTrend",
"resetCacheMetrics",
] as const;
for (const name of expectedFunctions) {
it(`exports "${name}" as a function`, () => {
assert.strictEqual(
typeof (settingsModule as Record<string, unknown>)[name],
"function",
`Expected "${name}" to be exported as a function`
);
});
}
});