fix: honor agent for models auth writes (#71933)

Honor the parent `models auth --agent <id>` flag across auth write commands: `add`, `login`, `setup-token`, `paste-token`, and `login-github-copilot`.

The auth helpers now resolve the requested configured agent before choosing the auth-profile store and provider workspace, while preserving default-agent behavior when `--agent` is omitted.

Validation:
- `pnpm test src/cli/models-cli.test.ts src/commands/models/auth.test.ts`
- `pnpm test src/commands/models/auth.test.ts`
- `pnpm docs:check-mdx`
- `pnpm check:changed`
- `pnpm check`
- `pnpm build`
- `pnpm test src/cli/run-main.test.ts`

Full `pnpm test` was also run; it failed in unrelated `src/cli/run-main.test.ts` assertions during the full-suite order, while the exact file passes on both latest main and this branch. The PR diff only touches models auth CLI/auth files, docs, and changelog.

Fixes #71864.

Thanks @neeravmakwana.
This commit is contained in:
Neerav Makwana
2026-04-26 00:30:47 -04:00
committed by GitHub
parent 1252da325f
commit dc9ce2a1bf
6 changed files with 323 additions and 36 deletions

View File

@@ -6,10 +6,19 @@ import { registerModelsCli } from "./models-cli.js";
const mocks = vi.hoisted(() => ({
modelsStatusCommand: vi.fn().mockResolvedValue(undefined),
noopAsync: vi.fn(async () => undefined),
modelsAuthAddCommand: vi.fn().mockResolvedValue(undefined),
modelsAuthLoginCommand: vi.fn().mockResolvedValue(undefined),
modelsAuthPasteTokenCommand: vi.fn().mockResolvedValue(undefined),
modelsAuthSetupTokenCommand: vi.fn().mockResolvedValue(undefined),
}));
const { modelsStatusCommand, modelsAuthLoginCommand } = mocks;
const {
modelsAuthAddCommand,
modelsAuthLoginCommand,
modelsAuthPasteTokenCommand,
modelsAuthSetupTokenCommand,
modelsStatusCommand,
} = mocks;
vi.mock("../commands/models.js", () => ({
modelsStatusCommand: mocks.modelsStatusCommand,
@@ -41,10 +50,10 @@ vi.mock("../commands/models/list.js", () => ({
modelsStatusCommand: mocks.modelsStatusCommand,
}));
vi.mock("../commands/models/auth.js", () => ({
modelsAuthAddCommand: mocks.noopAsync,
modelsAuthAddCommand: mocks.modelsAuthAddCommand,
modelsAuthLoginCommand: mocks.modelsAuthLoginCommand,
modelsAuthPasteTokenCommand: mocks.noopAsync,
modelsAuthSetupTokenCommand: mocks.noopAsync,
modelsAuthPasteTokenCommand: mocks.modelsAuthPasteTokenCommand,
modelsAuthSetupTokenCommand: mocks.modelsAuthSetupTokenCommand,
}));
vi.mock("../commands/models/auth-order.js", () => ({
modelsAuthOrderClearCommand: mocks.noopAsync,
@@ -80,7 +89,10 @@ vi.mock("../commands/models/set-image.js", () => ({
describe("models cli", () => {
beforeEach(() => {
modelsAuthAddCommand.mockClear();
modelsAuthLoginCommand.mockClear();
modelsAuthPasteTokenCommand.mockClear();
modelsAuthSetupTokenCommand.mockClear();
modelsStatusCommand.mockClear();
});
@@ -108,9 +120,10 @@ describe("models cli", () => {
const login = auth?.commands.find((cmd) => cmd.name() === "login-github-copilot");
expect(login).toBeTruthy();
await program.parseAsync(["models", "auth", "login-github-copilot", "--yes"], {
from: "user",
});
await program.parseAsync(
["models", "auth", "--agent", "poe", "login-github-copilot", "--yes"],
{ from: "user" },
);
expect(modelsAuthLoginCommand).toHaveBeenCalledTimes(1);
expect(modelsAuthLoginCommand).toHaveBeenCalledWith(
@@ -118,6 +131,7 @@ describe("models cli", () => {
provider: "github-copilot",
method: "device",
yes: true,
agent: "poe",
}),
expect.any(Object),
);
@@ -134,6 +148,43 @@ describe("models cli", () => {
);
});
it.each([
{
label: "add",
args: ["models", "auth", "--agent", "poe", "add"],
command: modelsAuthAddCommand,
expected: { agent: "poe" },
},
{
label: "login",
args: ["models", "auth", "--agent", "poe", "login", "--provider", "openai-codex"],
command: modelsAuthLoginCommand,
expected: { agent: "poe", provider: "openai-codex" },
},
{
label: "setup-token",
args: ["models", "auth", "--agent", "poe", "setup-token", "--provider", "anthropic"],
command: modelsAuthSetupTokenCommand,
expected: { agent: "poe", provider: "anthropic" },
},
{
label: "paste-token",
args: ["models", "auth", "--agent", "poe", "paste-token", "--provider", "anthropic"],
command: modelsAuthPasteTokenCommand,
expected: { agent: "poe", provider: "anthropic" },
},
{
label: "login-github-copilot",
args: ["models", "auth", "--agent", "poe", "login-github-copilot", "--yes"],
command: modelsAuthLoginCommand,
expected: { agent: "poe", provider: "github-copilot", method: "device", yes: true },
},
])("passes parent --agent to models auth $label", async ({ args, command, expected }) => {
await runModelsCommand(args);
expect(command).toHaveBeenCalledWith(expect.objectContaining(expected), expect.any(Object));
});
it("shows help for models auth without error exit", async () => {
const program = new Command();
program.exitOverride();