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 13db8794761..f9a35816133 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 @@ -90,9 +90,13 @@ vi.mock("../../hooks/models/useModelCostMap", () => ({ useModelCostMap: () => ({ data: { "gpt-4": { litellm_provider: "openai" } }, isLoading: false, error: null }), })); -const mockTeams = [{ team_id: "team-1", team_alias: "Engineering" }]; +const ENGINEERING_TEAMS = [{ team_id: "team-1", team_alias: "Engineering" }]; +const teamsState: { data: typeof ENGINEERING_TEAMS | undefined; isLoading: boolean } = { + data: ENGINEERING_TEAMS, + isLoading: false, +}; vi.mock("../../hooks/teams/useTeams", () => ({ - useTeams: () => ({ data: mockTeams, isLoading: false, error: null, refetch: vi.fn() }), + useTeams: () => ({ data: teamsState.data, isLoading: teamsState.isLoading, error: null, refetch: vi.fn() }), })); const BASE_MODEL_INFO = { @@ -160,19 +164,17 @@ const defaultProps = { 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; + observeMountUrlWrites?: boolean; } const renderTab = ( props: TabProps = {}, searchParams?: string, - { keepMountUpdates = false }: RenderTabOptions = {}, + { observeMountUrlWrites = false }: RenderTabOptions = {}, ) => { const onUrlUpdate = vi.fn(); const ui = ; - if (!keepMountUpdates) { + if (!observeMountUrlWrites) { const view = renderWithProviders(ui, { searchParams, onUrlUpdate }); return { ...view, onUrlUpdate }; } @@ -208,6 +210,8 @@ describe("AllModelsTab", () => { beforeEach(() => { vi.clearAllMocks(); modelsInfoCalls.length = 0; + teamsState.data = ENGINEERING_TEAMS; + teamsState.isLoading = false; setModelsInfo([makeRow()]); vi.spyOn(useAuthorizedModule, "default").mockReturnValue(MOCK_AUTHORIZED); }); @@ -312,11 +316,70 @@ describe("AllModelsTab", () => { expect(screen.queryByText(/create a Virtual Key/i)).not.toBeInTheDocument(); }); - it("translates a sortable column id in ?sort_by= into its server sort field", () => { + it.each([ + ["model_name", "model_name"], + ["model_info_created_by", "created_at"], + ["model_info_updated_at", "updated_at"], + ["input_cost", "costs"], + ["model_info_db_model", "status"], + ])("translates ?sort_by=%s into the server sort field %s", (columnId, serverField) => { + renderTab({}, `?sort_by=${columnId}&sort_order=desc`); + + expect(lastModelsInfoCall()).toMatchObject({ sortBy: serverField, sortOrder: "desc" }); + }); + + it("defaults ?sort_order= to ascending when only ?sort_by= is present", () => { renderTab({}, "?sort_by=model_info_created_by"); - expect(lastModelsInfoCall().sortBy).toBe("created_at"); - expect(lastModelsInfoCall().sortOrder).toBe("asc"); + expect(lastModelsInfoCall()).toMatchObject({ sortBy: "created_at", sortOrder: "asc" }); + }); + + it("falls back to Current Team Models for an unknown ?view_mode=", () => { + renderTab({}, "?view_mode=bogus"); + + expect(screen.getByTestId("models-view-select")).toHaveTextContent("Current Team Models"); + expect(screen.getByText(/create a Virtual Key without selecting a team/i)).toBeInTheDocument(); + }); + + it("falls back to Personal and clears a ?filter_team= that is not in the loaded team list", async () => { + const { onUrlUpdate } = renderTab({}, "?filter_team=ghost-team", { observeMountUrlWrites: true }); + + expect(modelsInfoCalls.some((call) => call.teamId !== undefined)).toBe(false); + expect(screen.getByTestId("models-team-select")).toHaveTextContent("Personal"); + expect(screen.getByText(/create a Virtual Key without selecting a team/i)).toBeInTheDocument(); + await waitFor(() => expect(lastUrl(onUrlUpdate).has("filter_team")).toBe(false)); + }); + + it("waits for the team list before querying a ?filter_team= deep link and keeps ?page=", async () => { + teamsState.data = undefined; + teamsState.isLoading = true; + setModelsInfo([makeRow()], 1); + const { onUrlUpdate, rerender } = renderTab({}, "?filter_team=team-1&page=3", { + observeMountUrlWrites: true, + }); + + expect(screen.getAllByTestId("skeleton-row").length).toBeGreaterThan(0); + await new Promise((resolve) => setTimeout(resolve, SEARCH_SETTLE_MS)); + expect(onUrlUpdate).not.toHaveBeenCalled(); + + teamsState.data = ENGINEERING_TEAMS; + teamsState.isLoading = false; + setModelsInfo([makeRow()], 200); + rerender(); + + await waitFor(() => expect(lastModelsInfoCall()).toMatchObject({ teamId: "team-1", page: 3 })); + expect(screen.getByTestId("models-team-select")).toHaveTextContent("Engineering"); + expect(onUrlUpdate).not.toHaveBeenCalled(); + }); + + it("queries Personal but keeps ?filter_team= when the team list failed to load", async () => { + teamsState.data = undefined; + const { onUrlUpdate } = renderTab({}, "?filter_team=team-1", { observeMountUrlWrites: true }); + + expect(lastModelsInfoCall().teamId).toBeUndefined(); + expect(screen.getByTestId("models-team-select")).toHaveTextContent("Personal"); + await new Promise((resolve) => setTimeout(resolve, SEARCH_SETTLE_MS)); + expect(onUrlUpdate).not.toHaveBeenCalled(); }); it("ignores a ?sort_by= that names a column which cannot be sorted", async () => { @@ -340,9 +403,24 @@ describe("AllModelsTab", () => { expect(lastModelsInfoCall().size).toBe(50); }); + it("snaps a ?page_size= the table does not offer to the default and drops it on the next page change", async () => { + const user = userEvent.setup(); + setModelsInfo([makeRow()], 200); + const { onUrlUpdate } = renderTab({}, "?page_size=30"); + + expect(lastModelsInfoCall().size).toBe(50); + expect(screen.getByTestId("pagination-page-size")).toHaveTextContent("50"); + expect(screen.getByTestId("pagination-range")).toHaveTextContent("Showing 1-50 of 200"); + + await user.click(screen.getByTestId("pagination-next")); + + await waitFor(() => expect(lastUrl(onUrlUpdate).get("page")).toBe("2")); + expect(lastUrl(onUrlUpdate).has("page_size")).toBe(false); + }); + it("keeps ?page= when the models request failed instead of snapping back to page 1", async () => { setModelsInfoError(); - const { onUrlUpdate } = renderTab({}, "?page=3", { keepMountUpdates: true }); + const { onUrlUpdate } = renderTab({}, "?page=3", { observeMountUrlWrites: true }); await new Promise((resolve) => setTimeout(resolve, SEARCH_SETTLE_MS)); @@ -352,7 +430,7 @@ describe("AllModelsTab", () => { it("clamps ?page= to the last page once the server reports fewer rows", async () => { setModelsInfo([makeRow()], 1); - const { onUrlUpdate } = renderTab({}, "?page=3", { keepMountUpdates: true }); + const { onUrlUpdate } = renderTab({}, "?page=3", { observeMountUrlWrites: true }); await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled()); expect(lastUrl(onUrlUpdate).has("page")).toBe(false); @@ -405,13 +483,16 @@ describe("AllModelsTab", () => { 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 () => { + it("writes header sorting to ?sort_by= and ?sort_order=, resets the page, and clears both when unsorted", async () => { const user = userEvent.setup(); - const { onUrlUpdate } = renderTab(); + setModelsInfo([makeRow()], 100); + const { onUrlUpdate } = renderTab({}, "?page=2"); 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); + expect(lastUrl(onUrlUpdate).has("page")).toBe(false); + await waitFor(() => expect(lastModelsInfoCall().page).toBe(1)); await user.click(sortHeader("model_info_updated_at")); await waitFor(() => expect(lastUrl(onUrlUpdate).get("sort_order")).toBe("desc")); @@ -421,9 +502,10 @@ describe("AllModelsTab", () => { 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 () => { + it("writes the access group filter to ?access_group=, resets the page, and drops it with its chip", async () => { const user = userEvent.setup(); - const { onUrlUpdate } = renderTab(); + setModelsInfo([makeRow()], 100); + const { onUrlUpdate } = renderTab({}, "?page=2"); await user.click(screen.getByTestId("datatable-filters-trigger")); await user.click(await screen.findByPlaceholderText("Filter by Model Access Group")); @@ -431,6 +513,7 @@ describe("AllModelsTab", () => { await user.click(screen.getByTestId("filter-drawer-apply")); await waitFor(() => expect(lastUrl(onUrlUpdate).get("access_group")).toBe("sales-team")); + expect(lastUrl(onUrlUpdate).has("page")).toBe(false); await user.click(await screen.findByTestId("filter-chip-remove-model_info_access_groups")); @@ -476,34 +559,10 @@ describe("AllModelsTab", () => { }); }); - it("queries the selected team and resets to the first page", async () => { + it("applies a public model name filter through the drawer and resets the page", async () => { const user = userEvent.setup(); - renderTab(); - - expect(lastModelsInfoCall().teamId).toBeUndefined(); - - await user.click(screen.getByTestId("models-team-select")); - await user.click(await screen.findByRole("option", { name: "Engineering" })); - - await waitFor(() => { - expect(lastModelsInfoCall().teamId).toBe("team-1"); - }); - expect(lastModelsInfoCall().page).toBe(1); - }); - - it("debounces the model name search into the server query", async () => { - renderTab(); - - fireEvent.change(screen.getByTestId("datatable-search"), { target: { value: "claude" } }); - - await waitFor(() => { - expect(lastModelsInfoCall().search).toBe("claude"); - }); - }); - - it("applies a public model name filter through the drawer", async () => { - const user = userEvent.setup(); - renderTab(); + setModelsInfo([makeRow()], 100); + const { onUrlUpdate } = renderTab({}, "?page=2"); await user.click(screen.getByTestId("datatable-filters-trigger")); await user.click(await screen.findByPlaceholderText("Filter by Public Model Name")); @@ -513,6 +572,8 @@ describe("AllModelsTab", () => { await waitFor(() => { expect(mockSetSelectedModelGroup).toHaveBeenCalledWith("gpt-3.5-turbo"); }); + await waitFor(() => expect(lastUrl(onUrlUpdate).has("page")).toBe(false)); + await waitFor(() => expect(lastModelsInfoCall().page).toBe(1)); }); it("renders every row the server returned for the selected model group so rows match the footer total", () => { @@ -577,23 +638,6 @@ describe("AllModelsTab", () => { expect(lastModelsInfoCall().modelName).toBe("claude-opus"); }); - it("resets search, filters, team and sorting from the drawer reset button", async () => { - const user = userEvent.setup(); - renderTab({ selectedModelGroup: "gpt-4" }); - - await user.click(screen.getByTestId("models-team-select")); - await user.click(await screen.findByRole("option", { name: "Engineering" })); - await waitFor(() => expect(lastModelsInfoCall().teamId).toBe("team-1")); - - await user.click(screen.getByTestId("datatable-filters-trigger")); - await user.click(await screen.findByTestId("filter-drawer-reset")); - - expect(mockSetSelectedModelGroup).toHaveBeenCalledWith("all"); - await waitFor(() => { - expect(lastModelsInfoCall().teamId).toBeUndefined(); - }); - }); - it("opens the delete modal from the row and deletes the model", async () => { const user = userEvent.setup(); renderTab(); 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 35a57b1e11d..942bd851062 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 @@ -9,13 +9,13 @@ 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"; +import { modelDeleteCall, modelPatchUpdateCall, type Team } from "@/components/networking"; 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 { parseAsString, parseAsStringLiteral, useQueryStates } from "nuqs"; -import { useCallback, useMemo, useState } from "react"; +import { useCallback, useEffect, useMemo, useState } from "react"; import { useModelsInfo } from "../../hooks/models/useModels"; import { transformModelData } from "../utils/modelDataTransformer"; @@ -24,6 +24,7 @@ import { AllModelsTable, MODEL_VIEW_MODES, ModelViewMode, + PAGE_SIZE_OPTIONS, PERSONAL_TEAM_VALUE, WILDCARD_MODEL_GROUP_VALUE, } from "./AllModelsTable"; @@ -36,7 +37,7 @@ import { const SEARCH_DEBOUNCE_WAIT_MS = 200; const DEFAULT_PAGE_SIZE = 50; -const MAX_PAGE_SIZE = 50; +const MAX_PAGE_SIZE = Math.max(...PAGE_SIZE_OPTIONS); const DEFAULT_PAGINATION: PaginationState = { pageIndex: 0, pageSize: DEFAULT_PAGE_SIZE }; const UNSORTED_COLUMN_ID = ""; const FILTER_COLUMNS = [ACCESS_GROUPS_COLUMN_ID] as const; @@ -56,11 +57,45 @@ const SCOPE_PARSERS = { view_mode: parseAsStringLiteral(MODEL_VIEW_MODES).withDefault("current_team"), }; +const toOfferedPageSize = (pageSize: number): number => + PAGE_SIZE_OPTIONS.includes(pageSize) ? pageSize : DEFAULT_PAGE_SIZE; + 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 ModelGroupQuery { + modelName?: string; + wildcardOnly: boolean; +} + +const toModelGroupQuery = (modelGroup: string | null): ModelGroupQuery => { + if (!modelGroup || modelGroup === ALL_MODEL_GROUPS_VALUE) return { wildcardOnly: false }; + if (modelGroup === WILDCARD_MODEL_GROUP_VALUE) return { wildcardOnly: true }; + return { modelName: modelGroup, wildcardOnly: false }; +}; + +const toAccessGroupQuery = (accessGroup: string | null): string | undefined => + accessGroup && accessGroup !== ALL_MODEL_GROUPS_VALUE ? accessGroup : undefined; + +const useTeamScope = (teams: Team[] | undefined, isLoadingTeams: boolean) => { + const [{ filter_team: urlTeamValue, view_mode }, setScope] = useQueryStates(SCOPE_PARSERS); + const isKnownTeam = + urlTeamValue === PERSONAL_TEAM_VALUE || (teams ?? []).some((team) => team.team_id === urlTeamValue); + + useEffect(() => { + if (teams !== undefined && !isKnownTeam) void setScope({ filter_team: null }); + }, [teams, isKnownTeam, setScope]); + + return { + selectedTeamValue: isKnownTeam ? urlTeamValue : PERSONAL_TEAM_VALUE, + isTeamPending: isLoadingTeams && !isKnownTeam, + modelViewMode: view_mode, + setScope, + }; +}; + interface ServerSort { sortBy?: string; sortOrder?: "asc" | "desc"; @@ -98,34 +133,30 @@ const AllModelsTab = ({ setSearch, sorting: urlSorting, onSortingChange, - pagination, - onPaginationChange, + pagination: urlPagination, + onPaginationChange: onUrlPaginationChange, columnFilters: urlColumnFilters, onColumnFiltersChange, } = useUrlTableState(TABLE_STATE_OPTIONS); - const [{ filter_team: selectedTeamValue, view_mode: modelViewMode }, setScope] = useQueryStates(SCOPE_PARSERS); + const { selectedTeamValue, isTeamPending, modelViewMode, setScope } = useTeamScope(teams, isLoadingTeams); const [debouncedSearch] = useDebouncedValue(modelNameSearch, { wait: SEARCH_DEBOUNCE_WAIT_MS }); const [isModelSettingsModalVisible, setIsModelSettingsModalVisible] = useState(false); const [deleteModalModelId, setDeleteModalModelId] = useState(null); const [deleteLoading, setDeleteLoading] = useState(false); const [pausingModelId, setPausingModelId] = useState(null); + const offeredPageSize = toOfferedPageSize(urlPagination.pageSize); + const pagination = useMemo( + () => ({ pageIndex: urlPagination.pageIndex, pageSize: offeredPageSize }), + [urlPagination.pageIndex, offeredPageSize], + ); + 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) && - selectedModelGroup !== ALL_MODEL_GROUPS_VALUE && - selectedModelGroup !== WILDCARD_MODEL_GROUP_VALUE; - const modelNameForQuery = isConcreteModelGroup ? selectedModelGroup ?? undefined : undefined; - const accessGroupForQuery = - selectedModelAccessGroupFilter && selectedModelAccessGroupFilter !== ALL_MODEL_GROUPS_VALUE - ? selectedModelAccessGroupFilter - : undefined; - const wildcardOnlyForQuery = selectedModelGroup === WILDCARD_MODEL_GROUP_VALUE; + const { modelName: modelNameForQuery, wildcardOnly: wildcardOnlyForQuery } = toModelGroupQuery(selectedModelGroup); + const accessGroupForQuery = toAccessGroupQuery(appliedAccessGroup(urlColumnFilters)); const { data: rawModelData, @@ -148,7 +179,7 @@ const AllModelsTab = ({ accessGroupForQuery, wildcardOnlyForQuery, ); - const isLoading = isLoadingModelsInfo || isLoadingModelCostMap; + const isLoading = isLoadingModelsInfo || isLoadingModelCostMap || isTeamPending; const getProviderFromModel = useCallback( (model: string) => { @@ -184,13 +215,17 @@ const AllModelsTab = ({ onColumnFiltersChange(next); }; + const handlePaginationChange: OnChangeFn = (updater) => { + onUrlPaginationChange(functionalUpdate(updater, pagination)); + }; + const handleSortingChange: OnChangeFn = (updater) => { onSortingChange(functionalUpdate(updater, sorting)); }; const handleTeamChange = (value: string) => { void setScope({ filter_team: value }); - onPaginationChange({ pageIndex: 0, pageSize: pagination.pageSize }); + onUrlPaginationChange({ pageIndex: 0, pageSize: pagination.pageSize }); }; const handleViewModeChange = (viewMode: ModelViewMode) => { @@ -202,7 +237,7 @@ const AllModelsTab = ({ setSelectedModelGroup(ALL_MODEL_GROUPS_VALUE); onColumnFiltersChange([]); onSortingChange([]); - onPaginationChange(DEFAULT_PAGINATION); + onUrlPaginationChange(DEFAULT_PAGINATION); void setScope(null); }; @@ -290,7 +325,7 @@ const AllModelsTab = ({ sorting={sorting} onSortingChange={handleSortingChange} pagination={pagination} - onPaginationChange={onPaginationChange} + onPaginationChange={handlePaginationChange} columnFilters={columnFilters} onColumnFiltersChange={handleColumnFiltersChange} onResetFilters={resetFilters} 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 874c3c95f2a..d3b455e0890 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 @@ -95,6 +95,14 @@ describe("AllModelsTable", () => { expect(screen.getByText("DB Model")).toBeInTheDocument(); }); + it("restores column choices saved under the all-models storage key", () => { + localStorage.setItem("litellm_table_columns_all-models", JSON.stringify({ model_info_db_model: true })); + + render(); + + expect(screen.getByRole("columnheader", { name: /^source$/i })).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 6346e0bfaab..c95ad054311 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 @@ -31,6 +31,7 @@ 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"; +export const PAGE_SIZE_OPTIONS: readonly number[] = [10, 25, 50]; const MODEL_TABLE_BODY_HEIGHT = 600; const COLUMN_VISIBILITY_TABLE_ID = "all-models"; @@ -195,7 +196,7 @@ export function AllModelsTable({ pagination={pagination} onPaginationChange={onPaginationChange} rowCount={rowCount} - pageSizeOptions={[10, 25, 50]} + pageSizeOptions={[...PAGE_SIZE_OPTIONS]} filterMode="server" columnFilters={columnFilters} onColumnFiltersChange={onColumnFiltersChange} 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 71d7093c885..cd6f2e671c9 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 @@ -38,8 +38,10 @@ vi.mock("@/components/team/TeamInfo", () => ({ const mockUseAuthorized = vi.fn(); vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({ default: () => mockUseAuthorized() })); -const teamsState: { data: Team[] | undefined } = { data: [] }; -vi.mock("@/app/(dashboard)/hooks/teams/useTeams", () => ({ useTeams: () => ({ data: teamsState.data }) })); +const teamsState: { data: Team[] | undefined; isLoading: boolean } = { data: [], isLoading: false }; +vi.mock("@/app/(dashboard)/hooks/teams/useTeams", () => ({ + useTeams: () => ({ data: teamsState.data, isLoading: teamsState.isLoading }), +})); vi.mock("@/app/(dashboard)/hooks/uiSettings/useUISettings", () => ({ useUISettings: () => ({ data: { values: {} } }), })); @@ -66,14 +68,12 @@ const TEAM_ADMINED_BY_U1 = { } 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; + observeMountUrlWrites?: boolean; } -const renderPage = (searchParams?: string, { keepMountUpdates = false }: RenderPageOptions = {}) => { +const renderPage = (searchParams?: string, { observeMountUrlWrites = false }: RenderPageOptions = {}) => { const onUrlUpdate = vi.fn(); - if (!keepMountUpdates) { + if (!observeMountUrlWrites) { const view = renderWithProviders(, { searchParams, onUrlUpdate }); return { ...view, onUrlUpdate }; } @@ -104,6 +104,7 @@ describe("ModelsAndEndpointsPage", () => { detailState.modelId = null; detailState.teamId = null; teamsState.data = []; + teamsState.isLoading = false; mockUseAuthorized.mockReturnValue(ADMIN); // eslint-disable-next-line @typescript-eslint/no-explicit-any (global as any).ResizeObserver = class { @@ -139,7 +140,7 @@ describe("ModelsAndEndpointsPage", () => { }); it("leaves a URL that names a visible tab untouched", async () => { - const { onUrlUpdate } = renderPage("?tab=llm-credentials", { keepMountUpdates: true }); + const { onUrlUpdate } = renderPage("?tab=llm-credentials", { observeMountUrlWrites: true }); await new Promise((resolve) => setTimeout(resolve, URL_SETTLE_MS)); @@ -161,7 +162,7 @@ describe("ModelsAndEndpointsPage", () => { 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 }); + const { onUrlUpdate } = renderPage("?tab=health&model_search=gpt", { observeMountUrlWrites: true }); expect(screen.getByTestId("panel-all-models")).toBeInTheDocument(); expect(screen.queryByTestId("panel-health")).not.toBeInTheDocument(); @@ -171,7 +172,7 @@ describe("ModelsAndEndpointsPage", () => { }); it("falls back to All Models and clears an unknown ?tab=", async () => { - const { onUrlUpdate } = renderPage("?tab=settings", { keepMountUpdates: true }); + const { onUrlUpdate } = renderPage("?tab=settings", { observeMountUrlWrites: true }); expect(screen.getByTestId("panel-all-models")).toBeInTheDocument(); await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled()); @@ -180,7 +181,7 @@ describe("ModelsAndEndpointsPage", () => { 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 }); + const { onUrlUpdate } = renderPage("?tab=llm-credentials", { observeMountUrlWrites: true }); expect(screen.getByTestId("panel-all-models")).toBeInTheDocument(); await waitFor(() => expect(lastUrl(onUrlUpdate).has("tab")).toBe(false)); @@ -189,13 +190,15 @@ describe("ModelsAndEndpointsPage", () => { 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 }); + teamsState.isLoading = true; + const { onUrlUpdate, rerender } = renderPage("?tab=add", { observeMountUrlWrites: 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]; + teamsState.isLoading = false; rerender(); expect(await screen.findByTestId("panel-add")).toBeInTheDocument(); @@ -206,15 +209,42 @@ describe("ModelsAndEndpointsPage", () => { it("clears a team-scoped ?tab= once teams load and the user cannot create models", async () => { mockUseAuthorized.mockReturnValue(NON_ADMIN); teamsState.data = undefined; + teamsState.isLoading = true; const { onUrlUpdate, rerender } = renderPage("?tab=add"); teamsState.data = []; + teamsState.isLoading = false; rerender(); expect(await screen.findByTestId("panel-all-models")).toBeInTheDocument(); await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled()); expect(lastUrl(onUrlUpdate).has("tab")).toBe(false); }); + + it.each(["add", "auto-routers"])( + "falls back to Your Models and clears ?tab=%s when the teams request failed", + async (tab) => { + mockUseAuthorized.mockReturnValue(NON_ADMIN); + teamsState.data = undefined; + teamsState.isLoading = false; + const { onUrlUpdate } = renderPage(`?tab=${tab}`, { observeMountUrlWrites: true }); + + expect(screen.getByTestId("panel-all-models")).toBeInTheDocument(); + expect(screen.getByRole("tab", { name: "Your Models" })).toHaveAttribute("aria-selected", "true"); + await waitFor(() => expect(lastUrl(onUrlUpdate).has("tab")).toBe(false)); + }, + ); + + it("does not hold ?tab=add for a view-only admin while teams load", async () => { + mockUseAuthorized.mockReturnValue(VIEW_ONLY_ADMIN); + teamsState.data = undefined; + teamsState.isLoading = true; + const { onUrlUpdate } = renderPage("?tab=add", { observeMountUrlWrites: true }); + + expect(screen.getByTestId("panel-all-models")).toBeInTheDocument(); + expect(screen.getByRole("tab", { name: "All Models" })).toHaveAttribute("aria-selected", "true"); + await waitFor(() => expect(lastUrl(onUrlUpdate).has("tab")).toBe(false)); + }); }); it("renders the model detail overlay from the ?model drill-in and hides the tabs", () => { 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 20894330720..577a28aef43 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 @@ -85,7 +85,7 @@ const renderPanel = (key: string) => { export default function ModelsAndEndpointsPage() { const { accessToken, userRole, userId: userID, premiumUser, isViewOnly } = useAuthorized(); - const { data: teams } = useTeams(); + const { data: teams, isLoading: isLoadingTeams } = useTeams(); const { data: uiSettings } = useUISettings(); const queryClient = useQueryClient(); const { modelId, teamId, close } = useModelDetailRouting(); @@ -126,9 +126,10 @@ export default function ModelsAndEndpointsPage() { [canCreate, canViewAutoRouters, isAdmin, isViewOnly], ); const visibleTabKeys = useMemo(() => visibleSlugs.map((slug) => slug || BASE_TAB_KEY), [visibleSlugs]); + const holdTeamScopedTabs = isLoadingTeams && !isViewOnly; const allowedTabKeys = useMemo( - () => (teams === undefined ? Array.from(new Set([...visibleTabKeys, ...TEAM_SCOPED_TAB_KEYS])) : visibleTabKeys), - [teams, visibleTabKeys], + () => (holdTeamScopedTabs ? Array.from(new Set([...visibleTabKeys, ...TEAM_SCOPED_TAB_KEYS])) : visibleTabKeys), + [holdTeamScopedTabs, visibleTabKeys], ); const [activeKey, setActiveKey] = useUrlTab(allowedTabKeys, BASE_TAB_KEY); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/panels/AllModelsPanel.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/panels/AllModelsPanel.test.tsx new file mode 100644 index 00000000000..4f7a0c376b4 --- /dev/null +++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/panels/AllModelsPanel.test.tsx @@ -0,0 +1,187 @@ +/* @vitest-environment jsdom */ +import { renderWithProviders, screen, waitFor } from "@/../tests/test-utils"; +import userEvent from "@testing-library/user-event"; +import type { OnUrlUpdateFunction } from "nuqs/adapters/testing"; +import { beforeEach, describe, expect, it, type Mock, vi } from "vitest"; + +import AllModelsPanel from "./AllModelsPanel"; + +vi.mock("@/components/networking", () => ({ + serverRootPath: "/", + modelDeleteCall: vi.fn(), + modelPatchUpdateCall: vi.fn(), +})); + +vi.mock("@/components/model_dashboard/ModelSettingsModal/ModelSettingsModal", () => ({ default: () => null })); + +interface ModelsInfoQuery { + page?: number; + modelName?: string; + wildcardOnly?: boolean; +} + +const modelsInfoCalls: ModelsInfoQuery[] = []; + +type UseModelsInfoArgs = [ + page?: number, + size?: number, + search?: string, + modelId?: string, + teamId?: string, + sortBy?: string, + sortOrder?: string, + excludeAutoRouters?: boolean, + modelName?: string, + accessGroup?: string, + wildcardOnly?: boolean, +]; + +const MODEL_ROW = { + model_name: "gpt-4", + litellm_params: { model: "openai/gpt-4", custom_llm_provider: "openai" }, + model_info: { + id: "model-1", + db_model: true, + created_by: "user-123", + created_at: "2024-01-01T00:00:00Z", + updated_at: "2024-01-02T00:00:00Z", + team_id: "team-1", + access_groups: [], + }, +}; + +vi.mock("@/app/(dashboard)/hooks/models/useModels", () => ({ + useModelsInfo: (...args: UseModelsInfoArgs) => { + const [page, , , , , , , , modelName, , wildcardOnly] = args; + modelsInfoCalls.push({ page, modelName, wildcardOnly }); + return { + data: { data: [MODEL_ROW], total_count: 100, current_page: page, total_pages: 2, size: 50 }, + isLoading: false, + isFetching: false, + isError: false, + refetch: vi.fn(), + }; + }, +})); + +vi.mock("@/app/(dashboard)/hooks/models/useModelCostMap", () => ({ + useModelCostMap: () => ({ data: {}, isLoading: false }), +})); + +vi.mock("@/app/(dashboard)/hooks/teams/useTeams", () => ({ + useTeams: () => ({ data: [{ team_id: "team-1", team_alias: "Engineering" }], isLoading: false }), +})); + +vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({ + default: () => ({ + accessToken: "at", + userId: "user-123", + userRole: "Admin", + isViewOnly: false, + }), +})); + +vi.mock("@/app/(dashboard)/models-and-endpoints/useModelDashboardData", () => ({ + useModelDashboardData: () => ({ + availableModelGroups: ["gpt-4", "gpt-3.5-turbo"], + availableModelAccessGroups: ["sales-team"], + }), +})); + +const renderPanel = (searchParams?: string) => { + const onUrlUpdate = vi.fn(); + renderWithProviders(, { searchParams, onUrlUpdate }); + return { onUrlUpdate }; +}; + +const lastUpdate = (onUrlUpdate: Mock) => { + const event = onUrlUpdate.mock.calls.at(-1)?.[0]; + if (!event) throw new Error("no URL update was emitted"); + return event; +}; + +const lastModelsInfoCall = (): ModelsInfoQuery => modelsInfoCalls[modelsInfoCalls.length - 1]; + +const URL_SETTLE_MS = 100; + +describe("AllModelsPanel", () => { + beforeEach(() => { + modelsInfoCalls.length = 0; + }); + + it("clears model_group together with every table key in one URL update from Reset Filters", async () => { + const user = userEvent.setup(); + const { onUrlUpdate } = renderPanel( + "?model_group=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", + ); + expect(lastModelsInfoCall().modelName).toBe("gpt-4"); + + await user.click(screen.getByTestId("datatable-filters-trigger")); + await user.click(await screen.findByTestId("filter-drawer-reset")); + + await waitFor(() => expect(onUrlUpdate).toHaveBeenCalledTimes(1)); + await new Promise((resolve) => setTimeout(resolve, URL_SETTLE_MS)); + expect(onUrlUpdate).toHaveBeenCalledTimes(1); + expect(lastUpdate(onUrlUpdate).searchParams.toString()).toBe(""); + await waitFor(() => expect(lastModelsInfoCall()).toMatchObject({ page: 1, modelName: undefined })); + }); + + it("queries the exact model group named in ?model_group= and shows it as a chip", () => { + renderPanel("?model_group=gpt-4"); + + expect(lastModelsInfoCall()).toMatchObject({ modelName: "gpt-4", wildcardOnly: false }); + expect(screen.getByTestId("filter-chip-model_name")).toHaveTextContent("gpt-4"); + }); + + it("treats ?model_group=wildcard as the wildcard-only sentinel", () => { + renderPanel("?model_group=wildcard"); + + expect(lastModelsInfoCall()).toMatchObject({ modelName: undefined, wildcardOnly: true }); + expect(screen.getByTestId("filter-chip-model_name")).toHaveTextContent("Wildcard Models (*)"); + }); + + it("treats ?model_group=all as no model group filter", () => { + renderPanel("?model_group=all"); + + expect(lastModelsInfoCall()).toMatchObject({ modelName: undefined, wildcardOnly: false }); + expect(screen.queryByTestId("filter-chip-model_name")).not.toBeInTheDocument(); + }); + + it("writes a drawer Public Model Name to ?model_group= and resets the page in the same update", async () => { + const user = userEvent.setup(); + const { onUrlUpdate } = renderPanel("?page=2"); + + await user.click(screen.getByTestId("datatable-filters-trigger")); + await user.click(await screen.findByPlaceholderText("Filter by Public Model Name")); + await user.click(await screen.findByRole("option", { name: "gpt-3.5-turbo" })); + await user.click(screen.getByTestId("filter-drawer-apply")); + + await waitFor(() => expect(lastUpdate(onUrlUpdate).searchParams.get("model_group")).toBe("gpt-3.5-turbo")); + expect(lastUpdate(onUrlUpdate).searchParams.has("page")).toBe(false); + await waitFor(() => expect(lastModelsInfoCall()).toMatchObject({ page: 1, modelName: "gpt-3.5-turbo" })); + }); + + it("drops ?model_group= when its filter chip is removed", async () => { + const user = userEvent.setup(); + const { onUrlUpdate } = renderPanel("?model_group=gpt-4&model_search=claude"); + + await user.click(screen.getByTestId("filter-chip-remove-model_name")); + + await waitFor(() => expect(lastUpdate(onUrlUpdate).searchParams.has("model_group")).toBe(false)); + expect(lastUpdate(onUrlUpdate).searchParams.get("model_search")).toBe("claude"); + }); + + it("pushes ?model= onto history for the drill-in and keeps the table state for Back", async () => { + const user = userEvent.setup(); + const { onUrlUpdate } = renderPanel("?model_search=claude&page=2&filter_team=team-1"); + + await user.click(await screen.findByTestId("model-id-model-1")); + + await waitFor(() => expect(lastUpdate(onUrlUpdate).searchParams.get("model")).toBe("model-1")); + const { searchParams, options } = lastUpdate(onUrlUpdate); + expect(options.history).toBe("push"); + expect(searchParams.get("model_search")).toBe("claude"); + expect(searchParams.get("page")).toBe("2"); + expect(searchParams.get("filter_team")).toBe("team-1"); + }); +});