repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/quota/__tests__/ui.test.ts
Rawimport { describe, expect, it } from "vitest";
import type { ProviderQuota, QuotaBucket } from "../src/types.ts";
import {
quotaPlainLabel,
quotaStatusLabel,
segmentText,
selectFooterBucket,
usedPercent,
} from "../src/ui.ts";
const reset = Date.now() + 4 * 3600000 + 50 * 60000 + 30000;
function quota(buckets: QuotaBucket[], provider = "anthropic"): ProviderQuota {
return { provider, name: "Anthropic", buckets, observedAt: 0 };
}
function bucket(
id: string,
label: string,
value: QuotaBucket["value"],
kind: QuotaBucket["kind"] = "allowance",
): QuotaBucket {
return {
id,
label,
kind,
value,
observedAt: 0,
period: { label, resetsAt: reset },
};
}
const measured: QuotaBucket["value"] = {
state: "measured",
unit: "percent",
used: 4,
remaining: 96,
total: 100,
};
describe("shared quota presentation", () => {
it("selects preferred label independently of ordering and falls back to measured allowance", () => {
const balance = bucket(
"credits",
"Credits",
{ state: "measured", unit: "credits", remaining: 12 },
"balance",
);
const weekly = bucket("weekly", "Weekly", measured);
const five = bucket("session", "5h", measured);
expect(selectFooterBucket(quota([balance, weekly, five]))).toBe(five);
expect(selectFooterBucket(quota([balance, weekly]))).toBe(weekly);
expect(selectFooterBucket(quota([balance]))).toBe(balance);
// Balance-only providers (DeepSeek, Moonshot) still get a footer.
expect(segmentText(quota([balance], "deepseek"), true)[0]).toContain(
"Credits 12 left",
);
expect(selectFooterBucket(quota([]))).toBeUndefined();
});
it("footer shows the account-wide allowance closest to exhaustion", () => {
const percent = (used: number): QuotaBucket["value"] => ({
state: "measured",
unit: "percent",
used,
remaining: 100 - used,
total: 100,
});
// Live Zai shape: exhausted weekly blocks use despite an idle 5h window.
const zaiFive = bucket("five", "5h", {
state: "measured",
unit: "credits",
used: 0,
total: 12000,
usedPercent: 0,
});
const zaiWeekly = bucket("weekly", "Weekly", {
state: "measured",
unit: "credits",
used: 60021,
total: 60000,
usedPercent: 100,
});
expect(selectFooterBucket(quota([zaiFive, zaiWeekly], "zai"))).toBe(
zaiWeekly,
);
// Model-scoped limits and paid balances never drive the footer.
const fable = bucket("weekly:fable", "Weekly", percent(82));
fable.scope = "Fable";
const cap = bucket("cap", "Extra usage", percent(99), "spend-cap");
const five = bucket("session", "5h", percent(10));
const weekly = bucket("weekly", "Weekly", percent(54));
expect(selectFooterBucket(quota([five, weekly, fable, cap]))).toBe(weekly);
});
it("uses unlimited/unknown states without fabricating utilization", () => {
const unknown = bucket("missing", "5h", { state: "unknown" });
const unlimited = bucket("unlimited", "Chat", { state: "unlimited" });
expect(selectFooterBucket(quota([unknown, unlimited]))).toBe(unlimited);
expect(usedPercent(unknown)).toBeUndefined();
expect(segmentText(quota([unknown]), true)[0]).toContain("unknown");
expect(segmentText(quota([unlimited]), true)[0]).toContain("unlimited");
});
it("keeps identity and window visible while shortening footer variants", () => {
const q = quota([bucket("session", "5h", measured)]);
expect(segmentText(q, true)).toEqual(["5h 4% (4h50m)", "5h 4%", "5h 4%"]);
expect(segmentText(q, false)[0]).toBe("Anthropic 5h 4% (4h50m)");
expect(quotaStatusLabel([q])).toBe("Anthropic 5h 4% (4h50m)");
expect(quotaPlainLabel([q])).toBe(
"Anthropic quota 5h 4% used, resets in 4h50m",
);
expect(quotaPlainLabel([])).toBeUndefined();
});
it("aligns bars across labels and formats multi-day resets", async () => {
const { widgetLines } = await import("../src/ui.ts");
const theme: Parameters<typeof widgetLines>[1] = {
fg: (_color, text) => text,
bold: (text) => text,
};
const weekly = bucket("weekly:fable", "Weekly", measured);
weekly.scope = "Fable";
weekly.period = {
label: "Weekly",
resetsAt: Date.now() + 6 * 86400000 + 22 * 3600000 + 14 * 60000 + 30000,
};
const rows = widgetLines(
[quota([bucket("session", "5h", measured), weekly])],
theme,
).filter((row) => /[\u2588\u2591]/.test(row));
expect(rows).toHaveLength(2);
const columns = rows.map((row) => row.search(/[\u2588\u2591]/));
expect(columns[0]).toBe(columns[1]);
expect(rows[1]).toContain("resets in 6d22h14m");
});
it("renders newly added arbitrary bucket types without provider parser changes", async () => {
const { widgetLines } = await import("../src/ui.ts");
const theme: Parameters<typeof widgetLines>[1] = {
fg: (_color, text) => text,
bold: (text) => text,
};
const rows = widgetLines(
[
quota([
bucket("odd", "90m", {
state: "measured",
unit: "requests",
used: 3,
total: 10,
}),
bucket(
"credits",
"Credits",
{ state: "measured", unit: "credits", remaining: 12.5 },
"balance",
),
bucket("unlimited", "Chat", { state: "unlimited" }),
bucket("unknown", "New pool", { state: "unknown" }),
]),
],
theme,
);
expect(rows.join("\n")).toContain("90m");
expect(rows.join("\n")).toContain("3 / 10 requests used");
expect(rows.join("\n")).toMatch(/Credits +12\.5 left/);
expect(rows.join("\n")).not.toContain("credits left");
expect(rows.join("\n")).toContain("unlimited");
expect(rows.join("\n")).toContain("unknown");
});
});