|
| 1 | +import { |
| 2 | + afterAll, |
| 3 | + afterEach, |
| 4 | + beforeAll, |
| 5 | + beforeEach, |
| 6 | + expect, |
| 7 | + mock, |
| 8 | + test, |
| 9 | +} from "bun:test"; |
| 10 | +import { GlobalRegistrator } from "@happy-dom/global-registrator"; |
| 11 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 12 | +import { |
| 13 | + createMemoryHistory, |
| 14 | + createRootRoute, |
| 15 | + createRouter, |
| 16 | + RouterProvider, |
| 17 | +} from "@tanstack/react-router"; |
| 18 | +import { act, cleanup, fireEvent, render } from "@testing-library/react"; |
| 19 | +import userEvent from "@testing-library/user-event"; |
| 20 | +import type { ReactNode } from "react"; |
| 21 | +import { settleReactWork } from "./settle-react-work"; |
| 22 | + |
| 23 | +/* |
| 24 | + * `import.meta.glob` is Vite's, and `bun test` has no Vite. Stubbed the way the desktop stubs its |
| 25 | + * own marks module, keeping the shape that matters here: a brand with a vendored mark resolves to |
| 26 | + * something drawable, and one without resolves to null so its row shows the name alone. |
| 27 | + */ |
| 28 | +mock.module("@/lib/marks", () => ({ |
| 29 | + markFor: (id: string | null | undefined) => |
| 30 | + id ? `data:image/svg+xml,${id}` : null, |
| 31 | +})); |
| 32 | + |
| 33 | +/* |
| 34 | + * Imported after the stub above, not beside the other imports: a static import is hoisted |
| 35 | + * and would evaluate the real marks module — and its Vite-only glob — before `mock.module` |
| 36 | + * ever ran. |
| 37 | + */ |
| 38 | +const { CreateAgentDialog } = await import( |
| 39 | + "@/components/agents/create-agent-dialog" |
| 40 | +); |
| 41 | + |
| 42 | +/** |
| 43 | + * The AG-UI endpoint, on a step of its own. |
| 44 | + * |
| 45 | + * It is generic: every framework leads here and none of them changes a word of it. The step exists |
| 46 | + * only for a Bot somebody else hosts, so the Bot that runs on this deployment never sees it. |
| 47 | + */ |
| 48 | + |
| 49 | +beforeAll(() => GlobalRegistrator.register()); |
| 50 | +afterEach(cleanup); |
| 51 | +afterAll(async () => { |
| 52 | + await settleReactWork(); |
| 53 | + GlobalRegistrator.unregister(); |
| 54 | +}); |
| 55 | + |
| 56 | +const originalFetch = global.fetch; |
| 57 | +/** Every write the dialog sent, so "the endpoint was discarded" is an assertion. */ |
| 58 | +let writes: { request: string; body: Record<string, unknown> }[] = []; |
| 59 | + |
| 60 | +beforeEach(() => { |
| 61 | + writes = []; |
| 62 | + global.fetch = Object.assign( |
| 63 | + async ( |
| 64 | + path: Parameters<typeof fetch>[0], |
| 65 | + init?: Parameters<typeof fetch>[1], |
| 66 | + ) => { |
| 67 | + const method = init?.method ?? "GET"; |
| 68 | + if (path === "/api/agents/capabilities") { |
| 69 | + return Response.json({ capabilities: { builtInAvailable: true } }); |
| 70 | + } |
| 71 | + if (method === "POST" && String(path).startsWith("/api/agents")) { |
| 72 | + writes.push({ |
| 73 | + request: `${method} ${path}`, |
| 74 | + body: typeof init?.body === "string" ? JSON.parse(init.body) : {}, |
| 75 | + }); |
| 76 | + return Response.json({ agent: { id: "expense-manager" } }); |
| 77 | + } |
| 78 | + return new Response(null, { status: 404 }); |
| 79 | + }, |
| 80 | + { preconnect: originalFetch.preconnect }, |
| 81 | + ); |
| 82 | +}); |
| 83 | + |
| 84 | +afterEach(() => { |
| 85 | + global.fetch = originalFetch; |
| 86 | +}); |
| 87 | + |
| 88 | +function draw(screen: ReactNode) { |
| 89 | + const queryClient = new QueryClient({ |
| 90 | + defaultOptions: { queries: { retry: false }, mutations: { retry: false } }, |
| 91 | + }); |
| 92 | + const router = createRouter({ |
| 93 | + history: createMemoryHistory({ initialEntries: ["/"] }), |
| 94 | + routeTree: createRootRoute({ component: () => screen }), |
| 95 | + }); |
| 96 | + return render( |
| 97 | + <QueryClientProvider client={queryClient}> |
| 98 | + <RouterProvider router={router} /> |
| 99 | + </QueryClientProvider>, |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +async function atKindStep() { |
| 104 | + const view = draw( |
| 105 | + <CreateAgentDialog onClose={() => {}} onCreated={() => {}} open />, |
| 106 | + ); |
| 107 | + const name = await view.findByLabelText("Name"); |
| 108 | + const user = userEvent.setup({ document: name.ownerDocument }); |
| 109 | + await user.type(name, "Expense Manager"); |
| 110 | + await user.type(view.getByLabelText("Title"), "Finance Operations"); |
| 111 | + await user.type(view.getByLabelText("What it does"), "Review receipts."); |
| 112 | + await user.click(view.getByText("Continue")); |
| 113 | + await view.findByText("Where does it run?"); |
| 114 | + return { view, user }; |
| 115 | +} |
| 116 | + |
| 117 | +test("a framework leads to one generic endpoint step", async () => { |
| 118 | + const { view, user } = await atKindStep(); |
| 119 | + await user.click(view.getByRole("button", { name: /CrewAI/ })); |
| 120 | + const field = await view.findByLabelText("Agent endpoint"); |
| 121 | + expect(field).toBeTruthy(); |
| 122 | + /* |
| 123 | + * Generic: the framework that led here is not named on it. Scoped to this step's own pane |
| 124 | + * rather than the popup, because the pane being slid out is still mounted mid-animation and |
| 125 | + * carries the name of the button that was just pressed. |
| 126 | + */ |
| 127 | + const pane = view |
| 128 | + .getByText("Where can it be reached?") |
| 129 | + .closest('[data-slot="questionnaire-item"]'); |
| 130 | + expect(pane?.textContent).not.toContain("CrewAI"); |
| 131 | +}); |
| 132 | + |
| 133 | +test("the Bot that runs here skips the endpoint step entirely", async () => { |
| 134 | + const { view, user } = await atKindStep(); |
| 135 | + await user.click(view.getByRole("button", { name: /Run it here/ })); |
| 136 | + expect(await view.findByText("Who can see it?")).toBeTruthy(); |
| 137 | + expect(view.queryByLabelText("Agent endpoint")).toBeNull(); |
| 138 | +}); |
| 139 | + |
| 140 | +test("going back keeps the framework that was chosen", async () => { |
| 141 | + const { view, user } = await atKindStep(); |
| 142 | + await user.click(view.getByRole("button", { name: /CrewAI/ })); |
| 143 | + await view.findByLabelText("Agent endpoint"); |
| 144 | + await user.click(view.getByText("Back")); |
| 145 | + const chosen = await view.findByRole("button", { name: /CrewAI/ }); |
| 146 | + expect(chosen.getAttribute("aria-pressed")).toBe("true"); |
| 147 | +}); |
| 148 | + |
| 149 | +/* |
| 150 | + * Review Focus: an endpoint typed on the way past must not ride along into a Bot that runs here. |
| 151 | + * The server stores whatever it is sent, and a built-in Bot carrying somebody's stale URL would be |
| 152 | + * dialled at it. |
| 153 | + */ |
| 154 | +test("choosing Run it here discards an endpoint typed earlier", async () => { |
| 155 | + const { view, user } = await atKindStep(); |
| 156 | + await user.click(view.getByRole("button", { name: /CrewAI/ })); |
| 157 | + await user.type( |
| 158 | + await view.findByLabelText("Agent endpoint"), |
| 159 | + "https://example.com/ag-ui", |
| 160 | + ); |
| 161 | + await user.click(view.getByText("Back")); |
| 162 | + await user.click(view.getByRole("button", { name: /Run it here/ })); |
| 163 | + await view.findByText("Who can see it?"); |
| 164 | + await user.click(view.getByText("Create coworker")); |
| 165 | + await act(async () => { |
| 166 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 167 | + }); |
| 168 | + expect(writes).toHaveLength(1); |
| 169 | + expect(writes[0]?.body.endpoint).toBe(""); |
| 170 | +}); |
| 171 | + |
| 172 | +test("Enter in the endpoint field advances the step", async () => { |
| 173 | + const { view, user } = await atKindStep(); |
| 174 | + await user.click(view.getByRole("button", { name: /CrewAI/ })); |
| 175 | + const field = await view.findByLabelText("Agent endpoint"); |
| 176 | + await user.type(field, "https://example.com/ag-ui"); |
| 177 | + await act(async () => { |
| 178 | + fireEvent.keyDown(field, { key: "Enter", keyCode: 13 }); |
| 179 | + }); |
| 180 | + expect(await view.findByText("Who can see it?")).toBeTruthy(); |
| 181 | +}); |
0 commit comments