263 lines
17 KiB
TypeScript
263 lines
17 KiB
TypeScript
import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|||
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||
|
|
import { QuickPairSetup } from "@/components/remote/QuickPairSetup";
|
||
|
|
import { Dialog, DialogContent } from "@/components/ui/dialog";
|
||
|
|
import i18n from "@/i18n";
|
||
|
|
|
||
|
|
const mocks = vi.hoisted(() => {
|
||
|
|
const action = vi.fn();
|
||
|
|
return { action, client: { requestMutation: action }, connect: vi.fn(), refresh: vi.fn(), cancel: vi.fn(), copy: vi.fn() };
|
||
|
|
});
|
||
|
|
vi.mock("@/providers/ClientProvider", () => ({ useClient: () => ({ client: mocks.client }) }));
|
||
|
|
vi.mock("@/components/remote/RemoteInstances", () => ({ useRemoteConnections: () => ({ connect: mocks.connect, refresh: mocks.refresh, cancel: mocks.cancel, directory: { profiles: [] } }) }));
|
||
|
|
vi.mock("@/lib/clipboard", () => ({ copyTextToClipboard: mocks.copy }));
|
||
|
|
const request = () => ({ id: "request-1", command: "nanobot remote pair nbpr1.public", expires: Date.now() / 1000 + 600 });
|
||
|
|
const preview = { id: "request-1", host: "ubuntu@server", hostname: "team", fingerprint: "SHA256:trusted-server", authorized_until: Date.now() / 1000 + 86400, revoke_command: "nanobot remote revoke request-1" };
|
||
|
|
|
||
|
|
beforeEach(async () => {
|
||
|
|
await i18n.changeLanguage("en");
|
||
|
|
mocks.action.mockReset().mockImplementation(async (action: string) => {
|
||
|
|
if (action === "remote.pair_start") return request();
|
||
|
|
if (action === "remote.pair_preview") return preview;
|
||
|
|
if (action === "remote.pair_finish") return { id: "paired-server" };
|
||
|
|
return {};
|
||
|
|
});
|
||
|
|
mocks.copy.mockReset().mockResolvedValue(true);
|
||
|
|
mocks.connect.mockReset().mockResolvedValue(undefined);
|
||
|
|
mocks.refresh.mockReset().mockResolvedValue(undefined);
|
||
|
|
});
|
||
|
|
afterEach(() => { cleanup(); vi.useRealTimers(); vi.restoreAllMocks(); });
|
||
|
|
const view = (onSSH = vi.fn(), onClose = vi.fn()) => render(<Dialog open><DialogContent><QuickPairSetup onSSH={onSSH} onClose={onClose} /></DialogContent></Dialog>);
|
||
|
|
function chooseOtherWay(name: string) {
|
||
|
|
fireEvent.pointerDown(screen.getByRole("button", { name: "Other ways" }), { button: 0, ctrlKey: false });
|
||
|
|
fireEvent.click(screen.getByRole("menuitem", { name }));
|
||
|
|
}
|
||
|
|
|
||
|
|
async function reviewed() {
|
||
|
|
await screen.findByRole("button", { name: "Copy command" });
|
||
|
|
chooseOtherWay("Use a connection code instead");
|
||
|
|
fireEvent.change(await screen.findByRole("textbox", { name: "Connection code" }), { target: { value: "nbpc1.encrypted" } });
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "Review connection" }));
|
||
|
|
await screen.findByRole("heading", { name: "Open this nanobot?" });
|
||
|
|
}
|
||
|
|
|
||
|
|
describe("quick pairing", () => {
|
||
|
|
it("never asks a returning user to copy another command while validating their link", async () => {
|
||
|
|
mocks.action.mockImplementation(() => new Promise(() => {}));
|
||
|
|
render(<Dialog open><DialogContent><QuickPairSetup returned={{ id: "request-1", code: "nbpc1.encrypted" }} onSSH={vi.fn()} onClose={vi.fn()} /></DialogContent></Dialog>);
|
||
|
|
expect(screen.getByRole("heading", { name: "Review connection" })).toBeVisible();
|
||
|
|
expect(screen.queryByRole("button", { name: "Copy command" })).toBeNull();
|
||
|
|
expect(screen.queryByText("Run one command in your server terminal to connect.")).toBeNull();
|
||
|
|
expect(screen.getByRole("button", { name: "Review connection" })).toBeDisabled();
|
||
|
|
expect(mocks.connect).not.toHaveBeenCalled();
|
||
|
|
});
|
||
|
|
it("shows only the next action and reveals requirements on demand", async () => {
|
||
|
|
view();
|
||
|
|
await waitFor(() => expect(screen.getByRole("button", { name: "Copy command" })).toBeEnabled());
|
||
|
|
expect(screen.getByText(/For Linux servers/).closest(".inline-disclosure")).toHaveAttribute("aria-hidden", "true");
|
||
|
|
expect(screen.queryByRole("menuitem")).toBeNull();
|
||
|
|
expect(screen.getByText(/After authorizing/)).toHaveAttribute("aria-hidden", "true");
|
||
|
|
chooseOtherWay("Requirements & help");
|
||
|
|
expect(screen.getByText(/For Linux servers/)).toBeVisible();
|
||
|
|
expect(screen.getByText(/Commands expire after 10 minutes/)).toBeVisible();
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "View command" }));
|
||
|
|
expect(screen.getByText(request().command)).toBeVisible();
|
||
|
|
expect(screen.getByText(/For Linux servers/).closest(".inline-disclosure")).toHaveAttribute("aria-hidden", "true");
|
||
|
|
});
|
||
|
|
it("keeps closed command details mounted but inert for reversible transitions", async () => {
|
||
|
|
view();
|
||
|
|
const toggle = await screen.findByRole("button", { name: "View command" });
|
||
|
|
const panel = document.getElementById(toggle.getAttribute("aria-controls")!);
|
||
|
|
expect(panel).toHaveAttribute("data-state", "closed");
|
||
|
|
expect(panel).toHaveAttribute("inert");
|
||
|
|
fireEvent.click(toggle);
|
||
|
|
expect(panel).toHaveAttribute("data-state", "open");
|
||
|
|
expect(panel).not.toHaveAttribute("inert");
|
||
|
|
expect(toggle).toHaveAttribute("aria-expanded", "true");
|
||
|
|
fireEvent.click(toggle);
|
||
|
|
expect(document.getElementById(toggle.getAttribute("aria-controls")!)).toBe(panel);
|
||
|
|
expect(panel).toHaveAttribute("aria-hidden", "true");
|
||
|
|
expect(panel).toHaveAttribute("inert");
|
||
|
|
});
|
||
|
|
it("keeps preparation in the button, and delays its indicator without delaying readiness", async () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
let finish!: (value: ReturnType<typeof request>) => void;
|
||
|
|
mocks.action.mockImplementation(() => new Promise((resolve) => { finish = resolve; }));
|
||
|
|
view();
|
||
|
|
const button = screen.getByRole("button", { name: "Copy command" });
|
||
|
|
expect(button).toBeDisabled();
|
||
|
|
expect(button).toHaveAttribute("aria-busy", "true");
|
||
|
|
expect(screen.queryByText("Preparing your connection…")).toBeNull();
|
||
|
|
act(() => { vi.advanceTimersByTime(199); });
|
||
|
|
expect(button).toHaveTextContent("Copy command");
|
||
|
|
act(() => { vi.advanceTimersByTime(1); });
|
||
|
|
expect(button).toHaveTextContent("Preparing your connection…");
|
||
|
|
expect(screen.getByRole("status").closest("button")).toBe(button);
|
||
|
|
await act(async () => finish(request()));
|
||
|
|
expect(button).toHaveTextContent("Copy command");
|
||
|
|
expect(button).toBeEnabled();
|
||
|
|
expect(button).toHaveAttribute("aria-busy", "false");
|
||
|
|
});
|
||
|
|
it("does not flash a delayed status after fast preparation finishes", async () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
view();
|
||
|
|
await act(async () => {});
|
||
|
|
act(() => { vi.advanceTimersByTime(250); });
|
||
|
|
expect(screen.getByRole("button", { name: "Copy command" })).toBeEnabled();
|
||
|
|
expect(screen.queryByText("Preparing your connection…")).toBeNull();
|
||
|
|
});
|
||
|
|
it("preserves the pasted code and SSH fallback during a slow review", async () => {
|
||
|
|
const ssh = vi.fn();
|
||
|
|
view(ssh);
|
||
|
|
await waitFor(() => expect(screen.getByRole("button", { name: "Copy command" })).toBeEnabled());
|
||
|
|
chooseOtherWay("Use a connection code instead");
|
||
|
|
fireEvent.change(screen.getByRole("textbox", { name: "Connection code" }), { target: { value: "nbpc1.unfinished" } });
|
||
|
|
mocks.action.mockImplementation(() => new Promise(() => {}));
|
||
|
|
vi.useFakeTimers();
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "Review connection" }));
|
||
|
|
act(() => { vi.advanceTimersByTime(200); });
|
||
|
|
expect(screen.getByRole("textbox", { name: "Connection code" })).toHaveValue("nbpc1.unfinished");
|
||
|
|
expect(screen.getByRole("button", { name: "Checking the connection link…" })).toBeDisabled();
|
||
|
|
chooseOtherWay("Use existing SSH settings");
|
||
|
|
expect(ssh).toHaveBeenCalledOnce();
|
||
|
|
});
|
||
|
|
it("advances only after copying succeeds and never implies a server connection", async () => {
|
||
|
|
let finish!: (success: boolean) => void;
|
||
|
|
mocks.copy.mockImplementation(() => new Promise((resolve) => { finish = resolve; }));
|
||
|
|
view();
|
||
|
|
await waitFor(() => expect(screen.getByRole("button", { name: "Copy command" })).toBeEnabled());
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "Copy command" }));
|
||
|
|
expect(screen.getByRole("heading", { name: "Add server" })).toBeVisible();
|
||
|
|
await act(async () => finish(true));
|
||
|
|
expect(screen.getByRole("heading", { name: "Command copied" })).toBeVisible();
|
||
|
|
expect(screen.getByText(/Paste it into your server terminal/)).toBeVisible();
|
||
|
|
expect(screen.getByRole("button", { name: "Copy again" })).toBeEnabled();
|
||
|
|
expect(screen.getByRole("button", { name: "Copy again" })).toHaveClass("border", "border-input");
|
||
|
|
expect(screen.getByRole("dialog")).toHaveAccessibleDescription(i18n.t("remote.pair.afterCopyHint"));
|
||
|
|
expect(mocks.connect).not.toHaveBeenCalled();
|
||
|
|
expect(mocks.action.mock.calls.map(([action]) => action)).toEqual(["remote.pair_start"]);
|
||
|
|
});
|
||
|
|
it("exposes the command when the clipboard fails without claiming it was copied", async () => {
|
||
|
|
mocks.copy.mockResolvedValue(false);
|
||
|
|
view();
|
||
|
|
await waitFor(() => expect(screen.getByRole("button", { name: "Copy command" })).toBeEnabled());
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "Copy command" }));
|
||
|
|
await screen.findByRole("alert");
|
||
|
|
expect(screen.getByText(request().command)).toBeVisible();
|
||
|
|
expect(screen.getByRole("heading", { name: "Add server" })).toBeVisible();
|
||
|
|
expect(screen.queryByRole("button", { name: "Copy again" })).toBeNull();
|
||
|
|
});
|
||
|
|
it("keeps a manual code when returning to the simpler command screen", async () => {
|
||
|
|
view();
|
||
|
|
await waitFor(() => expect(screen.getByRole("button", { name: "Copy command" })).toBeEnabled());
|
||
|
|
chooseOtherWay("Use a connection code instead");
|
||
|
|
await waitFor(() => expect(screen.getByRole("textbox", { name: "Connection code" })).toHaveFocus());
|
||
|
|
fireEvent.change(screen.getByRole("textbox", { name: "Connection code" }), { target: { value: "nbpc1.unfinished" } });
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "Back" }));
|
||
|
|
expect(screen.queryByRole("textbox")).toBeNull();
|
||
|
|
chooseOtherWay("Use a connection code instead");
|
||
|
|
expect(screen.getByRole("textbox", { name: "Connection code" })).toHaveValue("nbpc1.unfinished");
|
||
|
|
});
|
||
|
|
it("replaces copied guidance with recovery when the invitation expires", async () => {
|
||
|
|
const timer = vi.spyOn(window, "setTimeout");
|
||
|
|
view();
|
||
|
|
await waitFor(() => expect(screen.getByRole("button", { name: "Copy command" })).toBeEnabled());
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "Copy command" }));
|
||
|
|
await screen.findByRole("heading", { name: "Command copied" });
|
||
|
|
const expiry = timer.mock.calls.find(([, delay]) => typeof delay === "number" && delay > 599_000)?.[0];
|
||
|
|
expect(typeof expiry).toBe("function");
|
||
|
|
act(() => { if (typeof expiry === "function") expiry(); });
|
||
|
|
expect(screen.queryByRole("heading", { name: "Command copied" })).toBeNull();
|
||
|
|
expect(screen.getByText(/This request has expired/)).toBeVisible();
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "Get a new command" }));
|
||
|
|
await waitFor(() => expect(screen.getByRole("button", { name: "Copy command" })).toBeEnabled());
|
||
|
|
expect(screen.queryByText(/This request has expired/)).toBeNull();
|
||
|
|
});
|
||
|
|
it("can revisit the receipt from return-link confirmation without a disabled copy dead end", async () => {
|
||
|
|
render(<Dialog open><DialogContent><QuickPairSetup returned={{ id: "request-1", code: "nbpc1.encrypted" }} onSSH={vi.fn()} onClose={vi.fn()} /></DialogContent></Dialog>);
|
||
|
|
await screen.findByRole("heading", { name: "Open this nanobot?" });
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "Back" }));
|
||
|
|
expect(screen.getByRole("textbox", { name: "Connection code" })).toHaveValue("nbpc1.encrypted");
|
||
|
|
expect(screen.getByRole("button", { name: "Review connection" })).toBeEnabled();
|
||
|
|
expect(screen.queryByRole("button", { name: "Copy command" })).toBeNull();
|
||
|
|
expect(mocks.connect).not.toHaveBeenCalled();
|
||
|
|
});
|
||
|
|
it("prepares one command when Add server opens and keeps the SSH entry", async () => {
|
||
|
|
const ssh = vi.fn(); view(ssh);
|
||
|
|
await waitFor(() => expect(screen.getByRole("button", { name: "Copy command" })).toBeEnabled());
|
||
|
|
expect(mocks.action.mock.calls.filter(([action]) => action === "remote.pair_start")).toHaveLength(1);
|
||
|
|
expect(screen.queryByRole("textbox", { name: /Private key/ })).toBeNull();
|
||
|
|
expect(screen.queryByRole("textbox", { name: "Connection code" })).toBeNull();
|
||
|
|
chooseOtherWay("Use existing SSH settings");
|
||
|
|
expect(ssh).toHaveBeenCalledOnce();
|
||
|
|
});
|
||
|
|
it("reviews public details before storing and opening the connection", async () => {
|
||
|
|
const close = vi.fn(); view(vi.fn(), close); await reviewed();
|
||
|
|
expect(mocks.action).not.toHaveBeenCalledWith("remote.pair_finish", expect.anything(), expect.anything());
|
||
|
|
expect(screen.getByText(/full access/)).toBeVisible();
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "Connect" }));
|
||
|
|
await waitFor(() => expect(close).toHaveBeenCalledOnce());
|
||
|
|
expect(mocks.connect).toHaveBeenCalledWith("paired-server", expect.any(Function));
|
||
|
|
});
|
||
|
|
it("keeps the receipt and saved profile after a network failure, retries without re-authorizing", async () => {
|
||
|
|
mocks.connect.mockRejectedValueOnce(new Error("ssh_connection_closed"));
|
||
|
|
view(); await reviewed(); fireEvent.click(screen.getByRole("button", { name: "Connect" }));
|
||
|
|
await screen.findByRole("alert");
|
||
|
|
expect(screen.getByText(/Pairing is saved/)).toBeVisible();
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "Retry" }));
|
||
|
|
await waitFor(() => expect(mocks.connect).toHaveBeenCalledTimes(2));
|
||
|
|
expect(mocks.action.mock.calls.filter(([action]) => action === "remote.pair_finish")).toHaveLength(1);
|
||
|
|
});
|
||
|
|
it("shows an actionable invalid-code error without losing the pasted value", async () => {
|
||
|
|
mocks.action.mockImplementation(async (action: string) => {
|
||
|
|
if (action === "remote.pair_start") return request();
|
||
|
|
if (action === "remote.pair_preview") throw new Error("pair_invalid");
|
||
|
|
return {};
|
||
|
|
});
|
||
|
|
view(); await waitFor(() => expect(screen.getByRole("button", { name: "Copy command" })).toBeEnabled());
|
||
|
|
chooseOtherWay("Use a connection code instead");
|
||
|
|
fireEvent.change(await screen.findByRole("textbox", { name: "Connection code" }), { target: { value: "bad-code" } });
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "Review connection" }));
|
||
|
|
expect(await screen.findByRole("alert")).toHaveTextContent(/invalid/);
|
||
|
|
expect(screen.getByRole("textbox", { name: "Connection code" })).toHaveValue("bad-code");
|
||
|
|
});
|
||
|
|
it("cleans up an invitation returned after closing the dialog", async () => {
|
||
|
|
let resolve!: (value: ReturnType<typeof request>) => void;
|
||
|
|
mocks.action.mockImplementation((action: string) => action === "remote.pair_start" ? new Promise((done) => { resolve = done; }) : Promise.resolve({}));
|
||
|
|
const result = view();
|
||
|
|
result.unmount(); await act(async () => resolve(request()));
|
||
|
|
expect(mocks.action).toHaveBeenCalledWith("remote.pair_cancel", { id: "request-1" }, 65000);
|
||
|
|
});
|
||
|
|
it("keeps a displayed invitation when navigating away so the return link survives", async () => {
|
||
|
|
const result = view();
|
||
|
|
await waitFor(() => expect(screen.getByRole("button", { name: "Copy command" })).toBeEnabled());
|
||
|
|
result.unmount();
|
||
|
|
expect(mocks.action.mock.calls.some(([action]) => action === "remote.pair_cancel")).toBe(false);
|
||
|
|
});
|
||
|
|
it("reviews a returned receipt without generating another key or connecting automatically", async () => {
|
||
|
|
render(<Dialog open><DialogContent><QuickPairSetup returned={{ id: "request-1", code: "nbpc1.encrypted" }} onSSH={vi.fn()} onClose={vi.fn()} /></DialogContent></Dialog>);
|
||
|
|
await screen.findByRole("heading", { name: "Open this nanobot?" });
|
||
|
|
expect(mocks.action).toHaveBeenCalledWith("remote.pair_preview", { id: "request-1", code: "nbpc1.encrypted" }, 65000);
|
||
|
|
expect(mocks.action.mock.calls.some(([action]) => action === "remote.pair_start" || action === "remote.pair_finish")).toBe(false);
|
||
|
|
expect(mocks.connect).not.toHaveBeenCalled();
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "Connect" }));
|
||
|
|
await waitFor(() => expect(mocks.connect).toHaveBeenCalledOnce());
|
||
|
|
});
|
||
|
|
it("offers a fresh command when a returned link is invalid", async () => {
|
||
|
|
mocks.action.mockRejectedValue(new Error("pair_invalid"));
|
||
|
|
render(<Dialog open><DialogContent><QuickPairSetup returned={{ id: "", code: "" }} onSSH={vi.fn()} onClose={vi.fn()} /></DialogContent></Dialog>);
|
||
|
|
expect(await screen.findByRole("alert")).toHaveTextContent(/invalid/);
|
||
|
|
expect(screen.getByRole("button", { name: "Get a new command" })).toBeEnabled();
|
||
|
|
expect(screen.queryByRole("button", { name: "Copy command" })).toBeNull();
|
||
|
|
expect(mocks.connect).not.toHaveBeenCalled();
|
||
|
|
});
|
||
|
|
it("recovers from preparation failure without showing a stuck preparing state", async () => {
|
||
|
|
mocks.action.mockRejectedValueOnce(new Error("local_io_error"));
|
||
|
|
view();
|
||
|
|
await screen.findByRole("alert");
|
||
|
|
expect(screen.queryByText("Preparing your connection…")).toBeNull();
|
||
|
|
fireEvent.click(screen.getByRole("button", { name: "Get a new command" }));
|
||
|
|
await waitFor(() => expect(screen.getByRole("button", { name: "Copy command" })).toBeEnabled());
|
||
|
|
expect(screen.queryByRole("alert")).toBeNull();
|
||
|
|
});
|
||
|
|
});
|