From 690065716ff4cb9b18ea98e37b9d0ef2de309c86 Mon Sep 17 00:00:00 2001 From: ryan-crabbe-berri Date: Wed, 16 Sep 2026 10:53:16 -0700 Subject: [PATCH] fix(ui): address review on policies-tags url state Show a not-found state with a Back button for an unknown ?tag= instead of loading forever, never open a config-defined policy from ?edit_policy=, and select the Policies tab when a ?policy= or ?edit_policy= link has no ?tab=. Tighten the ?policy= test and pin the builder staying on a policy after an update --- .../policies/_components/index.test.tsx | 83 ++++++++++++++++++- .../policies/_components/index.tsx | 8 ++ .../policies/_components/usePolicyUrlState.ts | 3 +- .../_components/tag_info.integration.test.tsx | 45 ++++++++++ .../tag-management/_components/tag_info.tsx | 32 +++++-- 5 files changed, 161 insertions(+), 10 deletions(-) diff --git a/ui/litellm-dashboard/src/app/(dashboard)/policies/_components/index.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/policies/_components/index.test.tsx index c775878fa11..bd6fe9bed08 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/policies/_components/index.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/policies/_components/index.test.tsx @@ -65,12 +65,14 @@ vi.mock("./policy_templates", () => ({ vi.mock("./pipeline_flow_builder", () => ({ FlowBuilderPage: ({ onBack, + onSuccess, editingPolicy, onVersionCreated, onSelectVersion, onVersionStatusUpdated, }: { onBack: () => void; + onSuccess: () => void; editingPolicy?: Policy | null; onVersionCreated?: (policy: Policy) => void; onSelectVersion?: (policy: Policy) => void; @@ -95,6 +97,9 @@ vi.mock("./pipeline_flow_builder", () => ({ + @@ -364,15 +369,48 @@ describe("PoliciesPanel ?policy= detail", () => { networkingMocks.getPoliciesList.mockResolvedValue({ policies: [] }); }); - it("opens the policy named in ?policy= instead of the table", () => { + it("opens the policy named in ?policy= instead of the table", async () => { renderWithProviders(, { searchParams: `?tab=policies&policy=${POLICY_ID}`, }); expect(screen.getByTestId("policy-info")).toHaveTextContent(POLICY_ID); + expect(screen.queryByRole("columnheader", { name: /^Name/ })).not.toBeInTheDocument(); + await waitFor(() => expect(networkingMocks.getPoliciesList).toHaveBeenCalled()); expect(screen.queryByRole("button", { name: "pii-policy" })).not.toBeInTheDocument(); }); + it("selects the Policies tab for a ?policy= link that has no ?tab=, and still lets the user leave it", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderPanelKeepingMountUpdates(`?policy=${POLICY_ID}`, onUrlUpdate); + + expect(screen.getByTestId("policy-info")).toHaveTextContent(POLICY_ID); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("tab")).toBe("policies")); + expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("policy")).toBe(POLICY_ID); + expect(screen.getByRole("tab", { name: "Policies" })).toHaveAttribute("aria-selected", "true"); + + await user.click(screen.getByRole("tab", { name: "Templates" })); + + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.has("tab")).toBe(false)); + expect(screen.getByRole("tab", { name: "Templates" })).toHaveAttribute("aria-selected", "true"); + }); + + it("closes the open policy and removes ?policy= when Add New Policy is clicked", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { + searchParams: `?tab=policies&policy=${POLICY_ID}`, + onUrlUpdate, + }); + + await user.click(screen.getByRole("button", { name: "+ Add New Policy" })); + + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.has("policy")).toBe(false)); + expect(screen.queryByTestId("policy-info")).not.toBeInTheDocument(); + expect(screen.getByTestId("add-policy-prefill")).toHaveTextContent("empty"); + }); + it("pushes ?policy= when a policy name is clicked", async () => { const user = userEvent.setup(); const onUrlUpdate = vi.fn(); @@ -489,6 +527,49 @@ describe("PoliciesPanel flow builder", () => { expect(lastUrlUpdate(onUrlUpdate)?.options.history).toBe("replace"); }); + it("does not open a config-defined policy named in ?edit_policy=", async () => { + const configPolicy: Policy = { ...PII_POLICY, policy_id: "config-pii", definition_location: "config" }; + networkingMocks.getPoliciesList.mockResolvedValue({ policies: [configPolicy] }); + const onUrlUpdate = vi.fn(); + renderPanelKeepingMountUpdates("?tab=policies&edit_policy=config-pii", onUrlUpdate); + + expect(await screen.findByRole("tab", { name: "Policies" })).toHaveAttribute("aria-selected", "true"); + expect(screen.queryByTestId("flow-builder-target")).not.toBeInTheDocument(); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.has("edit_policy")).toBe(false)); + expect(lastUrlUpdate(onUrlUpdate)?.options.history).toBe("replace"); + }); + + it("returns to the Policies tab after closing a builder opened from an ?edit_policy= link with no ?tab=", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderPanelKeepingMountUpdates(`?edit_policy=${POLICY_ID}`, onUrlUpdate); + + expect(await screen.findByTestId("flow-builder-target")).toHaveTextContent(POLICY_ID); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("tab")).toBe("policies")); + + await user.click(screen.getByRole("button", { name: "Back to policies" })); + + expect(await screen.findByRole("tab", { name: "Policies" })).toHaveAttribute("aria-selected", "true"); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.has("edit_policy")).toBe(false)); + expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("tab")).toBe("policies"); + }); + + it("stays on the saved policy after an update instead of turning into a create form", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { + searchParams: `?tab=policies&edit_policy=${POLICY_ID}`, + onUrlUpdate, + }); + await screen.findByTestId("flow-builder-target"); + + await user.click(screen.getByRole("button", { name: "Save policy" })); + + await waitFor(() => expect(networkingMocks.getPoliciesList).toHaveBeenCalledTimes(2)); + expect(screen.getByTestId("flow-builder-target")).toHaveTextContent(`${POLICY_ID} production`); + expect(onUrlUpdate).not.toHaveBeenCalled(); + }); + it("keeps a freshly created version open before the refreshed list includes it", async () => { const user = userEvent.setup(); const onUrlUpdate = vi.fn(); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/policies/_components/index.tsx b/ui/litellm-dashboard/src/app/(dashboard)/policies/_components/index.tsx index 66a110d9384..cecb780abec 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/policies/_components/index.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/policies/_components/index.tsx @@ -172,6 +172,7 @@ const PoliciesPanel: React.FC = ({ accessToken, userRole }) const { selectedPolicyId, selectPolicy, + editPolicyId, editingPolicy, isEditingPolicyPending, openPolicyEditor, @@ -179,6 +180,13 @@ const PoliciesPanel: React.FC = ({ accessToken, userRole }) closePolicyEditor, } = usePolicyUrlState({ policies: policiesList, hasFetched: hasFetchedPolicies, isLoading }); + const [deepLinkNeedsPoliciesTab] = useState( + () => activeTab === "templates" && (selectedPolicyId !== null || editPolicyId !== null), + ); + useEffect(() => { + if (deepLinkNeedsPoliciesTab) setActiveTab("policies"); + }, [deepLinkNeedsPoliciesTab, setActiveTab]); + const editPolicy = (policy: Policy) => { setIsNewPolicyFlowOpen(false); openPolicyEditor(policy); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/policies/_components/usePolicyUrlState.ts b/ui/litellm-dashboard/src/app/(dashboard)/policies/_components/usePolicyUrlState.ts index 2a496bafe5c..5fde0289b1e 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/policies/_components/usePolicyUrlState.ts +++ b/ui/litellm-dashboard/src/app/(dashboard)/policies/_components/usePolicyUrlState.ts @@ -19,7 +19,7 @@ function resolveEditingPolicy( policies: readonly Policy[], ): Policy | null { if (policyId === null) return null; - const listed = policies.find((policy) => policy.policy_id === policyId); + const listed = policies.find((policy) => policy.policy_id === policyId && policy.definition_location !== "config"); if (handedBack?.policy.policy_id !== policyId) return listed ?? null; const listCaughtUp = listed !== undefined && handedBack.listWhenHandedBack !== policies; return listCaughtUp ? listed : handedBack.policy; @@ -62,6 +62,7 @@ export function usePolicyUrlState({ policies, hasFetched, isLoading }: PolicyLis return { selectedPolicyId, selectPolicy, + editPolicyId, editingPolicy, isEditingPolicyPending, openPolicyEditor, diff --git a/ui/litellm-dashboard/src/app/(dashboard)/tag-management/_components/tag_info.integration.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/tag-management/_components/tag_info.integration.test.tsx index 69588ca007a..511b773fe1c 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/tag-management/_components/tag_info.integration.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/tag-management/_components/tag_info.integration.test.tsx @@ -167,6 +167,51 @@ describe("TagInfoView save payload", () => { }); }); +describe("TagInfoView unknown ?tag=", () => { + beforeEach(() => { + vi.clearAllMocks(); + mockTagUpdateCall.mockResolvedValue(undefined); + }); + + it("shows a not-found message with a working Back button when the tag does not exist", async () => { + const user = userEvent.setup(); + const onClose = vi.fn(); + mockTagInfoCall.mockResolvedValue({}); + renderWithProviders(, { + searchParams: "?tag=deleted-tag", + }); + + expect(await screen.findByText("Tag not found")).toBeInTheDocument(); + expect(screen.queryByText("Loading...")).not.toBeInTheDocument(); + + await user.click(screen.getByRole("button", { name: /Back to Tags/ })); + + expect(onClose).toHaveBeenCalledTimes(1); + }); + + it("shows the not-found message when the first lookup fails", async () => { + mockTagInfoCall.mockRejectedValue(new Error("network down")); + renderTagInfo("?tag=prod-tag"); + + expect(await screen.findByText("Tag not found")).toBeInTheDocument(); + expect(screen.getByRole("button", { name: /Back to Tags/ })).toBeInTheDocument(); + }); + + it("keeps showing the loaded tag when a refresh after saving fails", async () => { + const consoleError = vi.spyOn(console, "error").mockImplementation(() => {}); + const refreshError = new Error("network down"); + mockTagInfoCall.mockResolvedValueOnce({ "prod-tag": tag }).mockRejectedValueOnce(refreshError); + const { user } = await renderEditor(); + + await user.click(screen.getByRole("button", { name: "Save Changes" })); + + await waitFor(() => expect(consoleError).toHaveBeenCalledWith("Error fetching tag details:", refreshError)); + expect(await screen.findByText("Tag Details")).toBeInTheDocument(); + expect(screen.queryByText("Tag not found")).not.toBeInTheDocument(); + consoleError.mockRestore(); + }); +}); + describe("TagInfoView ?edit= mode", () => { beforeEach(() => { vi.clearAllMocks(); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/tag-management/_components/tag_info.tsx b/ui/litellm-dashboard/src/app/(dashboard)/tag-management/_components/tag_info.tsx index eb179225ac4..df388819fbe 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/tag-management/_components/tag_info.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/tag-management/_components/tag_info.tsx @@ -150,8 +150,10 @@ interface TagInfoViewProps { is_admin: boolean; } +type TagDetailsState = { status: "loading" } | { status: "loaded"; tag: Tag } | { status: "not_found" }; + const TagInfoView: React.FC = ({ tagId, onClose, accessToken, is_admin }) => { - const [tagDetails, setTagDetails] = useState(null); + const [details, setDetails] = useState({ status: "loading" }); const [{ edit: isEditing }, setTagUrl] = useTagUrlState(); const [openedInEditMode] = useState(isEditing); const setIsEditing = (edit: boolean) => void setTagUrl({ edit }); @@ -173,12 +175,11 @@ const TagInfoView: React.FC = ({ tagId, onClose, accessToken, try { const response = await tagInfoCall(accessToken, [tagId]); const tagData = response[tagId]; - if (tagData) { - setTagDetails(tagData); - } + setDetails(tagData ? { status: "loaded", tag: tagData } : { status: "not_found" }); } catch (error) { console.error("Error fetching tag details:", error); toast.fromError("Error fetching tag details: " + error); + setDetails((current) => (current.status === "loaded" ? current : { status: "not_found" })); } }; @@ -215,17 +216,32 @@ const TagInfoView: React.FC = ({ tagId, onClose, accessToken, } }; - if (!tagDetails) { + if (details.status === "loading") { return
Loading...
; } + const backButton = ( + + ); + + if (details.status === "not_found") { + return ( +
+ {backButton} +

Tag not found

+
+ ); + } + + const tagDetails = details.tag; + return (
- + {backButton}
Tag Name: