74 lines · 2.7 KB · TypeScript
RawDownload
import { describe, expect, it } from "vite-plus/test"; import { applyUsageLimitsUpdate, resolveUsageLimitsAfterProbe } from "./providerUsageLimits.ts"; const checkedAt = "2026-09-03T12:00:00.000Z";const session = {  id: "five_hour",  kind: "session",  label: "Session",  usedPercent: 40,  windowDurationMins: 300,  resetsAt: "2026-09-03T14:00:00.000Z",} as const;const weekly = {  id: "seven_day",  kind: "weekly",  label: "Weekly",  usedPercent: 20,  windowDurationMins: 10_080,} as const;const published = { checkedAt, windows: [session, weekly] }; describe("applyUsageLimitsUpdate", () => {  it("returns the published object itself when no window moved", () => {    // Codex repeats the same numbers beside every token-usage tick; the    // ingestion path relies on identity to skip the publish.    const next = applyUsageLimitsUpdate({      previous: published,      checkedAt: "2026-09-03T12:00:05.000Z",      update: {        windows: [          { ...weekly },          { id: "five_hour", kind: "session", label: "Session", usedPercent: 40 },        ],      },    });    expect(next).toBe(published);  });   it("upserts by id and keeps the reset a percent-only update omits", () => {    const next = applyUsageLimitsUpdate({      previous: published,      checkedAt: "2026-09-03T12:00:05.000Z",      update: {        windows: [{ id: "five_hour", kind: "session", label: "Session", usedPercent: 55 }],      },    });    expect(next).not.toBe(published);    expect(next).toEqual({      checkedAt: "2026-09-03T12:00:05.000Z",      windows: [{ ...session, usedPercent: 55 }, weekly],    });  });   it("leaves an unsupported account and an empty update alone", () => {    const unsupported = { checkedAt, windows: [], unavailable: { reason: "unsupported" as const } };    expect(      applyUsageLimitsUpdate({ previous: unsupported, checkedAt, update: { windows: [session] } }),    ).toBe(unsupported);    expect(      applyUsageLimitsUpdate({ previous: published, checkedAt, update: { windows: [] } }),    ).toBe(published);  });}); describe("resolveUsageLimitsAfterProbe", () => {  it("keeps the last good windows through a failed probe but not an unsupported one", () => {    const failed = { checkedAt, windows: [], unavailable: { reason: "probeFailed" as const } };    const unsupported = { checkedAt, windows: [], unavailable: { reason: "unsupported" as const } };    expect(resolveUsageLimitsAfterProbe({ published, probed: failed })).toBe(published);    expect(resolveUsageLimitsAfterProbe({ published, probed: unsupported })).toBe(unsupported);    expect(resolveUsageLimitsAfterProbe({ published: undefined, probed: failed })).toBe(failed);  });});

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t