Файли
openidea/src/platform/deviceCode.test.ts
T
Deng Liuтаdengliu 4c5f4fa1e1 feat(github): add in-IDE GitHub account sign-in in Settings, Pull Requests, and Clone (#6642)
Settings › Version Control › GitHub only listed accounts already signed in with gh, Clone Repository › GitHub told signed-out users to run gh auth login in a terminal, and Pull Requests opened a raw terminal tab.

The PTY sign-in dialog and OAuth device-code parser move from features/agents to src/workbench/CliSignInDialog.tsx and src/platform/deviceCode.ts, so agents and GitHub share them without a cross-feature import. GitHub settings get Sign In… / Add account… (selecting a newly added second account for the project) and Install GitHub CLI when gh is missing; the Pull Requests tool window signs in in-IDE and reconnects, with Add account… in its error state, repository selector and account menu; Clone › GitHub gets Sign In… and Add account…. The merge with main carries the dialog's terminal-reply filtering and FIFO input over to the moved file.

Co-authored-by: dengliu <[email protected]>
2026-11-29 12:29:38 +08:00

81 рядки
3.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { appendLoginOutput, parseDeviceCodePrompt } from "./deviceCode";
describe("parseDeviceCodePrompt", () => {
it("parses Copilot CLI Ink output with SGR styling, OSC-8 links, and cursor-forward escapes", () => {
const raw =
"\x1b[?25l⠋ Waiting for authorization...\r\n" +
"Enter\x1b[1Cone-time\x1b[1Ccode:\x1b[1C\x1b[1m\x1b[36mWDJB-MJHT\x1b[39m\x1b[22m\x1b[1Cat\x1b[1C" +
"\x1b]8;;https://github.com/login/device\x07\x1b[4mhttps://github.com/login/device\x1b[24m\x1b]8;;\x07\r\n" +
"Press any key to copy to clipboard and open browser...";
expect(parseDeviceCodePrompt(raw)).toEqual({
code: "WDJB-MJHT",
url: "https://github.com/login/device",
needsEnterToPoll: false,
});
});
it("parses GitHub CLI device-code output across chunks and marks Press Enter to open", () => {
let buffer = "";
buffer = appendLoginOutput(buffer, "! First copy your one-time code: 9F4K-2L8P\r\n");
expect(parseDeviceCodePrompt(buffer)).toBeNull();
buffer = appendLoginOutput(buffer, "Press Enter to open https://github.com/login/device in your browser... ");
expect(parseDeviceCodePrompt(buffer)).toEqual({
code: "9F4K-2L8P",
url: "https://github.com/login/device",
needsEnterToPoll: true,
});
});
it("parses Codex, Microsoft devicelogin, and RFC 8628 verification_uri_complete prompts", () => {
expect(parseDeviceCodePrompt("│ Visit https://auth.openai.com/codex/device and enter code: ABCD-EFGH │")).toEqual({
code: "ABCD-EFGH",
url: "https://auth.openai.com/codex/device",
needsEnterToPoll: false,
});
expect(
parseDeviceCodePrompt(
"To sign in, use a web browser to open https://microsoft.com/devicelogin and enter the code K9P4M2X7Q to authenticate.",
),
).toEqual({
code: "K9P4M2X7Q",
url: "https://microsoft.com/devicelogin",
needsEnterToPoll: false,
});
expect(
parseDeviceCodePrompt("Open https://auth.example.com/activate?user_code=zx90-12ab to finish signing in."),
).toEqual({
code: "ZX90-12AB",
url: "https://auth.example.com/activate?user_code=zx90-12ab",
needsEnterToPoll: false,
});
});
it("updates to the newest code when an expired device code is re-issued", () => {
const raw =
"Enter one-time code: AAAA-1111 at https://github.com/login/device\n" +
"Code expired. Enter one-time code: BBBB-2222 at https://github.com/login/device\n";
expect(parseDeviceCodePrompt(raw)?.code).toBe("BBBB-2222");
});
it("matches all-caps code labels and preserves balanced parentheses in URLs", () => {
expect(parseDeviceCodePrompt("ONE-TIME CODE: K9P4M2X7Q at (https://auth.example.com/device/v1(beta)).")).toEqual({
code: "K9P4M2X7Q",
url: "https://auth.example.com/device/v1(beta)",
needsEnterToPoll: false,
});
});
it("ignores regular browser OAuth callback URLs with UUIDs and copyright year ranges", () => {
expect(
parseDeviceCodePrompt(
"Copyright 2024-2026 Anthropic\n" +
"Browser didn't open? Visit https://claude.ai/oauth/authorize?client_id=9D1C-4567-ABCD-EF01&state=XY12-3456",
),
).toBeNull();
});
});