Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const state = vi.hoisted(() => ({
openTerminal: vi.fn(),
writeTerminal: vi.fn(),
closeTerminal: vi.fn(),
attachedTerminals: new Map<string, () => void>(),
}));

vi.mock(import("@t3tools/client-runtime/state/session"), async (original) => {
Expand Down Expand Up @@ -175,7 +176,14 @@ vi.mock(import("../../state/terminalSessions"), async (original) => {
});
vi.mock(import("../ThreadTerminalDrawer"), async (original) => ({
...(await original()),
TerminalViewport: () => <div />,
TerminalViewport: ({ terminalId, onSessionExited }) => (
<div
ref={() => {
state.attachedTerminals.set(terminalId, onSessionExited);
return () => void state.attachedTerminals.delete(terminalId);
}}
/>
),
}));
vi.mock(import("../../state/use-atom-command"), async (original) => ({
...(await original()),
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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),
Expand All @@ -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<ReadonlyArray<ServerProvider>>, [
{ ...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 () => {
Expand Down
11 changes: 9 additions & 2 deletions apps/web/src/components/onboarding/WelcomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down Expand Up @@ -467,6 +468,9 @@ function AgentsSection({
const [addingProvider, setAddingProvider] = useState(false);
const [codexSetupId, setCodexSetupId] = useState<ProviderInstanceId | null>(null);
const [cliTerminalFor, setCliTerminalFor] = useState<ProviderInstanceId | null>(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(() => {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -739,7 +745,8 @@ function AgentsSection({
{isPrimary ? <CliPathSetup /> : null}
{terminalSession !== null ? (
<AgentInstallTerminal
key={`${terminalSession.environmentId}:${terminalSession.providerInstanceId}:${terminalSession.driver}`}
// A new step gets a new terminal: an attached viewport reads its PTY closing as an exit.
key={`${terminalSession.environmentId}:${terminalSession.providerInstanceId}:${terminalSession.driver}:${terminalSession.step}`}
session={terminalSession}
onCommandFinished={() =>
void refreshProviders({
Expand Down
Loading