116 lines
5.3 KiB
TypeScript
116 lines
5.3 KiB
TypeScript
/**
|
||
* E2E (2026-10-14): alone in a lobby, the browser's back button (Alt+← / Ctrl+←) takes me home and the empty room closes
|
||
* at once — no "하던 게임으로 돌아가기", not in the room list, the code no longer opens. 📖 규칙 in the room is a popup
|
||
* (it used to go to the rules page, which would now close the room). With a friend in the lobby, back keeps my seat.
|
||
* Run: E2E_PORT=4663 bun e2e/room-back.e2e.ts (needs apps/web/dist built)
|
||
*/
|
||
import { chromium, type Page } from 'playwright-core';
|
||
import { mkdirSync, mkdtempSync } from 'node:fs';
|
||
import { join } from 'node:path';
|
||
import { loadConfig } from '../apps/server/src/config';
|
||
import { startServer } from '../apps/server/src/server';
|
||
|
||
const PORT = Number(process.env.E2E_PORT ?? 4663);
|
||
const ORIGIN = `http://localhost:${PORT}`;
|
||
const SHOTS = process.env.E2E_SHOTS ?? join(import.meta.dir, '../test-results');
|
||
mkdirSync(SHOTS, { recursive: true });
|
||
|
||
const dbDir = mkdtempSync(join(process.env.TMPDIR ?? '/tmp', 'bg-e2e-'));
|
||
const app = startServer({
|
||
config: loadConfig({ PORT: String(PORT), DB_PATH: join(dbDir, 'app.db'), PUBLIC_ORIGIN: ORIGIN }),
|
||
log: () => {},
|
||
staticDir: join(import.meta.dir, '../apps/web/dist'),
|
||
});
|
||
|
||
const browser = await chromium.launch({ executablePath: process.env.CHROME ?? '/usr/bin/google-chrome', headless: true });
|
||
let failed = false;
|
||
const step = async (name: string, fn: () => Promise<void>) => {
|
||
const t0 = performance.now();
|
||
try {
|
||
await fn();
|
||
console.log(`✓ ${name} (${Math.round(performance.now() - t0)}ms)`);
|
||
} catch (e) {
|
||
failed = true;
|
||
console.log(`✗ ${name}: ${(e as Error).message.split('\n')[0]}`);
|
||
throw e;
|
||
}
|
||
};
|
||
|
||
async function makeRoom(p: Page, name?: string): Promise<string> {
|
||
await p.goto(ORIGIN + '/');
|
||
await p.getByRole('button', { name: '+ 방 만들기' }).click();
|
||
await p.getByRole('dialog', { name: '게임 고르기' }).getByRole('button', { name: /오목/ }).click();
|
||
if (name) {
|
||
await p.getByLabel('내 이름(닉네임)').fill(name);
|
||
await p.getByRole('button', { name: '이 이름으로 시작하기' }).click();
|
||
}
|
||
await p.waitForURL(/\/r\/\d{6}$/);
|
||
await p.getByRole('button', { name: '규칙 보기' }).waitFor();
|
||
return p.url().slice(-6);
|
||
}
|
||
const roomOpen = async (code: string) => {
|
||
const r = await fetch(`${ORIGIN}/api/rooms/${code}`);
|
||
if (r.status !== 200 && r.status !== 404) throw new Error(`room lookup ${r.status}`);
|
||
return r.ok;
|
||
};
|
||
|
||
try {
|
||
const ctxA = await browser.newContext({ viewport: { width: 1440, height: 900 }, locale: 'ko-KR' });
|
||
const ctxB = await browser.newContext({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true, locale: 'ko-KR' });
|
||
const a = await ctxA.newPage();
|
||
const b = await ctxB.newPage();
|
||
const errors: string[] = [];
|
||
for (const p of [a, b]) p.on('pageerror', (e) => errors.push(e.message));
|
||
for (const p of [a, b]) await p.addInitScript(() => localStorage.setItem('bg:qs:omok', '1'));
|
||
|
||
let code = '';
|
||
await step('혼자 방 만들기 → 📖 규칙은 팝업(주소 그대로, 방 그대로)', async () => {
|
||
code = await makeRoom(a, '호랑이');
|
||
await a.getByRole('button', { name: '규칙 보기' }).click();
|
||
const sheet = a.getByRole('dialog', { name: '오목 규칙' });
|
||
await sheet.waitFor();
|
||
if (!a.url().endsWith(`/r/${code}`)) throw new Error(`navigated to ${a.url()}`);
|
||
await a.waitForTimeout(600);
|
||
await a.screenshot({ path: join(SHOTS, 'room-rules-popup.png') });
|
||
await sheet.getByRole('button', { name: '닫기' }).click();
|
||
if (!(await roomOpen(code))) throw new Error('the room closed while reading the rules');
|
||
});
|
||
|
||
await step('혼자인 대기실에서 브라우저 뒤로가기 → 홈, 방이 바로 없어짐', async () => {
|
||
await a.goBack();
|
||
await a.waitForURL(ORIGIN + '/');
|
||
for (let i = 0; i < 6 && (await roomOpen(code)); i++) await a.waitForTimeout(500);
|
||
if (await roomOpen(code)) throw new Error('the empty room is still open');
|
||
await a.reload();
|
||
await a.getByRole('button', { name: '+ 방 만들기' }).waitFor();
|
||
if (await a.getByText(/하던 게임으로 돌아가기/).count()) throw new Error('home still offers to go back to the room');
|
||
await a.screenshot({ path: join(SHOTS, 'room-back-home.png') });
|
||
});
|
||
|
||
await step('친구가 있는 대기실에서 뒤로가기 → 내 자리는 남고, 다시 들어갈 수 있음', async () => {
|
||
code = await makeRoom(a);
|
||
await b.goto(`${ORIGIN}/r/${code}`);
|
||
await b.getByLabel('내 이름(닉네임)').fill('고양이');
|
||
await b.getByRole('button', { name: '이 이름으로 입장하기' }).click();
|
||
await a.getByLabel('자리').getByText('고양이').waitFor();
|
||
await a.goBack();
|
||
await a.waitForURL(ORIGIN + '/');
|
||
await a.waitForTimeout(500);
|
||
if (!(await roomOpen(code))) throw new Error('the room closed although a friend is in it');
|
||
await b.getByLabel('자리').getByText('호랑이').waitFor({ timeout: 3000 });
|
||
await a.goForward();
|
||
await a.waitForURL(`${ORIGIN}/r/${code}`);
|
||
await a.getByLabel('자리').getByText('고양이').waitFor();
|
||
});
|
||
|
||
if (errors.length) throw new Error(`page errors: ${errors.join(' | ')}`);
|
||
} catch (e) {
|
||
failed = true;
|
||
console.log(String((e as Error).stack ?? e));
|
||
} finally {
|
||
await browser.close();
|
||
await app.stop();
|
||
}
|
||
console.log(failed ? 'E2E FAILED' : 'E2E PASSED');
|
||
process.exit(failed ? 1 : 0);
|