177 lines
4.7 KiB
TypeScript
177 lines
4.7 KiB
TypeScript
import { fireEvent, render, screen } from "@testing-library/react";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import { ParamsForm } from "../ParamsForm";
|
|
import { StepCard } from "../StepCard";
|
|
import type { Notebook, RenderedStep, Step } from "@shellbook/core";
|
|
const notebook: Notebook = {
|
|
title: "Test",
|
|
params: [
|
|
{ name: "host", type: "host", required: true },
|
|
{
|
|
name: "mode",
|
|
type: "choice",
|
|
required: false,
|
|
options: ["safe", "fast"],
|
|
},
|
|
{ name: "token", type: "secret", required: true },
|
|
],
|
|
blocks: [],
|
|
diagnostics: [],
|
|
};
|
|
const host = {
|
|
name: "staging",
|
|
user: "ops",
|
|
address: "example.test",
|
|
port: 22,
|
|
tags: [],
|
|
};
|
|
const step: Step = {
|
|
kind: "check",
|
|
lang: "sh",
|
|
script: "id {{user}}",
|
|
range: { startLine: 1, endLine: 3 },
|
|
attrs: {
|
|
id: "id",
|
|
local: false,
|
|
sudo: false,
|
|
confirm: false,
|
|
artifacts: [],
|
|
uploads: [],
|
|
timeoutSec: 30,
|
|
},
|
|
};
|
|
const rendered: RenderedStep = {
|
|
stepId: "id",
|
|
kind: "check",
|
|
local: false,
|
|
host: "staging",
|
|
script: "id admin",
|
|
artifacts: [],
|
|
uploads: [],
|
|
missing: [],
|
|
invalid: {},
|
|
};
|
|
describe("parameter form", () => {
|
|
it("renders inventory and choices, reports required and validation errors", () => {
|
|
const change = vi.fn();
|
|
render(
|
|
<ParamsForm
|
|
notebook={notebook}
|
|
values={{}}
|
|
errors={{ host: "Required" }}
|
|
hosts={[host]}
|
|
onChange={change}
|
|
/>,
|
|
);
|
|
expect(screen.getByLabelText("host *")).toBeRequired();
|
|
expect(screen.getByLabelText("host *")).toHaveAttribute(
|
|
"aria-invalid",
|
|
"true",
|
|
);
|
|
fireEvent.change(screen.getByLabelText("host *"), {
|
|
target: { value: "staging" },
|
|
});
|
|
expect(change).toHaveBeenCalledWith("host", "staging");
|
|
fireEvent.change(screen.getByLabelText("mode"), {
|
|
target: { value: "safe" },
|
|
});
|
|
expect(change).toHaveBeenCalledWith("mode", "safe");
|
|
});
|
|
it("keeps secrets in a password field", () => {
|
|
const { container } = render(
|
|
<ParamsForm
|
|
notebook={notebook}
|
|
values={{ token: "sensitive" }}
|
|
errors={{}}
|
|
hosts={[]}
|
|
onChange={() => {}}
|
|
/>,
|
|
);
|
|
expect(screen.getByLabelText("token *")).toHaveAttribute(
|
|
"type",
|
|
"password",
|
|
);
|
|
expect(container.textContent).not.toContain("sensitive");
|
|
});
|
|
});
|
|
describe("step card", () => {
|
|
const props = {
|
|
step,
|
|
rendered,
|
|
host,
|
|
onRun: vi.fn(),
|
|
artifact: () => "/download",
|
|
};
|
|
it("highlights missing templates and disables execution", () => {
|
|
render(
|
|
<StepCard
|
|
{...props}
|
|
rendered={{ ...rendered, script: "id {{user}}", missing: ["user"] }}
|
|
/>,
|
|
);
|
|
expect(screen.getByRole("button", { name: "Check" })).toBeDisabled();
|
|
expect(screen.getByText("{{user}}").tagName).toBe("MARK");
|
|
});
|
|
it("disables invalid params", () => {
|
|
render(
|
|
<StepCard
|
|
{...props}
|
|
rendered={{ ...rendered, invalid: { user: "Invalid" } }}
|
|
/>,
|
|
);
|
|
expect(screen.getByRole("button", { name: "Check" })).toBeDisabled();
|
|
});
|
|
it("runs a ready step", () => {
|
|
render(<StepCard {...props} />);
|
|
fireEvent.click(screen.getByRole("button", { name: "Check" }));
|
|
expect(props.onRun).toHaveBeenCalled();
|
|
});
|
|
it.each(["running", "success", "failure"] as const)(
|
|
"shows %s state",
|
|
(status) => {
|
|
render(
|
|
<StepCard
|
|
{...props}
|
|
run={{
|
|
runId: "1",
|
|
notebookPath: "n",
|
|
stepId: "id",
|
|
status,
|
|
output: [{ stream: "stdout", data: "hello" }],
|
|
artifacts: ["key.pub"],
|
|
...(status !== "running"
|
|
? { exitCode: status === "success" ? 0 : 1, durationMs: 1234 }
|
|
: {}),
|
|
}}
|
|
/>,
|
|
);
|
|
expect(screen.getByText("hello")).toBeInTheDocument();
|
|
expect(screen.getByText("↓ key.pub")).toHaveAttribute(
|
|
"href",
|
|
"/download",
|
|
);
|
|
if (status === "running")
|
|
expect(screen.getByRole("button", { name: "Running…" })).toBeDisabled();
|
|
else
|
|
expect(
|
|
screen.getByText(
|
|
status === "success"
|
|
? /✅ Succeeded.*exit 0.*1.23s/
|
|
: /❌ Failed.*exit 1.*1.23s/,
|
|
),
|
|
).toBeInTheDocument();
|
|
},
|
|
);
|
|
it("copies exactly the displayed script", async () => {
|
|
const writeText = vi.fn().mockResolvedValue(undefined);
|
|
Object.defineProperty(navigator, "clipboard", {
|
|
configurable: true,
|
|
value: { writeText },
|
|
});
|
|
render(<StepCard {...props} />);
|
|
fireEvent.click(screen.getByRole("button", { name: "Copy" }));
|
|
expect(writeText).toHaveBeenCalledWith(rendered.script);
|
|
await screen.findByText("Copied");
|
|
});
|
|
});
|