mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-18 04:42:30 +03:00
Merged as part of the 39-PR owner batch of 2026-09-11, validated as a unit. Boarded into one consolidated worktree cut from `release/v3.8.51` with the other 38 — zero conflicts between them. - ESLint over every changed file: no errors (the only finding was one suppression entry the batch emptied, pruned on #13243) - `typecheck:core` clean; `check:dashboard-typecheck` OK (206 pre-existing, within baseline); `check:changelog-integrity` OK - complexity 2821 / baseline 3218 and cognitive-complexity 1272 / baseline 1437 — both under baseline - 256 assertions green: 246 under node:test and 10 under vitest, which is where `tests/unit/**/*.test.tsx` actually runs - `check-file-size`: `chatCore.ts` rebaselined 6144 → 6146 for #13278 and #13276, annotated and landed on #13243 ⚠️ base-red inherited: #12732 — the provider count (356 in the docs vs the 358 the modules define) and `open-sse/utils/stream.ts` at 3115 > frozen 3098 both reproduce on the pure tip with zero contribution from this batch.
268 lines
11 KiB
TypeScript
268 lines
11 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
const usage = await import("../../open-sse/services/usage.ts");
|
|
const { USAGE_SUPPORTED_PROVIDERS } = await import("../../src/shared/constants/providers.ts");
|
|
|
|
test("USAGE_SUPPORTED_PROVIDERS includes ollama-cloud", () => {
|
|
assert.ok(
|
|
(USAGE_SUPPORTED_PROVIDERS as string[]).includes("ollama-cloud"),
|
|
"ollama-cloud must be in the usage-supported providers allowlist"
|
|
);
|
|
});
|
|
|
|
test("USAGE_FETCHER_PROVIDERS includes ollama-cloud (#7026)", () => {
|
|
// getUsageForProvider's switch handles `case "ollama-cloud"`, and the array's doc comment
|
|
// requires it to stay in sync with that switch. If it drifts, registerGenericQuotaFetchers
|
|
// never registers a preflight quota fetcher for ollama-cloud even though the scraper exists.
|
|
assert.ok(
|
|
(usage.USAGE_FETCHER_PROVIDERS as readonly string[]).includes("ollama-cloud"),
|
|
"ollama-cloud is handled by getUsageForProvider's switch and must be listed in USAGE_FETCHER_PROVIDERS"
|
|
);
|
|
});
|
|
|
|
test("registerGenericQuotaFetchers wires a preflight quota fetcher for ollama-cloud (#7026)", async () => {
|
|
const { registerGenericQuotaFetchers } = await import(
|
|
"../../open-sse/services/genericQuotaFetcher.ts"
|
|
);
|
|
const { getQuotaFetcher } = await import("../../open-sse/services/quotaPreflight.ts");
|
|
registerGenericQuotaFetchers();
|
|
assert.ok(
|
|
getQuotaFetcher("ollama-cloud"),
|
|
"a generic quota fetcher must be registered for ollama-cloud after registerGenericQuotaFetchers()"
|
|
);
|
|
});
|
|
|
|
test("getUsageForProvider returns helpful message when Ollama Cloud has no usage cookie", async () => {
|
|
const originalCookie = process.env.OLLAMA_USAGE_COOKIE;
|
|
const originalOmniCookie = process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE;
|
|
delete process.env.OLLAMA_USAGE_COOKIE;
|
|
delete process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE;
|
|
|
|
let called = false;
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async () => {
|
|
called = true;
|
|
return new Response("unexpected", { status: 500 });
|
|
};
|
|
|
|
try {
|
|
const result = (await usage.getUsageForProvider({
|
|
id: "ollama-cloud-no-cookie",
|
|
provider: "ollama-cloud",
|
|
apiKey: "ollama-chat-key",
|
|
})) as { message?: string };
|
|
|
|
assert.equal(called, false, "settings scrape must not run without a cookie");
|
|
assert.match(result.message ?? "", /Ollama Cloud/);
|
|
assert.match(result.message ?? "", /OLLAMA_USAGE_COOKIE/);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
if (originalCookie === undefined) delete process.env.OLLAMA_USAGE_COOKIE;
|
|
else process.env.OLLAMA_USAGE_COOKIE = originalCookie;
|
|
if (originalOmniCookie === undefined) delete process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE;
|
|
else process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE = originalOmniCookie;
|
|
}
|
|
});
|
|
|
|
test("getUsageForProvider scrapes Ollama Cloud settings quota", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
const originalCookie = process.env.OLLAMA_USAGE_COOKIE;
|
|
const originalOmniCookie = process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE;
|
|
delete process.env.OLLAMA_USAGE_COOKIE;
|
|
process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE = "__Secure-session=test-cookie";
|
|
|
|
let requestUrl = "";
|
|
let requestHeaders: Headers | null = null;
|
|
let redirectMode: RequestRedirect | undefined;
|
|
|
|
globalThis.fetch = async (input, init) => {
|
|
requestUrl = String(input);
|
|
requestHeaders = new Headers(init?.headers as HeadersInit | undefined);
|
|
redirectMode = init?.redirect;
|
|
return new Response(
|
|
[
|
|
'<span class="capitalize">pro</span>',
|
|
'<div data-usage-track aria-label="34% used" style="width: 34%"></div>',
|
|
'<span class="local-time" data-time="2026-06-22T15:00:00.000Z"></span>',
|
|
'<div data-usage-track style="width: 67%"></div>',
|
|
'<span class="local-time" data-time="2026-06-29T15:00:00.000Z"></span>',
|
|
].join(""),
|
|
{ status: 200, headers: { "content-type": "text/html" } }
|
|
);
|
|
};
|
|
|
|
try {
|
|
const result = (await usage.getUsageForProvider({
|
|
id: "ollama-cloud-settings",
|
|
provider: "ollama-cloud",
|
|
apiKey: "ollama-chat-key",
|
|
})) as {
|
|
plan?: string | null;
|
|
quotas?: Record<string, { used: number; total: number; remainingPercentage: number }>;
|
|
};
|
|
|
|
assert.equal(requestUrl, "https://ollama.com/settings");
|
|
assert.equal(requestHeaders?.get("Cookie"), "__Secure-session=test-cookie");
|
|
assert.equal(redirectMode, "manual");
|
|
assert.equal(result.plan, "Ollama Cloud pro");
|
|
assert.deepEqual(Object.keys(result.quotas ?? {}), ["session", "weekly"]);
|
|
assert.equal(result.quotas!.session.used, 34);
|
|
assert.equal(result.quotas!.session.remainingPercentage, 66);
|
|
assert.equal(result.quotas!.weekly.used, 67);
|
|
assert.equal(result.quotas!.weekly.remainingPercentage, 33);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
if (originalCookie === undefined) delete process.env.OLLAMA_USAGE_COOKIE;
|
|
else process.env.OLLAMA_USAGE_COOKIE = originalCookie;
|
|
if (originalOmniCookie === undefined) delete process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE;
|
|
else process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE = originalOmniCookie;
|
|
}
|
|
});
|
|
|
|
test("getUsageForProvider keeps Ollama Cloud reset times aligned to usage tracks", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
const originalCookie = process.env.OLLAMA_USAGE_COOKIE;
|
|
const originalOmniCookie = process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE;
|
|
delete process.env.OLLAMA_USAGE_COOKIE;
|
|
process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE = "test-cookie";
|
|
|
|
globalThis.fetch = async () =>
|
|
new Response(
|
|
[
|
|
'<span class="local-time" data-time="2026-01-01T00:00:00.000Z"></span>',
|
|
'<div data-usage-track aria-label="34% used" style="width: 1%">',
|
|
'<span class="local-time" data-time="2026-06-22T15:00:00.000Z"></span>',
|
|
"</div>",
|
|
'<div data-usage-track style="width: 67%">',
|
|
'<span style="width: 1%"></span>',
|
|
'<span class="local-time" data-time="2026-06-29T15:00:00.000Z"></span>',
|
|
"</div>",
|
|
].join(""),
|
|
{ status: 200, headers: { "content-type": "text/html" } }
|
|
);
|
|
|
|
try {
|
|
const result = (await usage.getUsageForProvider({
|
|
id: "ollama-cloud-aligned-times",
|
|
provider: "ollama-cloud",
|
|
apiKey: "ollama-chat-key",
|
|
})) as {
|
|
quotas?: Record<string, { used: number; resetAt: string | null }>;
|
|
};
|
|
|
|
assert.equal(result.quotas!.session.used, 34);
|
|
assert.equal(result.quotas!.session.resetAt, "2026-06-22T15:00:00.000Z");
|
|
assert.equal(result.quotas!.weekly.used, 67);
|
|
assert.equal(result.quotas!.weekly.resetAt, "2026-06-29T15:00:00.000Z");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
if (originalCookie === undefined) delete process.env.OLLAMA_USAGE_COOKIE;
|
|
else process.env.OLLAMA_USAGE_COOKIE = originalCookie;
|
|
if (originalOmniCookie === undefined) delete process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE;
|
|
else process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE = originalOmniCookie;
|
|
}
|
|
});
|
|
|
|
test("getUsageForProvider reports expired Ollama Cloud cookies on redirect", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
const originalCookie = process.env.OLLAMA_USAGE_COOKIE;
|
|
process.env.OLLAMA_USAGE_COOKIE = "expired-cookie";
|
|
|
|
globalThis.fetch = async () =>
|
|
new Response("", {
|
|
status: 302,
|
|
headers: { location: "/signin" },
|
|
});
|
|
|
|
try {
|
|
const result = (await usage.getUsageForProvider({
|
|
id: "ollama-cloud-redirect",
|
|
provider: "ollama-cloud",
|
|
apiKey: "ollama-chat-key",
|
|
})) as { message?: string };
|
|
|
|
assert.match(result.message ?? "", /authentication expired/i);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
if (originalCookie === undefined) delete process.env.OLLAMA_USAGE_COOKIE;
|
|
else process.env.OLLAMA_USAGE_COOKIE = originalCookie;
|
|
}
|
|
});
|
|
|
|
test("getUsageForProvider parses the current $X-of-$Y aria-label with nested width style (#12749)", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
const originalCookie = process.env.OLLAMA_USAGE_COOKIE;
|
|
const originalOmniCookie = process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE;
|
|
delete process.env.OLLAMA_USAGE_COOKIE;
|
|
process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE = "__Secure-session=test-cookie";
|
|
|
|
globalThis.fetch = async () =>
|
|
new Response(
|
|
[
|
|
'<span class="capitalize">pro</span>',
|
|
'<div class="relative h-3 overflow-hidden rounded-full bg-neutral-200" data-usage-track aria-label="Monthly usage $60.01 of $60 used">',
|
|
'<div class="flex h-full overflow-hidden bg-neutral-950" style="width: 100%; background: #ef4444;"></div>',
|
|
'<span class="local-time" data-time="2026-06-22T15:00:00.000Z"></span>',
|
|
"</div>",
|
|
].join(""),
|
|
{ status: 200, headers: { "content-type": "text/html" } }
|
|
);
|
|
|
|
try {
|
|
const result = (await usage.getUsageForProvider({
|
|
id: "ollama-cloud-new-markup",
|
|
provider: "ollama-cloud",
|
|
apiKey: "ollama-chat-key",
|
|
})) as { message?: string; quotas?: Record<string, { used: number }> };
|
|
|
|
assert.ok(
|
|
result.quotas && Object.keys(result.quotas).length > 0,
|
|
`expected quotas, got message: ${result.message}`
|
|
);
|
|
assert.equal(result.quotas!.session.used, 100);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
if (originalCookie === undefined) delete process.env.OLLAMA_USAGE_COOKIE;
|
|
else process.env.OLLAMA_USAGE_COOKIE = originalCookie;
|
|
if (originalOmniCookie === undefined) delete process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE;
|
|
else process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE = originalOmniCookie;
|
|
}
|
|
});
|
|
|
|
test("getUsageForProvider still finds width style on a nested child when no aria-label percent exists (#12749)", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
const originalCookie = process.env.OLLAMA_USAGE_COOKIE;
|
|
const originalOmniCookie = process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE;
|
|
delete process.env.OLLAMA_USAGE_COOKIE;
|
|
process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE = "__Secure-session=test-cookie";
|
|
|
|
globalThis.fetch = async () =>
|
|
new Response(
|
|
[
|
|
'<div class="relative h-3" data-usage-track aria-label="Weekly usage $12 of $60 used">',
|
|
'<div class="flex h-full" style="width: 20%;"></div>',
|
|
'<span class="local-time" data-time="2026-06-29T15:00:00.000Z"></span>',
|
|
"</div>",
|
|
].join(""),
|
|
{ status: 200, headers: { "content-type": "text/html" } }
|
|
);
|
|
|
|
try {
|
|
const result = (await usage.getUsageForProvider({
|
|
id: "ollama-cloud-nested-width",
|
|
provider: "ollama-cloud",
|
|
apiKey: "ollama-chat-key",
|
|
})) as { quotas?: Record<string, { used: number }> };
|
|
|
|
// The aria-label ratio ($12 of $60 = 20%) is used, matching the nested style width fallback.
|
|
assert.equal(result.quotas!.session.used, 20);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
if (originalCookie === undefined) delete process.env.OLLAMA_USAGE_COOKIE;
|
|
else process.env.OLLAMA_USAGE_COOKIE = originalCookie;
|
|
if (originalOmniCookie === undefined) delete process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE;
|
|
else process.env.OMNIROUTE_OLLAMA_USAGE_COOKIE = originalOmniCookie;
|
|
}
|
|
});
|