|
| 1 | +import { cleanup, fireEvent, screen, waitFor, within } from "@testing-library/react"; |
| 2 | +import { afterEach, describe, expect, test } from "vitest"; |
| 3 | +import { en } from "../../i18n/en"; |
| 4 | +import { FakeBotloft } from "../../lib/fake"; |
| 5 | +import { crewOpened, openTab, renderApp } from "../../test/app"; |
| 6 | + |
| 7 | +afterEach(cleanup); |
| 8 | + |
| 9 | +const calls = (fake: FakeBotloft, method: string) => |
| 10 | + fake.calls.filter((call) => call.method === method); |
| 11 | + |
| 12 | +/** A crew "Site" with its chief, open on the crew's routines. */ |
| 13 | +async function openCrewRoutines(withChief = true) { |
| 14 | + const fake = new FakeBotloft(); |
| 15 | + if (withChief) { |
| 16 | + await fake.call("crews.create", { |
| 17 | + name: "Site", |
| 18 | + lead: { name: "Chief", role: "Leads the crew", instructions: "Build it" }, |
| 19 | + }); |
| 20 | + } else { |
| 21 | + fake.addCrew("Site"); |
| 22 | + } |
| 23 | + renderApp(fake); |
| 24 | + await crewOpened("Site"); |
| 25 | + openTab(/^Routines/); |
| 26 | + return { fake }; |
| 27 | +} |
| 28 | + |
| 29 | +describe("the daily summary", () => { |
| 30 | + test("is offered on the crew's routines and turned on for the chief, every day", async () => { |
| 31 | + const { fake } = await openCrewRoutines(); |
| 32 | + const card = await screen.findByRole("region", { name: "Daily summary" }); |
| 33 | + fireEvent.change(within(card).getByLabelText("Time"), { target: { value: "19:30" } }); |
| 34 | + fireEvent.click(within(card).getByRole("button", { name: "Turn on" })); |
| 35 | + |
| 36 | + await waitFor(() => expect(calls(fake, "routines.create")).toHaveLength(1)); |
| 37 | + const [create] = calls(fake, "routines.create"); |
| 38 | + expect(create?.params).toMatchObject({ |
| 39 | + name: "Daily summary", |
| 40 | + schedule: { kind: "weekly", days: [1, 2, 3, 4, 5, 6, 7], time: "19:30" }, |
| 41 | + overlap: "skip", |
| 42 | + missed: "run_once", |
| 43 | + }); |
| 44 | + // It asks the chief to use the tool that tells it what the bots did. |
| 45 | + const params = create?.params as { prompt: string; botId: string } | undefined; |
| 46 | + expect(params?.prompt).toContain("crew_activity"); |
| 47 | + const chief = (await fake.call("bots.list", {})).find((bot) => bot.name === "Chief"); |
| 48 | + expect(params?.botId).toBe(chief?.id); |
| 49 | + |
| 50 | + expect(await within(card).findByText(/^On · /)).toBeDefined(); |
| 51 | + expect(within(card).getByRole("button", { name: "Turn off" })).toBeDefined(); |
| 52 | + }); |
| 53 | + |
| 54 | + test("can be turned off and on again, and shows in the list like any routine", async () => { |
| 55 | + const { fake } = await openCrewRoutines(); |
| 56 | + const card = await screen.findByRole("region", { name: "Daily summary" }); |
| 57 | + fireEvent.click(within(card).getByRole("button", { name: "Turn on" })); |
| 58 | + await within(card).findByText(/^On · /); |
| 59 | + expect( |
| 60 | + within(await screen.findByRole("list", { name: "Routines" })).getByText("Daily summary"), |
| 61 | + ).toBeDefined(); |
| 62 | + |
| 63 | + fireEvent.click(within(card).getByRole("button", { name: "Turn off" })); |
| 64 | + expect(await within(card).findByText("Off")).toBeDefined(); |
| 65 | + fireEvent.click(within(card).getByRole("button", { name: "Turn on" })); |
| 66 | + await within(card).findByText(/^On · /); |
| 67 | + expect(calls(fake, "routines.setEnabled").map((call) => call.params)).toEqual([ |
| 68 | + expect.objectContaining({ enabled: false }), |
| 69 | + expect.objectContaining({ enabled: true }), |
| 70 | + ]); |
| 71 | + // One routine, not a second one each time. |
| 72 | + expect(calls(fake, "routines.create")).toHaveLength(1); |
| 73 | + }); |
| 74 | + |
| 75 | + test("is not offered to a crew without a chief", async () => { |
| 76 | + await openCrewRoutines(false); |
| 77 | + await screen.findByText(en.routines.crewEmpty); |
| 78 | + expect(screen.queryByRole("region", { name: "Daily summary" })).toBeNull(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments