import { fireEvent, render, screen, within } from "@testing-library/react"; import { beforeEach, expect, it, vi } from "vitest"; vi.mock("../Editor", () => ({ Editor: () => null })); import { App } from "../App"; import type { Api } from "../api"; import { parseNotebook } from "@shellbook/core"; const source = `--- title: Guided setup params: host: {type: host, required: true, label: Target server} user: {type: name, label: Login user} note: {type: string} --- # Guided setup :::group{title="1. Connect"} Pick the server first: ::param{name=host} \`\`\`sh {check host="{{host}}" id=whoami title="Who am I"} id {{user}} \`\`\` ::: :::group{title="2. Notes" collapsed} ::param{name=note} ::: `; function api(): Api { return { create: vi.fn(), move: vi.fn(), deleteNotebook: vi.fn(), me: vi.fn().mockResolvedValue({}), login: vi.fn(), list: vi.fn().mockResolvedValue({ notebooks: [{ path: "guide.md", title: "Guided setup" }] }), hosts: vi.fn().mockResolvedValue({ hosts: [{ name: "pve", address: "10.0.0.2", user: "root", port: 22, tags: [] }] }), notebook: vi.fn().mockResolvedValue({ path: "guide.md", source, notebook: parseNotebook(source), includes: [], builtins: {}, version: "1", }), save: vi.fn(), run: vi.fn(), addHost: vi.fn(), events: (_callback, connected) => { connected(true); return () => {}; }, artifact: () => "/download", }; } beforeEach(() => localStorage.clear()); it("shows parameter fields in the text, bound to the same value as the panel", async () => { render(); const connect = await screen.findByRole("region", { name: "1. Connect" }); const inline = within(connect).getByLabelText(/Target server/); const panel = within(screen.getByRole("form", { name: "Notebook parameters" })).getByLabelText(/Target server/); expect(inline).not.toBe(panel); fireEvent.change(inline, { target: { value: "pve" } }); expect(panel).toHaveValue("pve"); fireEvent.change(panel, { target: { value: "" } }); expect(inline).toHaveValue(""); }); it("colors a group by the state of its inputs and says so in text", async () => { render(); const connect = await screen.findByRole("region", { name: "1. Connect" }); // The check needs host (required) and user (referenced by the script). expect(connect).toHaveClass("status-missing"); expect(within(connect).getByText("needs Target server, Login user")).toBeInTheDocument(); fireEvent.change(within(connect).getByLabelText(/Target server/), { target: { value: "pve" } }); expect(connect).toHaveTextContent("needs Login user"); fireEvent.change(within(screen.getByRole("form", { name: "Notebook parameters" })).getByLabelText(/Login user/), { target: { value: "admin" }, }); expect(connect).toHaveClass("status-filled"); expect(within(connect).getByText("complete")).toBeInTheDocument(); // Only optional inputs: grey. expect(screen.getByRole("region", { name: "2. Notes" })).toHaveClass("status-optional"); }); it("collapses groups, starts collapsed ones closed and remembers the choice", async () => { const { unmount } = render(); const notes = await screen.findByRole("region", { name: "2. Notes" }); const toggle = within(notes).getByRole("button", { name: "2. Notes" }); expect(toggle).toHaveAttribute("aria-expanded", "false"); expect(within(notes).getByLabelText("note", { selector: "input" })).not.toBeVisible(); fireEvent.click(toggle); expect(within(notes).getByLabelText("note", { selector: "input" })).toBeVisible(); const connect = screen.getByRole("region", { name: "1. Connect" }); fireEvent.click(within(connect).getByRole("button", { name: "1. Connect" })); expect(within(connect).getByText("Who am I")).not.toBeVisible(); unmount(); render(); const again = await screen.findByRole("region", { name: "1. Connect" }); expect(within(again).getByRole("button", { name: "1. Connect" })).toHaveAttribute("aria-expanded", "false"); expect(within(screen.getByRole("region", { name: "2. Notes" })).getByRole("button", { name: "2. Notes" })) .toHaveAttribute("aria-expanded", "true"); });