import path from 'node:path' import { spawn, type ChildProcess } from 'node:child_process' import { type ElectronApplication, type Page, type JSHandle, expect, test, _electron as electron, } from '@playwright/test' import type { BrowserWindow } from 'electron' const root = path.resolve(import.meta.dirname, '..', '..') let electronApp: ElectronApplication let page: Page let xvfbProcess: ChildProcess | undefined function startXvfbOnLinux(): Promise { if (process.platform !== 'linux' || process.env.DISPLAY) { return Promise.resolve() } return new Promise((resolve, reject) => { xvfbProcess = spawn('Xvfb', [':99', '-screen', '0', '1280x720x24', '-ac'], { stdio: 'ignore', detached: true, }) xvfbProcess.once('error', reject) setTimeout(() => { process.env.DISPLAY = ':99' resolve() }, 500) }) } test.beforeAll(async () => { test.setTimeout(30000) await startXvfbOnLinux() electronApp = await electron.launch({ args: ['.', '--no-sandbox', `--user-data-dir=${path.join(root, 'test-results', `electron-${process.pid}`)}`], cwd: root, env: { ...process.env, NODE_ENV: 'development' }, }) page = await electronApp.firstWindow() const mainWin: JSHandle = await electronApp.browserWindow(page) await mainWin.evaluate(async (win) => { win.webContents.executeJavaScript('console.log("Execute JavaScript with e2e testing.")') }) }) test.afterAll(async () => { if (page) { await page.screenshot({ path: 'test/screenshots/e2e.png' }) await page.close() } if (electronApp) { await electronApp.close() } if (xvfbProcess?.pid) { process.kill(-xvfbProcess.pid) xvfbProcess = undefined } }) test.describe('[escape-reserve] e2e tests', () => { test('startup', async () => { const title = await page.title() expect(title).toBe('ESCAPE / RESERVE') }) test('loads the reservation desk', async () => { await expect(page.locator('h1')).toHaveText('방을 고르고, 탈출을 예약하세요.') }) test('exposes the reservation bridge', async () => { const methods = await page.evaluate(() => Object.keys(window.reservationApi).sort()) expect(methods).toEqual([ 'armAutoReservation', 'armAutoSalvo', 'configureClock', 'disarmAutoReservation', 'disarmAutoSalvo', 'getAutoReservationState', 'getAutoSalvoState', 'getBranches', 'getClockState', 'getThemeDetail', 'getThemes', 'getTimes', 'openCaptcha', 'prepare', 'runTimingTest', 'startClockMonitor', 'stopClockMonitor', 'submit', 'subscribeAutoSalvo', 'subscribeAutoState', 'subscribeClock', ]) }) test('loads live branches and themes without submitting', async () => { const branch = page.getByLabel('지점') await expect.poll(() => branch.locator('option').count()).toBeGreaterThan(1) await branch.selectOption({ label: 'STATION' }) const theme = page.getByLabel('테마') await expect.poll(() => theme.locator('option').count(), { timeout: 15_000, }).toBeGreaterThan(1) }) test('monitors live server time and runs a safe timing probe', async () => { await page.getByRole('button', { name: '자동 예약' }).click() await page.getByRole('button', { name: '정밀 보정 설정' }).click() await expect(page.getByText('경계 측정 간격 (ms)')).toBeVisible() await page.getByLabel('전송 모드').selectOption('http2') await page.getByRole('button', { name: '설정 적용' }).click() await page.getByRole('button', { name: '모니터 시작' }).click() await expect(page.getByRole('button', { name: '모니터 중지' })).toBeVisible({ timeout: 10_000 }) await expect(page.getByLabel('관측 서버시간 값')).toContainText(':') await expect.poll(() => page.evaluate(async () => (await window.reservationApi.getClockState()).protocol), { timeout: 10_000, }).toBe('h2') await page.getByRole('button', { name: '3초 테스트' }).click() await expect(page.getByText('arrival error')).toBeVisible({ timeout: 10_000 }) const h2State = await page.evaluate(() => window.reservationApi.getClockState()) expect(h2State.protocol).toBe('h2') expect(h2State.connectionId).toMatch(/^h2-\d+$/) expect(h2State.connectionReused).toBe(true) }) test('arms and disarms an automatic reservation without submitting', async () => { const theme = page.getByLabel('테마') await theme.selectOption({ index: 1 }) await expect(page.locator('button.time-slot').first()).toBeVisible({ timeout: 15_000 }) await page.locator('button.time-slot').first().click() await page.getByPlaceholder('홍길동').fill('테스트') await page.getByPlaceholder('010-0000-0000').fill('010-1234-5678') await page.getByLabel('캡차 토큰').fill('x'.repeat(100)) await page.getByRole('checkbox', { name: '개인정보 수집' }).check() const target = await page.evaluate(() => { const value = new Date(Date.now() + 30_000 + 9 * 60 * 60 * 1_000) return value.toISOString().slice(0, -1) }) await page.getByLabel('실행 시각 (KST, ms)').fill(target) await page.getByRole('button', { name: '자동 예약 시작' }).click() await expect(page.getByText('armed', { exact: true })).toBeVisible({ timeout: 10_000 }) await page.getByRole('button', { name: '자동 예약 해제' }).click() await expect(page.getByText('cancelled', { exact: true })).toBeVisible() }) })