import { screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest";
import { setAccessToken } from "@/lib/session";
import { mockFetch } from "@/test/fixtures";
import { renderUi } from "@/test/render";
import { DryRunView } from "./parts";
import type { DryRun } from "./types";
import { WorkflowsView } from "./workflows-view";
const push = vi.fn();
vi.mock("next/navigation", () => ({
useRouter: () => ({ push, replace: vi.fn() }),
usePathname: () => "/workflows",
}));
afterEach(() => {
vi.unstubAllGlobals();
setAccessToken(null);
push.mockReset();
});
const DRY_RUN: DryRun = {
ok: false,
problems: ["stops at step cancel"],
totals: { would_execute: 1, needs_approval: 1, denied: 1, error: 0, skipped: 0 },
messages: 0,
steps: [
{
id: "note",
kind: "tool",
tool: "demo.add_note",
status: "would_execute",
summary: "demo.add_note: 1 call",
items: [
{
outcome: "would_execute",
args: { order_number: "ORD-1021", note: "Morning check" },
effect: { notes_after: 2 },
reason: null,
tier: 1,
},
],
},
{
id: "refund",
kind: "tool",
tool: "demo.refund_order",
status: "needs_approval",
items: [{ outcome: "needs_approval", args: { amount: 500 }, effect: null, reason: "over limit", tier: 2 }],
},
{
id: "cancel",
kind: "tool",
tool: "demo.cancel_order",
status: "denied",
reason: "policy forbids",
items: [{ outcome: "denied", args: {}, effect: null, reason: "policy forbids", tier: 3 }],
},
],
lines: [
"[note] demo.add_note: 1 call",
" + demo.add_note(order_number=ORD-1021, note=Morning check)",
"[refund] needs approval",
" ? demo.refund_order(amount=500) [needs_approval: over limit]",
"[cancel] policy forbids",
" x demo.cancel_order() [denied: policy forbids]",
],
plan: [],
definition_hash: "h",
};
describe("DryRunView", () => {
it("renders the verdict, totals, colour-coded lines and per-step items", () => {
renderUi();
expect(screen.getByTestId("dry-run-verdict")).toHaveTextContent("Problems found");
expect(screen.getByText("The definition changed after this dry run.")).toBeInTheDocument();
expect(screen.getByTestId("dry-run-problems")).toHaveTextContent("stops at step cancel");
const totals = screen.getByTestId("dry-run-totals");
expect(within(totals).getByText("Would execute", { exact: false })).toHaveTextContent("1");
const lines = screen.getAllByTestId("dry-run-line");
expect(lines.map((l) => l.dataset.tone)).toEqual([
"plain",
"execute",
"plain",
"approval",
"plain",
"denied",
]);
expect(lines[1]).toHaveClass("text-success");
expect(lines[3]).toHaveClass("text-warning");
expect(lines[5]).toHaveClass("text-destructive");
const note = screen.getByTestId("dry-run-step-note");
expect(note).toHaveTextContent("demo.add_note: 1 call");
expect(within(note).getByTestId("dry-run-item")).toHaveAttribute("data-outcome", "would_execute");
expect(note).toHaveTextContent('"notes_after":2');
expect(screen.getByTestId("dry-run-step-cancel")).toHaveTextContent("policy forbids");
});
it("shows a passing verdict", () => {
renderUi();
expect(screen.getByTestId("dry-run-verdict")).toHaveAttribute("data-ok", "true");
expect(screen.queryByTestId("dry-run-problems")).toBeNull();
});
});
const WF = {
id: "w1",
key: "morning_order_check",
name: "Morning order check",
description: "Every morning",
status: "draft",
active_version: null,
source: "nl",
template: null,
created_at: "2026-12-05T08:00:00Z",
updated_at: "2026-12-05T08:00:00Z",
latest_version: { version: 1, status: "draft" },
};
describe("WorkflowsView", () => {
it("lists workflows and templates and shows compile errors from a 422", async () => {
const { calls } = mockFetch({
"GET /workflows": () => ({ items: [WF], pending_proposals: 0 }),
"GET /workflows/templates": () => [
{
id: "demo.morning_order_check",
pack: "demo",
name: "Morning order check",
description: "Look up an order every morning",
trigger: "schedule",
nl: "Every morning at 8, add a note to ORD-1021 with its status",
tags: ["demo"],
definition: {},
plan: [],
ok: true,
errors: [],
warnings: [],
},
],
"POST /workflows": () =>
new Response(
JSON.stringify({
detail: {
message: "compile failed",
ok: false,
errors: [{ code: "unknown_tool", message: "tool shop.nuke is not available", step: "go" }],
warnings: [],
},
}),
{ status: 422, headers: { "Content-Type": "application/json" } },
),
});
renderUi();
const table = await screen.findByTestId("workflows-table");
expect(within(table).getByText("Morning order check")).toBeInTheDocument();
expect(within(table).getAllByText("Draft")).toHaveLength(2);
const card = await screen.findByTestId("template-card");
expect(card).toHaveTextContent("Every morning at 8, add a note to ORD-1021");
await userEvent.type(screen.getByLabelText("New workflow"), "nuke the shop");
await userEvent.click(screen.getByRole("button", { name: "Create draft" }));
const errs = await screen.findByTestId("create-errors");
expect(errs).toHaveTextContent("compile failed");
expect(errs).toHaveTextContent("tool shop.nuke is not available");
expect(errs).toHaveTextContent("[go]");
expect(calls.find((c) => c.method === "POST" && c.path === "/workflows")?.body).toEqual({
text: "nuke the shop",
});
expect(push).not.toHaveBeenCalled();
});
});