litellm/ui/litellm-dashboard/src/contexts/PluginModeContext.test.tsx
Yuneng Jiang 6670061a33 test(ui): assert presence and state so dashboard tests fail when behaviour breaks
One test could not fail: it asserted on the Promise returned by an unawaited
findAllByText, and a pending Promise is always truthy, so the organization
detail view had no working coverage at all.

Rewrites 316 assertions across 128 files onto matchers that describe what the
user perceives, and awaits the queries that were being compared as Promises.

Most of this was mechanical, but the fixers behind these rules are not
trustworthy, so every site they damaged was repaired by hand. The quiet one
worth naming: prefer-to-have-text-content wraps strings in new RegExp() without
escaping, turning toContain("100K+ requests") into a pattern meaning "100
followed by one-or-more K". That compiles, lints clean, and keeps passing while
no longer asserting what it claims.
2026-08-17 14:46:55 -07:00

64 lines
2.2 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import { PluginModeProvider, usePluginMode } from "./PluginModeContext";
import type { Plugin } from "./PluginModeContext";
const { getMock } = vi.hoisted(() => ({ getMock: vi.fn() }));
vi.mock("@/lib/http/client", () => ({
createApiClient: () => ({ get: getMock }),
}));
vi.mock("@/components/networking", () => ({ getProxyBaseUrl: () => "" }));
function ModeProbe() {
const { mode, activePlugin } = usePluginMode();
return (
<div>
<span data-testid="mode">{mode}</span>
<span data-testid="active">{activePlugin?.name ?? "none"}</span>
</div>
);
}
const renderWithPlugins = (plugins: Plugin[]) => {
getMock.mockResolvedValueOnce(plugins);
return render(
<PluginModeProvider accessToken="sk-test">
<ModeProbe />
</PluginModeProvider>,
);
};
describe("PluginModeProvider effectiveMode fallback", () => {
beforeEach(() => {
vi.clearAllMocks();
localStorage.setItem("litellm_plugin_mode", "my-plugin");
});
it("falls back to ai-gateway once an empty plugins list loads", async () => {
renderWithPlugins([]);
await waitFor(() => expect(getMock).toHaveBeenCalled());
await waitFor(() => expect(screen.getByTestId("mode")).toHaveTextContent("ai-gateway"));
expect(screen.getByTestId("active")).toHaveTextContent("none");
});
it("keeps the stored mode when it is still registered", async () => {
renderWithPlugins([{ name: "my-plugin", display_name: "My Plugin", url: "https://p.example.com" }]);
await waitFor(() => expect(screen.getByTestId("active")).toHaveTextContent("my-plugin"));
expect(screen.getByTestId("mode")).toHaveTextContent("my-plugin");
});
it("falls back to ai-gateway when the plugins fetch fails, never stranding the user", async () => {
getMock.mockRejectedValueOnce(new Error("network down"));
render(
<PluginModeProvider accessToken="sk-test">
<ModeProbe />
</PluginModeProvider>,
);
await waitFor(() => expect(getMock).toHaveBeenCalled());
await waitFor(() => expect(screen.getByTestId("mode")).toHaveTextContent("ai-gateway"));
});
});