diff --git a/packages/adapters/claude-local/src/server/setup-token-parse.test.ts b/packages/adapters/claude-local/src/server/setup-token-parse.test.ts index 4205453ae4e..7837b192aa2 100644 --- a/packages/adapters/claude-local/src/server/setup-token-parse.test.ts +++ b/packages/adapters/claude-local/src/server/setup-token-parse.test.ts @@ -122,6 +122,36 @@ describe("parseSetupTokenPrompt", () => { expect(result?.prompt).toBe(SETUP_TOKEN_PROMPT); }); + it("keeps joining a hard-wrapped URL past a full-width row that already validates", () => { + const wrapped = hardWrap(CLAUDE_COM_URL, 64).split("\n"); + const lastRow = wrapped[wrapped.length - 1]; + expect(STATE.length - lastRow.length).toBeGreaterThanOrEqual(16); + const text = [ + PREAMBLE_LINE, + ...wrapped, + "Hold Shift while selecting to use your terminal's native copy", + PROMPT_LINE, + ].join("\n"); + const result = parseSetupTokenPrompt(text); + expect(result?.url).toBe(CLAUDE_COM_URL); + expect(result?.prompt).toBe(SETUP_TOKEN_PROMPT); + }); + + it("returns the full URL for every hard-wrap width from 40 to 200 columns", () => { + for (const url of [CLAUDE_AI_URL, CLAUDE_COM_URL]) { + for (let width = 40; width <= 200; width += 1) { + const text = [PREAMBLE_LINE, hardWrap(url, width), PROMPT_LINE].join("\n"); + expect(parseSetupTokenPrompt(text)?.url, `${url.slice(0, 20)} at ${width}`).toBe(url); + } + } + }); + + it("returns null, not a truncated URL, when a two-row wrap leaves the first row valid", () => { + const width = CLAUDE_COM_URL.length - 8; + const text = [PREAMBLE_LINE, hardWrap(CLAUDE_COM_URL, width), PROMPT_LINE].join("\n"); + expect(parseSetupTokenPrompt(text)).toBeNull(); + }); + it("returns the URL when the output arrives split across chunks", () => { // The streaming caller accumulates chunks and re-parses the whole buffer. The // chunk boundary falls inside the URL token with no line break, so the joined diff --git a/packages/adapters/claude-local/src/server/setup-token-parse.ts b/packages/adapters/claude-local/src/server/setup-token-parse.ts index d4f704d67aa..b52a472c11f 100644 --- a/packages/adapters/claude-local/src/server/setup-token-parse.ts +++ b/packages/adapters/claude-local/src/server/setup-token-parse.ts @@ -360,22 +360,26 @@ function validateAuthUrl(candidate: string): string | null { * The parser starts at `startLine`, then joins at most {@link MAX_URL_LINES} * consecutive URL-character-only physical lines into at most {@link * MAX_URL_LENGTH} characters. It validates the joined string after each join and - * returns the first joined string that passes {@link validateAuthUrl}. A CLI that - * emits the whole URL on one line passes on the first join. A CLI that wraps the - * URL passes once the parser joins the last fragment. Returns null when no bounded - * join validates. + * returns the last joined string that passes {@link validateAuthUrl} before a + * row shorter than the first row ends the wrap. A CLI that emits the whole URL + * on one line passes on the first join. A CLI that wraps the URL passes once the + * parser joins the last fragment. Returns null when no bounded join validates. */ function reassembleUrl(lines: string[], startLine: number): SetupTokenUrlMatch | null { let joined = ""; + let firstWidth = 0; + let match: SetupTokenUrlMatch | null = null; for (let offset = 0; offset < MAX_URL_LINES && startLine + offset < lines.length; offset += 1) { const trimmed = lines[startLine + offset].trim(); if (trimmed.length === 0 || !URL_CHARS_ONLY_RE.test(trimmed)) break; + if (offset === 0) firstWidth = trimmed.length; joined += trimmed; if (joined.length > MAX_URL_LENGTH) break; const url = validateAuthUrl(joined); - if (url) return { url, endLine: startLine + offset }; + if (url) match = { url, endLine: startLine + offset }; + if (offset === 0 ? match !== null : trimmed.length < firstWidth) break; } - return null; + return match; } /**