|
| 1 | +import { act, cleanup, fireEvent, screen, waitFor, within } from "@testing-library/react"; |
| 2 | +import { afterEach, describe, expect, test } from "vitest"; |
| 3 | +import { setLocaleChoice } from "../../i18n"; |
| 4 | +import { FakeBotloft } from "../../lib/fake"; |
| 5 | +import { crewOpened, openBot, renderApp } from "../../test/app"; |
| 6 | + |
| 7 | +afterEach(() => { |
| 8 | + act(() => setLocaleChoice("en")); |
| 9 | + cleanup(); |
| 10 | +}); |
| 11 | + |
| 12 | +const NOTEPAD = "C:\\Windows\\notepad.exe"; |
| 13 | + |
| 14 | +async function openScout() { |
| 15 | + const fake = new FakeBotloft(); |
| 16 | + const ops = fake.addCrew("Ops"); |
| 17 | + const scout = fake.addBot(ops.id, "Scout", "Finds sources"); |
| 18 | + fake.setBotState(scout.id, "idle"); |
| 19 | + renderApp(fake); |
| 20 | + await crewOpened("Ops"); |
| 21 | + openBot("Scout"); |
| 22 | + await screen.findByRole("list", { name: "Messages" }); |
| 23 | + return { fake, scout }; |
| 24 | +} |
| 25 | + |
| 26 | +const details = () => screen.getByRole("complementary", { name: "About Scout" }); |
| 27 | + |
| 28 | +describe("the real mouse and keyboard", () => { |
| 29 | + test("are off, and turning them on says first what changes", async () => { |
| 30 | + const { fake, scout } = await openScout(); |
| 31 | + act(() => { |
| 32 | + fake.desktop.grant(scout.id, NOTEPAD, "Notepad", "act"); |
| 33 | + }); |
| 34 | + fireEvent.click(screen.getByRole("button", { name: /^Show details/ })); |
| 35 | + const toggle = await within(details()).findByRole("switch", { |
| 36 | + name: "Real mouse and keyboard: Notepad", |
| 37 | + }); |
| 38 | + expect(toggle.getAttribute("aria-checked")).toBe("false"); |
| 39 | + |
| 40 | + fireEvent.click(toggle); |
| 41 | + const dialog = screen.getByRole("dialog", { |
| 42 | + name: "Let Scout use your real mouse and keyboard in Notepad?", |
| 43 | + }); |
| 44 | + expect(within(dialog).getByText(/Move the mouse or press a key and it stops/)).toBeDefined(); |
| 45 | + expect(within(dialog).getByText(/Ctrl\+Alt\+Esc/)).toBeDefined(); |
| 46 | + fireEvent.click(within(dialog).getByRole("button", { name: "Cancel" })); |
| 47 | + expect(fake.desktop.grants[0]?.realInput).toBe(false); |
| 48 | + |
| 49 | + fireEvent.click(toggle); |
| 50 | + fireEvent.click(screen.getByRole("button", { name: "Turn on" })); |
| 51 | + await waitFor(() => expect(fake.desktop.grants[0]?.realInput).toBe(true)); |
| 52 | + expect(toggle.getAttribute("aria-checked")).toBe("true"); |
| 53 | + |
| 54 | + // Off at once, without asking. |
| 55 | + fireEvent.click(toggle); |
| 56 | + await waitFor(() => expect(fake.desktop.grants[0]?.realInput).toBe(false)); |
| 57 | + }); |
| 58 | + |
| 59 | + test("are not offered where the bot may only see", async () => { |
| 60 | + const { fake, scout } = await openScout(); |
| 61 | + act(() => { |
| 62 | + fake.desktop.grant(scout.id, NOTEPAD, "Notepad"); |
| 63 | + }); |
| 64 | + fireEvent.click(screen.getByRole("button", { name: /^Show details/ })); |
| 65 | + await within(details()).findByText("Can see"); |
| 66 | + expect(within(details()).queryByRole("switch", { name: /Real mouse/ })).toBeNull(); |
| 67 | + }); |
| 68 | + |
| 69 | + test("the daemon hears the owner's language when it changes, for its notice", async () => { |
| 70 | + const { fake } = await openScout(); |
| 71 | + act(() => setLocaleChoice("pt-BR")); |
| 72 | + await waitFor(() => expect(fake.locale).toBe("pt-BR")); |
| 73 | + }); |
| 74 | +}); |
0 commit comments