|
| 1 | +import { mkdtempSync, rmSync } from 'node:fs' |
| 2 | +import { createServer, type Server } from 'node:http' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { fileURLToPath } from 'node:url' |
| 6 | +import { type ElectronApplication, _electron as electron, expect, test } from '@playwright/test' |
| 7 | +import type { SimDesktopApi } from '@sim/desktop-bridge' |
| 8 | +import { recordCheck } from './check-report' |
| 9 | + |
| 10 | +const DESKTOP_DIR = fileURLToPath(new URL('..', import.meta.url)) |
| 11 | +const SCOPE = 'continue-sign-in-fixture' |
| 12 | + |
| 13 | +/** |
| 14 | + * "Continue in Chrome" through the real preload, IPC table, and browser session. |
| 15 | + * |
| 16 | + * The shell runs with an empty home directory, so no browser profile exists and |
| 17 | + * nothing here can launch the developer's browser or reach their Keychain. That |
| 18 | + * is also the unsupported case the web app must hide the feature for. The |
| 19 | + * import-back half against a real Chrome profile needs a Keychain item and is |
| 20 | + * covered by the importer's fixture-database tests instead. |
| 21 | + */ |
| 22 | +test.describe('continue a sign-in in another browser', () => { |
| 23 | + let server: Server |
| 24 | + let origin: string |
| 25 | + let site: string |
| 26 | + let home: string |
| 27 | + let userData: string |
| 28 | + let app: ElectronApplication | undefined |
| 29 | + |
| 30 | + test.beforeAll(async () => { |
| 31 | + server = createServer((request, response) => { |
| 32 | + const path = new URL(request.url ?? '/', 'http://localhost').pathname |
| 33 | + if (path === '/api/auth/get-session') { |
| 34 | + response.writeHead(200, { 'Content-Type': 'application/json' }) |
| 35 | + response.end( |
| 36 | + JSON.stringify({ user: { id: 'fixture-user' }, session: { id: 'fixture-session' } }) |
| 37 | + ) |
| 38 | + return |
| 39 | + } |
| 40 | + if (path.startsWith('/api/')) { |
| 41 | + response.writeHead(200, { 'Content-Type': 'application/json' }) |
| 42 | + response.end('{}') |
| 43 | + return |
| 44 | + } |
| 45 | + if (request.headers.host?.startsWith('localhost')) { |
| 46 | + response.writeHead(200, { 'Content-Type': 'text/html' }) |
| 47 | + response.end('<!doctype html><title>Sign in</title><h1>Sign in</h1>') |
| 48 | + return |
| 49 | + } |
| 50 | + response.writeHead(200, { |
| 51 | + 'Content-Type': 'text/html', |
| 52 | + 'Set-Cookie': 'better-auth.session_token=fixture; HttpOnly; SameSite=Lax; Path=/', |
| 53 | + }) |
| 54 | + response.end(`<!doctype html><title>Sim fixture</title><h1>Continue fixture</h1> |
| 55 | +<button id="open">Open</button><button id="continue">Continue</button><button id="back">Back</button> |
| 56 | +<script> |
| 57 | + const api = globalThis.simDesktop |
| 58 | + const scope = ${JSON.stringify(SCOPE)} |
| 59 | + const record = (key) => (value) => { globalThis[key] = value } |
| 60 | + document.getElementById('open').onclick = () => |
| 61 | + api.browserAgent.openUrl(location.origin.replace('127.0.0.1', 'localhost') + '/login', scope) |
| 62 | + .then(record('opened')) |
| 63 | + document.getElementById('continue').onclick = () => |
| 64 | + api.browserImport.continueSignIn('chrome:Default', scope).then(record('continued')) |
| 65 | + document.getElementById('back').onclick = () => |
| 66 | + api.browserImport.bringSignInBack(scope).then(record('broughtBack')) |
| 67 | +</script>`) |
| 68 | + }) |
| 69 | + await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve)) |
| 70 | + const address = server.address() |
| 71 | + if (!address || typeof address === 'string') throw new Error('Missing fixture address') |
| 72 | + origin = `http://127.0.0.1:${address.port}` |
| 73 | + site = `http://localhost:${address.port}` |
| 74 | + }) |
| 75 | + |
| 76 | + test.beforeEach(async () => { |
| 77 | + home = mkdtempSync(join(tmpdir(), 'sim-continue-home-')) |
| 78 | + userData = mkdtempSync(join(tmpdir(), 'sim-continue-e2e-')) |
| 79 | + app = await electron.launch({ |
| 80 | + args: [process.env.SIM_DESKTOP_E2E_MAIN ?? '.'], |
| 81 | + cwd: DESKTOP_DIR, |
| 82 | + env: { |
| 83 | + ...process.env, |
| 84 | + HOME: home, |
| 85 | + SIM_DESKTOP_ORIGIN: origin, |
| 86 | + SIM_DESKTOP_USER_DATA: userData, |
| 87 | + }, |
| 88 | + }) |
| 89 | + const page = await app.firstWindow() |
| 90 | + await expect(page.getByRole('heading')).toHaveText('Continue fixture') |
| 91 | + await page.evaluate(async (scope) => { |
| 92 | + const api = (globalThis as typeof globalThis & { simDesktop: SimDesktopApi }).simDesktop |
| 93 | + await api.browserAgent.activateScope(scope) |
| 94 | + api.browserAgent.setPanelBounds({ x: 0, y: 120, width: 800, height: 400 }, null, scope) |
| 95 | + }, SCOPE) |
| 96 | + }) |
| 97 | + |
| 98 | + test.afterEach(async () => { |
| 99 | + const testInfo = test.info() |
| 100 | + recordCheck(process.env.CONTINUE_SIGN_IN_REPORT_PATH, 'continue-sign-in', { |
| 101 | + name: testInfo.title, |
| 102 | + status: testInfo.status ?? 'unknown', |
| 103 | + durationMs: testInfo.duration, |
| 104 | + retry: testInfo.retry, |
| 105 | + }) |
| 106 | + await app?.close() |
| 107 | + app = undefined |
| 108 | + rmSync(userData, { recursive: true, force: true }) |
| 109 | + rmSync(home, { recursive: true, force: true }) |
| 110 | + }) |
| 111 | + |
| 112 | + test.afterAll(async () => { |
| 113 | + await new Promise<void>((resolve, reject) => |
| 114 | + server.close((error) => (error ? reject(error) : resolve())) |
| 115 | + ) |
| 116 | + }) |
| 117 | + |
| 118 | + test('advertises the capability, but offers no browser when none is installed', async () => { |
| 119 | + const page = await app?.firstWindow() |
| 120 | + const surface = await page?.evaluate(async () => { |
| 121 | + const api = (globalThis as typeof globalThis & { simDesktop: SimDesktopApi }).simDesktop |
| 122 | + return { |
| 123 | + continueSignIn: typeof api.browserImport?.continueSignIn, |
| 124 | + bringSignInBack: typeof api.browserImport?.bringSignInBack, |
| 125 | + pending: await api.browserImport?.getPendingSignIn?.('continue-sign-in-fixture'), |
| 126 | + profiles: await api.browserImport?.listChromeProfiles(), |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + expect(surface).toEqual({ |
| 131 | + continueSignIn: 'function', |
| 132 | + bringSignInBack: 'function', |
| 133 | + pending: null, |
| 134 | + profiles: [], |
| 135 | + }) |
| 136 | + }) |
| 137 | + |
| 138 | + test('refuses page script that acts without the user', async () => { |
| 139 | + const page = await app?.firstWindow() |
| 140 | + const results = await page?.evaluate(async (scope) => { |
| 141 | + const api = (globalThis as typeof globalThis & { simDesktop: SimDesktopApi }).simDesktop |
| 142 | + return { |
| 143 | + continued: await api.browserImport?.continueSignIn?.('chrome:Default', scope), |
| 144 | + broughtBack: await api.browserImport?.bringSignInBack?.(scope), |
| 145 | + } |
| 146 | + }, SCOPE) |
| 147 | + |
| 148 | + expect(results).toEqual({ |
| 149 | + continued: { error: 'unknown' }, |
| 150 | + broughtBack: { cookiesImported: 0, cookiesSkipped: 0, error: 'unknown' }, |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + test('reads the page from the tab itself and hands over only https', async () => { |
| 155 | + const page = await app?.firstWindow() |
| 156 | + if (!page || !app) throw new Error('App did not launch') |
| 157 | + const read = (key: string) => page.evaluate((name) => Reflect.get(globalThis, name), key) |
| 158 | + |
| 159 | + await page.locator('#open').click() |
| 160 | + await expect.poll(() => read('opened')).toMatchObject({ activeTabId: expect.any(String) }) |
| 161 | + await expect |
| 162 | + .poll(() => |
| 163 | + app?.evaluate( |
| 164 | + ({ webContents }, target) => |
| 165 | + webContents.getAllWebContents().some((contents) => contents.getURL() === target), |
| 166 | + `${site}/login` |
| 167 | + ) |
| 168 | + ) |
| 169 | + .toBe(true) |
| 170 | + |
| 171 | + await page.locator('#continue').click() |
| 172 | + await expect.poll(() => read('continued')).toEqual({ error: 'unsupported-page' }) |
| 173 | + |
| 174 | + await page.locator('#back').click() |
| 175 | + await expect |
| 176 | + .poll(() => read('broughtBack')) |
| 177 | + .toEqual({ cookiesImported: 0, cookiesSkipped: 0, error: 'unknown' }) |
| 178 | + }) |
| 179 | +}) |
0 commit comments