Files
kandev/apps/web/components/settings/agent-profile-delete-dialog.test.tsx
T

174 lines
6.1 KiB
TypeScript

import { describe, it, expect, afterEach } from "vitest";
import { render, screen, cleanup } from "@testing-library/react";
import { StateProvider } from "@/components/state-provider";
import { AgentProfileDeleteConflictDialog } from "./agent-profile-delete-dialog";
import type { AgentProfileDeleteConflict } from "./agent-profile-delete-dialog";
afterEach(cleanup);
const FIXTURE_TIMESTAMP = "2026-01-01T00:00:00.000Z";
function renderConflictDialog(conflict: AgentProfileDeleteConflict | null) {
return render(
<StateProvider
initialState={{
workspaces: {
activeId: "ws-1",
items: [
{
id: "ws-1",
name: "Office Workspace",
owner_id: "user-1",
created_at: FIXTURE_TIMESTAMP,
updated_at: FIXTURE_TIMESTAMP,
},
],
},
settingsAgents: {
items: [
{
id: "codex-acp",
name: "Codex",
supports_mcp: false,
profiles: [],
created_at: FIXTURE_TIMESTAMP,
updated_at: FIXTURE_TIMESTAMP,
},
],
},
}}
>
<AgentProfileDeleteConflictDialog
conflict={conflict}
onOpenChange={() => {}}
onConfirm={() => {}}
/>
</StateProvider>,
);
}
describe("AgentProfileDeleteConflictDialog", () => {
// An automation is configuration, not a session: nothing is running, so it
// never reaches activeSessions. Before this was rendered, an automation-only
// conflict popped a dialog that listed nothing at all and then offered
// "Delete Anyway".
it("names the automations on an automation-only conflict", () => {
renderConflictDialog({
activeSessions: [],
watchers: [],
routingTiers: [],
automations: [{ id: "auto-1", name: "Nightly dependency sweep", workspace_id: "ws-1" }],
});
expect(screen.getByTestId("delete-conflict-automations")).toBeTruthy();
screen.getByText("Nightly dependency sweep");
});
it("renders the watcher list grouped by kind on a watcher-only conflict", () => {
renderConflictDialog({
activeSessions: [],
watchers: [
{ id: "linear-w1", kind: "linear", label: "team ENG" },
{ id: "linear-w2", kind: "linear", label: "team WEB" },
{ id: "github-w1", kind: "github_issue", label: "kdlbs/kandev" },
],
routingTiers: [],
automations: [],
});
// Watcher group headings render the human-friendly kind label, and
// each watcher's label string appears once. Critically: the dialog
// pops even with no active sessions — this is the bug class the
// backend self-heal pre-flight fix would have left unaddressed
// without the frontend wiring.
expect(screen.getByText(/Watchers \(will be disabled\)/)).toBeTruthy();
expect(screen.getByText(/Linear:/)).toBeTruthy();
expect(screen.getByText(/GitHub Issues:/)).toBeTruthy();
expect(screen.getByText(/team ENG/)).toBeTruthy();
expect(screen.getByText(/team WEB/)).toBeTruthy();
expect(screen.getByText(/kdlbs\/kandev/)).toBeTruthy();
});
it("does not render the watchers section when the conflict is sessions-only", () => {
renderConflictDialog({
activeSessions: [{ task_id: "t1", task_title: "Live task", is_ephemeral: false }],
watchers: [],
routingTiers: [],
automations: [],
});
expect(screen.getByText(/Tasks:/)).toBeTruthy();
expect(screen.getByText("Live task")).toBeTruthy();
expect(screen.queryByText(/Watchers \(will be disabled\)/)).toBeNull();
});
it("renders both sections when sessions and watchers coexist", () => {
renderConflictDialog({
activeSessions: [{ task_id: "t1", task_title: "Live task", is_ephemeral: false }],
watchers: [{ id: "jira-w1", kind: "jira", label: "project = ENG" }],
routingTiers: [],
automations: [],
});
expect(screen.getByText("Live task")).toBeTruthy();
expect(screen.getByText(/Jira:/)).toBeTruthy();
expect(screen.getByText(/project = ENG/)).toBeTruthy();
});
it("renders tier mappings as a hard blocker", () => {
renderConflictDialog({
activeSessions: [],
watchers: [],
routingTiers: [{ workspace_id: "ws-1", provider_id: "codex-acp", tier: "balanced" }],
automations: [],
});
expect(screen.getByText(/Cannot delete agent profile/i)).toBeTruthy();
expect(screen.getByText(/Workspace tier mappings:/)).toBeTruthy();
expect(
screen.getByText((_content, element) =>
Boolean(
element?.tagName === "LI" &&
element.textContent?.includes(
"Balanced tier in Office Workspace (ws-1) for Codex (codex-acp)",
),
),
),
).toBeTruthy();
expect(screen.queryByText(/Delete Anyway/)).toBeNull();
});
// The watcher `kind` is the wire enum and its label is a catalog key, so a
// renamed key or a dropped entry must fail here rather than silently render
// the raw enum value to the user.
it("resolves every known watcher kind to its label, and echoes an unknown one", () => {
renderConflictDialog({
activeSessions: [],
watchers: [
{ id: "w1", kind: "linear", label: "team ENG" },
{ id: "w2", kind: "jira", label: "project = ENG" },
{ id: "w3", kind: "github_issue", label: "kdlbs/kandev" },
{ id: "w4", kind: "github_review", label: "kdlbs/kandev PRs" },
// A kind the frontend does not know yet — the raw value is echoed
// rather than rendering an empty label.
{ id: "w5", kind: "sentry" as never, label: "proj/backend" },
],
routingTiers: [],
automations: [],
});
expect(screen.getByText(/Linear:/)).toBeTruthy();
expect(screen.getByText(/Jira:/)).toBeTruthy();
expect(screen.getByText(/GitHub Issues:/)).toBeTruthy();
expect(screen.getByText(/GitHub PR Reviews:/)).toBeTruthy();
expect(screen.getByText(/sentry:/)).toBeTruthy();
});
it("does not render the dialog when conflict is null", () => {
renderConflictDialog(null);
expect(screen.queryByText(/Delete agent profile/i)).toBeNull();
});
});