|
| 1 | +import { act, 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 | +/** A crew "Ops" with @scout, its browser open on a sign-in page, watched. */ |
| 9 | +async function openScout() { |
| 10 | + const fake = new FakeBotloft(); |
| 11 | + const ops = fake.addCrew("Ops"); |
| 12 | + const scout = fake.addBot(ops.id, "Scout", "Finds sources"); |
| 13 | + fake.setBotState(scout.id, "busy"); |
| 14 | + fake.browser.open(scout.id, "https://accounts.google.com/", "Sign in"); |
| 15 | + renderApp(fake); |
| 16 | + await crewOpened("Ops"); |
| 17 | + openBot("Scout"); |
| 18 | + await screen.findByRole("list", { name: "Messages" }); |
| 19 | + fireEvent.click(screen.getByRole("button", { name: /^Show browser/ })); |
| 20 | + await waitFor(() => expect(fake.browser.watching).toBe(scout.id)); |
| 21 | + act(() => { |
| 22 | + fake.browser.frame(scout.id, "AAAA"); |
| 23 | + }); |
| 24 | + return { fake, scout }; |
| 25 | +} |
| 26 | + |
| 27 | +const panel = () => screen.getByRole("complementary", { name: "Scout's browser" }); |
| 28 | + |
| 29 | +describe("the browser in a window of its own", () => { |
| 30 | + test("opens for signing in, says the bot waits, and goes back when the window closes", async () => { |
| 31 | + const { fake, scout } = await openScout(); |
| 32 | + expect(within(panel()).getByText(/like Google/)).toBeDefined(); |
| 33 | + fireEvent.click(within(panel()).getByRole("button", { name: "Sign in in a window" })); |
| 34 | + |
| 35 | + expect(await within(panel()).findAllByText("Open in a window")).not.toHaveLength(0); |
| 36 | + expect(within(panel()).getByText(/Scout waits meanwhile and keeps the login/)).toBeDefined(); |
| 37 | + expect( |
| 38 | + fake.calls.filter((call) => call.method === "browser.window").map((call) => call.params), |
| 39 | + ).toEqual([{ botId: scout.id }]); |
| 40 | + expect(within(panel()).queryByRole("button", { name: "Take control" })).toBeNull(); |
| 41 | + |
| 42 | + act(() => { |
| 43 | + fake.browser.closeWindow(scout.id); |
| 44 | + }); |
| 45 | + await waitFor(() => expect(within(panel()).queryByText(/keeps the login/)).toBeNull()); |
| 46 | + }); |
| 47 | + |
| 48 | + test("is offered when the bot asks for a hand", async () => { |
| 49 | + const { fake, scout } = await openScout(); |
| 50 | + act(() => { |
| 51 | + fake.browser.ask(scout.id, "Sign in to your Google account"); |
| 52 | + }); |
| 53 | + const asking = await within(panel()).findByText("Sign in to your Google account"); |
| 54 | + expect(asking).toBeDefined(); |
| 55 | + fireEvent.click(within(panel()).getByRole("button", { name: "Sign in in a window" })); |
| 56 | + expect(await within(panel()).findByText(/keeps the login/)).toBeDefined(); |
| 57 | + |
| 58 | + // Closing the window is the owner being done. |
| 59 | + act(() => { |
| 60 | + fake.browser.closeWindow(scout.id); |
| 61 | + }); |
| 62 | + expect(await screen.findByText("You did it: Sign in to your Google account")).toBeDefined(); |
| 63 | + }); |
| 64 | +}); |
0 commit comments