|
| 1 | +import { cleanup, fireEvent, screen, waitFor, within } from "@testing-library/react"; |
| 2 | +import { afterEach, describe, expect, test } from "vitest"; |
| 3 | +import { FakeBotloft } from "../../lib/fake"; |
| 4 | +import { FAKE_CATALOG } from "../../lib/fakeCatalog"; |
| 5 | +import { RpcError } from "../../lib/rpc"; |
| 6 | +import { crewOpened, renderApp, sidebar } from "../../test/app"; |
| 7 | +import { CREW_TEMPLATES } from "./crewTemplates"; |
| 8 | + |
| 9 | +afterEach(cleanup); |
| 10 | + |
| 11 | +/** The app with one crew open, and the template step of "New crew" showing. */ |
| 12 | +async function openTemplates() { |
| 13 | + const fake = new FakeBotloft(); |
| 14 | + fake.addCrew("Ops"); |
| 15 | + renderApp(fake); |
| 16 | + await crewOpened("Ops"); |
| 17 | + fireEvent.click(screen.getByRole("button", { name: "New crew" })); |
| 18 | + return { fake, chooser: await screen.findByRole("dialog", { name: "Start from a template" }) }; |
| 19 | +} |
| 20 | + |
| 21 | +describe("the crew templates", () => { |
| 22 | + test("every role of every template is a role of the catalog, and a crew has room for it", () => { |
| 23 | + const known = new Set(FAKE_CATALOG.map((role) => role.id)); |
| 24 | + for (const template of CREW_TEMPLATES) { |
| 25 | + for (const id of template.roles) { |
| 26 | + expect(known.has(id), `${template.id}: ${id}`).toBe(true); |
| 27 | + } |
| 28 | + expect(new Set(template.roles).size).toBe(template.roles.length); |
| 29 | + // The chief takes one of the 12 places of a crew. |
| 30 | + expect(template.roles.length).toBeLessThanOrEqual(11); |
| 31 | + } |
| 32 | + expect(new Set(CREW_TEMPLATES.map((template) => template.id)).size).toBe(CREW_TEMPLATES.length); |
| 33 | + }); |
| 34 | + |
| 35 | + test("the first step lists the templates and an empty crew", async () => { |
| 36 | + const { chooser } = await openTemplates(); |
| 37 | + expect(within(chooser).getAllByRole("listitem")).toHaveLength(CREW_TEMPLATES.length); |
| 38 | + expect(within(chooser).getByRole("button", { name: /Software team/ })).toBeDefined(); |
| 39 | + expect(within(chooser).getByRole("button", { name: /Start with an empty crew/ })).toBeDefined(); |
| 40 | + }); |
| 41 | + |
| 42 | + test("picking a template fills the form and says which bots it adds", async () => { |
| 43 | + const { chooser } = await openTemplates(); |
| 44 | + fireEvent.click(within(chooser).getByRole("button", { name: /Game studio/ })); |
| 45 | + |
| 46 | + const dialog = screen.getByRole("dialog", { name: "New crew" }); |
| 47 | + expect((within(dialog).getByLabelText("Name") as HTMLInputElement).value).toBe("Game studio"); |
| 48 | + expect( |
| 49 | + (within(dialog).getByLabelText("What is this crew for?") as HTMLTextAreaElement).value, |
| 50 | + ).toContain("Design my game"); |
| 51 | + expect( |
| 52 | + within(dialog).getByText(/Game Designer, Narrative Designer, Level Designer/), |
| 53 | + ).toBeDefined(); |
| 54 | + |
| 55 | + fireEvent.click(within(dialog).getByRole("button", { name: "Pick another template" })); |
| 56 | + expect(screen.getByRole("dialog", { name: "Start from a template" })).toBeDefined(); |
| 57 | + }); |
| 58 | + |
| 59 | + test("creating from a template makes the crew with its chief and then each bot", async () => { |
| 60 | + const { fake, chooser } = await openTemplates(); |
| 61 | + fireEvent.click(within(chooser).getByRole("button", { name: /Online store/ })); |
| 62 | + const dialog = screen.getByRole("dialog", { name: "New crew" }); |
| 63 | + fireEvent.click(within(dialog).getByRole("button", { name: "Create crew" })); |
| 64 | + |
| 65 | + await crewOpened("Online store"); |
| 66 | + const template = CREW_TEMPLATES.find((item) => item.id === "online-store"); |
| 67 | + await waitFor(() => |
| 68 | + expect(fake.calls.filter((call) => call.method === "catalog.add")).toHaveLength( |
| 69 | + template?.roles.length ?? -1, |
| 70 | + ), |
| 71 | + ); |
| 72 | + expect( |
| 73 | + fake.calls.filter((call) => call.method === "catalog.add").map((call) => call.params), |
| 74 | + ).toEqual((template?.roles ?? []).map((id) => expect.objectContaining({ templateId: id }))); |
| 75 | + const create = fake.calls.find((call) => call.method === "crews.create"); |
| 76 | + expect(create?.params).toMatchObject({ name: "Online store", lead: { name: "Chief" } }); |
| 77 | + expect(await within(sidebar()).findByRole("button", { name: /^Bookkeeper/ })).toBeDefined(); |
| 78 | + }); |
| 79 | + |
| 80 | + test("a bot that cannot be added is told, and the crew stays", async () => { |
| 81 | + const { fake, chooser } = await openTemplates(); |
| 82 | + fireEvent.click(within(chooser).getByRole("button", { name: /Research desk/ })); |
| 83 | + fake.failNext("catalog.add", new RpcError(-32603, "no room")); |
| 84 | + fireEvent.click( |
| 85 | + within(screen.getByRole("dialog", { name: "New crew" })).getByRole("button", { |
| 86 | + name: "Create crew", |
| 87 | + }), |
| 88 | + ); |
| 89 | + |
| 90 | + expect(await screen.findByText(/1 bot could not be added/)).toBeDefined(); |
| 91 | + expect(fake.calls.filter((call) => call.method === "catalog.add")).toHaveLength(6); |
| 92 | + expect(await screen.findByRole("heading", { level: 1, name: "Chief" })).toBeDefined(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments