mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-21 15:05:56 +00:00
refactor(analytics): move ModelTable row key and provider label into helpers
Keeps the row-render callback under the cyclomatic complexity cap after the per-provider rows from #20453, and covers the provider-labelled rows with a UI regression test.
This commit is contained in:
committed by
diegosouzapw
parent
cca194f8fa
commit
5fa146aceb
@@ -52,6 +52,17 @@ const COLUMNS: Column[] = [
|
||||
{ field: "cost", labelKey: "chartCost", align: "right" },
|
||||
];
|
||||
|
||||
// Rows are per (provider, model) since #20453: the same model name can appear once per
|
||||
// provider, so the provider takes part in the React key and is shown next to the name.
|
||||
function modelRowKey(m: ModelBreakdownRow, index: number): string {
|
||||
const model = String(m.model ?? index);
|
||||
return m.provider ? `${m.provider}:${model}` : model;
|
||||
}
|
||||
|
||||
function ProviderLabel({ provider }: { provider?: string | null }) {
|
||||
return provider ? <span className="text-xs text-text-muted">{provider}</span> : null;
|
||||
}
|
||||
|
||||
export function ModelTable({ byModel, summary, displayMode = "compact" }: ModelTableProps) {
|
||||
const t = useTranslations("analytics");
|
||||
const [sort, setSort] = useState<{
|
||||
@@ -106,9 +117,7 @@ export function ModelTable({ byModel, summary, displayMode = "compact" }: ModelT
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{sorted.map((m, i) => (
|
||||
<TableRow
|
||||
key={m.provider ? `${m.provider}:${String(m.model ?? i)}` : String(m.model ?? i)}
|
||||
>
|
||||
<TableRow key={modelRowKey(m, i)}>
|
||||
<TableCell className="px-4 py-2.5">
|
||||
<div className="flex items-center gap-2">
|
||||
<span
|
||||
@@ -116,9 +125,7 @@ export function ModelTable({ byModel, summary, displayMode = "compact" }: ModelT
|
||||
style={{ backgroundColor: getSeriesColor(i) }}
|
||||
/>
|
||||
<span className="font-medium">{m.model}</span>
|
||||
{m.provider ? (
|
||||
<span className="text-xs text-text-muted">{m.provider}</span>
|
||||
) : null}
|
||||
<ProviderLabel provider={m.provider} />
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="px-4 py-2.5 text-right font-mono text-text-muted">
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
// @vitest-environment jsdom
|
||||
// Regression for #20453: byModel carries one row per (provider, model), so the Usage
|
||||
// "Model Breakdown" table can receive two rows with the same model name. They must keep
|
||||
// distinct React keys and show which provider each row belongs to.
|
||||
import React, { act } from "react";
|
||||
import { createRoot, type Root } from "react-dom/client";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const translate = (key: string) => key;
|
||||
translate.has = () => true;
|
||||
|
||||
vi.mock("next-intl", () => ({
|
||||
useLocale: () => "en",
|
||||
useTranslations: () => translate,
|
||||
}));
|
||||
|
||||
const { ModelTable } = await import("@/shared/components/analytics/ModelTable");
|
||||
|
||||
let container: HTMLElement;
|
||||
let root: Root;
|
||||
|
||||
beforeEach(() => {
|
||||
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
|
||||
container = document.createElement("div");
|
||||
document.body.appendChild(container);
|
||||
root = createRoot(container);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
act(() => root.unmount());
|
||||
container.remove();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe("ModelTable per-provider rows (#20453)", () => {
|
||||
it("renders one distinct, provider-labelled row per provider for a shared model", () => {
|
||||
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
const shared = { model: "gpt-oss-120b", promptTokens: 100, completionTokens: 50, cost: 1 };
|
||||
|
||||
act(() =>
|
||||
root.render(
|
||||
<ModelTable
|
||||
byModel={[
|
||||
{ ...shared, provider: "cloudflare-ai", requests: 106, totalTokens: 150 },
|
||||
{ ...shared, provider: "groq", requests: 2, totalTokens: 150 },
|
||||
]}
|
||||
summary={{ totalTokens: 300 }}
|
||||
/>
|
||||
)
|
||||
);
|
||||
|
||||
const duplicateKeyWarnings = consoleError.mock.calls.filter((call) =>
|
||||
call.some((arg) => String(arg).includes("same key"))
|
||||
);
|
||||
expect(duplicateKeyWarnings).toEqual([]);
|
||||
const bodyRows = container.querySelectorAll("tbody tr");
|
||||
expect(bodyRows).toHaveLength(2);
|
||||
expect(bodyRows[0].textContent).toContain("cloudflare-ai");
|
||||
expect(bodyRows[1].textContent).toContain("groq");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user