diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTab.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTab.test.tsx index a5eb149e1f0..13db8794761 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTab.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTab.test.tsx @@ -1,10 +1,19 @@ import * as useAuthorizedModule from "@/app/(dashboard)/hooks/useAuthorized"; -import { fireEvent, screen, waitFor, within } from "@testing-library/react"; +import { + fireEvent, + render, + renderWithProviders, + screen, + testQueryClient, + waitFor, + within, +} from "@/../tests/test-utils"; +import { QueryClientProvider } from "@tanstack/react-query"; import userEvent from "@testing-library/user-event"; -import type { OnUrlUpdateFunction } from "nuqs/adapters/testing"; -import { beforeEach, describe, expect, it, Mock, vi } from "vitest"; +import { NuqsTestingAdapter, type OnUrlUpdateFunction } from "nuqs/adapters/testing"; +import type { PropsWithChildren } from "react"; +import { beforeEach, describe, expect, it, type Mock, vi } from "vitest"; -import { renderWithProviders } from "../../../../../tests/test-utils"; import AllModelsTab from "./AllModelsTab"; import { STATUS_COLUMN_ID, toServerSortField } from "./ModelsTableColumns"; @@ -107,14 +116,16 @@ const setModelsInfo = (rows: Record[], totalCount = rows.length data: { data: rows, total_count: totalCount, current_page: 1, total_pages: 1, size: 50 }, isLoading, isFetching: false, + isError: false, error: null, }; }; -const lastModelsInfoCall = (): ModelsInfoArgs => modelsInfoCalls[modelsInfoCalls.length - 1]; +const setModelsInfoError = () => { + modelsInfoResult = { data: undefined, isLoading: false, isFetching: false, isError: true, error: new Error("boom") }; +}; -const lastUrlParams = (onUrlUpdate: Mock): URLSearchParams | undefined => - onUrlUpdate.mock.calls.at(-1)?.[0].searchParams; +const lastModelsInfoCall = (): ModelsInfoArgs => modelsInfoCalls[modelsInfoCalls.length - 1]; const SEARCH_SETTLE_MS = 400; @@ -146,6 +157,53 @@ const defaultProps = { setSelectedTeamId: mockSetSelectedTeamId, }; +type TabProps = Partial; + +interface RenderTabOptions { + // NuqsTestingAdapter resets the update queue on mount, which swallows URL writes made by + // mount effects (the DataTable server page clamp). Opt out to observe those writes. + keepMountUpdates?: boolean; +} + +const renderTab = ( + props: TabProps = {}, + searchParams?: string, + { keepMountUpdates = false }: RenderTabOptions = {}, +) => { + const onUrlUpdate = vi.fn(); + const ui = ; + if (!keepMountUpdates) { + const view = renderWithProviders(ui, { searchParams, onUrlUpdate }); + return { ...view, onUrlUpdate }; + } + const MountPreservingProviders = ({ children }: PropsWithChildren) => ( + + {children} + + ); + const view = render(ui, { wrapper: MountPreservingProviders }); + return { ...view, onUrlUpdate }; +}; + +const lastUrl = (onUrlUpdate: Mock): URLSearchParams => { + const event = onUrlUpdate.mock.calls.at(-1)?.[0]; + if (!event) throw new Error("no URL update was emitted"); + return event.searchParams; +}; + +const sortHeader = (columnId: string): HTMLElement => screen.getByTestId(`sort-header-${columnId}`); + +const expectIndicator = async (columnId: string, state: "asc" | "desc" | "none") => { + await waitFor(() => { + expect(sortHeader(columnId).querySelector(`[data-sort-indicator="${state}"]`)).not.toBeNull(); + }); +}; + describe("AllModelsTab", () => { beforeEach(() => { vi.clearAllMocks(); @@ -156,14 +214,14 @@ describe("AllModelsTab", () => { it("renders the fetched models and the server row count", async () => { setModelsInfo([makeRow()], 137); - renderWithProviders(); + renderTab(); expect(await screen.findByText("gpt-4")).toBeInTheDocument(); expect(screen.getByTestId("pagination-range")).toHaveTextContent("Showing 1-50 of 137"); }); it("does not re-query after the mount-time debounced search settles unchanged", async () => { - renderWithProviders(); + renderTab(); const callsAfterMount = modelsInfoCalls.length; await new Promise((resolve) => setTimeout(resolve, SEARCH_SETTLE_MS)); @@ -173,28 +231,20 @@ describe("AllModelsTab", () => { it("shows the empty state when the proxy returns no models", () => { setModelsInfo([], 0); - renderWithProviders(); + renderTab(); expect(screen.getByText("No models found")).toBeInTheDocument(); }); it("shows the loading skeleton while the first page is in flight", () => { setModelsInfo([], 0, true); - renderWithProviders(); + renderTab(); expect(screen.getAllByTestId("skeleton-row").length).toBeGreaterThan(0); expect(screen.queryByText("No models found")).not.toBeInTheDocument(); }); describe("server sort contract", () => { - const sortHeader = (columnId: string): HTMLElement => screen.getByTestId(`sort-header-${columnId}`); - - const expectIndicator = async (columnId: string, state: "asc" | "desc" | "none") => { - await waitFor(() => { - expect(sortHeader(columnId).querySelector(`[data-sort-indicator="${state}"]`)).not.toBeNull(); - }); - }; - const cases: [string, string, string, "asc" | "desc"][] = [ ["Model Information", "model_name", "model_name", "asc"], ["Created By", "model_info_created_by", "created_at", "asc"], @@ -204,7 +254,7 @@ describe("AllModelsTab", () => { it.each(cases)("sorts %s using the server field %s", async (_label, columnId, serverField, firstDirection) => { const user = userEvent.setup(); - renderWithProviders(); + renderTab(); await user.click(sortHeader(columnId)); await expectIndicator(columnId, firstDirection); @@ -219,7 +269,7 @@ describe("AllModelsTab", () => { it("cycles a sorted column back to unsorted", async () => { const user = userEvent.setup(); - renderWithProviders(); + renderTab(); await user.click(sortHeader("model_info_updated_at")); await expectIndicator("model_info_updated_at", "asc"); @@ -235,9 +285,200 @@ describe("AllModelsTab", () => { }); }); + describe("URL state", () => { + it("reads search, page, page size, sort, team, access group and view from the URL", async () => { + setModelsInfo([makeRow()], 100); + renderTab( + {}, + "?model_search=claude&page=2&page_size=25&sort_by=model_name&sort_order=desc&filter_team=team-1&access_group=sales-team&view_mode=all", + ); + + const expectedQuery: ModelsInfoArgs = { + page: 2, + size: 25, + search: "claude", + sortBy: "model_name", + sortOrder: "desc", + teamId: "team-1", + accessGroup: "sales-team", + }; + expect(lastModelsInfoCall()).toMatchObject(expectedQuery); + expect(screen.getByTestId("datatable-search")).toHaveValue("claude"); + expect(screen.getByTestId("pagination-range")).toHaveTextContent("Showing 26-50 of 100"); + expect(screen.getByTestId("models-team-select")).toHaveTextContent("Engineering"); + expect(screen.getByTestId("models-view-select")).toHaveTextContent("All Available Models"); + expect(screen.getByTestId("filter-chip-model_info_access_groups")).toHaveTextContent("sales-team"); + await expectIndicator("model_name", "desc"); + expect(screen.queryByText(/create a Virtual Key/i)).not.toBeInTheDocument(); + }); + + it("translates a sortable column id in ?sort_by= into its server sort field", () => { + renderTab({}, "?sort_by=model_info_created_by"); + + expect(lastModelsInfoCall().sortBy).toBe("created_at"); + expect(lastModelsInfoCall().sortOrder).toBe("asc"); + }); + + it("ignores a ?sort_by= that names a column which cannot be sorted", async () => { + renderTab({}, "?sort_by=litellm_credential_name&sort_order=desc"); + + expect(lastModelsInfoCall().sortBy).toBeUndefined(); + expect(lastModelsInfoCall().sortOrder).toBeUndefined(); + await expectIndicator("model_name", "none"); + }); + + it("clamps a hand-edited ?page= and ?page_size= into the range the table offers", () => { + renderTab({}, "?page=0&page_size=5000"); + + expect(lastModelsInfoCall().page).toBe(1); + expect(lastModelsInfoCall().size).toBe(50); + }); + + it("keeps the default page size when ?page_size= is not a number", () => { + renderTab({}, "?page_size=lots"); + + expect(lastModelsInfoCall().size).toBe(50); + }); + + it("keeps ?page= when the models request failed instead of snapping back to page 1", async () => { + setModelsInfoError(); + const { onUrlUpdate } = renderTab({}, "?page=3", { keepMountUpdates: true }); + + await new Promise((resolve) => setTimeout(resolve, SEARCH_SETTLE_MS)); + + expect(onUrlUpdate).not.toHaveBeenCalled(); + expect(lastModelsInfoCall().page).toBe(3); + }); + + it("clamps ?page= to the last page once the server reports fewer rows", async () => { + setModelsInfo([makeRow()], 1); + const { onUrlUpdate } = renderTab({}, "?page=3", { keepMountUpdates: true }); + + await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled()); + expect(lastUrl(onUrlUpdate).has("page")).toBe(false); + await waitFor(() => expect(lastModelsInfoCall().page).toBe(1)); + }); + + it("writes the typed search to ?model_search= and resets the page", async () => { + setModelsInfo([makeRow()], 100); + const { onUrlUpdate } = renderTab({}, "?page=2"); + + fireEvent.change(screen.getByTestId("datatable-search"), { target: { value: "claude" } }); + + await waitFor(() => expect(lastUrl(onUrlUpdate).get("model_search")).toBe("claude")); + expect(lastUrl(onUrlUpdate).has("page")).toBe(false); + await waitFor(() => expect(lastModelsInfoCall().search).toBe("claude")); + expect(lastModelsInfoCall().page).toBe(1); + }); + + it("writes the selected team to ?filter_team=, resets the page, and drops the key for Personal", async () => { + const user = userEvent.setup(); + setModelsInfo([makeRow()], 100); + const { onUrlUpdate } = renderTab({}, "?page=2"); + + await user.click(screen.getByTestId("models-team-select")); + await user.click(await screen.findByRole("option", { name: "Engineering" })); + + await waitFor(() => expect(lastUrl(onUrlUpdate).get("filter_team")).toBe("team-1")); + expect(lastUrl(onUrlUpdate).has("page")).toBe(false); + await waitFor(() => expect(lastModelsInfoCall().teamId).toBe("team-1")); + + await user.click(screen.getByTestId("models-team-select")); + await user.click(await screen.findByRole("option", { name: "Personal" })); + + await waitFor(() => expect(lastUrl(onUrlUpdate).has("filter_team")).toBe(false)); + }); + + it("writes the view mode to ?view_mode= and drops the key for the default view", async () => { + const user = userEvent.setup(); + const { onUrlUpdate } = renderTab(); + + await user.click(screen.getByTestId("models-view-select")); + await user.click(await screen.findByRole("option", { name: "All Available Models" })); + + await waitFor(() => expect(lastUrl(onUrlUpdate).get("view_mode")).toBe("all")); + expect(screen.getByTestId("models-view-select")).toHaveTextContent("All Available Models"); + + await user.click(screen.getByTestId("models-view-select")); + await user.click(await screen.findByRole("option", { name: "Current Team Models" })); + + await waitFor(() => expect(lastUrl(onUrlUpdate).has("view_mode")).toBe(false)); + }); + + it("writes header sorting to ?sort_by= and ?sort_order= and clears both when unsorted", async () => { + const user = userEvent.setup(); + const { onUrlUpdate } = renderTab(); + + await user.click(sortHeader("model_info_updated_at")); + await waitFor(() => expect(lastUrl(onUrlUpdate).get("sort_by")).toBe("model_info_updated_at")); + expect(lastUrl(onUrlUpdate).has("sort_order")).toBe(false); + + await user.click(sortHeader("model_info_updated_at")); + await waitFor(() => expect(lastUrl(onUrlUpdate).get("sort_order")).toBe("desc")); + + await user.click(sortHeader("model_info_updated_at")); + await waitFor(() => expect(lastUrl(onUrlUpdate).has("sort_by")).toBe(false)); + expect(lastUrl(onUrlUpdate).has("sort_order")).toBe(false); + }); + + it("writes the access group filter to ?access_group= and drops it when the chip is removed", async () => { + const user = userEvent.setup(); + const { onUrlUpdate } = renderTab(); + + await user.click(screen.getByTestId("datatable-filters-trigger")); + await user.click(await screen.findByPlaceholderText("Filter by Model Access Group")); + await user.click(await screen.findByRole("option", { name: "sales-team" })); + await user.click(screen.getByTestId("filter-drawer-apply")); + + await waitFor(() => expect(lastUrl(onUrlUpdate).get("access_group")).toBe("sales-team")); + + await user.click(await screen.findByTestId("filter-chip-remove-model_info_access_groups")); + + await waitFor(() => expect(lastUrl(onUrlUpdate).has("access_group")).toBe(false)); + expect(lastModelsInfoCall().accessGroup).toBeUndefined(); + }); + + it("writes page and page size changes to ?page= and ?page_size=", async () => { + const user = userEvent.setup(); + setModelsInfo([makeRow()], 200); + const { onUrlUpdate } = renderTab(); + + await user.click(screen.getByTestId("pagination-next")); + await waitFor(() => expect(lastUrl(onUrlUpdate).get("page")).toBe("2")); + await waitFor(() => expect(lastModelsInfoCall().page).toBe(2)); + + await user.click(screen.getByTestId("pagination-page-size")); + await user.click(await screen.findByRole("option", { name: "25" })); + await waitFor(() => expect(lastUrl(onUrlUpdate).get("page_size")).toBe("25")); + await waitFor(() => expect(lastModelsInfoCall().size).toBe(25)); + }); + + it("clears every table key and the model group in a single URL update from the drawer reset", async () => { + const user = userEvent.setup(); + setModelsInfo([makeRow()], 100); + const { onUrlUpdate } = renderTab( + { selectedModelGroup: "gpt-4" }, + "?model_search=claude&page=2&page_size=25&sort_by=model_name&sort_order=desc&filter_team=team-1&access_group=sales-team&view_mode=all", + ); + const callsBefore = onUrlUpdate.mock.calls.length; + + await user.click(screen.getByTestId("datatable-filters-trigger")); + await user.click(await screen.findByTestId("filter-drawer-reset")); + + await waitFor(() => expect(onUrlUpdate.mock.calls.length).toBe(callsBefore + 1)); + expect(lastUrl(onUrlUpdate).toString()).toBe(""); + expect(screen.getByTestId("datatable-search")).toHaveValue(""); + expect(mockSetSelectedModelGroup).toHaveBeenCalledWith("all"); + await waitFor(() => expect(lastModelsInfoCall()).toMatchObject({ page: 1, size: 50 })); + expect(lastModelsInfoCall().teamId).toBeUndefined(); + expect(lastModelsInfoCall().sortBy).toBeUndefined(); + expect(lastModelsInfoCall().accessGroup).toBeUndefined(); + }); + }); + it("queries the selected team and resets to the first page", async () => { const user = userEvent.setup(); - renderWithProviders(); + renderTab(); expect(lastModelsInfoCall().teamId).toBeUndefined(); @@ -251,7 +492,7 @@ describe("AllModelsTab", () => { }); it("debounces the model name search into the server query", async () => { - renderWithProviders(); + renderTab(); fireEvent.change(screen.getByTestId("datatable-search"), { target: { value: "claude" } }); @@ -260,138 +501,9 @@ describe("AllModelsTab", () => { }); }); - describe("URL persistence", () => { - it("writes the typed search to the URL and drops the page so a reload keeps the search", async () => { - setModelsInfo([makeRow()], 200); - const onUrlUpdate = vi.fn(); - renderWithProviders(, { searchParams: { page: "3" }, onUrlUpdate }); - expect(lastModelsInfoCall().page).toBe(3); - - fireEvent.change(screen.getByTestId("datatable-search"), { target: { value: "claude" } }); - - await waitFor(() => { - expect(lastUrlParams(onUrlUpdate)?.get("model_search")).toBe("claude"); - }); - expect(lastUrlParams(onUrlUpdate)?.get("page")).toBeNull(); - await waitFor(() => { - expect(lastModelsInfoCall().page).toBe(1); - }); - }); - - it("restores the search box and server query from ?model_search= on mount", () => { - renderWithProviders(, { searchParams: { model_search: "haiku" } }); - - expect(screen.getByTestId("datatable-search")).toHaveValue("haiku"); - expect(lastModelsInfoCall().search).toBe("haiku"); - }); - - it("restores team, sort, page and page size from the URL into the server query", () => { - setModelsInfo([makeRow()], 200); - renderWithProviders(, { - searchParams: { - filter_team: "team-1", - sort_by: "model_info_updated_at", - sort_order: "desc", - page: "2", - page_size: "25", - }, - }); - - const expectedQuery: ModelsInfoArgs = { - teamId: "team-1", - sortBy: "updated_at", - sortOrder: "desc", - page: 2, - size: 25, - }; - expect(lastModelsInfoCall()).toMatchObject(expectedQuery); - expect(screen.getByTestId("models-team-select")).toHaveTextContent("Engineering"); - }); - - it("restores the access group and view mode from the URL", () => { - renderWithProviders(, { - searchParams: { access_group: "sales-team", view_mode: "all" }, - }); - - expect(lastModelsInfoCall().accessGroup).toBe("sales-team"); - expect(screen.queryByText(/To access these models/)).not.toBeInTheDocument(); - }); - - it("clamps a hand-edited page and page size into the range the table supports", () => { - renderWithProviders(, { searchParams: { page: "0", page_size: "5000" } }); - - expect(lastModelsInfoCall().page).toBe(1); - expect(lastModelsInfoCall().size).toBe(100); - }); - - it("keeps the default page size when the URL value is not a number", () => { - renderWithProviders(, { searchParams: { page_size: "lots" } }); - - expect(lastModelsInfoCall().size).toBe(50); - }); - - it("ignores a sort_by the table cannot sort by instead of forwarding it to the server", () => { - renderWithProviders(, { - searchParams: { sort_by: "litellm_credential_name", sort_order: "desc" }, - }); - - expect(lastModelsInfoCall().sortBy).toBeUndefined(); - expect(lastModelsInfoCall().sortOrder).toBeUndefined(); - }); - - it("writes sort changes to the URL with the page cleared", async () => { - setModelsInfo([makeRow()], 200); - const user = userEvent.setup(); - const onUrlUpdate = vi.fn(); - renderWithProviders(, { searchParams: { page: "2" }, onUrlUpdate }); - - await user.click(screen.getByTestId("sort-header-model_info_updated_at")); - - await waitFor(() => { - expect(lastUrlParams(onUrlUpdate)?.get("sort_by")).toBe("model_info_updated_at"); - }); - expect(lastUrlParams(onUrlUpdate)?.get("sort_order")).toBeNull(); - expect(lastUrlParams(onUrlUpdate)?.get("page")).toBeNull(); - - await user.click(screen.getByTestId("sort-header-model_info_updated_at")); - - await waitFor(() => { - expect(lastUrlParams(onUrlUpdate)?.get("sort_order")).toBe("desc"); - }); - }); - - it("clears every table param from the URL on drawer reset", async () => { - setModelsInfo([makeRow()], 200); - const user = userEvent.setup(); - const onUrlUpdate = vi.fn(); - renderWithProviders(, { - searchParams: { - model_search: "haiku", - filter_team: "team-1", - sort_by: "model_name", - page: "2", - view_mode: "all", - }, - onUrlUpdate, - }); - - await user.click(screen.getByTestId("datatable-filters-trigger")); - await user.click(await screen.findByTestId("filter-drawer-reset")); - - await waitFor(() => { - expect(lastUrlParams(onUrlUpdate)?.toString()).toBe(""); - }); - expect(screen.getByTestId("datatable-search")).toHaveValue(""); - const defaultQuery: ModelsInfoArgs = { search: undefined, teamId: undefined, sortBy: undefined, page: 1 }; - await waitFor(() => { - expect(lastModelsInfoCall()).toMatchObject(defaultQuery); - }); - }); - }); - it("applies a public model name filter through the drawer", async () => { const user = userEvent.setup(); - renderWithProviders(); + renderTab(); await user.click(screen.getByTestId("datatable-filters-trigger")); await user.click(await screen.findByPlaceholderText("Filter by Public Model Name")); @@ -405,7 +517,7 @@ describe("AllModelsTab", () => { it("renders every row the server returned for the selected model group so rows match the footer total", () => { setModelsInfo([makeRow(), { ...makeRow({ model_info: { id: "model-2" } }), model_name: "claude-opus" }], 2); - renderWithProviders(); + renderTab({ selectedModelGroup: "claude-opus" }); const table = screen.getByRole("table"); expect(within(table).getByText("claude-opus")).toBeInTheDocument(); @@ -415,7 +527,7 @@ describe("AllModelsTab", () => { it("asks the server for wildcard deployments instead of hiding rows client-side", () => { setModelsInfo([makeRow(), { ...makeRow({ model_info: { id: "model-2" } }), model_name: "openai/*" }], 2); - renderWithProviders(); + renderTab({ selectedModelGroup: "wildcard" }); expect(lastModelsInfoCall().wildcardOnly).toBe(true); expect(within(screen.getByRole("table")).getByText("gpt-4")).toBeInTheDocument(); @@ -424,7 +536,7 @@ describe("AllModelsTab", () => { it("asks the server for the selected access group instead of hiding rows client-side", async () => { const user = userEvent.setup(); - renderWithProviders(); + renderTab(); expect(lastModelsInfoCall().wildcardOnly).toBe(false); await user.click(screen.getByTestId("datatable-filters-trigger")); @@ -438,20 +550,26 @@ describe("AllModelsTab", () => { }); it("asks the server for the exact selected model group so deployments beyond the first page are found", () => { - renderWithProviders(); + renderTab({ selectedModelGroup: "claude-opus" }); expect(lastModelsInfoCall().modelName).toBe("claude-opus"); expect(lastModelsInfoCall().search).toBeUndefined(); }); it.each(["all", "wildcard"])("sends no exact model name for the %s pseudo group", (group) => { - renderWithProviders(); + renderTab({ selectedModelGroup: group }); expect(lastModelsInfoCall().modelName).toBeUndefined(); }); + it("shows no filter chip for the all pseudo group", () => { + renderTab({ selectedModelGroup: "all" }); + + expect(screen.queryByTestId("filter-chip-model_name")).not.toBeInTheDocument(); + }); + it("keeps the exact model group alongside a typed search", async () => { - renderWithProviders(); + renderTab({ selectedModelGroup: "claude-opus" }); fireEvent.change(screen.getByPlaceholderText("Search model names…"), { target: { value: "opus" } }); @@ -461,7 +579,7 @@ describe("AllModelsTab", () => { it("resets search, filters, team and sorting from the drawer reset button", async () => { const user = userEvent.setup(); - renderWithProviders(); + renderTab({ selectedModelGroup: "gpt-4" }); await user.click(screen.getByTestId("models-team-select")); await user.click(await screen.findByRole("option", { name: "Engineering" })); @@ -478,7 +596,7 @@ describe("AllModelsTab", () => { it("opens the delete modal from the row and deletes the model", async () => { const user = userEvent.setup(); - renderWithProviders(); + renderTab(); await user.click(await screen.findByTestId("model-delete-model-1")); expect(await screen.findByText("Delete Model")).toBeInTheDocument(); @@ -492,7 +610,7 @@ describe("AllModelsTab", () => { it("pauses a model through the row toggle", async () => { const user = userEvent.setup(); - renderWithProviders(); + renderTab(); await user.click(await screen.findByTestId("model-pause-toggle-model-1")); @@ -503,7 +621,7 @@ describe("AllModelsTab", () => { it("opens the model settings modal from the toolbar", async () => { const user = userEvent.setup(); - renderWithProviders(); + renderTab(); expect(screen.queryByTestId("model-settings-modal")).not.toBeInTheDocument(); await user.click(screen.getByTestId("models-settings-trigger")); @@ -512,7 +630,7 @@ describe("AllModelsTab", () => { it("opens the model detail view from the model ID cell", async () => { const user = userEvent.setup(); - renderWithProviders(); + renderTab(); await user.click(await screen.findByTestId("model-id-model-1")); @@ -521,7 +639,7 @@ describe("AllModelsTab", () => { it("opens the team detail view from the team ID cell", async () => { const user = userEvent.setup(); - renderWithProviders(); + renderTab(); await user.click(await screen.findByTestId("model-team-id-model-1")); @@ -530,20 +648,20 @@ describe("AllModelsTab", () => { describe("virtual key hint", () => { it("explains personal key creation while viewing current team models", () => { - renderWithProviders(); + renderTab(); expect(screen.getByText(/create a Virtual Key without selecting a team/i)).toBeInTheDocument(); }); it("links the Virtual Keys page through the migrated /ui route", () => { - renderWithProviders(); + renderTab(); expect(screen.getByRole("link", { name: "Virtual Keys page" })).toHaveAttribute("href", "/ui/api-keys"); }); it("links the team hint's Virtual Keys page through the migrated /ui route", async () => { const user = userEvent.setup(); - renderWithProviders(); + renderTab(); await user.click(screen.getByTestId("models-team-select")); await user.click(await screen.findByRole("option", { name: "Engineering" })); @@ -554,7 +672,7 @@ describe("AllModelsTab", () => { it("names the selected team in the hint", async () => { const user = userEvent.setup(); - renderWithProviders(); + renderTab(); await user.click(screen.getByTestId("models-team-select")); await user.click(await screen.findByRole("option", { name: "Engineering" })); @@ -564,7 +682,7 @@ describe("AllModelsTab", () => { it("hides the hint when viewing all available models", async () => { const user = userEvent.setup(); - renderWithProviders(); + renderTab(); await user.click(screen.getByTestId("models-view-select")); await user.click(await screen.findByRole("option", { name: "All Available Models" })); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTab.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTab.tsx index 2217bca0fa0..35a57b1e11d 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTab.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTab.tsx @@ -6,6 +6,7 @@ import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized"; import DeleteResourceModal from "@/components/common_components/DeleteResourceModal"; import ModelSettingsModal from "@/components/model_dashboard/ModelSettingsModal/ModelSettingsModal"; import { ModelData } from "@/components/model_dashboard/types"; +import { useUrlTableState, type UrlTableStateOptions } from "@/components/shared/DataTable"; import { toast } from "@/lib/toast"; import { uiHref } from "@/utils/uiHref"; import { modelDeleteCall, modelPatchUpdateCall } from "@/components/networking"; @@ -13,7 +14,7 @@ import { useQueryClient } from "@tanstack/react-query"; import { useDebouncedValue } from "@tanstack/react-pacer/debouncer"; import { ColumnFiltersState, functionalUpdate, OnChangeFn, PaginationState, SortingState } from "@tanstack/react-table"; import { Info } from "lucide-react"; -import { createParser, parseAsInteger, parseAsString, parseAsStringLiteral, useQueryStates } from "nuqs"; +import { parseAsString, parseAsStringLiteral, useQueryStates } from "nuqs"; import { useCallback, useMemo, useState } from "react"; import { useModelsInfo } from "../../hooks/models/useModels"; @@ -21,13 +22,13 @@ import { transformModelData } from "../utils/modelDataTransformer"; import { ALL_MODEL_GROUPS_VALUE, AllModelsTable, + MODEL_VIEW_MODES, ModelViewMode, PERSONAL_TEAM_VALUE, WILDCARD_MODEL_GROUP_VALUE, } from "./AllModelsTable"; import { ACCESS_GROUPS_COLUMN_ID, - isModelTableSortColumnId, MODEL_NAME_COLUMN_ID, MODEL_TABLE_SORT_COLUMN_IDS, toServerSortField, @@ -35,31 +36,41 @@ import { const SEARCH_DEBOUNCE_WAIT_MS = 200; const DEFAULT_PAGE_SIZE = 50; -const MAX_PAGE_SIZE = 100; -const MAX_PAGE = 100_000; +const MAX_PAGE_SIZE = 50; +const DEFAULT_PAGINATION: PaginationState = { pageIndex: 0, pageSize: DEFAULT_PAGE_SIZE }; +const UNSORTED_COLUMN_ID = ""; +const FILTER_COLUMNS = [ACCESS_GROUPS_COLUMN_ID] as const; +type FilterColumn = (typeof FILTER_COLUMNS)[number]; -const MODEL_VIEW_MODES = ["current_team", "all"] as const satisfies readonly ModelViewMode[]; - -const boundedInteger = (min: number, max: number, fallback: number) => - createParser({ - parse: (value: string) => { - const parsed = parseAsInteger.parse(value); - return parsed === null ? null : Math.min(Math.max(parsed, min), max); - }, - serialize: String, - }).withDefault(fallback); - -const TABLE_STATE = { - model_search: parseAsString.withDefault(""), - view_mode: parseAsStringLiteral(MODEL_VIEW_MODES).withDefault("current_team"), - filter_team: parseAsString.withDefault(PERSONAL_TEAM_VALUE), - access_group: parseAsString.withDefault(""), - sort_by: parseAsStringLiteral(MODEL_TABLE_SORT_COLUMN_IDS), - sort_order: parseAsStringLiteral(["asc", "desc"] as const).withDefault("asc"), - page: boundedInteger(1, MAX_PAGE, 1), - page_size: boundedInteger(1, MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE), +const TABLE_STATE_OPTIONS: UrlTableStateOptions = { + sortFields: MODEL_TABLE_SORT_COLUMN_IDS, + defaultSort: { id: UNSORTED_COLUMN_ID, desc: false }, + defaultPageSize: DEFAULT_PAGE_SIZE, + maxPageSize: MAX_PAGE_SIZE, + filterColumns: FILTER_COLUMNS, + urlKeys: { search: "model_search", filter_model_info_access_groups: "access_group" }, }; +const SCOPE_PARSERS = { + filter_team: parseAsString.withDefault(PERSONAL_TEAM_VALUE), + view_mode: parseAsStringLiteral(MODEL_VIEW_MODES).withDefault("current_team"), +}; + +const appliedAccessGroup = (filters: ColumnFiltersState): string | null => { + const value = filters.find((filter) => filter.id === ACCESS_GROUPS_COLUMN_ID)?.value; + return typeof value === "string" ? value : null; +}; + +interface ServerSort { + sortBy?: string; + sortOrder?: "asc" | "desc"; +} + +const toServerSortOrder = (sort: SortingState[number]): "asc" | "desc" => (sort.desc ? "desc" : "asc"); + +const toServerSort = (sort: SortingState[number] | undefined): ServerSort => + sort ? { sortBy: toServerSortField(sort.id), sortOrder: toServerSortOrder(sort) } : {}; + interface AllModelsTabProps { selectedModelGroup: string | null; setSelectedModelGroup: (selectedModelGroup: string) => void; @@ -82,25 +93,28 @@ const AllModelsTab = ({ const { data: teams, isLoading: isLoadingTeams } = useTeams(); const queryClient = useQueryClient(); - const [tableState, setTableState] = useQueryStates(TABLE_STATE); - const modelNameSearch = tableState.model_search; + const { + search: modelNameSearch, + setSearch, + sorting: urlSorting, + onSortingChange, + pagination, + onPaginationChange, + columnFilters: urlColumnFilters, + onColumnFiltersChange, + } = useUrlTableState(TABLE_STATE_OPTIONS); + const [{ filter_team: selectedTeamValue, view_mode: modelViewMode }, setScope] = useQueryStates(SCOPE_PARSERS); const [debouncedSearch] = useDebouncedValue(modelNameSearch, { wait: SEARCH_DEBOUNCE_WAIT_MS }); - const modelViewMode = tableState.view_mode; - const selectedTeamValue = tableState.filter_team; - const selectedModelAccessGroupFilter = tableState.access_group || null; - const pagination = useMemo( - () => ({ pageIndex: tableState.page - 1, pageSize: tableState.page_size }), - [tableState.page, tableState.page_size], - ); - const sorting = useMemo( - () => (tableState.sort_by ? [{ id: tableState.sort_by, desc: tableState.sort_order === "desc" }] : []), - [tableState.sort_by, tableState.sort_order], - ); const [isModelSettingsModalVisible, setIsModelSettingsModalVisible] = useState(false); const [deleteModalModelId, setDeleteModalModelId] = useState(null); const [deleteLoading, setDeleteLoading] = useState(false); const [pausingModelId, setPausingModelId] = useState(null); + const activeSort = urlSorting.find((entry) => entry.id !== UNSORTED_COLUMN_ID); + const sorting = useMemo(() => (activeSort ? [activeSort] : []), [activeSort]); + const { sortBy, sortOrder } = toServerSort(activeSort); + const selectedModelAccessGroupFilter = appliedAccessGroup(urlColumnFilters); + const teamIdForQuery = selectedTeamValue === PERSONAL_TEAM_VALUE ? undefined : selectedTeamValue; const isConcreteModelGroup = Boolean(selectedModelGroup) && @@ -113,20 +127,11 @@ const AllModelsTab = ({ : undefined; const wildcardOnlyForQuery = selectedModelGroup === WILDCARD_MODEL_GROUP_VALUE; - const sortBy = useMemo(() => { - if (sorting.length === 0) return undefined; - return toServerSortField(sorting[0].id); - }, [sorting]); - - const sortOrder = useMemo(() => { - if (sorting.length === 0) return undefined; - return sorting[0].desc ? "desc" : "asc"; - }, [sorting]); - const { data: rawModelData, isLoading: isLoadingModelsInfo, isFetching: isFetchingModelsInfo, + isError: isErrorModelsInfo, refetch: refetchModels, } = useModelsInfo( pagination.pageIndex + 1, @@ -163,59 +168,42 @@ const AllModelsTab = ({ }, [rawModelData, getProviderFromModel]); const columnFilters = useMemo( - () => - [ - selectedModelGroup && selectedModelGroup !== ALL_MODEL_GROUPS_VALUE - ? { id: MODEL_NAME_COLUMN_ID, value: selectedModelGroup } - : null, - selectedModelAccessGroupFilter ? { id: ACCESS_GROUPS_COLUMN_ID, value: selectedModelAccessGroupFilter } : null, - ].filter((entry) => entry !== null), - [selectedModelGroup, selectedModelAccessGroupFilter], - ); - - const handleSearchChange = useCallback( - (value: string) => { - void setTableState({ model_search: value || null, page: null }); - }, - [setTableState], + () => [ + ...(selectedModelGroup && selectedModelGroup !== ALL_MODEL_GROUPS_VALUE + ? [{ id: MODEL_NAME_COLUMN_ID, value: selectedModelGroup }] + : []), + ...urlColumnFilters, + ], + [selectedModelGroup, urlColumnFilters], ); const handleColumnFiltersChange: OnChangeFn = (updater) => { const next = functionalUpdate(updater, columnFilters); const modelGroup = next.find((entry) => entry.id === MODEL_NAME_COLUMN_ID)?.value; - const accessGroup = next.find((entry) => entry.id === ACCESS_GROUPS_COLUMN_ID)?.value; setSelectedModelGroup(typeof modelGroup === "string" ? modelGroup : ALL_MODEL_GROUPS_VALUE); - void setTableState({ access_group: typeof accessGroup === "string" ? accessGroup : null, page: null }); + onColumnFiltersChange(next); }; const handleSortingChange: OnChangeFn = (updater) => { - const active = functionalUpdate(updater, sorting)[0]; - void setTableState({ - sort_by: active && isModelTableSortColumnId(active.id) ? active.id : null, - sort_order: active?.desc ? "desc" : null, - page: null, - }); + onSortingChange(functionalUpdate(updater, sorting)); }; - const handlePaginationChange = useCallback>( - (updater) => { - const next = functionalUpdate(updater, pagination); - void setTableState({ page: next.pageIndex + 1, page_size: next.pageSize }); - }, - [pagination, setTableState], - ); - const handleTeamChange = (value: string) => { - void setTableState({ filter_team: value, page: null }); + void setScope({ filter_team: value }); + onPaginationChange({ pageIndex: 0, pageSize: pagination.pageSize }); }; - const handleViewModeChange = (value: ModelViewMode) => { - void setTableState({ view_mode: value }); + const handleViewModeChange = (viewMode: ModelViewMode) => { + void setScope({ view_mode: viewMode }); }; const resetFilters = () => { + setSearch(""); setSelectedModelGroup(ALL_MODEL_GROUPS_VALUE); - void setTableState(null); + onColumnFiltersChange([]); + onSortingChange([]); + onPaginationChange(DEFAULT_PAGINATION); + void setScope(null); }; const teamOptions = useMemo( @@ -296,17 +284,18 @@ const AllModelsTab = ({ data={modelData.data} rowCount={rawModelData?.total_count ?? 0} isLoading={isLoading} + isError={isErrorModelsInfo} isRefreshing={isFetchingModelsInfo} onRefresh={handleRefresh} sorting={sorting} onSortingChange={handleSortingChange} pagination={pagination} - onPaginationChange={handlePaginationChange} + onPaginationChange={onPaginationChange} columnFilters={columnFilters} onColumnFiltersChange={handleColumnFiltersChange} onResetFilters={resetFilters} searchValue={modelNameSearch} - onSearchChange={handleSearchChange} + onSearchChange={setSearch} teamOptions={teamOptions} selectedTeamValue={selectedTeamValue} onTeamChange={handleTeamChange} diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTable.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTable.test.tsx index 726070c4bb6..874c3c95f2a 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTable.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTable.test.tsx @@ -1,6 +1,6 @@ import { fireEvent, render, screen, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; -import { describe, expect, it, vi } from "vitest"; +import { beforeEach, describe, expect, it, vi } from "vitest"; import { ModelData } from "@/components/model_dashboard/types"; @@ -76,6 +76,25 @@ const row = (modelId: string): HTMLElement => { }; describe("AllModelsTable", () => { + beforeEach(() => { + localStorage.clear(); + }); + + it("remembers a Source column toggle across remounts", async () => { + const user = userEvent.setup(); + const { unmount } = render(); + expect(screen.queryByRole("columnheader", { name: /^source$/i })).not.toBeInTheDocument(); + + await user.click(screen.getByRole("button", { name: /columns/i })); + await user.click(await screen.findByRole("menuitemcheckbox", { name: /^source$/i })); + expect(await screen.findByRole("columnheader", { name: /^source$/i })).toBeInTheDocument(); + unmount(); + + render(); + expect(screen.getByRole("columnheader", { name: /^source$/i })).toBeInTheDocument(); + expect(screen.getByText("DB Model")).toBeInTheDocument(); + }); + it("renders the nine design columns and hides Source behind the Columns menu", async () => { const user = userEvent.setup(); render(); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTable.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTable.tsx index f46130d2386..6346e0bfaab 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTable.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AllModelsTable.tsx @@ -1,6 +1,6 @@ "use client"; -import { ColumnFiltersState, OnChangeFn, PaginationState, SortingState } from "@tanstack/react-table"; +import { ColumnFiltersState, OnChangeFn, PaginationState, SortingState, VisibilityState } from "@tanstack/react-table"; import { Search, Settings } from "lucide-react"; import { useMemo, useState } from "react"; @@ -10,6 +10,7 @@ import { DataTableFilterDrawer, DataTableFilterField, DataTableToolbar, + usePersistedColumnVisibility, } from "@/components/shared/DataTable"; import { SearchSelect } from "@/components/shared/SearchSelect"; import { Button } from "@/components/ui/button"; @@ -24,13 +25,16 @@ import { STATUS_COLUMN_ID, } from "./ModelsTableColumns"; -export type ModelViewMode = "all" | "current_team"; +export const MODEL_VIEW_MODES = ["current_team", "all"] as const; +export type ModelViewMode = (typeof MODEL_VIEW_MODES)[number]; export const PERSONAL_TEAM_VALUE = "personal"; export const ALL_MODEL_GROUPS_VALUE = "all"; export const WILDCARD_MODEL_GROUP_VALUE = "wildcard"; const MODEL_TABLE_BODY_HEIGHT = 600; +const COLUMN_VISIBILITY_TABLE_ID = "all-models"; +const DEFAULT_COLUMN_VISIBILITY: VisibilityState = { [STATUS_COLUMN_ID]: false }; const FILTER_LABELS: Record = { [MODEL_NAME_COLUMN_ID]: "Public Model Name", @@ -51,6 +55,7 @@ interface AllModelsTableProps { data: ModelData[]; rowCount: number; isLoading: boolean; + isError?: boolean; isRefreshing: boolean; onRefresh: () => void; sorting: SortingState; @@ -99,6 +104,7 @@ export function AllModelsTable({ data, rowCount, isLoading, + isError, isRefreshing, onRefresh, sorting, @@ -129,6 +135,10 @@ export function AllModelsTable({ pausingModelId, }: AllModelsTableProps) { const [filtersOpen, setFiltersOpen] = useState(false); + const { columnVisibility, onColumnVisibilityChange } = usePersistedColumnVisibility( + COLUMN_VISIBILITY_TABLE_ID, + DEFAULT_COLUMN_VISIBILITY, + ); const columns = useMemo(() => { const columnDeps = { @@ -189,10 +199,12 @@ export function AllModelsTable({ filterMode="server" columnFilters={columnFilters} onColumnFiltersChange={onColumnFiltersChange} - defaultColumnVisibility={{ [STATUS_COLUMN_ID]: false }} + columnVisibility={columnVisibility} + onColumnVisibilityChange={onColumnVisibilityChange} enableColumnResizing maxBodyHeight={MODEL_TABLE_BODY_HEIGHT} isLoading={isLoading} + isError={isError} loadingMessage="Loading models…" noDataMessage={} size="compact" diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/ModelsTableColumns.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/ModelsTableColumns.tsx index c5bab598a8b..2f978239c41 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/ModelsTableColumns.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/ModelsTableColumns.tsx @@ -32,11 +32,6 @@ export const MODEL_TABLE_SORT_COLUMN_IDS = [ STATUS_COLUMN_ID, ] as const; -export type ModelTableSortColumnId = (typeof MODEL_TABLE_SORT_COLUMN_IDS)[number]; - -export const isModelTableSortColumnId = (columnId: string): columnId is ModelTableSortColumnId => - (MODEL_TABLE_SORT_COLUMN_IDS as readonly string[]).includes(columnId); - const COLUMN_ID_TO_SERVER_SORT_FIELD: Record = { [COSTS_COLUMN_ID]: "costs", [STATUS_COLUMN_ID]: "status", diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.test.tsx index 105f6ff3043..71d7093c885 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.test.tsx @@ -1,8 +1,11 @@ /* @vitest-environment jsdom */ -import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; -import { render, screen } from "@testing-library/react"; +import { render, renderWithProviders, screen, testQueryClient, waitFor } from "@/../tests/test-utils"; +import type { Team } from "@/components/networking"; +import { QueryClientProvider } from "@tanstack/react-query"; import userEvent from "@testing-library/user-event"; -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { NuqsTestingAdapter, type OnUrlUpdateFunction } from "nuqs/adapters/testing"; +import type { PropsWithChildren } from "react"; +import { beforeEach, describe, expect, it, type Mock, vi } from "vitest"; import ModelsAndEndpointsPage from "./page"; vi.mock("./panels/AllModelsPanel", () => ({ default: () =>
})); @@ -35,7 +38,8 @@ vi.mock("@/components/team/TeamInfo", () => ({ const mockUseAuthorized = vi.fn(); vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({ default: () => mockUseAuthorized() })); -vi.mock("@/app/(dashboard)/hooks/teams/useTeams", () => ({ useTeams: () => ({ data: [] }) })); +const teamsState: { data: Team[] | undefined } = { data: [] }; +vi.mock("@/app/(dashboard)/hooks/teams/useTeams", () => ({ useTeams: () => ({ data: teamsState.data }) })); vi.mock("@/app/(dashboard)/hooks/uiSettings/useUISettings", () => ({ useUISettings: () => ({ data: { values: {} } }), })); @@ -55,19 +59,51 @@ const NON_ADMIN = { // A proxy_admin_viewer session: effectiveSessionRole masquerades the role as "Admin". const VIEW_ONLY_ADMIN = { ...ADMIN, isViewOnly: true }; -const renderPage = () => { - const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: 0 } } }); - return render( - - - , +const TEAM_ADMINED_BY_U1 = { + team_id: "team-1", + team_alias: "Engineering", + members_with_roles: [{ user_id: "u1", role: "admin" }], +} as unknown as Team; + +interface RenderPageOptions { + // NuqsTestingAdapter resets the update queue on mount, which swallows URL writes made by + // mount effects (the useUrlTab fallback clear). Opt out to observe those writes. + keepMountUpdates?: boolean; +} + +const renderPage = (searchParams?: string, { keepMountUpdates = false }: RenderPageOptions = {}) => { + const onUrlUpdate = vi.fn(); + if (!keepMountUpdates) { + const view = renderWithProviders(, { searchParams, onUrlUpdate }); + return { ...view, onUrlUpdate }; + } + const MountPreservingProviders = ({ children }: PropsWithChildren) => ( + + {children} + ); + const view = render(, { wrapper: MountPreservingProviders }); + return { ...view, onUrlUpdate }; }; +const lastUrl = (onUrlUpdate: Mock): URLSearchParams => { + const event = onUrlUpdate.mock.calls.at(-1)?.[0]; + if (!event) throw new Error("no URL update was emitted"); + return event.searchParams; +}; + +const URL_SETTLE_MS = 100; + describe("ModelsAndEndpointsPage", () => { beforeEach(() => { detailState.modelId = null; detailState.teamId = null; + teamsState.data = []; mockUseAuthorized.mockReturnValue(ADMIN); // eslint-disable-next-line @typescript-eslint/no-explicit-any (global as any).ResizeObserver = class { @@ -85,14 +121,102 @@ describe("ModelsAndEndpointsPage", () => { expect(screen.getByTestId("panel-all-models")).toBeInTheDocument(); }); - it("switches tabs in-memory, mounting only the active panel", async () => { + it("switches tabs, mounting only the active panel", async () => { const user = userEvent.setup(); renderPage(); await user.click(screen.getByRole("tab", { name: "Health Status" })); - expect(screen.getByTestId("panel-health")).toBeInTheDocument(); + expect(await screen.findByTestId("panel-health")).toBeInTheDocument(); expect(screen.queryByTestId("panel-all-models")).not.toBeInTheDocument(); }); + describe("?tab= deep links", () => { + it("opens the tab named in the URL on load", () => { + renderPage("?tab=health"); + + expect(screen.getByRole("tab", { name: "Health Status" })).toHaveAttribute("aria-selected", "true"); + expect(screen.getByTestId("panel-health")).toBeInTheDocument(); + expect(screen.queryByTestId("panel-all-models")).not.toBeInTheDocument(); + }); + + it("leaves a URL that names a visible tab untouched", async () => { + const { onUrlUpdate } = renderPage("?tab=llm-credentials", { keepMountUpdates: true }); + + await new Promise((resolve) => setTimeout(resolve, URL_SETTLE_MS)); + + expect(onUrlUpdate).not.toHaveBeenCalled(); + expect(screen.getByTestId("panel-credentials")).toBeInTheDocument(); + }); + + it("writes the selected tab to ?tab= and drops the key when All Models is reselected", async () => { + const user = userEvent.setup(); + const { onUrlUpdate } = renderPage(); + + await user.click(screen.getByRole("tab", { name: "Health Status" })); + await waitFor(() => expect(lastUrl(onUrlUpdate).get("tab")).toBe("health")); + + await user.click(screen.getByRole("tab", { name: "All Models" })); + await waitFor(() => expect(lastUrl(onUrlUpdate).has("tab")).toBe(false)); + expect(screen.getByTestId("panel-all-models")).toBeInTheDocument(); + }); + + it("falls back to All Models and clears ?tab= for a tab the role cannot see", async () => { + mockUseAuthorized.mockReturnValue(NON_ADMIN); + const { onUrlUpdate } = renderPage("?tab=health&model_search=gpt", { keepMountUpdates: true }); + + expect(screen.getByTestId("panel-all-models")).toBeInTheDocument(); + expect(screen.queryByTestId("panel-health")).not.toBeInTheDocument(); + await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled()); + expect(lastUrl(onUrlUpdate).has("tab")).toBe(false); + expect(lastUrl(onUrlUpdate).get("model_search")).toBe("gpt"); + }); + + it("falls back to All Models and clears an unknown ?tab=", async () => { + const { onUrlUpdate } = renderPage("?tab=settings", { keepMountUpdates: true }); + + expect(screen.getByTestId("panel-all-models")).toBeInTheDocument(); + await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled()); + expect(lastUrl(onUrlUpdate).has("tab")).toBe(false); + }); + + it("hides the write-only tabs from a view-only admin deep link", async () => { + mockUseAuthorized.mockReturnValue(VIEW_ONLY_ADMIN); + const { onUrlUpdate } = renderPage("?tab=llm-credentials", { keepMountUpdates: true }); + + expect(screen.getByTestId("panel-all-models")).toBeInTheDocument(); + await waitFor(() => expect(lastUrl(onUrlUpdate).has("tab")).toBe(false)); + }); + + it("holds a team-scoped ?tab= while teams load, then opens it once the user is a team admin", async () => { + mockUseAuthorized.mockReturnValue(NON_ADMIN); + teamsState.data = undefined; + const { onUrlUpdate, rerender } = renderPage("?tab=add", { keepMountUpdates: true }); + + await new Promise((resolve) => setTimeout(resolve, URL_SETTLE_MS)); + expect(onUrlUpdate).not.toHaveBeenCalled(); + expect(screen.queryByTestId("panel-all-models")).not.toBeInTheDocument(); + + teamsState.data = [TEAM_ADMINED_BY_U1]; + rerender(); + + expect(await screen.findByTestId("panel-add")).toBeInTheDocument(); + expect(screen.getByRole("tab", { name: "Add Model" })).toHaveAttribute("aria-selected", "true"); + expect(onUrlUpdate).not.toHaveBeenCalled(); + }); + + it("clears a team-scoped ?tab= once teams load and the user cannot create models", async () => { + mockUseAuthorized.mockReturnValue(NON_ADMIN); + teamsState.data = undefined; + const { onUrlUpdate, rerender } = renderPage("?tab=add"); + + teamsState.data = []; + rerender(); + + expect(await screen.findByTestId("panel-all-models")).toBeInTheDocument(); + await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled()); + expect(lastUrl(onUrlUpdate).has("tab")).toBe(false); + }); + }); + it("renders the model detail overlay from the ?model drill-in and hides the tabs", () => { detailState.modelId = "abc-123"; renderPage(); @@ -186,7 +310,7 @@ describe("ModelsAndEndpointsPage", () => { renderPage(); await user.click(screen.getByRole("tab", { name: /Auto-Routers/ })); - expect(screen.getByTestId("panel-auto-routers")).toBeInTheDocument(); + expect(await screen.findByTestId("panel-auto-routers")).toBeInTheDocument(); }); it("is hidden from non-admins, who cannot write models", () => { diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.tsx index bbc803af700..20894330720 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.tsx @@ -26,6 +26,7 @@ import AccessGroupBudgetsPanel from "@/app/(dashboard)/models-and-endpoints/pane import PriceDataPanel from "@/app/(dashboard)/models-and-endpoints/panels/PriceDataPanel"; import { Button } from "@/components/ui/button"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import { useUrlTab } from "@/hooks/useUrlTab"; type ModelTabSlug = | "add" @@ -39,6 +40,9 @@ type ModelTabSlug = | "price-data"; const BASE_TAB_KEY = "all-models"; +type ModelTabKey = typeof BASE_TAB_KEY | ModelTabSlug; + +const TEAM_SCOPED_TAB_KEYS: readonly ModelTabKey[] = ["add", "auto-routers"]; const TAB_LABELS: Record = { add: "Add Model", @@ -87,7 +91,6 @@ export default function ModelsAndEndpointsPage() { const { modelId, teamId, close } = useModelDetailRouting(); const { availableModelAccessGroups, allModelsOnProxy } = useModelDashboardData(); - const [activeKey, setActiveKey] = useState(BASE_TAB_KEY); const [lastRefreshed, setLastRefreshed] = useState(""); const isInternalUser = userRole && internalUserRoles.includes(userRole); @@ -122,6 +125,12 @@ export default function ModelsAndEndpointsPage() { ], [canCreate, canViewAutoRouters, isAdmin, isViewOnly], ); + const visibleTabKeys = useMemo(() => visibleSlugs.map((slug) => slug || BASE_TAB_KEY), [visibleSlugs]); + const allowedTabKeys = useMemo( + () => (teams === undefined ? Array.from(new Set([...visibleTabKeys, ...TEAM_SCOPED_TAB_KEYS])) : visibleTabKeys), + [teams, visibleTabKeys], + ); + const [activeKey, setActiveKey] = useUrlTab(allowedTabKeys, BASE_TAB_KEY); const allModelsLabel = isAdmin ? "All Models" : "Your Models"; const tabLabel = (slug: "" | ModelTabSlug): React.ReactNode => {