| 1 | import assert from "node:assert/strict"; |
| 2 | import test from "node:test"; |
| 3 | import { LoginBrowser } from "./browser-login.mjs"; |
| 4 | |
| 5 | test("login redirects and popups stay private, closing one login leaves the other open", async () => { |
| 6 | const closed = []; |
| 7 | let contexts = 0; |
| 8 | const browser = new LoginBrowser(async (method, params, sessionId) => { |
| 9 | if (method === "Target.createBrowserContext") return { browserContextId: String(++contexts) }; |
| 10 | if (method === "Target.createTarget") { |
| 11 | assert.equal(params.url, "about:blank", "login must be prepared before navigation"); |
| 12 | return { targetId: "tab-" + params.browserContextId }; |
| 13 | } |
| 14 | if (method === "Page.navigate") assert.equal(sessionId, "prepared-page"); |
| 15 | if (method === "Target.disposeBrowserContext") closed.push(params.browserContextId); |
| 16 | }); |
| 17 | const page = { url: "https://claude.ai/oauth/authorize?state=secret", title: "secret" }; |
| 18 | assert.equal(browser.tab(page), page); |
| 19 | for (const id of ["a", "b"]) { |
| 20 | await browser.open(id.repeat(24), page.url, async tab => { |
| 21 | assert.equal(browser.tab(tab).url, "https://claude.ai"); |
| 22 | return "prepared-page"; |
| 23 | }); |
| 24 | } |
| 25 | const popup = { ...page, targetId: "popup", browserContextId: "1", url: "http://localhost:9123/callback?code=secret" }; |
| 26 | const privateTab = browser.tab(popup); |
| 27 | assert.equal(privateTab.title, "账号授权"); |
| 28 | assert.equal(privateTab.url, "http://localhost:9123"); |
| 29 | await browser.finish("a".repeat(24)); |
| 30 | assert.deepEqual(closed, ["1"]); |
| 31 | assert.equal(browser.tab(popup, privateTab).url, privateTab.url); |
| 32 | await browser.finish("b".repeat(24)); |
| 33 | assert.deepEqual(closed, ["1", "2"]); |
| 34 | }); |