Files
kandev/apps/web/components/settings/plugins/plugin-config-form.agent-profile.test.tsx
2026-09-15 09:40:39 +01:00

101 lines
3.5 KiB
TypeScript

import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { StateProvider } from "@/components/state-provider";
import type { AgentProfileOption } from "@/lib/state/slices/settings/types";
import type { PluginConfigField } from "@/lib/plugins/config-schema";
import { PluginConfigForm } from "./plugin-config-form";
vi.mock("@/hooks/domains/settings/use-settings-data", () => ({ useSettingsData: vi.fn() }));
const AGENT_PROFILE_FIELD = "agent_profile";
const ELIGIBLE_PROFILE_LABEL = "Claude · Notes";
const field: PluginConfigField = {
name: AGENT_PROFILE_FIELD,
label: "Notes agent",
type: "agent_profile",
required: false,
secret: false,
};
const profile = (overrides: Partial<AgentProfileOption>): AgentProfileOption => ({
id: "profile-1",
label: ELIGIBLE_PROFILE_LABEL,
agent_id: "claude",
agent_name: "claude",
cli_passthrough: false,
enabled: true,
inference_capable: true,
...overrides,
});
afterEach(() => {
cleanup();
vi.clearAllMocks();
});
function renderField(
profiles: AgentProfileOption[],
value = "",
fieldOverride: Partial<PluginConfigField> = {},
) {
const onChange = vi.fn<(name: string, value: string | boolean) => void>();
render(
<StateProvider initialState={{ agentProfiles: { items: profiles, version: 0 } }}>
<PluginConfigForm
fields={[{ ...field, ...fieldOverride }]}
values={{ [AGENT_PROFILE_FIELD]: value }}
initialValues={{ [AGENT_PROFILE_FIELD]: value }}
disabled={false}
onChange={onChange}
/>
</StateProvider>,
);
return onChange;
}
describe("PluginConfigForm agent profile field", () => {
it("offers only enabled global non-CLI inference profiles and stores the stable ID", () => {
const onChange = renderField([
profile({ id: "eligible", label: ELIGIBLE_PROFILE_LABEL }),
profile({ id: "disabled", label: "Disabled", enabled: false }),
profile({ id: "workspace", label: "Workspace", workspace_id: "workspace-1" }),
profile({ id: "cli", label: "CLI", cli_passthrough: true }),
profile({ id: "non-inference", label: "Non-inference", inference_capable: false }),
]);
fireEvent.click(screen.getByRole("combobox"));
expect(screen.getByRole("option", { name: ELIGIBLE_PROFILE_LABEL })).not.toBeNull();
expect(screen.queryByRole("option", { name: "Disabled" })).toBeNull();
expect(screen.queryByRole("option", { name: "Workspace" })).toBeNull();
expect(screen.queryByRole("option", { name: "CLI" })).toBeNull();
expect(screen.queryByRole("option", { name: "Non-inference" })).toBeNull();
fireEvent.click(screen.getByRole("option", { name: ELIGIBLE_PROFILE_LABEL }));
expect(onChange).toHaveBeenCalledWith(AGENT_PROFILE_FIELD, "eligible");
});
it("shows a stale saved profile without making it selectable", () => {
renderField([], "deleted-profile");
expect(screen.getByText("Unavailable: deleted-profile")).not.toBeNull();
});
it("marks an ineligible saved profile as unavailable", () => {
renderField([profile({ id: "disabled", label: "Disabled", enabled: false })], "disabled");
expect(screen.getByText("Unavailable: Disabled")).not.toBeNull();
});
it("hides the Not set option for required fields", () => {
renderField([profile({ id: "eligible", label: ELIGIBLE_PROFILE_LABEL })], "", {
required: true,
});
fireEvent.click(screen.getByRole("combobox"));
expect(screen.queryByRole("option", { name: /not set/i })).toBeNull();
});
});