fix quality gates

This commit is contained in:
Jacky Fong
2026-11-20 12:12:16 +08:00
parent 55e72bc7e9
commit a1ca8e9111
2 changed files with 31 additions and 4 deletions

View File

@@ -62,6 +62,14 @@ function buildQuota(label: string, raw: unknown): UsageQuota | null {
};
}
function parsePlan(whoami: Record<string, unknown> | null) {
const planType =
typeof whoami?.plan_type === "string" ? whoami.plan_type.trim().toLowerCase() : "";
const planName = typeof whoami?.plan_name === "string" ? whoami.plan_name.toUpperCase() : "";
const plan = planType === "api" ? PLAN_LABELS.API : (PLAN_LABELS[planName] ?? (planName || null));
return { planType, plan };
}
export async function getMistralUsage(apiKey: string) {
if (!apiKey) {
return { message: "Mistral API key not available. Add a key to view usage." };
@@ -76,10 +84,7 @@ export async function getMistralUsage(apiKey: string) {
return { message: "Mistral connected. Unable to fetch budget from /v1/billing/budget." };
}
const planType =
typeof whoami?.plan_type === "string" ? whoami.plan_type.trim().toLowerCase() : "";
const planName = typeof whoami?.plan_name === "string" ? whoami.plan_name.toUpperCase() : "";
const plan = planType === "api" ? PLAN_LABELS.API : (PLAN_LABELS[planName] ?? (planName || null));
const { planType, plan } = parsePlan(whoami);
const showVibe = planType.length > 0 && planType !== "api";
const showApi = planType.length > 0 && planType !== "chat";

View File

@@ -79,6 +79,28 @@ test("known non-CHAT/API plan types select both budgets", async () => {
assert.deepEqual(Object.keys(usage.quotas ?? {}), ["Vibe Budget", "API Budget"]);
});
test("plan parsing preserves normalization, labels, and fallback behavior", async (t) => {
const cases = [
{ whoami: { plan_type: " aPi ", plan_name: "FREE" }, plan: "Studio", keys: ["API Budget"] },
{ whoami: { plan_type: " cHaT ", plan_name: "edu" }, plan: "Student", keys: ["Vibe Budget"] },
{ whoami: { plan_type: "CHAT", plan_name: "custom" }, plan: "CUSTOM", keys: ["Vibe Budget"] },
{ whoami: { plan_type: "CHAT" }, plan: null, keys: ["Vibe Budget"] },
{ whoami: { plan_type: 42, plan_name: 42 }, plan: null, keys: [] },
{ whoami: null, plan: null, keys: [] },
];
for (const { whoami, plan, keys } of cases) {
await t.test(JSON.stringify(whoami), async () => {
mockMistralFetch(whoami, { vibe_budget: vibeBudget, api_budget: apiBudget });
const usage = await getMistralUsage("secret-key");
assert.equal(usage.plan, plan);
assert.deepEqual(Object.keys(usage.quotas ?? {}), keys);
});
}
});
test("failed whoami lookup creates no enforceable budget windows", async () => {
globalThis.fetch = async (input: string | URL | Request) => {
if (String(input).includes("whoami")) throw new Error("network failure");