217 lines
6.5 KiB
TypeScript
217 lines
6.5 KiB
TypeScript
import {
|
|
act,
|
|
fireEvent,
|
|
render,
|
|
screen,
|
|
waitFor,
|
|
within,
|
|
} from "@testing-library/react";
|
|
import { expect, it, vi } from "vitest";
|
|
vi.mock("../Editor", () => ({
|
|
Editor: ({
|
|
source,
|
|
onChange,
|
|
}: {
|
|
source: string;
|
|
onChange(value: string): void;
|
|
}) => (
|
|
<textarea
|
|
aria-label="Notebook Markdown"
|
|
value={source}
|
|
onChange={(e) => onChange(e.target.value)}
|
|
/>
|
|
),
|
|
}));
|
|
import { App } from "../App";
|
|
import { HttpError } from "../api";
|
|
import type { Api } from "../api";
|
|
import type { ServerEvent, StartRunRequest } from "@shellbook/core";
|
|
import { parseNotebook } from "@shellbook/core";
|
|
import source from "../mock/ssh-key.md?raw";
|
|
|
|
function harness() {
|
|
let receive: (event: ServerEvent) => void = () => {};
|
|
let id = 0;
|
|
const api: Api = {
|
|
me: vi.fn().mockResolvedValue({}),
|
|
login: vi.fn().mockResolvedValue(undefined),
|
|
list: vi
|
|
.fn()
|
|
.mockResolvedValue({
|
|
notebooks: [{ path: "ssh-key.md", title: "New server SSH key" }],
|
|
}),
|
|
hosts: vi
|
|
.fn()
|
|
.mockResolvedValue({
|
|
hosts: [
|
|
{
|
|
name: "staging",
|
|
user: "admin",
|
|
address: "example.test",
|
|
port: 22,
|
|
tags: [],
|
|
},
|
|
],
|
|
}),
|
|
notebook: vi
|
|
.fn()
|
|
.mockResolvedValue({
|
|
path: "ssh-key.md",
|
|
source,
|
|
notebook: parseNotebook(source),
|
|
version: "1",
|
|
}),
|
|
save: vi.fn().mockResolvedValue({ version: "2" }),
|
|
run: vi.fn(async (_request: StartRunRequest) => ({ runId: String(++id) })),
|
|
events: (callback, connected) => {
|
|
receive = callback;
|
|
connected(true);
|
|
return () => {};
|
|
},
|
|
artifact: () => "/download",
|
|
};
|
|
return { api, emit: (event: ServerEvent) => act(() => receive(event)) };
|
|
}
|
|
async function ready(api: Api) {
|
|
render(<App api={api} />);
|
|
fireEvent.change(await screen.findByLabelText("Target server *"), {
|
|
target: { value: "staging" },
|
|
});
|
|
}
|
|
it("shows login on 401 and sends the password only to login", async () => {
|
|
const { api } = harness();
|
|
vi.mocked(api.me).mockRejectedValue(new HttpError(401));
|
|
render(<App api={api} />);
|
|
fireEvent.change(await screen.findByLabelText("Password"), {
|
|
target: { value: "login-secret" },
|
|
});
|
|
fireEvent.click(screen.getByRole("button", { name: "Sign in" }));
|
|
await screen.findByLabelText("Target server *");
|
|
expect(api.login).toHaveBeenCalledWith("login-secret");
|
|
expect(document.body.textContent).not.toContain("login-secret");
|
|
});
|
|
it("runs all checks and summarizes their streamed results", async () => {
|
|
const { api, emit } = harness();
|
|
await ready(api);
|
|
fireEvent.click(screen.getByRole("button", { name: "Run all checks" }));
|
|
await waitFor(() => expect(api.run).toHaveBeenCalledTimes(2));
|
|
emit({
|
|
type: "run.finished",
|
|
runId: "1",
|
|
exitCode: 0,
|
|
durationMs: 20,
|
|
artifacts: [],
|
|
});
|
|
emit({
|
|
type: "run.finished",
|
|
runId: "2",
|
|
exitCode: 1,
|
|
durationMs: 20,
|
|
artifacts: [],
|
|
});
|
|
expect(screen.getByText(/1 \/ 2/)).toHaveTextContent("❌ 1");
|
|
});
|
|
it("reveals after sections only on success and passes artifact provenance", async () => {
|
|
const { api, emit } = harness();
|
|
await ready(api);
|
|
expect(
|
|
screen.queryByText(/Download the private key/),
|
|
).not.toBeInTheDocument();
|
|
fireEvent.click(
|
|
within(
|
|
screen.getByRole("article", { name: "Generate key pair" }),
|
|
).getByRole("button", { name: "Run" }),
|
|
);
|
|
await waitFor(() => expect(api.run).toHaveBeenCalledTimes(1));
|
|
emit({
|
|
type: "run.finished",
|
|
runId: "1",
|
|
exitCode: 0,
|
|
durationMs: 30,
|
|
artifacts: ["key.pub"],
|
|
captured: { name: "captured", value: "value" },
|
|
});
|
|
expect(screen.getByText(/Download the private key/)).toBeInTheDocument();
|
|
fireEvent.click(
|
|
within(screen.getByRole("article", { name: "Authorize key" })).getByRole(
|
|
"button",
|
|
{ name: "Run" },
|
|
),
|
|
);
|
|
fireEvent.click(screen.getByRole("button", { name: "Confirm and run" }));
|
|
await waitFor(() => expect(api.run).toHaveBeenCalledTimes(2));
|
|
expect(vi.mocked(api.run).mock.calls[1]?.[0]).toMatchObject({
|
|
runtime: { captured: "value" },
|
|
artifactSources: { "key.pub": "1" },
|
|
});
|
|
emit({
|
|
type: "run.hostKeyUnknown",
|
|
runId: "2",
|
|
host: "staging",
|
|
fingerprint: "SHA256:test",
|
|
changed: true,
|
|
});
|
|
expect(screen.getByRole("dialog")).toHaveTextContent("HOST KEY CHANGED");
|
|
fireEvent.click(
|
|
screen.getByRole("button", { name: "Trust this fingerprint and retry" }),
|
|
);
|
|
await waitFor(() => expect(api.run).toHaveBeenCalledTimes(3));
|
|
expect(vi.mocked(api.run).mock.calls[2]?.[0].acceptHostKey).toBe(
|
|
"SHA256:test",
|
|
);
|
|
});
|
|
it("sends baseVersion and retains draft on conflict; file events preserve dirty edits", async () => {
|
|
const { api, emit } = harness();
|
|
vi.mocked(api.save).mockRejectedValue(new HttpError(409));
|
|
await ready(api);
|
|
fireEvent.click(screen.getByRole("button", { name: "Edit Markdown" }));
|
|
fireEvent.change(await screen.findByLabelText("Notebook Markdown"), {
|
|
target: { value: source + "\nDraft text\n" },
|
|
});
|
|
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
|
await screen.findByText(/The file changed on disk/);
|
|
expect(api.save).toHaveBeenCalledWith(
|
|
"ssh-key.md",
|
|
source + "\nDraft text\n",
|
|
"1",
|
|
);
|
|
emit({ type: "file.changed", path: "ssh-key.md" });
|
|
expect(api.notebook).toHaveBeenCalledTimes(1);
|
|
expect(screen.getByLabelText("Notebook Markdown")).toHaveValue(
|
|
source + "\nDraft text\n",
|
|
);
|
|
});
|
|
it("reloads the open notebook on clean file changes", async () => {
|
|
const { api, emit } = harness();
|
|
await ready(api);
|
|
emit({ type: "file.changed", path: "ssh-key.md" });
|
|
await waitFor(() => expect(api.notebook).toHaveBeenCalledTimes(2));
|
|
});
|
|
it("retains edits made while a file reload is in flight", async () => {
|
|
const { api, emit } = harness();
|
|
await ready(api);
|
|
let finish!: (value: Awaited<ReturnType<Api["notebook"]>>) => void;
|
|
vi.mocked(api.notebook).mockImplementationOnce(
|
|
() =>
|
|
new Promise((resolve) => {
|
|
finish = resolve;
|
|
}),
|
|
);
|
|
emit({ type: "file.changed", path: "ssh-key.md" });
|
|
fireEvent.click(screen.getByRole("button", { name: "Edit Markdown" }));
|
|
fireEvent.change(await screen.findByLabelText("Notebook Markdown"), {
|
|
target: { value: source + "\nIn-flight draft\n" },
|
|
});
|
|
await act(async () =>
|
|
finish({
|
|
path: "ssh-key.md",
|
|
source,
|
|
notebook: parseNotebook(source),
|
|
version: "2",
|
|
}),
|
|
);
|
|
expect(screen.getByLabelText("Notebook Markdown")).toHaveValue(
|
|
source + "\nIn-flight draft\n",
|
|
);
|
|
});
|