|
| 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 type { ToolItem } from "../../lib/protocol.gen"; |
| 5 | +import { crewOpened, openBot, renderApp } from "../../test/app"; |
| 6 | +import { readImagePath } from "./readImage"; |
| 7 | + |
| 8 | +afterEach(cleanup); |
| 9 | + |
| 10 | +const read = (input: string, name = "Read") => ({ name, input }) as ToolItem; |
| 11 | + |
| 12 | +describe("the image a bot reads", () => { |
| 13 | + test("is found in a Read of a picture, even with its input cut short", () => { |
| 14 | + expect(readImagePath(read(JSON.stringify({ file_path: "C:\\Work\\q10.png" })))).toBe( |
| 15 | + "C:\\Work\\q10.png", |
| 16 | + ); |
| 17 | + expect(readImagePath(read('{"file_path":"C:\\\\Work\\\\shot.JPG","limit":20'))).toBe( |
| 18 | + "C:\\Work\\shot.JPG", |
| 19 | + ); |
| 20 | + expect(readImagePath(read(JSON.stringify({ file_path: "C:\\Work\\notes.md" })))).toBeNull(); |
| 21 | + expect(readImagePath(read(JSON.stringify({ file_path: "a.png" }), "Write"))).toBeNull(); |
| 22 | + }); |
| 23 | + |
| 24 | + test("shows on the call's line and larger when it opens", async () => { |
| 25 | + const fake = new FakeBotloft(); |
| 26 | + const ops = fake.addCrew("Ops"); |
| 27 | + const scout = fake.addBot(ops.id, "Scout", "Finds sources"); |
| 28 | + const file = fake.files.add(scout.id, "q10.png", { text: "picture" }); |
| 29 | + fake.setBotState(scout.id, "busy"); |
| 30 | + renderApp(fake); |
| 31 | + await crewOpened("Ops"); |
| 32 | + openBot("Scout"); |
| 33 | + const chat = await screen.findByRole("list", { name: "Messages" }); |
| 34 | + |
| 35 | + act(() => { |
| 36 | + fake.chat.tool(scout.id, "Read", { |
| 37 | + summary: "q10.png", |
| 38 | + input: JSON.stringify({ file_path: file.path }), |
| 39 | + }); |
| 40 | + }); |
| 41 | + const now = within(chat).getByRole("button", { name: /Read a file/ }); |
| 42 | + const thumb = await within(now).findByRole("img", { name: "q10.png" }); |
| 43 | + expect(thumb.getAttribute("src")).toMatch(/^data:image\/png;base64,/); |
| 44 | + |
| 45 | + act(() => { |
| 46 | + fake.chat.turn(scout.id); |
| 47 | + fake.setBotState(scout.id, "idle"); |
| 48 | + }); |
| 49 | + const line = within(chat).getByRole("button", { name: /Read a file/ }); |
| 50 | + fireEvent.click(line); |
| 51 | + expect(within(chat).getAllByRole("img", { name: "q10.png" })).toHaveLength(2); |
| 52 | + }); |
| 53 | + |
| 54 | + test("an image outside the bot's folders keeps the plain line", async () => { |
| 55 | + const fake = new FakeBotloft(); |
| 56 | + const ops = fake.addCrew("Ops"); |
| 57 | + const scout = fake.addBot(ops.id, "Scout", "Finds sources"); |
| 58 | + fake.setBotState(scout.id, "idle"); |
| 59 | + fake.chat.tool(scout.id, "Read", { |
| 60 | + summary: "secret.png", |
| 61 | + status: "done", |
| 62 | + input: JSON.stringify({ file_path: "D:\\Elsewhere\\secret.png" }), |
| 63 | + }); |
| 64 | + fake.chat.turn(scout.id); |
| 65 | + renderApp(fake); |
| 66 | + await crewOpened("Ops"); |
| 67 | + openBot("Scout"); |
| 68 | + const chat = await screen.findByRole("list", { name: "Messages" }); |
| 69 | + await within(chat).findByRole("button", { name: /Read a file/ }); |
| 70 | + await act(async () => {}); |
| 71 | + expect(within(chat).queryByRole("img", { name: "secret.png" })).toBeNull(); |
| 72 | + }); |
| 73 | +}); |
0 commit comments