|
| 1 | +import { act, cleanup, fireEvent, screen, 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 openScout() { |
| 9 | + const fake = new FakeBotloft(); |
| 10 | + const ops = fake.addCrew("Ops"); |
| 11 | + const scout = fake.addBot(ops.id, "Scout", "Finds sources"); |
| 12 | + fake.setBotState(scout.id, "idle"); |
| 13 | + renderApp(fake); |
| 14 | + await crewOpened("Ops"); |
| 15 | + openBot("Scout"); |
| 16 | + await screen.findByRole("list", { name: "Messages" }); |
| 17 | + return { fake, scout }; |
| 18 | +} |
| 19 | + |
| 20 | +const details = () => screen.getByRole("complementary", { name: "About Scout" }); |
| 21 | + |
| 22 | +describe("Allow always", () => { |
| 23 | + test("allows the request for good and lists it in the bot's details, where it can go", async () => { |
| 24 | + const { fake, scout } = await openScout(); |
| 25 | + act(() => { |
| 26 | + fake.chat.ask(scout.id, "Bash", "git status", '{"command":"git status"}'); |
| 27 | + }); |
| 28 | + const card = screen.getByRole("region", { name: "Scout asks to run a command" }); |
| 29 | + const always = within(card).getByRole("button", { name: "Always allow this command" }); |
| 30 | + expect(always.getAttribute("title")).toBe( |
| 31 | + "Scout will not ask for this again. You can undo it in Scout's details.", |
| 32 | + ); |
| 33 | + fireEvent.click(always); |
| 34 | + expect(await screen.findByText("Allowed: run a command")).toBeDefined(); |
| 35 | + expect(fake.calls.find((call) => call.method === "approvals.answer")?.params).toMatchObject({ |
| 36 | + allow: true, |
| 37 | + always: true, |
| 38 | + }); |
| 39 | + |
| 40 | + fireEvent.click(screen.getByRole("button", { name: /^Show details/ })); |
| 41 | + const rule = await within(details()).findByText("git status"); |
| 42 | + expect(rule).toBeDefined(); |
| 43 | + fireEvent.click( |
| 44 | + within(details()).getByRole("button", { name: "Ask again: Run a command: git status" }), |
| 45 | + ); |
| 46 | + expect( |
| 47 | + await within(details()).findByText( |
| 48 | + 'Nothing yet. When Scout asks for something, "Always allow" puts it here.', |
| 49 | + ), |
| 50 | + ).toBeDefined(); |
| 51 | + }); |
| 52 | + |
| 53 | + test("names the site, and is not offered for what cannot be allowed for good", async () => { |
| 54 | + const { fake, scout } = await openScout(); |
| 55 | + act(() => { |
| 56 | + fake.chat.ask( |
| 57 | + scout.id, |
| 58 | + "WebFetch", |
| 59 | + "https://www.example.com/a", |
| 60 | + '{"url":"https://www.example.com/a"}', |
| 61 | + ); |
| 62 | + }); |
| 63 | + const page = screen.getByRole("region", { name: /^Scout asks to/ }); |
| 64 | + expect(within(page).getByRole("button", { name: "Always allow example.com" })).toBeDefined(); |
| 65 | + fireEvent.click(within(page).getByRole("button", { name: "Allow" })); |
| 66 | + await screen.findByText(/^Allowed:/); |
| 67 | + // A plain Allow keeps no rule. |
| 68 | + expect(fake.allow.rules).toEqual([]); |
| 69 | + |
| 70 | + act(() => { |
| 71 | + fake.chat.ask(scout.id, "Glob", "*.md", '{"pattern":"*.md"}'); |
| 72 | + }); |
| 73 | + const glob = screen.getByRole("region", { name: /^Scout asks to/ }); |
| 74 | + expect(within(glob).queryByRole("button", { name: /^Always allow/ })).toBeNull(); |
| 75 | + }); |
| 76 | +}); |
0 commit comments