feat(settings): add browser setup and administration (#10)
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { PasswordForm } from "../PasswordForm";
|
||||
import { SetupScreen } from "../SetupScreen";
|
||||
import { SettingsPage } from "../SettingsPage";
|
||||
import { HeaderNavigation } from "../HeaderNavigation";
|
||||
import { settingsApi } from "../settingsApi";
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
function fill(label: string, value: string) {
|
||||
fireEvent.change(screen.getByLabelText(label), { target: { value } });
|
||||
}
|
||||
it("validates confirmation and clears credentials after setup", async () => {
|
||||
const submit = vi.fn().mockResolvedValue(undefined);
|
||||
render(<PasswordForm setup submit={submit} />);
|
||||
fill("Setup token", "token");
|
||||
fill("New password", "long password here");
|
||||
fill("Confirm password", "wrong password here");
|
||||
fireEvent.click(screen.getByRole("button", { name: "Set up ShellBook" }));
|
||||
expect(await screen.findByText("Passwords do not match")).toBeInTheDocument();
|
||||
expect(submit).not.toHaveBeenCalled();
|
||||
fill("Confirm password", "long password here");
|
||||
fireEvent.click(screen.getByRole("button", { name: "Set up ShellBook" }));
|
||||
await waitFor(() =>
|
||||
expect(submit).toHaveBeenCalledWith({
|
||||
token: "token",
|
||||
currentPassword: "",
|
||||
newPassword: "long password here",
|
||||
confirmPassword: "long password here",
|
||||
}),
|
||||
);
|
||||
await waitFor(() =>
|
||||
expect(screen.getByLabelText("New password")).toHaveValue(""),
|
||||
);
|
||||
});
|
||||
it("submits setup and enters the app", async () => {
|
||||
vi.spyOn(settingsApi, "setup").mockResolvedValue();
|
||||
const done = vi.fn();
|
||||
render(<SetupScreen onComplete={done} />);
|
||||
fill("Setup token", "token");
|
||||
fill("New password", "long password here");
|
||||
fill("Confirm password", "long password here");
|
||||
fireEvent.click(screen.getByRole("button", { name: "Set up ShellBook" }));
|
||||
await waitFor(() => expect(done).toHaveBeenCalledOnce());
|
||||
});
|
||||
it("shows password failure and preserves the form for retry", async () => {
|
||||
render(
|
||||
<PasswordForm submit={vi.fn().mockRejectedValue(new Error("invalid"))} />,
|
||||
);
|
||||
fill("Current password", "wrong");
|
||||
fill("New password", "long password here");
|
||||
fill("Confirm password", "long password here");
|
||||
fireEvent.click(screen.getByRole("button", { name: "Change password" }));
|
||||
expect(await screen.findByRole("status")).toHaveTextContent("Unable to save");
|
||||
});
|
||||
it("renders system/session/Claude information and opens the terminal", async () => {
|
||||
vi.spyOn(settingsApi, "system").mockResolvedValue({
|
||||
version: "1.2",
|
||||
workspacePath: "/workspace",
|
||||
dataDirPath: "/data",
|
||||
nodeVersion: "v22",
|
||||
});
|
||||
vi.spyOn(settingsApi, "session").mockResolvedValue({
|
||||
lifetimeSeconds: 86400,
|
||||
});
|
||||
vi.spyOn(settingsApi, "claude").mockResolvedValue({
|
||||
available: true,
|
||||
version: "Claude 1",
|
||||
loginAppearsPresent: true,
|
||||
});
|
||||
const revoke = vi.spyOn(settingsApi, "revoke").mockResolvedValue();
|
||||
const open = vi.fn();
|
||||
render(<SettingsPage openTerminal={open} />);
|
||||
expect(await screen.findByText("/workspace")).toBeInTheDocument();
|
||||
expect(screen.getByText(/86400 seconds/)).toBeInTheDocument();
|
||||
expect(screen.getByText("Available: Claude 1")).toBeInTheDocument();
|
||||
fireEvent.click(screen.getByText("Open terminal to log in"));
|
||||
expect(open).toHaveBeenCalledOnce();
|
||||
fireEvent.click(screen.getByText("Sign out all sessions"));
|
||||
await waitFor(() => expect(revoke).toHaveBeenCalledOnce());
|
||||
expect(await screen.findByRole("status")).toHaveTextContent(
|
||||
"This browser remains signed in",
|
||||
);
|
||||
});
|
||||
it("provides the shared feature navigation", () => {
|
||||
const change = vi.fn();
|
||||
render(<HeaderNavigation page="Notebooks" onChange={change} />);
|
||||
fireEvent.click(screen.getByText("Settings"));
|
||||
expect(change).toHaveBeenCalledWith("Settings");
|
||||
expect(screen.getByText("Hosts")).toBeInTheDocument();
|
||||
expect(screen.getByText("Notebooks")).toHaveAttribute("aria-current", "page");
|
||||
});
|
||||
Reference in New Issue
Block a user