Spaces:
Running
Running
File size: 6,307 Bytes
03608f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | 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(<DryRunView dryRun={DRY_RUN} current={false} />);
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(<DryRunView dryRun={{ ...DRY_RUN, ok: true, problems: [] }} />);
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(<WorkflowsView />);
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();
});
});
|