|
2 | 2 | import "@testing-library/jest-dom/vitest"; |
3 | 3 | import { act, cleanup, render, screen } from "@testing-library/react"; |
4 | 4 | import userEvent from "@testing-library/user-event"; |
5 | | -import { StrictMode } from "react"; |
6 | | -import { afterEach, expect, it } from "vitest"; |
| 5 | +import { StrictMode, useState } from "react"; |
| 6 | +import { afterEach, beforeEach, expect, it, vi } from "vitest"; |
| 7 | +import type { Host } from "../../../features/host/service"; |
| 8 | +import { createAgentClient } from "../agents/client"; |
7 | 9 | import { createOAuthSession } from "../oauth/session"; |
8 | | -import { deferred } from "../test-helpers"; |
9 | 10 | import type { Credential } from "../oauth/browser"; |
10 | | -import { createCreatePanel } from "./Create"; |
| 11 | +import { deferred } from "../test-helpers"; |
| 12 | +import { createCreationFields, EMPTY_CREATION } from "./Create"; |
| 13 | + |
| 14 | +beforeEach(() => |
| 15 | + vi.stubEnv("VITE_BUZZ_BUILDERLAB_URL", "https://builderlab.example"), |
| 16 | +); |
| 17 | +afterEach(() => { |
| 18 | + cleanup(); |
| 19 | + vi.unstubAllEnvs(); |
| 20 | +}); |
| 21 | +const catalog = [ |
| 22 | + { |
| 23 | + id: "one", |
| 24 | + display_name: "Model One", |
| 25 | + supported_reasoning_efforts: ["low", "high"], |
| 26 | + }, |
| 27 | + { |
| 28 | + id: "two", |
| 29 | + display_name: "Model Two", |
| 30 | + supported_reasoning_efforts: ["none"], |
| 31 | + }, |
| 32 | +]; |
| 33 | +async function fixture(signedIn = true) { |
| 34 | + const acquire = vi.fn(async () => ({ |
| 35 | + value: "credential", |
| 36 | + account: { |
| 37 | + subject: "account", |
| 38 | + |
| 39 | + canUseRemoteAgents: true, |
| 40 | + }, |
| 41 | + })); |
| 42 | + const session = createOAuthSession(acquire); |
| 43 | + if (signedIn) await session.signIn(); |
| 44 | + let fail = false; |
| 45 | + const pending = deferred<void>(); |
| 46 | + const request = vi.fn<Host["request"]>(async () => { |
| 47 | + await pending.promise; |
| 48 | + if (fail) throw new Error("offline"); |
| 49 | + return { |
| 50 | + status: 200, |
| 51 | + headers: {}, |
| 52 | + body: JSON.stringify({ models: catalog }), |
| 53 | + }; |
| 54 | + }); |
| 55 | + const client = createAgentClient( |
| 56 | + { request, runCommand: vi.fn() }, |
| 57 | + session, |
| 58 | + null, |
| 59 | + ); |
| 60 | + const Fields = createCreationFields(client, session); |
| 61 | + let draft = { ...EMPTY_CREATION }; |
| 62 | + function Form() { |
| 63 | + const [config, setConfig] = useState(draft); |
| 64 | + draft = config; |
| 65 | + return ( |
| 66 | + <form onSubmit={(event) => event.preventDefault()}> |
| 67 | + <Fields config={config} onChange={setConfig} disabled={false} /> |
| 68 | + </form> |
| 69 | + ); |
| 70 | + } |
| 71 | + render(<Form />, { wrapper: StrictMode }); |
| 72 | + return { |
| 73 | + session, |
| 74 | + acquire, |
| 75 | + request, |
| 76 | + pending, |
| 77 | + draft: () => draft, |
| 78 | + fail: (value: boolean) => { |
| 79 | + fail = value; |
| 80 | + }, |
| 81 | + }; |
| 82 | +} |
| 83 | +async function choose(label: string, value: string) { |
| 84 | + const user = userEvent.setup(); |
| 85 | + await user.click(await screen.findByRole("combobox", { name: label })); |
| 86 | + await user.click(await screen.findByRole("option", { name: value })); |
| 87 | +} |
| 88 | + |
| 89 | +it("loads the catalog and keeps instructions alongside explicit model and effort choices", async () => { |
| 90 | + const h = await fixture(); |
| 91 | + expect(screen.getByRole("status")).toHaveTextContent("Loading models"); |
| 92 | + await act(async () => h.pending.resolve()); |
| 93 | + const model = await screen.findByRole("combobox", { name: "Model" }); |
| 94 | + expect(model).toHaveTextContent("Choose a model"); |
| 95 | + expect( |
| 96 | + screen.getByRole("combobox", { name: "Reasoning effort" }), |
| 97 | + ).toBeDisabled(); |
| 98 | + await userEvent |
| 99 | + .setup() |
| 100 | + .type( |
| 101 | + screen.getByRole("textbox", { name: "Instructions Optional" }), |
| 102 | + "Be precise.", |
| 103 | + ); |
| 104 | + await choose("Model", "Model One"); |
| 105 | + await choose("Reasoning effort", "high"); |
| 106 | + await choose("Model", "Model Two"); |
| 107 | + expect(h.draft().reasoningEffort).toBe(""); |
| 108 | + expect( |
| 109 | + screen.getByRole("textbox", { name: "Instructions Optional" }), |
| 110 | + ).toHaveValue("Be precise."); |
| 111 | + await choose("Reasoning effort", "none"); |
| 112 | + expect(h.draft()).toEqual({ |
| 113 | + instructions: "Be precise.", |
| 114 | + modelId: "two", |
| 115 | + reasoningEffort: "none", |
| 116 | + subject: "account", |
| 117 | + }); |
| 118 | + expect( |
| 119 | + h.request.mock.calls.every(([input]) => |
| 120 | + input.url.endsWith("/get-model-catalog"), |
| 121 | + ), |
| 122 | + ).toBe(true); |
| 123 | +}); |
| 124 | + |
| 125 | +it("retains instructions when catalog loading fails and is retried", async () => { |
| 126 | + const h = await fixture(); |
| 127 | + h.fail(true); |
| 128 | + await userEvent |
| 129 | + .setup() |
| 130 | + .type( |
| 131 | + screen.getByRole("textbox", { name: "Instructions Optional" }), |
| 132 | + "Keep this.", |
| 133 | + ); |
| 134 | + await act(async () => h.pending.resolve()); |
| 135 | + const retry = await screen.findByRole("button", { name: "Retry models" }); |
| 136 | + expect(screen.getByRole("alert")).toHaveTextContent( |
| 137 | + "Could not reach Builderlab", |
| 138 | + ); |
| 139 | + h.fail(false); |
| 140 | + await userEvent.setup().click(retry); |
| 141 | + await screen.findByRole("combobox", { name: "Model" }); |
| 142 | + expect(h.draft().instructions).toBe("Keep this."); |
| 143 | + expect(h.draft().modelId).toBe(""); |
| 144 | +}); |
11 | 145 |
|
12 | | -afterEach(cleanup); |
| 146 | +it("waits for explicit sign-in before loading the creation catalog", async () => { |
| 147 | + const h = await fixture(false); |
| 148 | + expect(h.acquire).not.toHaveBeenCalled(); |
| 149 | + expect(h.request).not.toHaveBeenCalled(); |
| 150 | + await act(async () => h.pending.resolve()); |
| 151 | + await userEvent |
| 152 | + .setup() |
| 153 | + .click(screen.getByRole("button", { name: "Sign in with Builderlab" })); |
| 154 | + await screen.findByRole("combobox", { name: "Model" }); |
| 155 | + expect(h.acquire).toHaveBeenCalledTimes(1); |
| 156 | +}); |
13 | 157 |
|
14 | | -it("offers Cancel sign-in only while sign-in is pending, then restores the provider copy", async () => { |
| 158 | +it("can cancel a pending shared sign-in even while the creation form is disabled", async () => { |
15 | 159 | const login = deferred<Credential>(); |
16 | 160 | let aborted = false; |
17 | 161 | const session = createOAuthSession((signal) => { |
18 | 162 | signal.addEventListener("abort", () => (aborted = true)); |
19 | 163 | return login.promise; |
20 | 164 | }); |
21 | | - const Create = createCreatePanel(session); |
22 | | - render(<Create config={{}} disabled={false} onChange={() => {}} />, { |
23 | | - wrapper: StrictMode, |
24 | | - }); |
25 | | - expect(screen.getByText(/Its provider keeps its key/)).toBeInTheDocument(); |
| 165 | + const request = vi.fn<Host["request"]>(); |
| 166 | + const client = createAgentClient( |
| 167 | + { request, runCommand: vi.fn() }, |
| 168 | + session, |
| 169 | + null, |
| 170 | + ); |
| 171 | + const Create = createCreationFields(client, session); |
| 172 | + render( |
| 173 | + <Create config={{ ...EMPTY_CREATION }} disabled onChange={() => {}} />, |
| 174 | + { wrapper: StrictMode }, |
| 175 | + ); |
26 | 176 | expect( |
27 | 177 | screen.queryByRole("button", { name: "Cancel sign-in" }), |
28 | 178 | ).not.toBeInTheDocument(); |
29 | | - |
30 | 179 | await act(async () => { |
31 | 180 | void session.signIn(); |
32 | 181 | }); |
33 | 182 | expect(screen.getByRole("status")).toHaveTextContent( |
34 | 183 | "Finish sign-in in your browser", |
35 | 184 | ); |
36 | | - await userEvent |
37 | | - .setup() |
38 | | - .click(screen.getByRole("button", { name: "Cancel sign-in" })); |
| 185 | + const cancel = screen.getByRole("button", { name: "Cancel sign-in" }); |
| 186 | + expect(cancel).toBeEnabled(); |
| 187 | + await userEvent.setup().click(cancel); |
39 | 188 | expect(aborted).toBe(true); |
40 | 189 | expect(session.snapshot().status).toBe("signed-out"); |
41 | | - expect(screen.getByText(/Its provider keeps its key/)).toBeInTheDocument(); |
42 | 190 | expect( |
43 | 191 | screen.queryByRole("button", { name: "Cancel sign-in" }), |
44 | 192 | ).not.toBeInTheDocument(); |
| 193 | + expect(screen.getByRole("status")).toHaveTextContent( |
| 194 | + "Sign in to choose a model", |
| 195 | + ); |
| 196 | + expect(request).not.toHaveBeenCalled(); |
45 | 197 | }); |
0 commit comments