The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
import * as networking from "@/components/networking";
import { fireEvent, render, waitFor, within, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest";
import GuardrailInfoView from "./guardrail_info";
// Mock the networking module
vi.mock("@/components/networking", () => ({
getGuardrailInfo: vi.fn(),
getGuardrailUISettings: vi.fn(),
getGuardrailProviderSpecificParams: vi.fn(),
updateGuardrailCall: vi.fn(),
deleteGuardrailCall: vi.fn(),
}));
// Mock ContentFilterManager
vi.mock("./content_filter/ContentFilterManager", () => ({
__esModule: true,
default: ({ onUnsavedChanges, onDataChange, isEditing }: any) => (
<div data-testid="mock-content-filter-manager">
{isEditing && (
<button
onClick={() => {
onUnsavedChanges(true);
onDataChange?.(["new_pattern"], ["new_word"], []);
}}
>
Simulate Change
</button>
)}
</div>
),
formatContentFilterDataForAPI: (patterns: any[], blockedWords: any[], categories?: any[]) => ({
patterns,
blocked_words: blockedWords,
categories: categories ?? [],
}),
}));
describe("Guardrail Info", () => {
afterEach(() => {
vi.clearAllMocks();
});
it("should render the guardrail info after loading", async () => {
// Mock the network responses
vi.mocked(networking.getGuardrailInfo).mockResolvedValue({
guardrail_id: "123",
guardrail_name: "Test Guardrail",
litellm_params: {
guardrail: "presidio",
mode: "pre_call",
default_on: true,
},
created_at: "2024-01-01T00:00:00Z",
updated_at: "2024-01-01T00:00:00Z",
guardrail_definition_location: "database",
});
vi.mocked(networking.getGuardrailUISettings).mockResolvedValue({
supported_entities: ["PERSON", "EMAIL"],
supported_actions: ["MASK", "REDACT"],
pii_entity_categories: [],
supported_modes: ["pre_call", "post_call"],
});
vi.mocked(networking.getGuardrailProviderSpecificParams).mockResolvedValue({});
render(
<GuardrailInfoView guardrailId="123" onClose={() => {}} onDeleted={() => {}} accessToken="123" isAdmin={true} />,
);
// Wait for the loading to complete and data to be rendered
await waitFor(() => {
// The guardrail name appears in multiple places (title and settings tab)
const elements = screen.getAllByText("Test Guardrail");
expect(elements.length).toBeGreaterThan(0);
});
// Verify other key elements are present
expect(screen.getByText("Back to Guardrails")).toBeInTheDocument();
expect(screen.getByText("Overview")).toBeInTheDocument();
expect(screen.getByText("Settings")).toBeInTheDocument();
});
it("should render a tag-based mode object rather than crashing the detail view", async () => {
vi.mocked(networking.getGuardrailInfo).mockResolvedValue({
guardrail_id: "123",
guardrail_name: "Test Guardrail",
litellm_params: {
guardrail: "bedrock",
mode: { tags: { "Service-Type: internal-service": "post_call" }, default: ["pre_call", "post_call"] },
default_on: true,
},
created_at: "2024-01-01T00:00:00Z",
updated_at: "2024-01-01T00:00:00Z",
guardrail_definition_location: "database",
});
vi.mocked(networking.getGuardrailUISettings).mockResolvedValue({
supported_entities: [],
supported_actions: [],
pii_entity_categories: [],
supported_modes: ["pre_call", "post_call"],
});
vi.mocked(networking.getGuardrailProviderSpecificParams).mockResolvedValue({});
render(
<GuardrailInfoView guardrailId="123" onClose={() => {}} onDeleted={() => {}} accessToken="123" isAdmin={true} />,
);
expect(await screen.findAllByText("pre_call, post_call (tag-based)")).not.toHaveLength(0);
});
it("should render the provider logo from the bundled guardrail logo map", async () => {
vi.mocked(networking.getGuardrailInfo).mockResolvedValue({
guardrail_id: "123",
guardrail_name: "Test Guardrail",
litellm_params: {
guardrail: "presidio",
mode: "pre_call",
default_on: true,
},
created_at: "2024-01-01T00:00:00Z",
updated_at: "2024-01-01T00:00:00Z",
guardrail_definition_location: "database",
});
vi.mocked(networking.getGuardrailUISettings).mockResolvedValue({
supported_entities: [],
supported_actions: [],
pii_entity_categories: [],
supported_modes: ["pre_call", "post_call"],
});
vi.mocked(networking.getGuardrailProviderSpecificParams).mockResolvedValue({});
render(
<GuardrailInfoView guardrailId="123" onClose={() => {}} onDeleted={() => {}} accessToken="123" isAdmin={true} />,
);
const logo = await screen.findByAltText("Presidio PII logo");
expect(logo).toHaveAttribute("src", expect.stringContaining("microsoft_azure.svg"));
});
it("should not render the edit button for config guardrails", async () => {
// Mock the network responses
vi.mocked(networking.getGuardrailInfo).mockResolvedValue({
guardrail_id: "123",
guardrail_name: "Test Guardrail",
litellm_params: {
guardrail: "presidio",
mode: "pre_call",
default_on: true,
},
created_at: "2024-01-01T00:00:00Z",
updated_at: "2024-01-01T00:00:00Z",
guardrail_definition_location: "config",
});
vi.mocked(networking.getGuardrailUISettings).mockResolvedValue({
supported_entities: ["PERSON", "EMAIL"],
supported_actions: ["MASK", "REDACT"],
pii_entity_categories: [],
supported_modes: ["pre_call", "post_call"],
});
vi.mocked(networking.getGuardrailProviderSpecificParams).mockResolvedValue({});
const { container } = render(
<GuardrailInfoView guardrailId="123" onClose={() => {}} onDeleted={() => {}} accessToken="123" isAdmin={true} />,
);
await waitFor(() => {
expect(screen.getByText("Settings")).toBeInTheDocument();
});
// Click the Settings tab
fireEvent.click(screen.getByText("Settings"));
// Wait for the Settings panel to render
await waitFor(() => {
expect(screen.getByText("Guardrail Settings")).toBeInTheDocument();
});
await userEvent.hover(within(container).getByRole("img", { name: "Config guardrail details" }));
expect(
await screen.findByText("Guardrail is defined in the config file and cannot be edited."),
).toBeInTheDocument();
});
it("should render the guardrail info", async () => {
// Mock the network responses
vi.mocked(networking.getGuardrailInfo).mockResolvedValue({
guardrail_id: "123",
guardrail_name: "Test Guardrail",
litellm_params: {
guardrail: "presidio",
mode: "pre_call",
default_on: true,
pii_entities_config: {
PERSON: "MASK",
EMAIL: "REDACT",
},
},
created_at: "2024-01-01T00:00:00Z",
updated_at: "2024-01-01T00:00:00Z",
guardrail_definition_location: "database",
});
vi.mocked(networking.getGuardrailUISettings).mockResolvedValue({
supported_entities: ["PERSON", "EMAIL"],
supported_actions: ["MASK", "REDACT"],
pii_entity_categories: [],
supported_modes: ["pre_call", "post_call"],
});
vi.mocked(networking.getGuardrailProviderSpecificParams).mockResolvedValue({});
render(
<GuardrailInfoView guardrailId="123" onClose={() => {}} onDeleted={() => {}} accessToken="123" isAdmin={true} />,
);
await waitFor(() => {
expect(screen.getByText("PII Entity Configuration")).toBeInTheDocument();
});
});
it("should handle content filter updates correctly", async () => {
// Mock the network responses
vi.mocked(networking.getGuardrailInfo).mockResolvedValue({
guardrail_id: "123",
guardrail_name: "Content Filter Guardrail",
litellm_params: {
guardrail: "litellm_content_filter",
mode: "pre_call",
default_on: true,
patterns: ["initial_pattern"],
blocked_words: ["initial_word"],
},
created_at: "2024-01-01T00:00:00Z",
updated_at: "2024-01-01T00:00:00Z",
guardrail_definition_location: "database",
});
vi.mocked(networking.getGuardrailUISettings).mockResolvedValue({
supported_entities: [],
supported_actions: [],
pii_entity_categories: [],
supported_modes: ["pre_call", "post_call"],
});
vi.mocked(networking.getGuardrailProviderSpecificParams).mockResolvedValue({});
vi.mocked(networking.updateGuardrailCall).mockResolvedValue({ status: "success" });
render(
<GuardrailInfoView guardrailId="123" onClose={() => {}} onDeleted={() => {}} accessToken="123" isAdmin={true} />,
);
await waitFor(() => {
expect(screen.getByText("Settings")).toBeInTheDocument();
});
// Go to Settings tab
fireEvent.click(screen.getByText("Settings"));
await waitFor(() => {
expect(screen.getByText("Guardrail Settings")).toBeInTheDocument();
});
// Enter Edit Mode
fireEvent.click(screen.getByText("Edit Settings"));
// Modify Guardrail Name to force an update
const nameInput = screen.getByLabelText("Guardrail Name");
fireEvent.change(nameInput, { target: { value: "Updated Name" } });
// Save with only name change
const saveButton = screen.getByText("Save Changes");
fireEvent.click(saveButton);
await waitFor(() => {
expect(networking.updateGuardrailCall).toHaveBeenCalled();
});
// Verify call did NOT include patterns or blocked_words (because no changes)
// updateGuardrailCall(accessToken, guardrailId, updateData) -> index 2 is updateData
const firstCallArgs: any = vi.mocked(networking.updateGuardrailCall).mock.calls[0][2];
// Verify attributes that definitely changed
expect(firstCallArgs.guardrail_name).toBe("Updated Name");
// litellm_params might be undefined if empty, which is correct.
// If it exists, ensure patterns/blocked_words are not in it.
if (firstCallArgs.litellm_params) {
expect(firstCallArgs.litellm_params.patterns).toBeUndefined();
expect(firstCallArgs.litellm_params.blocked_words).toBeUndefined();
}
// Clear mocks to reset call count
vi.clearAllMocks();
// Enter Edit Mode again to make changes
await waitFor(() => {
expect(screen.getByText("Edit Settings")).toBeInTheDocument();
});
fireEvent.click(screen.getByText("Edit Settings"));
// Now modify the values using the mock button
const simulateChangeButton = screen.getByText("Simulate Change");
fireEvent.click(simulateChangeButton);
// Save again
fireEvent.click(screen.getByText("Save Changes"));
await waitFor(() => {
expect(networking.updateGuardrailCall).toHaveBeenCalled();
});
// Verify call INCLUDES patterns and blocked_words
const secondCallArgs: any = vi.mocked(networking.updateGuardrailCall).mock.calls[0][2];
expect(secondCallArgs.litellm_params).toBeDefined();
expect(secondCallArgs.litellm_params.patterns).toEqual(["new_pattern"]);
expect(secondCallArgs.litellm_params.blocked_words).toEqual(["new_word"]);
});
it("keeps the settings panel mounted while the overview tab is active", async () => {
vi.mocked(networking.getGuardrailInfo).mockResolvedValue({
guardrail_id: "123",
guardrail_name: "Test Guardrail",
litellm_params: { guardrail: "presidio", mode: "pre_call", default_on: true },
created_at: "2024-01-01T00:00:00Z",
updated_at: "2024-01-01T00:00:00Z",
guardrail_definition_location: "database",
});
vi.mocked(networking.getGuardrailUISettings).mockResolvedValue({
supported_entities: [],
supported_actions: [],
pii_entity_categories: [],
supported_modes: ["pre_call"],
});
vi.mocked(networking.getGuardrailProviderSpecificParams).mockResolvedValue({});
render(
<GuardrailInfoView guardrailId="123" onClose={() => {}} onDeleted={() => {}} accessToken="123" isAdmin={true} />,
);
expect(await screen.findByRole("tab", { name: "Overview" })).toHaveAttribute("aria-selected", "true");
expect(screen.getByRole("tab", { name: "Settings" })).toHaveAttribute("aria-selected", "false");
expect(screen.getByText("Guardrail Settings")).toBeInTheDocument();
});
});
describe("Guardrail Info when the guardrail cannot be loaded", () => {
afterEach(() => {
vi.clearAllMocks();
});
it("should keep Back to Guardrails reachable so a stale ?guardrail= link is not a dead end", async () => {
vi.mocked(networking.getGuardrailInfo).mockRejectedValue(new Error("Guardrail stale-id not found"));
vi.mocked(networking.getGuardrailUISettings).mockResolvedValue({
supported_entities: [],
supported_actions: [],
pii_entity_categories: [],
supported_modes: [],
});
vi.mocked(networking.getGuardrailProviderSpecificParams).mockResolvedValue({});
const onClose = vi.fn();
render(
<GuardrailInfoView
guardrailId="stale-id"
onClose={onClose}
onDeleted={() => {}}
accessToken="123"
isAdmin={true}
/>,
);
expect(await screen.findByText("Guardrail not found")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: /back to guardrails/i }));
expect(onClose).toHaveBeenCalledTimes(1);
});
describe("Delete from the Settings card", () => {
const dbGuardrail = {
guardrail_id: "123",
guardrail_name: "Test Guardrail",
litellm_params: { guardrail: "presidio", mode: "pre_call", default_on: true },
guardrail_definition_location: "db",
};
const uiSettings = {
supported_entities: [],
supported_actions: [],
pii_entity_categories: [],
supported_modes: ["pre_call"],
};
const mockDbGuardrail = () => {
vi.mocked(networking.getGuardrailInfo).mockResolvedValue(dbGuardrail);
vi.mocked(networking.getGuardrailUISettings).mockResolvedValue(uiSettings);
vi.mocked(networking.getGuardrailProviderSpecificParams).mockResolvedValue({});
};
const openDeleteModalAndConfirm = async () => {
fireEvent.click(await screen.findByRole("tab", { name: "Settings" }));
fireEvent.click(await screen.findByRole("button", { name: "Delete" }));
const modal = within(await screen.findByRole("dialog"));
expect(modal.getByText(/silently stop running it/)).toBeInTheDocument();
const confirmButton = modal.getByRole("button", { name: "Delete" });
expect(confirmButton).toBeDisabled();
fireEvent.change(modal.getByPlaceholderText("Test Guardrail"), { target: { value: "Test Guardrail" } });
fireEvent.click(confirmButton);
};
it("deletes the guardrail and calls onDeleted once the admin types its name", async () => {
mockDbGuardrail();
vi.mocked(networking.deleteGuardrailCall).mockResolvedValue({});
const onDeleted = vi.fn();
render(
<GuardrailInfoView guardrailId="123" onClose={() => {}} onDeleted={onDeleted} accessToken="tok" isAdmin />,
);
await openDeleteModalAndConfirm();
await waitFor(() => expect(onDeleted).toHaveBeenCalledTimes(1));
expect(networking.deleteGuardrailCall).toHaveBeenCalledWith("tok", "123");
});
it("hides Delete while the settings are being edited", async () => {
mockDbGuardrail();
render(<GuardrailInfoView guardrailId="123" onClose={() => {}} onDeleted={() => {}} accessToken="tok" isAdmin />);
fireEvent.click(await screen.findByRole("tab", { name: "Settings" }));
fireEvent.click(await screen.findByRole("button", { name: "Edit Settings" }));
expect(await screen.findByRole("button", { name: "Save Changes" })).toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Delete" })).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Edit Settings" })).not.toBeInTheDocument();
});
it("does not call onDeleted when the delete request fails", async () => {
mockDbGuardrail();
vi.mocked(networking.deleteGuardrailCall).mockRejectedValue(new Error("boom"));
const onDeleted = vi.fn();
render(
<GuardrailInfoView guardrailId="123" onClose={() => {}} onDeleted={onDeleted} accessToken="tok" isAdmin />,
);
await openDeleteModalAndConfirm();
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
expect(networking.deleteGuardrailCall).toHaveBeenCalledTimes(1);
expect(onDeleted).not.toHaveBeenCalled();
});
});
});