From a1379db81847cc70b2c9535fd8370b69f9ebf291 Mon Sep 17 00:00:00 2001
From: Rakshith Bhat <88523594+RakshithBhat03@users.noreply.github.com>
Date: Tue, 25 Aug 2026 03:21:32 +0530
Subject: [PATCH] fix(web): sort usage models by token count (#8108)
---
.../src/components/usage/UsagePage.test.tsx | 75 ++++++++++++++++++-
apps/web/src/components/usage/UsagePage.tsx | 13 +++-
2 files changed, 83 insertions(+), 5 deletions(-)
diff --git a/apps/web/src/components/usage/UsagePage.test.tsx b/apps/web/src/components/usage/UsagePage.test.tsx
index 373e8a07c79e8cc7458b32b6ee6c14476abb5008..8e86b521e890a748a3d97c21a7f29c9cf71611e2 100644
--- a/apps/web/src/components/usage/UsagePage.test.tsx
+++ b/apps/web/src/components/usage/UsagePage.test.tsx
@@ -5,6 +5,8 @@ import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
const testState = vi.hoisted(() => ({
useUsage: vi.fn(),
+ metric: "cost" as "cost" | "tokens",
+ breakdown: "time" as "model" | "time",
}));
vi.mock("react", async (importOriginal) => {
@@ -24,9 +26,11 @@ vi.mock("react", async (importOriginal) => {
untilTime: "2026-08-11T12:37:00.000Z",
},
}
- : initial === "model"
- ? "time"
- : initial,
+ : initial === "cost"
+ ? testState.metric
+ : initial === "model"
+ ? testState.breakdown
+ : initial,
vi.fn(),
]),
};
@@ -72,10 +76,40 @@ const providerTotals = (codex: number, claude: number) =>
["claude", { costUsd: claude, totalTokens: claude * 1_000 }],
] as const);
+const modelTotals = Object.freeze([
+ {
+ model: "expensive-model",
+ provider: "claude" as const,
+ costUsd: 10,
+ totalTokens: 100,
+ records: 1,
+ costShare: 10 / 16,
+ },
+ {
+ model: "token-heavy-model",
+ provider: "codex" as const,
+ costUsd: 5,
+ totalTokens: 1_000,
+ records: 1,
+ costShare: 5 / 16,
+ },
+ {
+ model: "token-heavy-cheaper-model",
+ provider: "codex" as const,
+ costUsd: 1,
+ totalTokens: 1_000,
+ records: 1,
+ costShare: 1 / 16,
+ },
+]);
+
beforeEach(() => {
+ testState.metric = "cost";
+ testState.breakdown = "time";
testState.useUsage.mockReturnValue({
merged: {
...mergeUsage([], USAGE_CONTRACT_VERSION),
+ models: modelTotals,
hourly: [
{
day: "2026-08-10",
@@ -110,4 +144,39 @@ describe("UsagePage hourly breakdown", () => {
expect(body).toContain("$13.00");
expect(body.indexOf("$11.00")).toBeLessThan(body.indexOf("$13.00"));
});
+
+ it("keeps chronological ordering when the token metric is selected", () => {
+ testState.metric = "tokens";
+
+ const markup = renderToStaticMarkup(