import { render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { afterEach, expect, it, vi } from "vitest"; import { serve } from "../test/http.ts"; import { AliasField, wordsOf } from "./commands-aliases.tsx"; afterEach(() => { vi.unstubAllGlobals(); }); const RELOADED = { commands: 28, name: "cat", status: "reloaded", triggers: ["!kotek", "!kicia"] }; function field(initial: string[] = []): void { render(); } it("splits on commas and whitespace alike, and keeps no empty word", () => { expect(wordsOf("!kotek, !kicia")).toEqual(["!kotek", "!kicia"]); expect(wordsOf(" kotek kicia ,, ")).toEqual(["kotek", "kicia"]); expect(wordsOf("")).toEqual([]); }); it("shows the words the command already has, on one line", () => { field(["!kotek", "!kicia"]); expect(screen.getByLabelText("!kot aliases")).toHaveValue("!kotek, !kicia"); }); it("sends the words typed and shows the line the bot took, with its count", async () => { const fetcher = serve({ "/api/aliases/cat": { body: RELOADED } }); field(); await userEvent.type(screen.getByLabelText("!kot aliases"), "kotek kicia"); await userEvent.click(screen.getByRole("button", { name: "Aliases !kot" })); await waitFor(() => { expect(screen.getByTestId("aliases-note-cat")).toHaveTextContent("reloaded, 28 commands"); }); const [url, init] = fetcher.mock.calls[0] as [string, RequestInit]; expect(url).toBe("/api/aliases/cat"); expect(init.method).toBe("PUT"); expect(JSON.parse(init.body as string)).toEqual({ triggers: ["kotek", "kicia"] }); // The bot's spelling wins: the "!" it added is what the field shows now. expect(screen.getByLabelText("!kot aliases")).toHaveValue("!kotek, !kicia"); }); it("escapes a command name with a colon in it, as the pack's have", async () => { const fetcher = serve({ "/api/aliases/pack%3Ap1": { body: { ...RELOADED, name: "pack:p1" } } }); render(); await userEvent.click(screen.getByRole("button", { name: "Aliases !alfa" })); await waitFor(() => { expect(screen.getByTestId("aliases-note-pack:p1")).toHaveTextContent("reloaded"); }); const [url] = fetcher.mock.calls[0] as [string]; expect(url).toBe("/api/aliases/pack%3Ap1"); }); it("shows the bot's own sentence when the reload is refused", async () => { serve({ "/api/aliases/cat": { body: { detail: "trigger '!pies' is taken" }, status: 409 } }); field(); await userEvent.type(screen.getByLabelText("!kot aliases"), "pies"); await userEvent.click(screen.getByRole("button", { name: "Aliases !kot" })); await waitFor(() => { expect(screen.getByTestId("aliases-note-cat")).toHaveTextContent("trigger '!pies' is taken"); }); // Refused means not kept -- the field still says what was typed, so it can be fixed. expect(screen.getByLabelText("!kot aliases")).toHaveValue("pies"); }); it("says 'could not save' when the failure is not the server's", async () => { vi.stubGlobal("fetch", vi.fn(() => Promise.reject(new TypeError("offline")))); field(); await userEvent.click(screen.getByRole("button", { name: "Aliases !kot" })); await waitFor(() => { expect(screen.getByTestId("aliases-note-cat")).toHaveTextContent("could not save"); }); });