mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-16 04:03:02 +03:00
* feat(dashboard): add RADAR_ENABLED flag (default off) * feat(db): radar feed cache + settings with encrypted supporter key * feat(radar): signed feed sync with pinned key and version floor - feedSchema.ts: Zod v4 schema mirroring the server feed format (discriminated union on budget.kind, enum constraints, etc.) - pinnedKeys.ts: Ed25519 SPKI-DER pinned key + env override for forks - verify.ts: signature verification over exact wire bytes, never throws - sync.ts: full download/verify/validate/cache pipeline with injectable deps, feature-flag gate, opt-in gate, version floor (numeric compare), and sanitized error reasons (no stack traces) - 40 tests covering: contract hash, key handling, sig verification, schema validation, version compare, all sync paths (disabled, opt_out, invalid_signature, invalid_schema, stale, updated, error), auth header injection, and cache-untouched assertions for every failure mode * feat(radar): read-time overlay merge rules over the free catalog Pure function applyFeed() merges the cached Radar feed over the static baseline catalog at read time, honoring 4 rules: 1. Feed never overwrites a local override field. 2. enabled:false disables the entry with disabledBy:"radar" provenance. 3. User-added entry NOT in the feed survives untouched. 4. User deletion tombstone prevents feed resurrection. getRadarCatalog() accessor in index.ts: flag off / no cache / corrupt payload all fall back to baseline. Valid cache applies the overlay and returns feed metadata (version, tier, fetchedAt). TDD: 19 tests (4 rules + dedup + origin + accessor flag/cache/corrupt/ valid/bad-feed + baselineToMergedEntries converter). * feat(dashboard): radar catalog and guided setup screens - API routes: GET /api/radar/catalog, POST /api/radar/sync, POST /api/radar/settings - All gated on RADAR_ENABLED flag (404 when off) - Error responses via buildErrorBody(), never raw stack/message - Settings never echoes clear supporter key (masked omr_****<last4>) - Sync delegates to syncRadar() server-side, never proxies feed URL - Dashboard pages: - /dashboard/radar: 4 states (flag off, opt-in pending, empty, populated) - /dashboard/radar/setup?provider=X: guided setup with steps, key URL, test connection - Uses existing Card component and next-intl patterns - Sidebar: radar entry in costs group with icon - i18n: pt-BR and en keys for radarPage and radarSetupPage namespaces - Tests: - radar-api-routes.test.ts: 11 tests (flag-off 404, flag-on shape, error sanitization) - radar-page-state.test.ts: 5 tests (pure state logic) - All 90 radar tests pass (including prior 74) * docs(radar): module doc and flag-off inertia test Add docs/frameworks/RADAR.md covering the flag gate, the separate data-sync opt-in and privacy promise, the Ed25519 signature/pinned-key security model, tiers, the read-time overlay merge rules, and the self-hosting env vars — plus index entries in CLAUDE.md/AGENTS.md/docs/README.md/REPOSITORY_MAP.md. Document RADAR_FEED_URL and RADAR_FEED_PUBKEY in .env.example and docs/reference/ENVIRONMENT.md to satisfy check:env-doc-sync, which was failing on this branch since the sync.ts commit added the reads. Add tests/unit/radar-inertia.test.ts as the single canonical place asserting the "RADAR_ENABLED off => zero behavioral delta" claim end to end: the three /api/radar/* routes 404, the flag resolves to the definition default with no override, getRadarCatalog() returns exactly the baseline without touching the cache, and computeFreeModelTotals() keeps its pinned values with the Radar module imported alongside it. * fix(db): renumber radar migration to 135 after collision with 134 The base branch introduced 134_proxy_logs_egress_ip while this branch carried 134_radar_cache_settings; the migration runner rejects duplicate numeric prefixes. This migration has never been applied to a real database (the PR is unmerged), so no retroactive isSchemaAlreadyApplied guard is needed. * i18n(radar): translate radar catalog and setup strings to all locales The UI-coverage ratchet measures (present - placeholder) / total_en, so the __MISSING__ sentinels that i18n:sync-ui writes do not count as covered — only real translations restore the metric. Scoped to this PR's namespaces (radarPage, radarSetupPage, sidebar.radar*) instead of a bulk sync, which would have pulled ~978 unrelated pending keys into this diff. Placeholders and code identifiers verified preserved across all 1682 strings. * fix(radar): trust the served-tier header instead of the signed body field The signed feed body always carries tier:"live" by design (one signed artifact per version — rewriting the field server-side per request would break the exact-bytes Ed25519 signature). The server now returns the tier ACTUALLY served via the x-omniroute-feed-tier response header, so free users on a delayed community snapshot no longer see "Ao vivo (tempo real)" in the UI. sync.ts now reads and validates that header (falling back to the body's tier only when the header is absent or holds an unrecognized value) and stores the served tier in the cache; index.ts already surfaces cache.tier to the UI unchanged. * test(combo): shorten an assert message that exceeded the line limit The assertion added by #9507 was 104 chars, so prettier reformatted it into five lines on the next commit that touched the file, pushing it past its frozen size (3449) and failing check:file-size. The message is shortened (the issue reference stays in the comment directly above); the assertion itself is unchanged, and the file is back to 3448 lines and prettier-clean. * i18n(radar): use the canonical zh-TW glossary terms The machine translation produced retired renderings the glossary gate blocks: 供應商 for provider (canonical 提供者) and 文檔 for documentation (canonical 文件). Fixed across the 11 affected radar strings; tests/unit/i18n-glossary-consistency-check.test.ts is back to 17/17. * fix(radar): point the default feed URL at the domain that exists radar.omniroute.dev was a placeholder for a domain that was never registered, so an out-of-the-box sync would fail DNS resolution for every user. The live feed is served from radar.omniroute.online (the subdomain the design always specified), now behind Cloudflare TLS. Forks still override it via RADAR_FEED_URL. --------- Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
690 lines
21 KiB
TypeScript
690 lines
21 KiB
TypeScript
/**
|
|
* tests/unit/radar-apply-feed.test.ts
|
|
*
|
|
* TDD regression guard for the Radar read-time overlay merge rules.
|
|
*
|
|
* Tests cover:
|
|
* - 4 merge rules (local override, feed disable, user-added, tombstone)
|
|
* - flag off => baseline passthrough
|
|
* - no cache => baseline
|
|
* - corrupt cache => baseline (defensive)
|
|
* - feed-only entry gets added
|
|
* - feed fields merge over baseline when no local override
|
|
* - getRadarCatalog() accessor
|
|
*/
|
|
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
applyFeed,
|
|
type MergedEntry,
|
|
type FeedModel,
|
|
} from "../../src/lib/radar/applyFeed.ts";
|
|
import {
|
|
getRadarCatalog,
|
|
baselineToMergedEntries,
|
|
type RadarCatalogResult,
|
|
} from "../../src/lib/radar/index.ts";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Minimal fixtures — shape-matched to real types
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/** Slimmed-down baseline entries (the static free catalog shape). */
|
|
function makeBaseline(): MergedEntry[] {
|
|
return [
|
|
{
|
|
provider: "groq",
|
|
modelId: "llama-3.3-70b-versatile",
|
|
displayName: "Llama 3.3 70B Versatile",
|
|
monthlyTokens: 1_000_000,
|
|
creditTokens: 0,
|
|
freeType: "recurring-daily",
|
|
poolKey: null,
|
|
tos: "ok",
|
|
trainsOnPrompts: false,
|
|
origin: "baseline",
|
|
},
|
|
{
|
|
provider: "gemini",
|
|
modelId: "gemini-2.5-flash",
|
|
displayName: "Gemini 2.5 Flash",
|
|
monthlyTokens: 500_000,
|
|
creditTokens: 0,
|
|
freeType: "recurring-daily",
|
|
poolKey: "gemini-free-pool",
|
|
tos: "ok",
|
|
origin: "baseline",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
modelId: "mistral-small-3.1-24b-instruct:free",
|
|
displayName: "Mistral Small 3.1 24B",
|
|
monthlyTokens: 200_000,
|
|
creditTokens: 0,
|
|
freeType: "recurring-daily",
|
|
poolKey: null,
|
|
tos: "caution",
|
|
origin: "baseline",
|
|
},
|
|
];
|
|
}
|
|
|
|
function makeFeedModel(overrides: Partial<FeedModel> & { provider: string; modelId: string }): FeedModel {
|
|
return {
|
|
displayName: overrides.displayName ?? overrides.modelId,
|
|
familyId: null,
|
|
freeType: overrides.freeType ?? "recurring-daily",
|
|
budget: overrides.budget ?? { kind: "per_model", tokensPerMonth: 1_000_000 },
|
|
limits: { rpm: null, rpd: null, tpm: null, tpd: null },
|
|
contextWindow: 131072,
|
|
capabilities: { tools: true, vision: false, thinking: false },
|
|
trainsOnPrompts: null,
|
|
tosRisk: overrides.tosRisk ?? "ok",
|
|
setup: null,
|
|
enabled: overrides.enabled ?? true,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Rule 1: Feed never overwrites a local override
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("rule 1: feed does NOT overwrite a local override field", () => {
|
|
const baseline = makeBaseline();
|
|
const feed: FeedModel[] = [
|
|
makeFeedModel({
|
|
provider: "groq",
|
|
modelId: "llama-3.3-70b-versatile",
|
|
displayName: "Feed Updated Name",
|
|
tosRisk: "avoid",
|
|
budget: { kind: "per_model", tokensPerMonth: 9_999_999 },
|
|
}),
|
|
];
|
|
|
|
// User has locally overridden displayName and tos for this entry
|
|
const localOverrides = new Map<string, Partial<MergedEntry>>([
|
|
["groq:llama-3.3-70b-versatile", { displayName: "My Custom Name", tos: "ok" }],
|
|
]);
|
|
|
|
const result = applyFeed({
|
|
baseline,
|
|
feed,
|
|
localOverrides,
|
|
tombstones: new Set(),
|
|
});
|
|
|
|
const groq = result.find(
|
|
(e) => e.provider === "groq" && e.modelId === "llama-3.3-70b-versatile",
|
|
)!;
|
|
|
|
// Local override fields must survive
|
|
assert.equal(groq.displayName, "My Custom Name");
|
|
assert.equal(groq.tos, "ok");
|
|
|
|
// Feed fields that the user did NOT override should still merge
|
|
assert.equal(groq.monthlyTokens, 9_999_999);
|
|
assert.equal(groq.origin, "local");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Rule 2: enabled:false in the feed disables the entry with provenance
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("rule 2: feed enabled:false disables entry and carries disabledBy provenance", () => {
|
|
const baseline = makeBaseline();
|
|
const feed: FeedModel[] = [
|
|
makeFeedModel({
|
|
provider: "groq",
|
|
modelId: "llama-3.3-70b-versatile",
|
|
enabled: false,
|
|
}),
|
|
];
|
|
|
|
const result = applyFeed({
|
|
baseline,
|
|
feed,
|
|
localOverrides: new Map(),
|
|
tombstones: new Set(),
|
|
});
|
|
|
|
const groq = result.find(
|
|
(e) => e.provider === "groq" && e.modelId === "llama-3.3-70b-versatile",
|
|
)!;
|
|
|
|
assert.equal(groq.enabled, false);
|
|
assert.equal(groq.disabledBy, "radar");
|
|
assert.equal(groq.origin, "radar");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Rule 3: User-added entry NOT in the feed survives untouched
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("rule 3: user-added entry not in feed survives untouched", () => {
|
|
// Add a user-created entry to baseline
|
|
const baseline = [
|
|
...makeBaseline(),
|
|
{
|
|
provider: "custom",
|
|
modelId: "my-local-model",
|
|
displayName: "My Local Model",
|
|
monthlyTokens: 50_000,
|
|
creditTokens: 0,
|
|
freeType: "recurring-daily" as const,
|
|
poolKey: null,
|
|
tos: "ok" as const,
|
|
origin: "local" as const,
|
|
},
|
|
];
|
|
|
|
// Feed does NOT mention custom:my-local-model
|
|
const feed: FeedModel[] = [
|
|
makeFeedModel({ provider: "groq", modelId: "llama-3.3-70b-versatile" }),
|
|
];
|
|
|
|
const result = applyFeed({
|
|
baseline,
|
|
feed,
|
|
localOverrides: new Map(),
|
|
tombstones: new Set(),
|
|
});
|
|
|
|
const custom = result.find(
|
|
(e) => e.provider === "custom" && e.modelId === "my-local-model",
|
|
)!;
|
|
|
|
assert.equal(custom.displayName, "My Local Model");
|
|
assert.equal(custom.monthlyTokens, 50_000);
|
|
assert.equal(custom.origin, "local");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Rule 3b: User-added entry that IS in the feed => rule 1 applies (merge)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("rule 3b: user-added entry that IS in the feed merges with rule 1", () => {
|
|
const baseline = [
|
|
...makeBaseline(),
|
|
{
|
|
provider: "groq",
|
|
modelId: "llama-3.3-70b-versatile",
|
|
displayName: "My Custom Groq",
|
|
monthlyTokens: 999_000,
|
|
creditTokens: 0,
|
|
freeType: "recurring-daily" as const,
|
|
poolKey: null,
|
|
tos: "ok" as const,
|
|
origin: "local" as const,
|
|
},
|
|
];
|
|
|
|
const feed: FeedModel[] = [
|
|
makeFeedModel({
|
|
provider: "groq",
|
|
modelId: "llama-3.3-70b-versatile",
|
|
displayName: "Feed Name",
|
|
budget: { kind: "per_model", tokensPerMonth: 2_000_000 },
|
|
}),
|
|
];
|
|
|
|
// User has overridden displayName locally
|
|
const localOverrides = new Map<string, Partial<MergedEntry>>([
|
|
["groq:llama-3.3-70b-versatile", { displayName: "My Custom Groq" }],
|
|
]);
|
|
|
|
const result = applyFeed({
|
|
baseline,
|
|
feed,
|
|
localOverrides,
|
|
tombstones: new Set(),
|
|
});
|
|
|
|
const groq = result.filter(
|
|
(e) => e.provider === "groq" && e.modelId === "llama-3.3-70b-versatile",
|
|
);
|
|
|
|
// Should be deduplicated to ONE entry
|
|
assert.equal(groq.length, 1);
|
|
|
|
// Local override preserved
|
|
assert.equal(groq[0].displayName, "My Custom Groq");
|
|
|
|
// Feed field that user did not override merges through
|
|
assert.equal(groq[0].monthlyTokens, 2_000_000);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Rule 4: Tombstone prevents feed from resurrecting a deleted entry
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("rule 4: tombstone prevents feed from resurrecting a deleted entry", () => {
|
|
// Baseline has an entry for gemini, but user deleted it
|
|
const baseline = makeBaseline().filter(
|
|
(e) => !(e.provider === "gemini" && e.modelId === "gemini-2.5-flash"),
|
|
);
|
|
|
|
const feed: FeedModel[] = [
|
|
makeFeedModel({
|
|
provider: "gemini",
|
|
modelId: "gemini-2.5-flash",
|
|
displayName: "Gemini 2.5 Flash",
|
|
}),
|
|
];
|
|
|
|
// Tombstone marks this key as deleted by the user
|
|
const tombstones = new Set<string>(["gemini:gemini-2.5-flash"]);
|
|
|
|
const result = applyFeed({
|
|
baseline,
|
|
feed,
|
|
localOverrides: new Map(),
|
|
tombstones,
|
|
});
|
|
|
|
const gemini = result.find(
|
|
(e) => e.provider === "gemini" && e.modelId === "gemini-2.5-flash",
|
|
);
|
|
|
|
// Must NOT be resurrected
|
|
assert.equal(gemini, undefined);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Flag off => accessor returns baseline byte-for-byte equivalent
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("getRadarCatalog: flag off returns baseline unchanged", async () => {
|
|
// We test applyFeed directly: when called with empty feed, result = baseline
|
|
const baseline = makeBaseline();
|
|
const result = applyFeed({
|
|
baseline,
|
|
feed: [],
|
|
localOverrides: new Map(),
|
|
tombstones: new Set(),
|
|
});
|
|
|
|
assert.equal(result.length, baseline.length);
|
|
for (let i = 0; i < result.length; i++) {
|
|
assert.equal(result[i].provider, baseline[i].provider);
|
|
assert.equal(result[i].modelId, baseline[i].modelId);
|
|
assert.equal(result[i].displayName, baseline[i].displayName);
|
|
assert.equal(result[i].monthlyTokens, baseline[i].monthlyTokens);
|
|
assert.equal(result[i].origin, baseline[i].origin);
|
|
}
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// No feed (empty) => baseline passthrough
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("applyFeed: empty feed returns baseline unchanged", () => {
|
|
const baseline = makeBaseline();
|
|
const result = applyFeed({
|
|
baseline,
|
|
feed: [],
|
|
localOverrides: new Map(),
|
|
tombstones: new Set(),
|
|
});
|
|
|
|
assert.deepEqual(result, baseline);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Feed entry NOT in baseline is ADDED with origin "radar"
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("applyFeed: feed-only entry is added with origin 'radar'", () => {
|
|
const baseline = makeBaseline();
|
|
const feed: FeedModel[] = [
|
|
makeFeedModel({
|
|
provider: "new-provider",
|
|
modelId: "new-model",
|
|
displayName: "Brand New Model",
|
|
budget: { kind: "per_model", tokensPerMonth: 3_000_000 },
|
|
}),
|
|
];
|
|
|
|
const result = applyFeed({
|
|
baseline,
|
|
feed,
|
|
localOverrides: new Map(),
|
|
tombstones: new Set(),
|
|
});
|
|
|
|
const added = result.find(
|
|
(e) => e.provider === "new-provider" && e.modelId === "new-model",
|
|
);
|
|
|
|
assert.ok(added, "feed-only entry should be present");
|
|
assert.equal(added.displayName, "Brand New Model");
|
|
assert.equal(added.monthlyTokens, 3_000_000);
|
|
assert.equal(added.origin, "radar");
|
|
assert.equal(added.enabled, true);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Feed merges over baseline where no local override exists
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("applyFeed: feed fields merge over baseline where no local override", () => {
|
|
const baseline = makeBaseline();
|
|
const feed: FeedModel[] = [
|
|
makeFeedModel({
|
|
provider: "groq",
|
|
modelId: "llama-3.3-70b-versatile",
|
|
displayName: "Feed Updated Name",
|
|
tosRisk: "avoid",
|
|
budget: { kind: "per_model", tokensPerMonth: 5_000_000 },
|
|
}),
|
|
];
|
|
|
|
const result = applyFeed({
|
|
baseline,
|
|
feed,
|
|
localOverrides: new Map(),
|
|
tombstones: new Set(),
|
|
});
|
|
|
|
const groq = result.find(
|
|
(e) => e.provider === "groq" && e.modelId === "llama-3.3-70b-versatile",
|
|
)!;
|
|
|
|
// Feed values win when no local override
|
|
assert.equal(groq.displayName, "Feed Updated Name");
|
|
assert.equal(groq.tos, "avoid");
|
|
assert.equal(groq.monthlyTokens, 5_000_000);
|
|
assert.equal(groq.origin, "radar");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Corrupt feed payload => baseline passthrough (defensive)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("applyFeed: returns baseline when feed is empty (defensive corrupt scenario)", () => {
|
|
const baseline = makeBaseline();
|
|
|
|
// Simulate a corrupt/invalid feed by passing an empty array
|
|
// (the real accessor would catch JSON.parse failures before calling applyFeed)
|
|
const result = applyFeed({
|
|
baseline,
|
|
feed: [],
|
|
localOverrides: new Map(),
|
|
tombstones: new Set(),
|
|
});
|
|
|
|
assert.deepEqual(result, baseline);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Deduplication: baseline + feed with same key produces one entry
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("applyFeed: duplicate key (baseline + feed) produces single merged entry", () => {
|
|
const baseline = makeBaseline();
|
|
const feed: FeedModel[] = [
|
|
makeFeedModel({
|
|
provider: "groq",
|
|
modelId: "llama-3.3-70b-versatile",
|
|
displayName: "Feed Groq",
|
|
budget: { kind: "per_model", tokensPerMonth: 7_000_000 },
|
|
}),
|
|
];
|
|
|
|
const result = applyFeed({
|
|
baseline,
|
|
feed,
|
|
localOverrides: new Map(),
|
|
tombstones: new Set(),
|
|
});
|
|
|
|
const groqEntries = result.filter(
|
|
(e) => e.provider === "groq" && e.modelId === "llama-3.3-70b-versatile",
|
|
);
|
|
|
|
assert.equal(groqEntries.length, 1, "should be deduplicated to one entry");
|
|
assert.equal(groqEntries[0].displayName, "Feed Groq");
|
|
assert.equal(groqEntries[0].monthlyTokens, 7_000_000);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Tombstone: feed entry is tombstoned even when baseline also has it
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("rule 4b: tombstoned entry removed even when baseline has it", () => {
|
|
const baseline = makeBaseline();
|
|
const feed: FeedModel[] = [];
|
|
|
|
const tombstones = new Set<string>(["groq:llama-3.3-70b-versatile"]);
|
|
|
|
const result = applyFeed({
|
|
baseline,
|
|
feed,
|
|
localOverrides: new Map(),
|
|
tombstones,
|
|
});
|
|
|
|
const groq = result.find(
|
|
(e) => e.provider === "groq" && e.modelId === "llama-3.3-70b-versatile",
|
|
);
|
|
|
|
assert.equal(groq, undefined, "tombstoned entry should be excluded");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Entry with origin "baseline" that feed updates gets origin "radar"
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("origin switches to 'radar' when feed updates a baseline entry", () => {
|
|
const baseline = makeBaseline();
|
|
const feed: FeedModel[] = [
|
|
makeFeedModel({
|
|
provider: "gemini",
|
|
modelId: "gemini-2.5-flash",
|
|
displayName: "Updated Gemini",
|
|
budget: { kind: "per_model", tokensPerMonth: 800_000 },
|
|
}),
|
|
];
|
|
|
|
const result = applyFeed({
|
|
baseline,
|
|
feed,
|
|
localOverrides: new Map(),
|
|
tombstones: new Set(),
|
|
});
|
|
|
|
const gemini = result.find(
|
|
(e) => e.provider === "gemini" && e.modelId === "gemini-2.5-flash",
|
|
)!;
|
|
|
|
assert.equal(gemini.origin, "radar");
|
|
assert.equal(gemini.displayName, "Updated Gemini");
|
|
});
|
|
|
|
// ===========================================================================
|
|
// getRadarCatalog() accessor tests
|
|
// ===========================================================================
|
|
|
|
/** Minimal valid RadarFeed payload (passes RadarFeedSchema.parse). */
|
|
const VALID_FEED_JSON = JSON.stringify({
|
|
feed: "omniroute-radar",
|
|
schemaVersion: 1,
|
|
version: "2026.08.01.1",
|
|
generatedAt: "2026-08-01T12:00:00Z",
|
|
tier: "community",
|
|
counts: { providers: 1, models: 1 },
|
|
providers: [{ id: "groq", name: "Groq" }],
|
|
models: [
|
|
{
|
|
provider: "groq",
|
|
modelId: "llama-3.3-70b-versatile",
|
|
displayName: "Feed Groq Name",
|
|
familyId: "llama-3.3-70b",
|
|
freeType: "recurring-daily",
|
|
budget: { kind: "per_model", tokensPerMonth: 5_000_000 },
|
|
limits: { rpm: 30, rpd: 14400, tpm: 6000, tpd: null },
|
|
contextWindow: 131072,
|
|
capabilities: { tools: true, vision: false, thinking: false },
|
|
trainsOnPrompts: null,
|
|
tosRisk: "ok",
|
|
setup: { keyUrl: "https://console.groq.com/keys", steps: ["Step 1"] },
|
|
enabled: true,
|
|
},
|
|
],
|
|
quirks: [],
|
|
totals: { dedupedTokensPerMonth: 5_000_000, modelCount: 1, poolCount: 0 },
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Accessor: flag off => baseline passthrough, no cache read
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("getRadarCatalog: flag off returns baseline and does NOT read cache", () => {
|
|
let cacheRead = false;
|
|
|
|
const result = getRadarCatalog({
|
|
getFlag: () => false,
|
|
getCache: () => {
|
|
cacheRead = true;
|
|
return null;
|
|
},
|
|
baseline: makeBaseline(),
|
|
});
|
|
|
|
assert.equal(result.entries.length, 3, "baseline entries returned");
|
|
assert.equal(result.meta, null, "no meta when flag off");
|
|
assert.equal(cacheRead, false, "cache should not be read when flag is off");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Accessor: no cache => baseline
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("getRadarCatalog: no cache returns baseline", () => {
|
|
const result = getRadarCatalog({
|
|
getFlag: () => true,
|
|
getCache: () => null,
|
|
baseline: makeBaseline(),
|
|
});
|
|
|
|
assert.equal(result.entries.length, 3);
|
|
assert.equal(result.meta, null);
|
|
assert.equal(result.entries[0].origin, "baseline");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Accessor: corrupt cached payload => baseline (defensive), no throw
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("getRadarCatalog: corrupt payload returns baseline without throwing", () => {
|
|
const result = getRadarCatalog({
|
|
getFlag: () => true,
|
|
getCache: () => ({
|
|
version: "2026.08.01.1",
|
|
tier: "community",
|
|
payload: "{invalid json!!!",
|
|
fetchedAt: "2026-08-01T12:00:00Z",
|
|
}),
|
|
baseline: makeBaseline(),
|
|
});
|
|
|
|
assert.equal(result.entries.length, 3);
|
|
assert.equal(result.meta, null);
|
|
assert.equal(result.entries[0].origin, "baseline");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Accessor: valid cache => applyFeed output + meta
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("getRadarCatalog: valid cache returns merged entries with meta", () => {
|
|
const result = getRadarCatalog({
|
|
getFlag: () => true,
|
|
getCache: () => ({
|
|
version: "2026.08.01.1",
|
|
tier: "community",
|
|
payload: VALID_FEED_JSON,
|
|
fetchedAt: "2026-08-01T12:00:00Z",
|
|
}),
|
|
baseline: makeBaseline(),
|
|
});
|
|
|
|
// The feed has groq:llama-3.3-70b-versatile, which merges over baseline
|
|
const groq = result.entries.find(
|
|
(e) => e.provider === "groq" && e.modelId === "llama-3.3-70b-versatile",
|
|
)!;
|
|
|
|
assert.equal(groq.displayName, "Feed Groq Name");
|
|
assert.equal(groq.monthlyTokens, 5_000_000);
|
|
assert.equal(groq.origin, "radar");
|
|
|
|
// Meta is present
|
|
assert.ok(result.meta);
|
|
assert.equal(result.meta.version, "2026.08.01.1");
|
|
assert.equal(result.meta.tier, "community");
|
|
assert.equal(result.meta.fetchedAt, "2026-08-01T12:00:00Z");
|
|
|
|
// Other baseline entries survive
|
|
assert.equal(result.entries.length, 3);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Accessor: schema-valid but wrong feed name => falls back to baseline
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("getRadarCatalog: wrong feed literal falls back to baseline", () => {
|
|
const badPayload = JSON.stringify({
|
|
feed: "wrong-feed-name",
|
|
schemaVersion: 1,
|
|
version: "2026.08.01.1",
|
|
generatedAt: "2026-08-01T12:00:00Z",
|
|
tier: "community",
|
|
counts: { providers: 0, models: 0 },
|
|
providers: [],
|
|
models: [],
|
|
quirks: [],
|
|
totals: { dedupedTokensPerMonth: 0, modelCount: 0, poolCount: 0 },
|
|
});
|
|
|
|
const result = getRadarCatalog({
|
|
getFlag: () => true,
|
|
getCache: () => ({
|
|
version: "2026.08.01.1",
|
|
tier: "community",
|
|
payload: badPayload,
|
|
fetchedAt: "2026-08-01T12:00:00Z",
|
|
}),
|
|
baseline: makeBaseline(),
|
|
});
|
|
|
|
assert.equal(result.entries.length, 3, "baseline returned for bad feed literal");
|
|
assert.equal(result.meta, null);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// baselineToMergedEntries converter
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("baselineToMergedEntries: converts FreeModelBudget shape to MergedEntry", () => {
|
|
const budgets = [
|
|
{
|
|
provider: "test",
|
|
modelId: "model-1",
|
|
displayName: "Test Model",
|
|
monthlyTokens: 100_000,
|
|
creditTokens: 0,
|
|
freeType: "recurring-daily" as const,
|
|
poolKey: null,
|
|
tos: "ok" as const,
|
|
},
|
|
];
|
|
|
|
const entries = baselineToMergedEntries(budgets);
|
|
|
|
assert.equal(entries.length, 1);
|
|
assert.equal(entries[0].provider, "test");
|
|
assert.equal(entries[0].origin, "baseline");
|
|
assert.equal(entries[0].enabled, true);
|
|
});
|