mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 08:18:10 +01:00
* chore: add a formatter, matching the style this repo already writes singleQuote=false was chosen by counting this repo's own imports, not by fleet decree. The fleet is genuinely split and the two repos that already had a .prettierrc disagreed with each other, so there was no standard to restore. Quote style does not cross repo boundaries; having a gate does. Markdown is ignored for now — prettier rewraps prose, which would bury the real diff. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * style: format with prettier (1062 files) Mechanical. No behaviour change. This SHA is listed in .git-blame-ignore-revs so `git blame` skips it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * chore: teach git blame to skip the reformat Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(lint): reattach two guards the reformat detached Both effects were one-liners, so a trailing eslint-disable-line covered the setState call: useEffect(() => { setSelected(new Set()); }, [q.length]); // eslint-disable-line ... prettier expanded the block. The comment stayed on the closing line while the call moved above it, so the guard protected nothing and the rule its author had deliberately silenced failed the build. This is the second shape of the same defect — hirnli had the eslint-disable-NEXT-line variant. Both come from the same fact: an eslint directive binds to a line number, so reformatting can silently unbind it, which makes 'mechanical, no behaviour change' false for any bulk reformat. * chore(desktop): bump the runner version the reformat requires desktop-release-drift enforces that anything built into Fleet Runner which differs from the last released tag must carry a version ahead of that tag — main read 0.8.15 and fleet-runner-v0.8.15 already shipped. A whole-repo reformat changes bytes under desktop/, so the gate fires. The alternative was adding desktop/ to .prettierignore, which would buy a quiet PR by carving a permanent hole in the formatter's coverage — the precise shape of silent drift this test was written to catch. Bumping is what the rule asks for and costs one patch release whose only content is whitespace. * chore: merge main and re-format A whole-repo reformat conflicts with any concurrent commit, and a conflicting PR runs no checks at all while reporting nothing. Conflicts resolved to main, which is correct because this branch changes formatting and nothing else. * chore: leave desktop/ out of this reformat desktop-release-drift is right to refuse this. Everything under desktop/ is built into Fleet Runner, so changing it means shipping: bump the version AND add a FLEET_RUNNER_RELEASES entry, whose stated audience is 'person who installed Fleet Runner and wants to know what changed'. For a whitespace-only build the honest answer is 'nothing', so that release should not exist — and I should not write release notes claiming otherwise. I had bumped to 0.8.16 to get the gate green. That was the wrong direction: publishing a build to three OS runners to keep a formatting PR quiet. Reverted. desktop/ is excluded in .prettierignore with the reasoning recorded there, to be formatted alongside the next real runner change, where the bump and the notes are true anyway. --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
178 lines
6.5 KiB
TypeScript
178 lines
6.5 KiB
TypeScript
/**
|
|
* Inline tests for the chat model chain (config/chat-models.ts).
|
|
*
|
|
* The chain is the difference between "one vendor's free tier runs dry and Loki
|
|
* is down" and "Loki keeps answering on someone else's". Its whole value is in
|
|
* the ORDER and the FILTERING, both of which are silent when wrong: a chain that
|
|
* drops every link still returns a plausible-looking empty array, and a chain
|
|
* that ignores a key still "works" right up until the vendor it forgot was the
|
|
* only one configured.
|
|
*
|
|
* Run: npm run test:chat-chain
|
|
*/
|
|
import {
|
|
CHAT_CHAIN,
|
|
usableChatChain,
|
|
chainFrom,
|
|
providerModels,
|
|
type ChatProvider,
|
|
} from "@/config/chat-models";
|
|
|
|
function assert(condition: boolean, message: string): void {
|
|
if (!condition) throw new Error(message);
|
|
}
|
|
|
|
let passed = 0;
|
|
const check = (label: string, fn: () => void) => {
|
|
fn();
|
|
passed += 1;
|
|
console.log(` ✓ ${label}`);
|
|
};
|
|
|
|
/** Run `fn` with exactly these env vars set, restoring whatever was there. */
|
|
function withEnv(vars: Record<string, string | undefined>, fn: () => void): void {
|
|
const before: Record<string, string | undefined> = {};
|
|
for (const [k, v] of Object.entries(vars)) {
|
|
before[k] = process.env[k];
|
|
if (v === undefined) delete process.env[k];
|
|
else process.env[k] = v;
|
|
}
|
|
try {
|
|
fn();
|
|
} finally {
|
|
for (const [k, v] of Object.entries(before)) {
|
|
if (v === undefined) delete process.env[k];
|
|
else process.env[k] = v;
|
|
}
|
|
}
|
|
}
|
|
|
|
const KEYS = { GROQ_API_KEY: undefined, OPENROUTER_API_KEY: undefined } as const;
|
|
const NO_OVERRIDES = { LOKI_GROQ_MODELS: undefined, LOKI_OPENROUTER_MODELS: undefined } as const;
|
|
const groq = CHAT_CHAIN.find((p) => p.id === "groq") as ChatProvider;
|
|
const openrouter = CHAT_CHAIN.find((p) => p.id === "openrouter") as ChatProvider;
|
|
|
|
check("the shipped chain spans MORE THAN ONE vendor", () => {
|
|
// A "chain" within a single vendor is the bug this file exists to fix: every
|
|
// link would share that vendor's daily budget, so all of them die together.
|
|
const vendors = new Set(CHAT_CHAIN.map((p) => p.id));
|
|
assert(
|
|
vendors.size >= 2,
|
|
`only ${vendors.size} vendor(s) configured: ${[...vendors].join(", ")}`,
|
|
);
|
|
});
|
|
|
|
check("every provider declares a key env and at least one model", () => {
|
|
for (const p of CHAT_CHAIN) {
|
|
assert(Boolean(p.keyEnv), `${p.id} has no keyEnv`);
|
|
assert(p.models.length > 0, `${p.id} ships no models`);
|
|
assert(p.baseUrl.startsWith("https://"), `${p.id} baseUrl is not https: ${p.baseUrl}`);
|
|
}
|
|
});
|
|
|
|
check("a provider with no key is skipped silently, not thrown on", () => {
|
|
withEnv({ ...KEYS, ...NO_OVERRIDES, GROQ_API_KEY: "x" }, () => {
|
|
const chain = usableChatChain();
|
|
assert(chain.length > 0, "keyed provider produced no links");
|
|
assert(
|
|
chain.every((l) => l.provider.id === "groq"),
|
|
"unkeyed provider leaked into the chain",
|
|
);
|
|
});
|
|
});
|
|
|
|
check("no keys at all yields an empty chain rather than a broken link", () => {
|
|
withEnv({ ...KEYS, ...NO_OVERRIDES }, () => {
|
|
assert(usableChatChain().length === 0, "produced links with no keys configured");
|
|
});
|
|
});
|
|
|
|
check("links are provider-ordered, and each carries its own provider", () => {
|
|
withEnv({ ...KEYS, ...NO_OVERRIDES, GROQ_API_KEY: "x", OPENROUTER_API_KEY: "y" }, () => {
|
|
const chain = usableChatChain();
|
|
const firstOpenRouter = chain.findIndex((l) => l.provider.id === "openrouter");
|
|
const lastGroq = chain.map((l) => l.provider.id).lastIndexOf("groq");
|
|
assert(lastGroq < firstOpenRouter, "providers are interleaved — order must follow CHAT_CHAIN");
|
|
assert(
|
|
chain.every((l) => providerModels(l.provider).includes(l.model)),
|
|
"a link carries a model its own provider does not offer",
|
|
);
|
|
});
|
|
});
|
|
|
|
// ── env override: routing around a rotted model without a deploy ─────────────
|
|
check("an env override replaces a provider's models AT CALL TIME", () => {
|
|
withEnv(
|
|
{ ...KEYS, ...NO_OVERRIDES, GROQ_API_KEY: "x", LOKI_GROQ_MODELS: "only-this-one" },
|
|
() => {
|
|
// Read after the env was set — a value frozen at import would fail here,
|
|
// and would need a redeploy to route around a dead model.
|
|
assert(providerModels(groq).join() === "only-this-one", `got ${providerModels(groq).join()}`);
|
|
assert(
|
|
usableChatChain().every((l) => l.model === "only-this-one"),
|
|
"override not applied to the chain",
|
|
);
|
|
},
|
|
);
|
|
});
|
|
|
|
check("an override accepts commas, spaces, or both", () => {
|
|
withEnv({ LOKI_OPENROUTER_MODELS: "a/one:free, b/two:free c/three:free" }, () => {
|
|
assert(
|
|
providerModels(openrouter).join("|") === "a/one:free|b/two:free|c/three:free",
|
|
"bad split",
|
|
);
|
|
});
|
|
});
|
|
|
|
check("a blank override falls back to the shipped models, not to nothing", () => {
|
|
withEnv({ LOKI_GROQ_MODELS: " " }, () => {
|
|
assert(
|
|
providerModels(groq).length === groq.models.length,
|
|
"whitespace override emptied the provider",
|
|
);
|
|
});
|
|
});
|
|
|
|
// ── chainFrom: a pin must not reintroduce the single point of failure ────────
|
|
const FAKE: ChatLinkFixture = [
|
|
{ provider: groq, model: "m1" },
|
|
{ provider: groq, model: "m2" },
|
|
{ provider: openrouter, model: "m3" },
|
|
];
|
|
type ChatLinkFixture = Array<{ provider: ChatProvider; model: string }>;
|
|
|
|
check("no pin means the whole chain", () => {
|
|
assert(chainFrom(undefined, FAKE).length === 3, "dropped links with no pin");
|
|
assert(chainFrom(" ", FAKE).length === 3, "blank pin should behave as no pin");
|
|
});
|
|
|
|
check("a pinned model STARTS the chain and keeps everything after it", () => {
|
|
const out = chainFrom("m2", FAKE);
|
|
assert(out.length === 2, `expected 2 links, got ${out.length}`);
|
|
assert(out[0]!.model === "m2" && out[1]!.model === "m3", "pin did not start the chain");
|
|
});
|
|
|
|
check("pinning the last link still leaves a chain of one, not zero", () => {
|
|
assert(
|
|
chainFrom("m3", FAKE)
|
|
.map((l) => l.model)
|
|
.join() === "m3",
|
|
"lost the pinned link",
|
|
);
|
|
});
|
|
|
|
check("an UNKNOWN model is tried first, then falls through to the real chain", () => {
|
|
// The escape hatch ("run Loki on this model") must not become the single
|
|
// point of failure this file exists to remove.
|
|
const out = chainFrom("brand-new-model", FAKE);
|
|
assert(out[0]!.model === "brand-new-model", "unknown model was not tried first");
|
|
assert(out.length === 4, `expected fallthrough to the full chain, got ${out.length}`);
|
|
});
|
|
|
|
check("an unknown model with NO usable chain yields nothing to call", () => {
|
|
assert(chainFrom("anything", []).length === 0, "invented a link with no provider configured");
|
|
});
|
|
|
|
console.log(`\n${passed} passed`);
|