|
| 1 | +// @vitest-environment jsdom |
| 2 | +import "@testing-library/jest-dom/vitest"; |
| 3 | +import { |
| 4 | + cleanup, |
| 5 | + render, |
| 6 | + screen, |
| 7 | + waitFor, |
| 8 | + within, |
| 9 | +} from "@testing-library/react"; |
| 10 | +import userEvent from "@testing-library/user-event"; |
| 11 | +import { afterEach, expect, it, vi } from "vitest"; |
| 12 | +import type { Agent, Agents } from "../../features/agents/service"; |
| 13 | +import type { RelaySession } from "../../features/relay/session"; |
| 14 | +import { BestieChooser } from "./BestieChooser"; |
| 15 | + |
| 16 | +vi.mock("../agents/AgentCreateDialog", () => ({ |
| 17 | + AgentCreateDialog: ({ |
| 18 | + onCreated, |
| 19 | + }: { |
| 20 | + onCreated(agent: { pubkey: string }): void; |
| 21 | + }) => ( |
| 22 | + <button type="button" onClick={() => onCreated({ pubkey: pollen })}> |
| 23 | + Finish creation |
| 24 | + </button> |
| 25 | + ), |
| 26 | +})); |
| 27 | + |
| 28 | +afterEach(() => { |
| 29 | + cleanup(); |
| 30 | + localStorage.clear(); |
| 31 | +}); |
| 32 | + |
| 33 | +const pollen = "b".repeat(64); |
| 34 | +const instruction = "Choose an eligible agent or create a new one:"; |
| 35 | + |
| 36 | +function deferred() { |
| 37 | + let resolve!: () => void; |
| 38 | + const promise = new Promise<void>((done) => { |
| 39 | + resolve = done; |
| 40 | + }); |
| 41 | + return { promise, resolve }; |
| 42 | +} |
| 43 | + |
| 44 | +function fixture( |
| 45 | + write: (agent: string | null) => Promise<void>, |
| 46 | + initial = [{ pubkey: pollen, name: "Pollen", managed: true }], |
| 47 | + place: Agent["place"] = "here", |
| 48 | +) { |
| 49 | + let selectable = initial; |
| 50 | + const makeSnapshot = () => |
| 51 | + ({ |
| 52 | + status: "ready" as const, |
| 53 | + complete: true, |
| 54 | + pending: false, |
| 55 | + selectable, |
| 56 | + identities: selectable, |
| 57 | + definitions: [], |
| 58 | + archives: { status: "ready" as const, archived: [] }, |
| 59 | + templates: { |
| 60 | + status: "ready" as const, |
| 61 | + complete: true, |
| 62 | + pending: false, |
| 63 | + identities: selectable, |
| 64 | + definitions: [], |
| 65 | + }, |
| 66 | + }) as const; |
| 67 | + let choices = makeSnapshot(); |
| 68 | + const session = { |
| 69 | + mainAgent: { read: async () => undefined, write: vi.fn(write) }, |
| 70 | + agentChoices: { |
| 71 | + snapshot: () => choices, |
| 72 | + subscribe: () => () => {}, |
| 73 | + retain: () => () => {}, |
| 74 | + ensure: () => {}, |
| 75 | + refresh: vi.fn(async () => {}), |
| 76 | + }, |
| 77 | + } as unknown as RelaySession; |
| 78 | + const agents = { |
| 79 | + subscribe: () => () => {}, |
| 80 | + snapshot: () => undefined, |
| 81 | + types: () => empty, |
| 82 | + find: (pubkey: string) => ({ pubkey, place }), |
| 83 | + } as unknown as Agents; |
| 84 | + return { |
| 85 | + session, |
| 86 | + removeChoice: () => { |
| 87 | + selectable = []; |
| 88 | + choices = makeSnapshot(); |
| 89 | + }, |
| 90 | + mount: () => |
| 91 | + render( |
| 92 | + <BestieChooser |
| 93 | + session={session} |
| 94 | + scope="community:viewer" |
| 95 | + agents={agents} |
| 96 | + />, |
| 97 | + ), |
| 98 | + }; |
| 99 | +} |
| 100 | + |
| 101 | +const empty: readonly never[] = []; |
| 102 | + |
| 103 | +async function selectPollen() { |
| 104 | + const user = userEvent.setup(); |
| 105 | + await user.click(screen.getByRole("combobox", { name: instruction })); |
| 106 | + await user.click(await screen.findByRole("option", { name: /^Pollen / })); |
| 107 | + return user; |
| 108 | +} |
| 109 | + |
| 110 | +it("searches namesakes, shows distinct identities, and selects with the keyboard", async () => { |
| 111 | + const other = "c".repeat(64); |
| 112 | + const f = fixture(async () => {}, [ |
| 113 | + { pubkey: pollen, name: "Pollen", managed: true }, |
| 114 | + { pubkey: other, name: "Pollen", managed: true }, |
| 115 | + { pubkey: "d".repeat(64), name: "Fizz", managed: true }, |
| 116 | + ]); |
| 117 | + f.mount(); |
| 118 | + const user = userEvent.setup(); |
| 119 | + const trigger = screen.getByRole("combobox", { name: instruction }); |
| 120 | + await user.click(trigger); |
| 121 | + const search = await screen.findByRole("combobox", { name: "Search" }); |
| 122 | + await user.type(search, "Nobody"); |
| 123 | + expect(await screen.findByText("No matching agents.")).toBeVisible(); |
| 124 | + expect(f.session.mainAgent?.write).not.toHaveBeenCalled(); |
| 125 | + await user.clear(search); |
| 126 | + await user.type(search, "poll"); |
| 127 | + await waitFor(() => { |
| 128 | + const options = screen.getAllByRole("option"); |
| 129 | + expect(options).toHaveLength(2); |
| 130 | + expect(options[0]?.textContent).not.toBe(options[1]?.textContent); |
| 131 | + }); |
| 132 | + await user.keyboard("{Escape}"); |
| 133 | + await waitFor(() => expect(trigger).toHaveFocus()); |
| 134 | + expect(f.session.mainAgent?.write).not.toHaveBeenCalled(); |
| 135 | + await user.click(trigger); |
| 136 | + const reopened = await screen.findByRole("combobox", { name: "Search" }); |
| 137 | + expect(reopened).toHaveValue(""); |
| 138 | + await user.type(reopened, "poll"); |
| 139 | + await user.keyboard("{ArrowDown}{End}{Enter}"); |
| 140 | + await waitFor(() => |
| 141 | + expect(f.session.mainAgent?.write).toHaveBeenCalledWith(other), |
| 142 | + ); |
| 143 | +}); |
| 144 | + |
| 145 | +it("holds the selected agent disabled until the account save settles", async () => { |
| 146 | + const gate = deferred(); |
| 147 | + const f = fixture(async () => gate.promise); |
| 148 | + f.mount(); |
| 149 | + await selectPollen(); |
| 150 | + expect(f.session.mainAgent?.write).toHaveBeenCalledWith(pollen); |
| 151 | + expect(screen.getByRole("combobox", { name: instruction })).toBeDisabled(); |
| 152 | + expect(screen.getByRole("status")).toHaveTextContent("Saving your Bestie…"); |
| 153 | + gate.resolve(); |
| 154 | + await waitFor(() => |
| 155 | + expect(screen.getByRole("combobox", { name: instruction })).toBeEnabled(), |
| 156 | + ); |
| 157 | +}); |
| 158 | + |
| 159 | +it("keeps the chooser available when the account save fails", async () => { |
| 160 | + const f = fixture(async () => { |
| 161 | + throw new Error("save failed"); |
| 162 | + }); |
| 163 | + f.mount(); |
| 164 | + await selectPollen(); |
| 165 | + expect(await screen.findByRole("alert")).toHaveTextContent( |
| 166 | + "Your bestie couldn't be saved to your account. Try again.", |
| 167 | + ); |
| 168 | + expect(screen.getByRole("combobox", { name: instruction })).toBeEnabled(); |
| 169 | +}); |
| 170 | + |
| 171 | +it("rejects a choice that became stale before selection", async () => { |
| 172 | + const f = fixture(async () => {}); |
| 173 | + f.mount(); |
| 174 | + const user = userEvent.setup(); |
| 175 | + await user.click(screen.getByRole("combobox", { name: instruction })); |
| 176 | + const option = await screen.findByRole("option", { name: /^Pollen / }); |
| 177 | + f.removeChoice(); |
| 178 | + await user.click(option); |
| 179 | + expect(await screen.findByRole("alert")).toHaveTextContent( |
| 180 | + "That agent is no longer available. Choose another.", |
| 181 | + ); |
| 182 | + expect(f.session.mainAgent?.write).not.toHaveBeenCalled(); |
| 183 | +}); |
| 184 | + |
| 185 | +it.each(["hosted", "elsewhere"] as const)( |
| 186 | + "warns before saving an agent running %s and allows cancellation or confirmation", |
| 187 | + async (place) => { |
| 188 | + const f = fixture(async () => {}, undefined, place); |
| 189 | + f.mount(); |
| 190 | + const user = await selectPollen(); |
| 191 | + const dialog = await screen.findByRole("alertdialog", { |
| 192 | + name: "Use Pollen as your bestie?", |
| 193 | + }); |
| 194 | + expect(dialog).toHaveAccessibleDescription( |
| 195 | + "Some bestie features aren’t available with remote agents.", |
| 196 | + ); |
| 197 | + expect(f.session.mainAgent?.write).not.toHaveBeenCalled(); |
| 198 | + await user.click(within(dialog).getByRole("button", { name: "Cancel" })); |
| 199 | + await waitFor(() => expect(dialog).not.toBeInTheDocument()); |
| 200 | + expect(f.session.mainAgent?.write).not.toHaveBeenCalled(); |
| 201 | + await selectPollen(); |
| 202 | + await user.click( |
| 203 | + await screen.findByRole("button", { name: "Use as bestie" }), |
| 204 | + ); |
| 205 | + await waitFor(() => |
| 206 | + expect(f.session.mainAgent?.write).toHaveBeenCalledWith(pollen), |
| 207 | + ); |
| 208 | + }, |
| 209 | +); |
| 210 | + |
| 211 | +it("rechecks a remote agent's eligibility when its confirmation is accepted", async () => { |
| 212 | + const f = fixture(async () => {}, undefined, "hosted"); |
| 213 | + f.mount(); |
| 214 | + const user = await selectPollen(); |
| 215 | + await screen.findByRole("alertdialog"); |
| 216 | + f.removeChoice(); |
| 217 | + await user.click(screen.getByRole("button", { name: "Use as bestie" })); |
| 218 | + expect(await screen.findByRole("alert")).toHaveTextContent( |
| 219 | + "That agent is no longer available. Choose another.", |
| 220 | + ); |
| 221 | + expect(f.session.mainAgent?.write).not.toHaveBeenCalled(); |
| 222 | +}); |
| 223 | + |
| 224 | +it("keeps a remote choice open for retry after a save fails", async () => { |
| 225 | + const gate = deferred(); |
| 226 | + let attempts = 0; |
| 227 | + const f = fixture( |
| 228 | + async () => { |
| 229 | + if (++attempts === 1) throw new Error("offline"); |
| 230 | + await gate.promise; |
| 231 | + }, |
| 232 | + undefined, |
| 233 | + "hosted", |
| 234 | + ); |
| 235 | + f.mount(); |
| 236 | + const user = await selectPollen(); |
| 237 | + await user.click( |
| 238 | + await screen.findByRole("button", { name: "Use as bestie" }), |
| 239 | + ); |
| 240 | + const dialog = await screen.findByRole("alertdialog"); |
| 241 | + expect(await within(dialog).findByRole("alert")).toHaveTextContent( |
| 242 | + "Your bestie couldn't be saved to your account. Try again.", |
| 243 | + ); |
| 244 | + await user.click( |
| 245 | + within(dialog).getByRole("button", { name: "Use as bestie" }), |
| 246 | + ); |
| 247 | + try { |
| 248 | + expect( |
| 249 | + within(dialog).getByRole("button", { name: "Cancel" }), |
| 250 | + ).toBeDisabled(); |
| 251 | + await user.keyboard("{Escape}"); |
| 252 | + expect(dialog).toBeInTheDocument(); |
| 253 | + } finally { |
| 254 | + gate.resolve(); |
| 255 | + } |
| 256 | + await waitFor(() => expect(dialog).not.toBeInTheDocument()); |
| 257 | + expect(f.session.mainAgent?.write).toHaveBeenCalledTimes(2); |
| 258 | +}); |
| 259 | + |
| 260 | +it("warns after creating a hosted agent without automatically saving it as Bestie", async () => { |
| 261 | + const f = fixture(async () => {}, undefined, "hosted"); |
| 262 | + f.mount(); |
| 263 | + const user = userEvent.setup(); |
| 264 | + await user.click(screen.getByRole("button", { name: "Create agent" })); |
| 265 | + await user.click(screen.getByRole("button", { name: "Finish creation" })); |
| 266 | + await screen.findByRole("alertdialog", { |
| 267 | + name: "Use Pollen as your bestie?", |
| 268 | + }); |
| 269 | + expect(f.session.agentChoices.refresh).toHaveBeenCalled(); |
| 270 | + expect(f.session.mainAgent?.write).not.toHaveBeenCalled(); |
| 271 | + await user.click(screen.getByRole("button", { name: "Use as bestie" })); |
| 272 | + await waitFor(() => |
| 273 | + expect(f.session.mainAgent?.write).toHaveBeenCalledWith(pollen), |
| 274 | + ); |
| 275 | +}); |
| 276 | + |
| 277 | +it("retries the handoff after agent creation refresh fails", async () => { |
| 278 | + const f = fixture(async () => {}); |
| 279 | + vi.mocked(f.session.agentChoices.refresh) |
| 280 | + .mockRejectedValueOnce(new Error("inventory failed")) |
| 281 | + .mockResolvedValue(undefined); |
| 282 | + f.mount(); |
| 283 | + const user = userEvent.setup(); |
| 284 | + await user.click(screen.getByRole("button", { name: "Create agent" })); |
| 285 | + await user.click(screen.getByRole("button", { name: "Finish creation" })); |
| 286 | + expect(await screen.findByRole("alert")).toHaveTextContent( |
| 287 | + "inventory failed", |
| 288 | + ); |
| 289 | + expect(f.session.mainAgent?.write).not.toHaveBeenCalled(); |
| 290 | + await user.click(screen.getByRole("button", { name: "Try again" })); |
| 291 | + await waitFor(() => |
| 292 | + expect(f.session.mainAgent?.write).toHaveBeenCalledWith(pollen), |
| 293 | + ); |
| 294 | +}); |
0 commit comments