Files
OmniRoute/tests/unit/moonshot-quota-writeback.test.ts
Bob.Hou 831ea040c3 feat(quota): Moonshot Open Platform balance and TPD lock for custom nodes (#12590)
Validado em lote numa worktree combinada com os 9 PRs desta leva sobre o tip de `release/v3.8.51`: `typecheck:core` limpo, `check:provider-consistency` OK (273 entradas REGISTRY, **356** providers canônicos), `check-docs-counts-sync` exit 0 e **300/300** nos testes que a leva toca.

O crescimento de arquivo que os PRs empilham uns sobre os outros foi rebaselinado num único registro datado (`_rebaseline_2026_09_03_houminxi_batch`), com a decomposição por arquivo: `providers/page.tsx` +18 (import CSV do #12504 + busca do #12495 no mesmo painel), `accountFallback.ts` +6 (o #12566 sobre o rebaseline que o #12590 já registrou — os dois tocam `checkFallbackError`) e `chatCore.ts` +3 (invalidez de cache de quota no 429 do #12325). As violações restantes (`codex.ts`, `stream.ts`) foram medidas também no tip puro e são drift da base, não desta leva.
2026-09-03 12:47:50 -03:00

51 lines
2.1 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { classify429 } from "../../src/shared/utils/classify429.ts";
import { checkFallbackError } from "../../open-sse/services/accountFallback.ts";
import { registerMoonshotFetchersForNodes } from "../../open-sse/services/moonshotQuotaFetcher.ts";
import { getQuotaFetcher } from "../../open-sse/services/quotaPreflight.ts";
const MOONSHOT_TPD =
"Your account org-73b383ab6d45484eb2ef72161074495c / proj-30863601b47548bdbbabc42ff4c72eee <ak-test> request reached organization TPD rate limit, current: 1537190, limit: 1500000";
const MOONSHOT_BROKE = "insufficient balance";
const COMPAT = "openai-compatible-chat-e2971611-bc02-4c37-8fc5-39b8e3906fdf";
test("classify429 maps Moonshot TPD to quota_exhausted so combo persist stays on", () => {
assert.equal(classify429({ status: 429, body: MOONSHOT_TPD }), "quota_exhausted");
});
test("checkFallbackError TPD with node clock returns future cooldown, not host midnight", () => {
const now = Date.parse("2026-09-02T07:30:00Z");
const result = checkFallbackError(
429,
MOONSHOT_TPD,
0,
"kimi-k2.5",
COMPAT,
null,
null,
null,
null,
{ timezone: "Asia/Shanghai", hour: 0, nowMs: now },
);
assert.equal(result.shouldFallback, true);
assert.equal(result.dailyQuotaExhausted, true);
assert.ok(result.cooldownMs > 8 * 3600_000);
const until = now + result.cooldownMs;
assert.ok(Math.abs(until - Date.parse("2026-09-02T16:00:00Z")) < 60_000);
});
test("checkFallbackError insufficient balance on compatible node is creditsExhausted", () => {
const result = checkFallbackError(429, MOONSHOT_BROKE, 0, "kimi-k2.5", COMPAT);
assert.equal(result.creditsExhausted, true);
assert.equal(result.shouldFallback, true);
});
test("registerMoonshotFetchersForNodes wires uuid and prefix after startup scan", () => {
registerMoonshotFetchersForNodes([
{ id: COMPAT, prefix: "mnative", baseUrl: "https://api.moonshot.cn/v1" },
]);
assert.equal(typeof getQuotaFetcher(COMPAT), "function");
assert.equal(typeof getQuotaFetcher("mnative"), "function");
});