|
| 1 | +import { ProviderDriverKind, type ServerProviderUsageWindow } from "@t3tools/contracts"; |
| 2 | +import { act, type ReactNode } from "react"; |
| 3 | +import { createRoot } from "react-dom/client"; |
| 4 | +import { expect, it, vi } from "vite-plus/test"; |
| 5 | +import { page } from "vite-plus/test/browser"; |
| 6 | + |
| 7 | +import "~/index.css"; |
| 8 | + |
| 9 | +vi.mock("../../state/session", () => ({ |
| 10 | + useEnvironmentScope: () => true, |
| 11 | + readEnvironmentScope: () => true, |
| 12 | +})); |
| 13 | +vi.mock("../../state/server", () => ({ serverEnvironment: { consumeResetCredit: null } })); |
| 14 | +vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() })); |
| 15 | +vi.mock("../../hooks/useSettings", () => ({ usePrimarySettings: () => "24h" })); |
| 16 | + |
| 17 | +import { LimitWindows } from "./UsageLimits"; |
| 18 | + |
| 19 | +const codex = ProviderDriverKind.make("codex"); |
| 20 | +const now = Date.parse("2026-09-03T12:00:00Z"); |
| 21 | + |
| 22 | +async function render(ui: ReactNode) { |
| 23 | + const container = document.createElement("div"); |
| 24 | + container.style.width = "640px"; |
| 25 | + document.body.append(container); |
| 26 | + const root = createRoot(container); |
| 27 | + await act(async () => root.render(ui)); |
| 28 | + return { |
| 29 | + [Symbol.asyncDispose]: async () => { |
| 30 | + await act(async () => root.unmount()); |
| 31 | + container.remove(); |
| 32 | + }, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +const sessionWindow = (minutesUntilReset: number | null): ServerProviderUsageWindow => ({ |
| 37 | + id: "five_hour", |
| 38 | + kind: "session", |
| 39 | + label: "Session", |
| 40 | + usedPercent: 40, |
| 41 | + ...(minutesUntilReset === null |
| 42 | + ? {} |
| 43 | + : { |
| 44 | + windowDurationMins: 300, |
| 45 | + resetsAt: new Date(now + minutesUntilReset * 60_000).toISOString(), |
| 46 | + }), |
| 47 | +}); |
| 48 | + |
| 49 | +const tick = () => { |
| 50 | + const bar = page.getByRole("img", { name: /^Session:/ }).element(); |
| 51 | + const mark = bar.querySelector(":scope > span[aria-hidden]"); |
| 52 | + return { bar: bar.getBoundingClientRect(), mark: mark?.getBoundingClientRect() }; |
| 53 | +}; |
| 54 | + |
| 55 | +it("places the even-pace tick at the share of the window left", async () => { |
| 56 | + await using _screen = await render( |
| 57 | + <LimitWindows driver={codex} windows={[sessionWindow(90)]} now={now} />, |
| 58 | + ); |
| 59 | + const { bar, mark } = tick(); |
| 60 | + expect(mark).toBeDefined(); |
| 61 | + expect(mark!.left + mark!.width / 2 - bar.left).toBeCloseTo(bar.width * 0.3, 0); |
| 62 | +}); |
| 63 | + |
| 64 | +it("keeps the tick inside the bar at both ends of the window", async () => { |
| 65 | + for (const minutesUntilReset of [0, 300]) { |
| 66 | + await using _screen = await render( |
| 67 | + <LimitWindows driver={codex} windows={[sessionWindow(minutesUntilReset)]} now={now} />, |
| 68 | + ); |
| 69 | + const { bar, mark } = tick(); |
| 70 | + expect(mark!.left).toBeGreaterThanOrEqual(bar.left); |
| 71 | + expect(mark!.right).toBeLessThanOrEqual(bar.right); |
| 72 | + } |
| 73 | +}); |
| 74 | + |
| 75 | +it("shows no tick when the window timing is unknown", async () => { |
| 76 | + await using _screen = await render( |
| 77 | + <LimitWindows driver={codex} windows={[sessionWindow(null)]} now={now} />, |
| 78 | + ); |
| 79 | + expect(tick().mark).toBeUndefined(); |
| 80 | +}); |
0 commit comments