235 lines
7.6 KiB
TypeScript
235 lines
7.6 KiB
TypeScript
import { fireEvent, render, screen, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { beforeAll, describe, expect, it, vi } from "vitest";
|
|
import { Workbench } from "../../src/components/Workbench";
|
|
|
|
class WorkerStub {
|
|
addEventListener() {}
|
|
postMessage() {}
|
|
terminate() {}
|
|
}
|
|
|
|
beforeAll(() => {
|
|
vi.stubGlobal("Worker", WorkerStub);
|
|
});
|
|
|
|
describe("Sudoku workbench", () => {
|
|
it("opens with the local classic example rendered as an accessible grid", () => {
|
|
render(<Workbench />);
|
|
|
|
expect(
|
|
screen.getByRole("heading", { name: "A first classic" }),
|
|
).toBeInTheDocument();
|
|
const grid = screen.getByRole("grid", { name: "9 by 9 Sudoku grid" });
|
|
expect(within(grid).getAllByRole("gridcell")).toHaveLength(81);
|
|
expect(
|
|
within(grid).getByRole("gridcell", {
|
|
name: "Row 1, column 1, 4",
|
|
}),
|
|
).toHaveClass("is-given");
|
|
expect(
|
|
within(grid).getByRole("gridcell", {
|
|
name: "Row 1, column 3, empty",
|
|
}),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
it("enters a digit in an empty cell and restores it with undo", async () => {
|
|
const user = userEvent.setup();
|
|
render(<Workbench />);
|
|
|
|
const emptyCell = screen.getByRole("gridcell", {
|
|
name: "Row 1, column 3, empty",
|
|
});
|
|
fireEvent.pointerDown(emptyCell, { buttons: 1 });
|
|
await user.click(
|
|
within(screen.getByRole("group", { name: "Digits" })).getByRole(
|
|
"button",
|
|
{ name: "2" },
|
|
),
|
|
);
|
|
|
|
expect(
|
|
screen.getByRole("gridcell", { name: "Row 1, column 3, 2" }),
|
|
).toBeInTheDocument();
|
|
const undo = screen.getByRole("button", { name: "Undo" });
|
|
expect(undo).toBeEnabled();
|
|
await user.click(undo);
|
|
expect(
|
|
screen.getByRole("gridcell", {
|
|
name: "Row 1, column 3, empty",
|
|
}),
|
|
).toBeInTheDocument();
|
|
expect(undo).toBeDisabled();
|
|
});
|
|
|
|
it("shows digit progress and toggles matching-digit highlights separately from selection", async () => {
|
|
const user = userEvent.setup();
|
|
render(<Workbench />);
|
|
|
|
const completion = screen.getByRole("group", {
|
|
name: "Highlight matching digits",
|
|
});
|
|
expect(within(completion).getAllByRole("button")).toHaveLength(9);
|
|
|
|
const grid = screen.getByRole("grid", { name: "9 by 9 Sudoku grid" });
|
|
const four = within(grid).getByRole("gridcell", {
|
|
name: "Row 1, column 1, 4",
|
|
});
|
|
fireEvent.pointerDown(four, { buttons: 1, ctrlKey: true });
|
|
|
|
const highlighted = grid.querySelectorAll(".is-digit-highlighted");
|
|
expect(highlighted.length).toBeGreaterThan(1);
|
|
expect(four).toHaveAttribute("aria-selected", "true");
|
|
|
|
fireEvent.pointerDown(four, { buttons: 1, ctrlKey: true });
|
|
expect(grid.querySelectorAll(".is-digit-highlighted")).toHaveLength(0);
|
|
|
|
await user.click(screen.getByLabelText("Show digit completion bar"));
|
|
expect(
|
|
screen.queryByRole("group", { name: "Highlight matching digits" }),
|
|
).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("replaces and removes setter cages through the selected cells", async () => {
|
|
const user = userEvent.setup();
|
|
render(<Workbench />);
|
|
|
|
await user.click(screen.getByRole("button", { name: "New blank" }));
|
|
const grid = screen.getByRole("grid", { name: "9 by 9 Sudoku grid" });
|
|
fireEvent.pointerDown(
|
|
within(grid).getByRole("gridcell", {
|
|
name: "Row 1, column 2, empty",
|
|
}),
|
|
{ buttons: 1, shiftKey: true },
|
|
);
|
|
|
|
const cageSum = screen.getByRole("spinbutton", { name: "Cage sum" });
|
|
await user.clear(cageSum);
|
|
await user.type(cageSum, "3");
|
|
await user.click(
|
|
screen.getByRole("button", { name: "Add / replace cage" }),
|
|
);
|
|
expect(screen.getByText("3 cage · 2 cells")).toBeInTheDocument();
|
|
|
|
await user.clear(cageSum);
|
|
await user.type(cageSum, "4");
|
|
await user.click(
|
|
screen.getByRole("button", { name: "Add / replace cage" }),
|
|
);
|
|
expect(screen.queryByText("3 cage · 2 cells")).not.toBeInTheDocument();
|
|
expect(screen.getByText("4 cage · 2 cells")).toBeInTheDocument();
|
|
|
|
await user.click(
|
|
screen.getByRole("button", { name: "Remove selected cage" }),
|
|
);
|
|
expect(screen.queryByText("4 cage · 2 cells")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("opens the expanded built-in variant examples", async () => {
|
|
const user = userEvent.setup();
|
|
const { container } = render(<Workbench />);
|
|
|
|
await user.selectOptions(
|
|
screen.getByLabelText("Open built-in puzzle"),
|
|
"thermo",
|
|
);
|
|
expect(
|
|
screen.getByRole("heading", { name: "Warm fronts" }),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.getByRole("grid", { name: "6 by 6 Sudoku grid" }),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
container.querySelectorAll(".constraint-thermo").length,
|
|
).toBeGreaterThan(0);
|
|
|
|
await user.selectOptions(
|
|
screen.getByLabelText("Open built-in puzzle"),
|
|
"xv",
|
|
);
|
|
expect(
|
|
screen.getByRole("heading", { name: "Five or ten" }),
|
|
).toBeInTheDocument();
|
|
expect(container.querySelector(".constraint-xv--5 text")).toHaveTextContent(
|
|
"5",
|
|
);
|
|
expect(
|
|
container.querySelector(".constraint-xv--10 text"),
|
|
).toHaveTextContent("10");
|
|
});
|
|
|
|
it("switches between setting, solving, playing and helper workspaces", async () => {
|
|
const user = userEvent.setup();
|
|
render(<Workbench />);
|
|
|
|
await user.click(screen.getByRole("button", { name: "Set" }));
|
|
expect(
|
|
screen.getByRole("heading", { name: "Set a puzzle" }),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.getByRole("heading", { name: "Enter givens" }),
|
|
).toBeInTheDocument();
|
|
|
|
await user.click(screen.getByRole("button", { name: "Generate" }));
|
|
expect(
|
|
screen.getByRole("heading", { name: "Sudoku generator" }),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.getByRole("button", { name: "Rate current puzzle" }),
|
|
).toBeInTheDocument();
|
|
|
|
await user.click(screen.getByRole("button", { name: "Solve" }));
|
|
expect(
|
|
screen.getByRole("heading", { name: "Solve and verify" }),
|
|
).toBeInTheDocument();
|
|
|
|
await user.click(screen.getByRole("button", { name: "Helpers" }));
|
|
expect(
|
|
screen.getByRole("heading", { name: "Sudoku helpers" }),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.getByRole("tab", { name: "Killer combinations" }),
|
|
).toHaveAttribute("aria-selected", "true");
|
|
expect(document.querySelector(".helper-result")).toHaveTextContent(
|
|
"combinations",
|
|
);
|
|
|
|
await user.click(screen.getByRole("tab", { name: "45-rule residual" }));
|
|
expect(
|
|
screen.getByLabelText("Accounted values or cage sums"),
|
|
).toBeInTheDocument();
|
|
|
|
await user.click(screen.getByRole("button", { name: "Play" }));
|
|
expect(
|
|
screen.getByRole("heading", { name: "Enter your solve" }),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
it("makes the import boundary and self-contained local sharing explicit", async () => {
|
|
const user = userEvent.setup();
|
|
const fetchSpy = vi.fn();
|
|
vi.stubGlobal("fetch", fetchSpy);
|
|
render(<Workbench />);
|
|
|
|
await user.click(screen.getByRole("button", { name: "Import / export" }));
|
|
expect(
|
|
screen.getByRole("heading", { name: "Import and export" }),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.getByText(/Server-only short IDs are never fetched\./u),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.getByText(/Share links are self-contained\./u),
|
|
).toBeInTheDocument();
|
|
|
|
await user.click(
|
|
screen.getByRole("button", { name: "Copy local share URL" }),
|
|
);
|
|
expect(
|
|
await screen.findByText("Local share URL copied."),
|
|
).toBeInTheDocument();
|
|
expect(fetchSpy).not.toHaveBeenCalled();
|
|
});
|
|
});
|