mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
Merge 16d639982b into 252c71c0b2
This commit is contained in:
commit
822026153d
4 changed files with 135 additions and 27 deletions
|
|
@ -6,7 +6,7 @@ import OrganizationFilters, { FilterState } from "./OrganizationFilters";
|
|||
describe("OrganizationFilters", () => {
|
||||
const defaultFilters: FilterState = {
|
||||
org_id: "",
|
||||
org_alias: "",
|
||||
search: "",
|
||||
};
|
||||
|
||||
it("should render", () => {
|
||||
|
|
@ -24,7 +24,7 @@ describe("OrganizationFilters", () => {
|
|||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByPlaceholderText("Search by Organization Name")).toBeInTheDocument();
|
||||
expect(screen.getByPlaceholderText("Search by organization name or ID")).toBeInTheDocument();
|
||||
expect(screen.getByRole("button", { name: /^filters$/i })).toBeInTheDocument();
|
||||
expect(screen.getByRole("button", { name: /reset filters/i })).toBeInTheDocument();
|
||||
});
|
||||
|
|
@ -63,12 +63,12 @@ describe("OrganizationFilters", () => {
|
|||
/>,
|
||||
);
|
||||
|
||||
const input = screen.getByPlaceholderText("Search by Organization Name");
|
||||
const input = screen.getByPlaceholderText("Search by organization name or ID");
|
||||
fireEvent.change(input, { target: { value: "test" } });
|
||||
|
||||
await waitFor(
|
||||
() => {
|
||||
expect(onChange).toHaveBeenCalledWith("org_alias", expect.any(String));
|
||||
expect(onChange).toHaveBeenCalledWith("search", expect.any(String));
|
||||
},
|
||||
{ timeout: 500 },
|
||||
);
|
||||
|
|
@ -103,7 +103,7 @@ describe("OrganizationFilters", () => {
|
|||
|
||||
const filtersWithActive: FilterState = {
|
||||
...defaultFilters,
|
||||
org_alias: "test org",
|
||||
search: "test org",
|
||||
};
|
||||
|
||||
const { container } = render(
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ interface OrganizationFiltersProps {
|
|||
|
||||
type FilterState = {
|
||||
org_id: string;
|
||||
org_alias: string;
|
||||
search: string;
|
||||
};
|
||||
|
||||
const OrganizationFilters = ({
|
||||
|
|
@ -23,16 +23,16 @@ const OrganizationFilters = ({
|
|||
onChange,
|
||||
onReset,
|
||||
}: OrganizationFiltersProps) => {
|
||||
const hasActiveFilters = !!(filters.org_id || filters.org_alias);
|
||||
const hasActiveFilters = !!(filters.org_id || filters.search);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col space-y-4">
|
||||
{/* Search and Filter Controls */}
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
<FilterInput
|
||||
placeholder="Search by Organization Name"
|
||||
value={filters.org_alias}
|
||||
onChange={(value) => onChange("org_alias", value)}
|
||||
placeholder="Search by organization name or ID"
|
||||
value={filters.search}
|
||||
onChange={(value) => onChange("search", value)}
|
||||
icon={Search}
|
||||
className="w-64"
|
||||
/>
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { act, render, screen, waitFor } from "@testing-library/react";
|
||||
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||
import { NuqsTestingAdapter, type UrlUpdateEvent } from "nuqs/adapters/testing";
|
||||
import React from "react";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type OrganizationsTableComponent from "./OrganizationsTable";
|
||||
import type { Organization } from "@/components/networking";
|
||||
import type OrganizationInfoViewComponent from "@/components/organization/organization_view";
|
||||
|
||||
vi.mock("@/components/vector_store_management/VectorStoreSelector", () => ({
|
||||
|
|
@ -14,13 +15,59 @@ vi.mock("@/components/mcp_server_management/MCPServerSelector", () => ({
|
|||
__esModule: true,
|
||||
default: () => null,
|
||||
}));
|
||||
type AuthState = { accessToken: string | null; userId: string | null; userRole: string | null };
|
||||
const SIGNED_OUT: AuthState = { accessToken: null, userId: null, userRole: null };
|
||||
const SIGNED_IN: AuthState = { accessToken: "sk-test", userId: "user-1", userRole: "Admin" };
|
||||
const mockUseAuthorized = vi.fn<() => AuthState>(() => SIGNED_OUT);
|
||||
vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({
|
||||
default: () => ({
|
||||
accessToken: null,
|
||||
userId: null,
|
||||
userRole: null,
|
||||
}),
|
||||
default: () => mockUseAuthorized(),
|
||||
}));
|
||||
|
||||
type ServerOrganization = Omit<Organization, "organization_alias"> & { organization_alias: string | null };
|
||||
const makeOrganization = (organization_id: string, organization_alias: string | null): ServerOrganization => ({
|
||||
organization_id,
|
||||
organization_alias,
|
||||
budget_id: "",
|
||||
metadata: {},
|
||||
models: [],
|
||||
spend: 0,
|
||||
model_spend: {},
|
||||
created_at: "2024-01-01T00:00:00Z",
|
||||
created_by: "user-1",
|
||||
updated_at: "2024-01-01T00:00:00Z",
|
||||
updated_by: "user-1",
|
||||
litellm_budget_table: null,
|
||||
teams: null,
|
||||
users: null,
|
||||
members: null,
|
||||
});
|
||||
const ALPHA_ORG_ID = "ff9eb074-3f07-4e13-91fb-5337fb10d760";
|
||||
const BETA_ORG_ID = "0a1b2c3d-1111-4222-8333-444455556666";
|
||||
const NO_ALIAS_ORG_ID = "7b6a5f4e-0000-4aaa-8bbb-cccddd111222";
|
||||
const SERVER_ORGANIZATIONS: readonly ServerOrganization[] = [
|
||||
makeOrganization(ALPHA_ORG_ID, "Alpha Org"),
|
||||
makeOrganization(BETA_ORG_ID, "Beta Org"),
|
||||
makeOrganization("9f8e7d6c-9999-4888-8777-666655554444", "Gamma Org"),
|
||||
];
|
||||
const matchesServerFilters = (organization: ServerOrganization, orgId: string | null, orgAlias: string | null) => {
|
||||
const idMatches = !orgId || organization.organization_id === orgId;
|
||||
const aliasMatches =
|
||||
!orgAlias || (organization.organization_alias?.toLowerCase().includes(orgAlias.toLowerCase()) ?? false);
|
||||
return idMatches && aliasMatches;
|
||||
};
|
||||
const fakeOrganizationList =
|
||||
(organizations: readonly ServerOrganization[]) =>
|
||||
(_accessToken: string, orgId: string | null = null, orgAlias: string | null = null) =>
|
||||
Promise.resolve(organizations.filter((organization) => matchesServerFilters(organization, orgId, orgAlias)));
|
||||
const mockOrganizationListCall = vi.fn(fakeOrganizationList(SERVER_ORGANIZATIONS));
|
||||
vi.mock("@/components/networking", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("@/components/networking")>();
|
||||
return {
|
||||
...actual,
|
||||
organizationListCall: (...args: Parameters<typeof mockOrganizationListCall>) => mockOrganizationListCall(...args),
|
||||
modelAvailableCall: () => Promise.resolve({ data: [] }),
|
||||
};
|
||||
});
|
||||
type OrganizationsTableProps = React.ComponentProps<typeof OrganizationsTableComponent>;
|
||||
type OrganizationInfoViewProps = React.ComponentProps<typeof OrganizationInfoViewComponent>;
|
||||
|
||||
|
|
@ -48,9 +95,18 @@ const onUrlUpdate = vi.fn<(event: UrlUpdateEvent) => void>();
|
|||
interface RenderPanelOptions {
|
||||
premiumUser?: boolean;
|
||||
searchParams?: string;
|
||||
auth?: AuthState;
|
||||
organizations?: readonly ServerOrganization[];
|
||||
}
|
||||
|
||||
const renderPanel = ({ premiumUser = true, searchParams = "" }: RenderPanelOptions = {}) => {
|
||||
const renderPanel = ({
|
||||
premiumUser = true,
|
||||
searchParams = "",
|
||||
auth = SIGNED_OUT,
|
||||
organizations = SERVER_ORGANIZATIONS,
|
||||
}: RenderPanelOptions = {}) => {
|
||||
mockUseAuthorized.mockReturnValue(auth);
|
||||
mockOrganizationListCall.mockImplementation(fakeOrganizationList(organizations));
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } },
|
||||
});
|
||||
|
|
@ -168,3 +224,45 @@ describe("OrganizationsPanel - org detail deep link (?org=)", () => {
|
|||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("OrganizationsPanel - search by organization name or ID", () => {
|
||||
const renderSignedInPanel = async (organizations: readonly ServerOrganization[] = SERVER_ORGANIZATIONS) => {
|
||||
renderPanel({ auth: SIGNED_IN, organizations });
|
||||
await waitFor(() => expect(capturedTableProps?.organizations).toHaveLength(organizations.length));
|
||||
};
|
||||
|
||||
const search = (value: string) =>
|
||||
fireEvent.change(screen.getByPlaceholderText("Search by organization name or ID"), { target: { value } });
|
||||
|
||||
const visibleOrganizationIds = () =>
|
||||
capturedTableProps?.organizations.map((organization) => organization.organization_id);
|
||||
|
||||
it.each([
|
||||
["a full organization_id", ALPHA_ORG_ID, ALPHA_ORG_ID],
|
||||
["a substring of an organization_id", "5337fb10", ALPHA_ORG_ID],
|
||||
["an alias substring, ignoring case", "BETA", BETA_ORG_ID],
|
||||
])("keeps only the organization matching %s and drops the others", async (_label, query, expectedId) => {
|
||||
await renderSignedInPanel();
|
||||
|
||||
search(query);
|
||||
|
||||
await waitFor(() => expect(visibleOrganizationIds()).toEqual([expectedId]));
|
||||
});
|
||||
|
||||
it("shows the search empty state when neither an alias nor an id matches", async () => {
|
||||
await renderSignedInPanel();
|
||||
|
||||
search("no-such-org");
|
||||
|
||||
await waitFor(() => expect(capturedTableProps?.organizations).toEqual([]));
|
||||
expect(capturedTableProps?.searchActive).toBe(true);
|
||||
});
|
||||
|
||||
it("still finds a row by id when the server hands back a null alias", async () => {
|
||||
await renderSignedInPanel([...SERVER_ORGANIZATIONS, makeOrganization(NO_ALIAS_ORG_ID, null)]);
|
||||
|
||||
search(NO_ALIAS_ORG_ID);
|
||||
|
||||
await waitFor(() => expect(visibleOrganizationIds()).toEqual([NO_ALIAS_ORG_ID]));
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@ import { useUserModels } from "@/app/(dashboard)/hooks/models/useModels";
|
|||
import OrganizationFilters, { FilterState } from "@/app/(dashboard)/organizations/OrganizationFilters";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import { parseAsString, useQueryState } from "nuqs";
|
||||
import React, { useState } from "react";
|
||||
import React, { useMemo, useState } from "react";
|
||||
import DeleteResourceModal from "@/components/common_components/DeleteResourceModal";
|
||||
import { toast } from "@/lib/toast";
|
||||
import { organizationDeleteCall } from "@/components/networking";
|
||||
import { Organization, organizationDeleteCall } from "@/components/networking";
|
||||
import { OrgCreateDialog } from "@/components/organization/org-create/OrgCreateDialog";
|
||||
import OrganizationInfoView from "@/components/organization/organization_view";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
|
@ -19,6 +19,14 @@ interface OrganizationsPanelProps {
|
|||
premiumUser: boolean;
|
||||
}
|
||||
|
||||
const matchesOrganizationSearch = (organization: Organization, search: string): boolean => {
|
||||
const needle = search.trim().toLowerCase();
|
||||
if (needle === "") return true;
|
||||
return [organization.organization_alias, organization.organization_id].some((field) =>
|
||||
field?.toLowerCase().includes(needle),
|
||||
);
|
||||
};
|
||||
|
||||
const OrganizationsPanel: React.FC<OrganizationsPanelProps> = ({ userRole, accessToken, premiumUser }) => {
|
||||
const [selectedOrgId, setSelectedOrgId] = useQueryState("org", parseAsString.withOptions({ history: "push" }));
|
||||
const [editOrg, setEditOrg] = useState(false);
|
||||
|
|
@ -27,16 +35,18 @@ const OrganizationsPanel: React.FC<OrganizationsPanelProps> = ({ userRole, acces
|
|||
const [isDeleting, setIsDeleting] = useState(false);
|
||||
const [isOrgModalVisible, setIsOrgModalVisible] = useState(false);
|
||||
const [showFilters, setShowFilters] = useState(false);
|
||||
const [filters, setFilters] = useState<FilterState>({ org_id: "", org_alias: "" });
|
||||
const [filters, setFilters] = useState<FilterState>({ org_id: "", search: "" });
|
||||
|
||||
const queryClient = useQueryClient();
|
||||
const { data: organizations = [], isLoading } = useOrganizations({
|
||||
org_id: filters.org_id,
|
||||
org_alias: filters.org_alias,
|
||||
});
|
||||
const { data: organizations = [], isLoading } = useOrganizations({ org_id: filters.org_id });
|
||||
const { data: userModels = [] } = useUserModels();
|
||||
|
||||
const searchActive = Boolean(filters.org_id || filters.org_alias);
|
||||
const visibleOrganizations = useMemo(
|
||||
() => organizations.filter((organization) => matchesOrganizationSearch(organization, filters.search)),
|
||||
[organizations, filters.search],
|
||||
);
|
||||
|
||||
const searchActive = Boolean(filters.org_id || filters.search);
|
||||
|
||||
const refetchOrganizations = () => queryClient.invalidateQueries({ queryKey: organizationKeys.lists() });
|
||||
|
||||
|
|
@ -45,7 +55,7 @@ const OrganizationsPanel: React.FC<OrganizationsPanelProps> = ({ userRole, acces
|
|||
};
|
||||
|
||||
const handleFilterReset = () => {
|
||||
setFilters({ org_id: "", org_alias: "" });
|
||||
setFilters({ org_id: "", search: "" });
|
||||
};
|
||||
|
||||
const handleDelete = (orgId: string | null) => {
|
||||
|
|
@ -129,7 +139,7 @@ const OrganizationsPanel: React.FC<OrganizationsPanelProps> = ({ userRole, acces
|
|||
onReset={handleFilterReset}
|
||||
/>
|
||||
<OrganizationsTable
|
||||
organizations={organizations}
|
||||
organizations={visibleOrganizations}
|
||||
isLoading={isLoading}
|
||||
userRole={userRole}
|
||||
searchActive={searchActive}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue