test(ai): isolate Synthetic usage fixture

This commit is contained in:
Gareth-Rouse
2026-07-23 09:32:51 +01:00
parent e2839d1288
commit 7c7726645a
2 changed files with 9 additions and 18 deletions
+5 -5
View File
@@ -8,7 +8,7 @@ import type {
UsageStatus,
UsageWindow,
} from "../usage";
import { isRecord } from "../utils";
import { isRecord } from "@oh-my-pi/pi-utils/type-guards";
const QUOTAS_URL = "https://api.synthetic.new/v2/quotas";
const FIVE_HOUR_MS = 5 * 60 * 60 * 1000;
@@ -70,7 +70,7 @@ function parseRollingFiveHourLimit(raw: unknown, provider: UsageFetchParams["pro
const regenPercent = tickPercent !== undefined ? Number((tickPercent * 100).toFixed(2)) : undefined;
const window: UsageWindow = {
id: "5h",
label: regenPercent !== undefined ? `5h Rolling · regen ${regenPercent}%/tick` : "5 Hour Rolling",
label: regenPercent !== undefined ? `5h · regen ${regenPercent}%/tick` : "5h",
durationMs: FIVE_HOUR_MS,
...(nextTickAt !== undefined ? { resetsAt: nextTickAt, resetLabel: "tick" } : {}),
};
@@ -84,7 +84,7 @@ function parseRollingFiveHourLimit(raw: unknown, provider: UsageFetchParams["pro
const status: UsageStatus = limited ? "exhausted" : (getUsageStatus(amount.usedFraction) ?? "ok");
return {
id: "synthetic:requests:5h",
label: "Synthetic 5h Rolling Limit",
label: "Synthetic Requests",
scope: { provider, windowId: "5h", shared: true },
window,
amount,
@@ -107,7 +107,7 @@ function parseWeeklyTokenLimit(raw: unknown, provider: UsageFetchParams["provide
const nextRegenCredits = parseDollarAmount(raw.nextRegenCredits);
const window: UsageWindow = {
id: "7d",
label: nextRegenCredits !== undefined ? `7d Rolling · regen $${nextRegenCredits.toFixed(2)}/tick` : "Weekly",
label: nextRegenCredits !== undefined ? `7d · regen $${nextRegenCredits.toFixed(2)}/tick` : "7d",
durationMs: WEEK_MS,
...(nextRegenAt !== undefined ? { resetsAt: nextRegenAt, resetLabel: "regen" } : {}),
};
@@ -120,7 +120,7 @@ function parseWeeklyTokenLimit(raw: unknown, provider: UsageFetchParams["provide
});
return {
id: "synthetic:usd:7d",
label: "Synthetic Weekly Credit Limit",
label: "Synthetic Credits",
scope: { provider, windowId: "7d", shared: true },
window,
amount,
+4 -13
View File
@@ -1,13 +1,4 @@
import { describe, expect, it, mock } from "bun:test";
// pi-natives requires a built Rust nightly .node addon — mock it so the
// @oh-my-pi/pi-utils barrel (which re-exports procmgr/ptree) doesn't crash.
mock.module("@oh-my-pi/pi-natives", () => ({
Process: class {},
ProcessStatus: { Running: "running", Exited: "exited" },
Shell: class {},
PtySession: class {},
}));
import { describe, expect, it } from "bun:test";
import type { FetchImpl } from "@oh-my-pi/pi-ai/types";
import type { UsageFetchContext, UsageFetchParams } from "@oh-my-pi/pi-ai/usage";
@@ -67,7 +58,7 @@ describe("synthetic usage provider", () => {
expect(report).not.toBeNull();
expect(report!.limits).toHaveLength(2);
expect(report!.limits.map(l => l.id)).toEqual(["synthetic:requests:5h", "synthetic:usd:7d"]);
expect(report!.limits.map(l => l.label)).toEqual(["Synthetic 5h Rolling Limit", "Synthetic Weekly Credit Limit"]);
expect(report!.limits.map(l => l.label)).toEqual(["Synthetic Requests", "Synthetic Credits"]);
expect(report!.limits.map(l => l.scope.windowId)).toEqual(["5h", "7d"]);
expect(report!.limits.map(l => l.window?.durationMs)).toEqual([5 * 60 * 60 * 1000, 7 * 24 * 60 * 60 * 1000]);
});
@@ -95,7 +86,7 @@ describe("synthetic usage provider", () => {
const fiveHour = report!.limits.find(l => l.id === "synthetic:requests:5h")!;
// Single-line rendering: no notes; tickPercent 0.05 → "5%" in the window label.
expect(fiveHour.notes).toBeUndefined();
expect(fiveHour.window?.label).toContain("regen 5%/tick");
expect(fiveHour.window?.label).toBe("5h · regen 5%/tick");
// nextTickAt drives the countdown, rendered as "tick in …" (not a window reset).
expect(fiveHour.window?.resetsAt).toBe(Date.parse("2026-07-10T06:46:05.000Z"));
expect(fiveHour.window?.resetLabel).toBe("tick");
@@ -117,7 +108,7 @@ describe("synthetic usage provider", () => {
expect(weekly.status).toBe("warning");
// Weekly credits regenerate incrementally too: "regen in …" + $/tick in the label.
expect(weekly.window?.resetLabel).toBe("regen");
expect(weekly.window?.label).toContain("regen $0.48/tick");
expect(weekly.window?.label).toBe("7d · regen $0.48/tick");
expect(weekly.window?.resetsAt).toBe(Date.parse("2026-07-10T08:17:04.000Z"));
});