|
| 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 { crewOpened, openBot, renderApp } from "../../test/app"; |
| 5 | + |
| 6 | +afterEach(cleanup); |
| 7 | + |
| 8 | +async function setup() { |
| 9 | + const fake = new FakeBotloft(); |
| 10 | + const ops = fake.addCrew("Ops"); |
| 11 | + const scout = fake.addBot(ops.id, "Scout", "Finds sources"); |
| 12 | + const writer = fake.addBot(ops.id, "Writer", "Writes"); |
| 13 | + fake.setBotState(scout.id, "idle"); |
| 14 | + const said = fake.chat.reply(scout.id, "Acme signs monthly.\n\nDana approves."); |
| 15 | + fake.chat.turn(scout.id); |
| 16 | + fake.conversation.say({ from: writer.id, to: scout.id, body: "The draft is ready." }); |
| 17 | + renderApp(fake); |
| 18 | + await crewOpened("Ops"); |
| 19 | + openBot("Scout"); |
| 20 | + await screen.findByRole("list", { name: "Messages" }); |
| 21 | + return { fake, scout, said }; |
| 22 | +} |
| 23 | + |
| 24 | +const field = () => screen.getByRole("textbox", { name: "Message to Scout" }); |
| 25 | +const sent = (fake: FakeBotloft) => |
| 26 | + fake.calls.filter((call) => call.method === "messages.send").map((call) => call.params); |
| 27 | + |
| 28 | +describe("replying to a message", () => { |
| 29 | + test("quotes the bot's reply over the composer, sends it, and shows it on the bubble", async () => { |
| 30 | + const { fake, scout, said } = await setup(); |
| 31 | + fireEvent.click(screen.getByRole("button", { name: "Reply to Scout" })); |
| 32 | + expect(screen.getByText("Replying to Scout")).toBeDefined(); |
| 33 | + expect(document.activeElement).toBe(field()); |
| 34 | + |
| 35 | + fireEvent.change(field(), { target: { value: "Make it yearly" } }); |
| 36 | + fireEvent.keyDown(field(), { key: "Enter" }); |
| 37 | + await waitFor(() => |
| 38 | + expect(sent(fake)).toEqual([{ botId: scout.id, body: "Make it yearly", replyTo: said.id }]), |
| 39 | + ); |
| 40 | + // The bar leaves with the message, and the bubble keeps the quote. |
| 41 | + await waitFor(() => expect(screen.queryByText("Replying to Scout")).toBeNull()); |
| 42 | + const quote = await screen.findByRole("button", { name: "Acme signs monthly. Dana approves." }); |
| 43 | + expect(quote.title).toBe("Show what this replies to"); |
| 44 | + }); |
| 45 | + |
| 46 | + test("replies to another bot's message too, and Esc or X cancels", async () => { |
| 47 | + const { fake } = await setup(); |
| 48 | + fireEvent.click(screen.getByRole("button", { name: "Reply to Writer" })); |
| 49 | + const bar = screen.getByText("Replying to Writer").parentElement as HTMLElement; |
| 50 | + expect(within(bar).getByText("The draft is ready.")).toBeDefined(); |
| 51 | + fireEvent.keyDown(field(), { key: "Escape" }); |
| 52 | + expect(screen.queryByText("Replying to Writer")).toBeNull(); |
| 53 | + |
| 54 | + fireEvent.click(screen.getByRole("button", { name: "Reply to Writer" })); |
| 55 | + fireEvent.click(screen.getByRole("button", { name: "Cancel the reply" })); |
| 56 | + fireEvent.change(field(), { target: { value: "Plain" } }); |
| 57 | + fireEvent.keyDown(field(), { key: "Enter" }); |
| 58 | + await waitFor(() => expect(sent(fake).at(-1)).not.toHaveProperty("replyTo")); |
| 59 | + }); |
| 60 | +}); |
0 commit comments