diff --git a/packages/inkstone/browser.test.mjs b/packages/inkstone/browser.test.mjs index a012686..0d3a9f0 100644 --- a/packages/inkstone/browser.test.mjs +++ b/packages/inkstone/browser.test.mjs @@ -116,3 +116,25 @@ test('off Windows the login wait ends on process exit, never on a title', async } }) +test('the window title is read on Windows only, even when it already shows the console (#824)', async () => { + const { spawn } = await import('node:child_process') + const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 10000)'], { stdio: 'ignore' }) + try { + // a title that would end the wait at once, if anything read it + let reads = 0 + const titleOf = () => { + reads += 1 + return '书生·端砚 Intern InkStone' + } + for (const platform of ['darwin', 'linux']) { + reads = 0 + expect(await waitForConsoleWindow(child, { platform, titleOf, settleMs: 0, timeoutMs: 200, pollMs: 20 })).toBe('timeout') + expect(reads).toBe(0) + } + // the same title does end it on Windows, so the check above is not empty + expect(await waitForConsoleWindow(child, { platform: 'win32', titleOf, settleMs: 0, timeoutMs: 2000, pollMs: 20 })).toBe('console') + } finally { + try { child.kill() } catch {} + } +}) + diff --git a/packages/inkstone/fake-browser.test.mjs b/packages/inkstone/fake-browser.test.mjs index dca7f6b..f0bc01e 100644 --- a/packages/inkstone/fake-browser.test.mjs +++ b/packages/inkstone/fake-browser.test.mjs @@ -13,13 +13,15 @@ import { renewSilently } from "./lib/browser.mjs" import { fakeInkStone, json, ok, fail } from "./fake.mjs" let fakeExe = "" +let fakeDir = "" let profile = "" const savedEnv = {} let f beforeAll(() => { // compile the fake browser once (bun build --compile takes under a second) - const out = join(mkdtempSync(join(tmpdir(), "inkstone-fake-browser-")), process.platform === "win32" ? "fake-chrome.exe" : "fake-chrome") + fakeDir = mkdtempSync(join(tmpdir(), "inkstone-fake-browser-")) + const out = join(fakeDir, process.platform === "win32" ? "fake-chrome.exe" : "fake-chrome") const entry = join(import.meta.dir, "testdata/fake-browser.mjs") execSync(`${JSON.stringify(process.execPath)} build --compile ${JSON.stringify(entry)} --outfile ${JSON.stringify(out)}`, { stdio: "pipe" }) fakeExe = out @@ -42,9 +44,12 @@ afterAll(() => { if (value === undefined) delete process.env[key] else process.env[key] = value } - try { - rmSync(profile, { recursive: true, force: true }) - } catch {} + // the compiled fake is ~59 MB: never leave it behind (#824) + for (const dir of [profile, fakeDir]) { + try { + if (dir) rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 }) + } catch {} + } }) afterEach(() => { diff --git a/packages/inkstone/lib/browser.mjs b/packages/inkstone/lib/browser.mjs index c83fb6d..ecfca45 100644 --- a/packages/inkstone/lib/browser.mjs +++ b/packages/inkstone/lib/browser.mjs @@ -531,7 +531,10 @@ export async function closeWindowGracefully(child, { timeoutMs = 15_000, log = ( * 登录窗口的完成等待。可测试:`platform` 参数让非 Windows 的"只认进程退出" * 行为在任何宿主上都能被验证。 */ -export async function waitForConsoleWindow(child, { timeoutMs = 8 * 60_000, pollMs = 1000, signal, log = () => {}, platform = process.platform } = {}) { +export async function waitForConsoleWindow( + child, + { timeoutMs = 8 * 60_000, pollMs = 1000, signal, log = () => {}, platform = process.platform, titleOf = windowTitleOf, settleMs = 5000 } = {}, +) { const deadline = Date.now() + timeoutMs const started = Date.now() // 窗口标题只能靠 Windows 的 Get-Process 读;别的平台就以“用户关掉窗口” @@ -546,7 +549,7 @@ export async function waitForConsoleWindow(child, { timeoutMs = 8 * 60_000, poll continue } // 只看我们自己的窗口;并且给它至少 5 秒,控制台标题在跳转 SSO 前会闪一下。 - const title = Date.now() - started > 5000 ? windowTitleOf(child.pid) : "" + const title = Date.now() - started >= settleMs ? titleOf(child.pid) : "" if (title && /Intern InkStone|端砚/i.test(title) && !/用户认证|登录/i.test(title)) { hits += 1 if (hits >= 2) return "console"