Skip to content
Merged
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
29 changes: 19 additions & 10 deletions packages/spark/src/clack-wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,25 @@ export async function runClackWizard(deps: WizardDeps): Promise<WizardResult> {
}
}

const session =
deps.options.apiKey !== undefined && deps.options.projectId !== undefined
? await loginWithCiCredentials({
apiKey: deps.options.apiKey,
projectId: deps.options.projectId,
apiUrl: deps.options.apiUrl,
})
: await loginWithBrowser(deps, {
authMode: (await hasBraintrustAccount()) ? "signin" : "signup",
});
let session: WizardSessionCompleteResult;
if (
deps.options.apiKey !== undefined &&
deps.options.projectId !== undefined
) {
session = await loginWithCiCredentials({
apiKey: deps.options.apiKey,
projectId: deps.options.projectId,
apiUrl: deps.options.apiUrl,
});
} else {
const authMode =
deps.options.orgId !== undefined && deps.options.projId !== undefined
? "signin"
: (await hasBraintrustAccount())
? "signin"
: "signup";
session = await loginWithBrowser(deps, { authMode });
}

await writeLocalEnvBraintrust(deps, session.apiKey);

Expand Down
33 changes: 33 additions & 0 deletions packages/spark/test/clack-wizard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ function buildDeps(
readonly codingTools?: CodingToolRuntime;
readonly braintrustCli?: BraintrustCliRuntime;
readonly writeClipboard?: (text: string) => Promise<void>;
readonly options?: Partial<WizardDeps["options"]>;
} = {},
): WizardDeps {
const cwd = args.cwd ?? createGitTempDir();
Expand Down Expand Up @@ -330,6 +331,7 @@ function buildDeps(
orgId: undefined,
projId: undefined,
yolo: false,
...args.options,
},
loginWithWizardSession: stubLogin,
openBrowser: () => Promise.resolve(true),
Expand Down Expand Up @@ -700,6 +702,37 @@ describe("runClackWizard", () => {
}
});

it("skips the account question when browser org and project ids are provided", async () => {
let loginUrlParams: WizardSessionLoginArgs["loginUrlParams"];
const { events } = createPrompts({
selects: ["no", "manual", "confirm", "checked", "confirmed"],
});
const deps = buildDeps({
options: { orgId: "org-123", projId: "proj-456" },
loginWithWizardSession: async ({ events, loginUrlParams: params }) => {
loginUrlParams = params;
events.onLoginUrl({
loginUrl: "https://app.test/app/cli-login?session_token=test",
expiresAt: "2099-01-01T00:00:00.000Z",
verificationCode: "123456",
});
await events.onTryOpenBrowser(
"https://app.test/app/cli-login?session_token=test",
);
return DEFAULT_LOGIN_RESULT;
},
});

await runClackWizard(deps);

expect(events).not.toContain(`select:${ACCOUNT_QUESTION}`);
expect(loginUrlParams).toEqual({
orgId: "org-123",
projectId: "proj-456",
authMode: "signin",
});
});

it("overwrites existing local token files without prompting", async () => {
const cwd = createGitTempDir();
const envFilePath = join(cwd, ".env.braintrust");
Expand Down
Loading