mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
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
This commit is contained in:
parent
c6a9878ce0
commit
690065716f
5 changed files with 161 additions and 10 deletions
|
|
@ -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", () => ({
|
|||
<button type="button" onClick={onBack}>
|
||||
Back to policies
|
||||
</button>
|
||||
<button type="button" onClick={onSuccess}>
|
||||
Save policy
|
||||
</button>
|
||||
<button type="button" onClick={() => onVersionCreated?.(draftVersion)}>
|
||||
Create draft version
|
||||
</button>
|
||||
|
|
@ -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(<PoliciesPanel accessToken="test-token" userRole="Admin" />, {
|
||||
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<OnUrlUpdateFunction>();
|
||||
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<OnUrlUpdateFunction>();
|
||||
renderWithProviders(<PoliciesPanel accessToken="test-token" userRole="Admin" />, {
|
||||
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<OnUrlUpdateFunction>();
|
||||
|
|
@ -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<OnUrlUpdateFunction>();
|
||||
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<OnUrlUpdateFunction>();
|
||||
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<OnUrlUpdateFunction>();
|
||||
renderWithProviders(<PoliciesPanel accessToken="test-token" userRole="Admin" />, {
|
||||
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<OnUrlUpdateFunction>();
|
||||
|
|
|
|||
|
|
@ -172,6 +172,7 @@ const PoliciesPanel: React.FC<PoliciesPanelProps> = ({ accessToken, userRole })
|
|||
const {
|
||||
selectedPolicyId,
|
||||
selectPolicy,
|
||||
editPolicyId,
|
||||
editingPolicy,
|
||||
isEditingPolicyPending,
|
||||
openPolicyEditor,
|
||||
|
|
@ -179,6 +180,13 @@ const PoliciesPanel: React.FC<PoliciesPanelProps> = ({ 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);
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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(<TagInfoView tagId="deleted-tag" onClose={onClose} accessToken="sk-test" is_admin />, {
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -150,8 +150,10 @@ interface TagInfoViewProps {
|
|||
is_admin: boolean;
|
||||
}
|
||||
|
||||
type TagDetailsState = { status: "loading" } | { status: "loaded"; tag: Tag } | { status: "not_found" };
|
||||
|
||||
const TagInfoView: React.FC<TagInfoViewProps> = ({ tagId, onClose, accessToken, is_admin }) => {
|
||||
const [tagDetails, setTagDetails] = useState<Tag | null>(null);
|
||||
const [details, setDetails] = useState<TagDetailsState>({ 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<TagInfoViewProps> = ({ 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<TagInfoViewProps> = ({ tagId, onClose, accessToken,
|
|||
}
|
||||
};
|
||||
|
||||
if (!tagDetails) {
|
||||
if (details.status === "loading") {
|
||||
return <div>Loading...</div>;
|
||||
}
|
||||
|
||||
const backButton = (
|
||||
<Button onClick={onClose} className="mb-4">
|
||||
← Back to Tags
|
||||
</Button>
|
||||
);
|
||||
|
||||
if (details.status === "not_found") {
|
||||
return (
|
||||
<div className="p-4">
|
||||
{backButton}
|
||||
<p className="text-sm text-destructive">Tag not found</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const tagDetails = details.tag;
|
||||
|
||||
return (
|
||||
<div className="p-4">
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
<div>
|
||||
<Button onClick={onClose} className="mb-4">
|
||||
← Back to Tags
|
||||
</Button>
|
||||
{backButton}
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-sm font-medium">Tag Name:</span>
|
||||
<span className="font-mono px-2 py-1 bg-muted rounded-sm text-sm border border-border">
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue