diff --git a/apps/web/src/components/onboarding/WelcomePage.import.browser.test.tsx b/apps/web/src/components/onboarding/WelcomePage.import.browser.test.tsx index 5615fa3081842..b1429b4801692 100644 --- a/apps/web/src/components/onboarding/WelcomePage.import.browser.test.tsx +++ b/apps/web/src/components/onboarding/WelcomePage.import.browser.test.tsx @@ -52,6 +52,7 @@ const state = vi.hoisted(() => ({ openTerminal: vi.fn(), writeTerminal: vi.fn(), closeTerminal: vi.fn(), + attachedTerminals: new Map void>(), })); vi.mock(import("@t3tools/client-runtime/state/session"), async (original) => { @@ -175,7 +176,14 @@ vi.mock(import("../../state/terminalSessions"), async (original) => { }); vi.mock(import("../ThreadTerminalDrawer"), async (original) => ({ ...(await original()), - TerminalViewport: () =>
, + TerminalViewport: ({ terminalId, onSessionExited }) => ( +
{ + state.attachedTerminals.set(terminalId, onSessionExited); + return () => void state.attachedTerminals.delete(terminalId); + }} + /> + ), })); vi.mock(import("../../state/use-atom-command"), async (original) => ({ ...(await original()), @@ -367,6 +375,12 @@ function setup() { for (const command of [state.openTerminal, state.writeTerminal, state.closeTerminal]) { command.mockReset().mockResolvedValue(AsyncResult.success(undefined)); } + // Closing a terminal whose viewport is still attached reports an exit, as in the real one. + state.attachedTerminals.clear(); + state.closeTerminal.mockImplementation(async ({ input }: { input: { terminalId: string } }) => { + state.attachedTerminals.get(input.terminalId)?.(); + return AsyncResult.success(undefined); + }); localStorage.removeItem(welcomeImportSelectionKey(primaryId)); return now; } @@ -665,7 +679,7 @@ it("offers Update for a signed-in agent whose version is broken, and keeps Start expect(button("Import 2 projects and start").disabled).toBe(true); }); -it("checks an agent again once its install command finishes in the setup terminal", async () => { +it("checks an agent again once its install command finishes, then replaces it with sign-in", async () => { using _now = setup(); appAtomRegistry.set( sessionAtom(primaryId), @@ -690,10 +704,35 @@ it("checks an agent again once its install command finishes in the setup termina await act(async () => appAtomRegistry.set(runningTerminalIds, [terminalId])); expect(state.refreshProviders).not.toHaveBeenCalledWith(recheck); + // Another check sees the new, signed-out binary before the installer exits. + await act(async () => + appAtomRegistry.set(state.providers as Atom.Writable>, [ + { ...readyClaude, status: "error", auth: { status: "unauthenticated" } }, + ]), + ); + await vi.waitFor(() => expect(button("Sign in").disabled).toBe(true)); // The shell stays open at its prompt after the install, so only the command exits. await act(async () => appAtomRegistry.set(runningTerminalIds, [])); expect(state.refreshProviders).toHaveBeenCalledWith(recheck); expect(state.closeTerminal).not.toHaveBeenCalled(); + + await click("Sign in"); + await vi.waitFor(() => + expect(state.writeTerminal).toHaveBeenLastCalledWith({ + environmentId: primaryId, + input: { + threadId: expect.any(String), + terminalId: expect.any(String), + data: "claude auth login", + }, + }), + ); + await vi.waitFor(() => + expect(pageText()).toContain("Review the command, then press Enter to run it."), + ); + expect(state.openTerminal.mock.calls[1]![0].input.terminalId).not.toBe(terminalId); + expect(state.closeTerminal).toHaveBeenCalledOnce(); + expect(state.closeTerminal.mock.calls[0]![0].input.terminalId).toBe(terminalId); }); it("keeps an import opt-out on the page when storage cannot save it", async () => { diff --git a/apps/web/src/components/onboarding/WelcomePage.tsx b/apps/web/src/components/onboarding/WelcomePage.tsx index 5c1b4218117fc..4fecca6b6b72b 100644 --- a/apps/web/src/components/onboarding/WelcomePage.tsx +++ b/apps/web/src/components/onboarding/WelcomePage.tsx @@ -416,6 +416,7 @@ interface AgentTerminalSession { readonly environmentId: EnvironmentId; readonly driver: OnboardingAgentDriver; readonly providerInstanceId: ServerProvider["instanceId"]; + readonly step: "install" | "signIn"; readonly cwd: string; readonly command: string; readonly keybindings: ServerConfig["keybindings"]; @@ -467,6 +468,9 @@ function AgentsSection({ const [addingProvider, setAddingProvider] = useState(false); const [codexSetupId, setCodexSetupId] = useState(null); const [cliTerminalFor, setCliTerminalFor] = useState(null); + // Another check can see a new binary before its installer exits; Sign in must not cut it off. + const setupCommandRunning = + useThreadRunningTerminalIds({ environmentId, threadId: AGENT_ONBOARDING_THREAD_ID }).length > 0; // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- asks the environment to refresh its provider list when the environment changes useEffect(() => { @@ -516,6 +520,7 @@ function AgentsSection({ environmentId, driver: provider.driver, providerInstanceId: provider.instanceId, + step: provider.installed ? "signIn" : "install", cwd: serverConfig.cwd, command: provider.installed ? resolveOnboardingProviderLoginCommand( @@ -623,7 +628,8 @@ function AgentsSection({ name={name} signIn={state === "signIn"} disabled={ - terminalSession?.providerInstanceId === provider.instanceId || + (terminalSession?.providerInstanceId === provider.instanceId && + (terminalSession.step === state || setupCommandRunning)) || serverConfig === null || !canOperateTerminal } @@ -739,7 +745,8 @@ function AgentsSection({ {isPrimary ? : null} {terminalSession !== null ? ( void refreshProviders({