- 난이도 1~60(쉬움 1~20·보통 21~40·어려움 41~60): 넣을 때 랜덤, 방장이 대기실에서 [랜덤][쉬움][보통][어려움] 선택(botLevel) → 범위 안 무작위 - 다음 판마다 -2~+2, 처음 단계 ±5·같은 범위 안(drift); 말투는 처음 단계(>30 존댓말) - humanBest 확률을 단계별로 보간(BOT_PLAY_ANCHORS: 1·10·30·50·60) - 운(-1~+1): 카드·주사위 게임에서 시작 전에 시드 후보 1~5개를 작업 스레드에서 끝까지 둬 보고 운에 맞는 것을 고름(luck.ts, luck-worker.ts). 해시 공개는 그대로 - 결과 화면 공정성 확인에 컴퓨터별 단계·운, 섞어 본 후보 수 - 업데이트 알림: 빌드 ID meta + /api/version + welcome.build → 상단 팝업 [지금 새로고침하기] [✕] - 문서 03·04 §5.1·06 §12·07 §11.1·09 §9.1, 테스트(bot-level.room, luck, version, e2e bots·pwa) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
330 lines
19 KiB
TypeScript
330 lines
19 KiB
TypeScript
/**
|
|
* E2E (docs/06 §12): practice against computer players from the UI.
|
|
* 1) PC: 오목 상세 → [컴퓨터와 연습하기] → 대기실(컴퓨터 1) → 시작 → 컴퓨터가 여러 번 받아 둔다.
|
|
* 2) 휴대폰: 원카드 연습 → [컴퓨터 넣기]로 2명, 하나 더 넣었다 빼기 → 시작 → 사람·컴퓨터가 여러 턴.
|
|
* Run: E2E_PORT=4534 bun e2e/bots.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 ?? 4534);
|
|
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;
|
|
}
|
|
};
|
|
|
|
const status = (p: Page) => p.locator('.turn-status').innerText().catch(() => '');
|
|
const resultShown = async (p: Page) => (await p.getByRole('dialog', { name: '게임 결과' }).count()) > 0;
|
|
|
|
async function startPractice(p: Page, gameId: string, nick: string) {
|
|
await p.goto(`${ORIGIN}/g/${gameId}`);
|
|
await p.getByRole('button', { name: '🤖 컴퓨터와 연습하기' }).click();
|
|
await p.getByLabel('내 이름(닉네임)').fill(nick);
|
|
await p.getByRole('button', { name: '이 이름으로 시작하기' }).click();
|
|
await p.waitForURL(/\/r\/\d{6}$/);
|
|
await p.getByLabel('자리').getByText('컴퓨터 1').waitFor();
|
|
await p.getByText('연습 게임이에요. 기록에는 남지 않아요.').first().waitFor();
|
|
}
|
|
|
|
// ---------------------------------------------------------------- omok (PC)
|
|
const PAD = 0.9; // board viewBox padding (coordinate labels)
|
|
async function stones(p: Page): Promise<Set<number>> {
|
|
const pts = await p.locator('svg[role="grid"] circle[r="0.46"]').evaluateAll((els) =>
|
|
els.filter((e) => e.closest('g')?.getAttribute('opacity') !== '0.55').map((e) => [Number(e.getAttribute('cx')), Number(e.getAttribute('cy'))]),
|
|
);
|
|
return new Set(pts.map(([x, y]) => y! * 15 + x!));
|
|
}
|
|
async function clickCell(p: Page, x: number, y: number) {
|
|
const box = (await p.locator('svg[role="grid"]').boundingBox())!;
|
|
const span = 14 + 2 * PAD;
|
|
const px = box.x + ((x + PAD) / span) * box.width;
|
|
const py = box.y + ((y + PAD) / span) * box.height;
|
|
await p.mouse.move(px, py);
|
|
await p.mouse.click(px, py);
|
|
}
|
|
|
|
try {
|
|
const pc = await browser.newContext({ viewport: { width: 1440, height: 900 }, locale: 'ko-KR' });
|
|
const phone = await browser.newContext({ viewport: { width: 390, height: 844 }, deviceScaleFactor: 2, isMobile: true, hasTouch: true, locale: 'ko-KR' });
|
|
// The first-game "30초 규칙 요약" cards are tested in e2e/tutorial.e2e.ts; mark them seen here.
|
|
for (const ctx of [pc, phone]) await ctx.addInitScript(() => { for (const g of ['omok', 'one-card', 'yacht', 'mafia']) localStorage.setItem(`bg:qs:${g}`, '1'); });
|
|
const a = await pc.newPage();
|
|
const b = await phone.newPage();
|
|
const errors: string[] = [];
|
|
for (const p of [a, b]) p.on('pageerror', (e) => errors.push(e.message));
|
|
|
|
await step('오목: 상세 → 컴퓨터와 연습하기 → 대기실', async () => {
|
|
await startPractice(a, 'omok', '호랑이');
|
|
await a.getByText('🤖 컴퓨터').first().waitFor();
|
|
await a.getByText('🤖 연습 게임이에요. 기록에는 남지 않아요.').waitFor();
|
|
await a.screenshot({ path: join(SHOTS, 'bots-1-lobby-pc.png') });
|
|
});
|
|
|
|
await step('오목: 시작 → 컴퓨터가 3번 받아 둔다', async () => {
|
|
await a.getByRole('button', { name: '▶ 게임 시작' }).click();
|
|
await a.locator('svg[role="grid"]').waitFor();
|
|
let answers = 0;
|
|
// Cells around the centre, nearest first.
|
|
const order = Array.from({ length: 225 }, (_, i) => i).sort((i, j) => Math.hypot((i % 15) - 7, Math.floor(i / 15) - 7) - Math.hypot((j % 15) - 7, Math.floor(j / 15) - 7));
|
|
// Three answers at most: even moving first the computer has only four stones, so the game is still running for
|
|
// the steps below.
|
|
for (let turn = 0; turn < 8 && answers < 3; turn++) {
|
|
await a.waitForFunction(() => /내 차례예요|게임이 끝|이겼|졌/.test(document.querySelector('.turn-status')?.textContent ?? ''), null, { timeout: 15000 });
|
|
if (await resultShown(a)) break;
|
|
const before = await stones(a);
|
|
const cell = order.find((i) => !before.has(i) && (i % 15) % 2 === turn % 2)!;
|
|
await clickCell(a, cell % 15, Math.floor(cell / 15));
|
|
// Our stone, then the computer's answer (it thinks ~1 s).
|
|
await a.waitForFunction((n) => document.querySelectorAll('svg[role="grid"] g:not([opacity="0.55"]) > circle[r="0.46"]').length >= n + 2 || !!document.querySelector('[role="dialog"]'), before.size, { timeout: 15000 });
|
|
if (await resultShown(a)) break;
|
|
answers++;
|
|
if (answers === 2) await a.screenshot({ path: join(SHOTS, 'bots-2-omok-pc.png') });
|
|
}
|
|
if (answers < 3) throw new Error(`the computer answered only ${answers} times`);
|
|
console.log(` omok: computer answered ${answers} times`);
|
|
});
|
|
|
|
// ---------------------------------------------------------------- leaving practice rooms (right after omok: its turn timer must not run out first)
|
|
const codeOf = (p: Page) => new URL(p.url()).pathname.split('/').pop()!;
|
|
const userId = (nick: string) => app.db.query<{ id: string }, [string]>('SELECT id FROM users WHERE nickname = ?').get(nick)?.id;
|
|
|
|
await step('오목(게임 중): 사이트 안에서 홈으로·새로고침 → 자리는 남고 접속은 끊김, 사이트가 열려 있으면 30분 뒤 정리', async () => {
|
|
const code = codeOf(a);
|
|
// The room hides the site header (one top bar), so move within the app the way a back gesture would.
|
|
await a.evaluate(() => {
|
|
history.pushState({}, '', '/');
|
|
dispatchEvent(new PopStateEvent('popstate'));
|
|
});
|
|
await a.waitForURL(`${ORIGIN}/`);
|
|
const room = app.rooms.get(code)!;
|
|
for (let i = 0; i < 50 && room.conns.size; i++) await Bun.sleep(20);
|
|
if (room.conns.size !== 0) throw new Error(`room still counts ${room.conns.size} connection(s)`);
|
|
if (room.seatOf(userId('호랑이')!) < 0) throw new Error('seat was not kept');
|
|
app.rooms.sweep();
|
|
if (!app.rooms.get(code)) throw new Error('closed too early');
|
|
// A fresh page load on the home page reconnects (the seat is there), so the room still sees the site as open.
|
|
const id = userId('호랑이')!;
|
|
await a.reload();
|
|
await a.getByRole('link', { name: /하던 게임으로 돌아가기/ }).waitFor();
|
|
for (let i = 0; i < 100 && !app.gateway.isOnline(id); i++) await Bun.sleep(20);
|
|
if (!app.gateway.isOnline(id)) throw new Error('home page did not reconnect');
|
|
if (room.playersOfflineSince((u) => app.gateway.offlineSince(u)) !== null) throw new Error('site counted as closed');
|
|
room.emptySince = Date.now() - 29 * 60_000;
|
|
app.rooms.sweep();
|
|
if (!app.rooms.get(code)) throw new Error('running practice game closed before 30 minutes');
|
|
room.emptySince = Date.now() - 31 * 60_000;
|
|
app.rooms.sweep();
|
|
if (app.rooms.get(code)) throw new Error('practice room nobody looks at was not closed');
|
|
});
|
|
|
|
await step('오목(게임 중): 창을 닫으면 1분 뒤 정리 대상', async () => {
|
|
const ctx = await browser.newContext({ viewport: { width: 1440, height: 900 }, locale: 'ko-KR' });
|
|
await ctx.addInitScript(() => localStorage.setItem('bg:qs:omok', '1'));
|
|
const c = await ctx.newPage();
|
|
c.on('pageerror', (e) => errors.push(e.message));
|
|
await startPractice(c, 'omok', '사자');
|
|
await c.getByRole('button', { name: '▶ 게임 시작' }).click();
|
|
await c.locator('svg[role="grid"]').waitFor();
|
|
const room = app.rooms.get(codeOf(c))!;
|
|
const id = userId('사자')!;
|
|
const off = () => room.playersOfflineSince((u) => app.gateway.offlineSince(u));
|
|
if (off() !== null) throw new Error('counted as gone while playing');
|
|
const before = Date.now();
|
|
await ctx.close();
|
|
for (let i = 0; i < 100 && off() === null; i++) await Bun.sleep(20);
|
|
const since = off();
|
|
if (since === null || since < before) throw new Error(`closing the window was not seen (${since})`);
|
|
if (app.gateway.isOnline(id)) throw new Error('still online after closing');
|
|
app.rooms.sweep();
|
|
if (!app.rooms.get(room.code)) throw new Error('closed before the minute was up');
|
|
});
|
|
|
|
// ---------------------------------------------------------------- one-card (phone)
|
|
await step('원카드: 연습 방 → 컴퓨터 넣기·빼기 → 3명', async () => {
|
|
await startPractice(b, 'one-card', '고양이');
|
|
const seats = b.getByLabel('자리');
|
|
await seats.getByRole('button', { name: '🤖 컴퓨터 넣기' }).click();
|
|
await seats.getByText('컴퓨터 2').waitFor();
|
|
await seats.getByRole('button', { name: '🤖 컴퓨터 넣기' }).click();
|
|
await seats.getByText('컴퓨터 3').waitFor();
|
|
await seats.getByRole('button', { name: '컴퓨터 3 빼기' }).click();
|
|
await seats.getByText('컴퓨터 3').waitFor({ state: 'detached' });
|
|
// Difficulty per computer (docs/06 §12): 랜덤 at first, the host picks 쉬움.
|
|
const level = seats.getByRole('group', { name: '컴퓨터 2 난이도' });
|
|
if ((await level.getByRole('button', { name: '랜덤' }).getAttribute('aria-pressed')) !== 'true') throw new Error('a new computer is not 랜덤');
|
|
await level.getByRole('button', { name: '쉬움' }).click();
|
|
await b.waitForFunction(() => document.querySelector('[aria-label="컴퓨터 2 난이도"] [aria-pressed="true"]')?.textContent === '쉬움');
|
|
await b.screenshot({ path: join(SHOTS, 'bots-3-lobby-phone.png'), fullPage: true });
|
|
});
|
|
|
|
await step('원카드: 시작 → 내 차례 4번, 컴퓨터도 둔다', async () => {
|
|
await b.getByRole('button', { name: '▶ 게임 시작' }).click();
|
|
await b.locator('.turn-status').waitFor();
|
|
let mine = 0;
|
|
let botTurns = 0;
|
|
let last = '';
|
|
for (let t = 0; t < 400 && mine < 4; t++) {
|
|
if (await resultShown(b)) break;
|
|
const one = b.locator('.oc-onebtn');
|
|
if ((await one.count()) && (await one.isEnabled().catch(() => false))) await one.click({ timeout: 1000 }).catch(() => {});
|
|
const s = await status(b);
|
|
if (s !== last && /컴퓨터 \d님 차례예요/.test(s)) botTurns++;
|
|
last = s;
|
|
if (!s.startsWith('내 차례예요')) {
|
|
const next = b.getByRole('button', { name: /다음 판/ });
|
|
if (await next.count()) await next.first().click().catch(() => {});
|
|
await b.waitForTimeout(150);
|
|
continue;
|
|
}
|
|
const accept = b.getByRole('button', { name: /^받아들이기/ });
|
|
const card = b.locator('button.oc-card[data-legal="true"]').first();
|
|
if (await accept.count()) await accept.click();
|
|
else if (await card.count()) {
|
|
await card.click();
|
|
await card.click();
|
|
const pick = b.locator('.oc-pick button').first();
|
|
if (await pick.waitFor({ timeout: 400 }).then(() => true, () => false)) await pick.click();
|
|
} else {
|
|
const pass = b.getByRole('button', { name: '넘기기' });
|
|
if (await pass.count()) await pass.click();
|
|
else await b.getByRole('button', { name: /장 먹기$/ }).first().click();
|
|
}
|
|
await b.waitForFunction((x) => document.querySelector('.turn-status')?.textContent?.trim() !== x, s.trim(), { timeout: 8000 });
|
|
mine++;
|
|
if (mine === 2) await b.screenshot({ path: join(SHOTS, 'bots-4-one-card-phone.png') });
|
|
}
|
|
if (mine < 4 && !(await resultShown(b))) throw new Error(`only ${mine} of my turns were played`);
|
|
if (botTurns < 2) throw new Error(`computers took only ${botTurns} turns`);
|
|
console.log(` one-card: my turns ${mine}, computer turns seen ${botTurns}`);
|
|
});
|
|
|
|
// ---------------------------------------------------------------- leaving with ← 홈 / 🚪 나가기, guest logout
|
|
await step('원카드(게임 중): ← 홈 → 기권 확인 → 방이 바로 닫힘', async () => {
|
|
const code = codeOf(b);
|
|
if (!app.rooms.get(code)) throw new Error('room missing before leaving');
|
|
const result = b.getByRole('dialog', { name: '게임 결과' });
|
|
if (await result.count()) await result.getByRole('button', { name: '방 나가기' }).click();
|
|
else {
|
|
await b.getByRole('button', { name: '방 나가고 홈으로' }).click();
|
|
await b.getByText('게임 중에 나가면 기권으로 처리돼요.').waitFor();
|
|
await b.getByRole('button', { name: '나가기', exact: true }).click();
|
|
}
|
|
await b.waitForURL(`${ORIGIN}/`);
|
|
for (let i = 0; i < 50 && app.rooms.get(code); i++) await Bun.sleep(20);
|
|
if (app.rooms.get(code)) throw new Error('room still open after everyone left');
|
|
});
|
|
|
|
await step('대기실: 🚪 나가기 한 번에 나감(확인 없음) → 방이 바로 닫힘', async () => {
|
|
await b.goto(`${ORIGIN}/g/one-card`);
|
|
await b.getByRole('button', { name: '🤖 컴퓨터와 연습하기' }).click();
|
|
await b.waitForURL(/\/r\/\d{6}$/);
|
|
await b.getByLabel('자리').getByText('컴퓨터 1').waitFor();
|
|
const code = codeOf(b);
|
|
await b.getByRole('button', { name: '🚪 나가기' }).click();
|
|
await b.waitForURL(`${ORIGIN}/`);
|
|
for (let i = 0; i < 50 && app.rooms.get(code); i++) await Bun.sleep(20);
|
|
if (app.rooms.get(code)) throw new Error('room still open after leaving the lobby');
|
|
});
|
|
|
|
await step('요트(연습): 기권 → 결과 창에 등수와 최종 점수', async () => {
|
|
await b.goto(`${ORIGIN}/g/yacht`);
|
|
await b.getByRole('button', { name: '🤖 컴퓨터와 연습하기' }).click();
|
|
await b.waitForURL(/\/r\/\d{6}$/);
|
|
await b.getByLabel('자리').getByText('컴퓨터 1').waitFor();
|
|
await b.getByRole('group', { name: '컴퓨터 1 난이도' }).getByRole('button', { name: '어려움' }).click();
|
|
await b.waitForFunction(() => document.querySelector('[aria-label="컴퓨터 1 난이도"] [aria-pressed="true"]')?.textContent === '어려움');
|
|
// The best luck: the server plays out 5 shuffles on its worker before the game (docs/04 §5.1).
|
|
const room = app.rooms.get(codeOf(b))!;
|
|
const bot = room.seatedIds().find((u) => u.startsWith('bot:'))!;
|
|
room.botProfiles[bot]!.luck = 1;
|
|
await b.getByRole('button', { name: '▶ 게임 시작' }).click();
|
|
await b.getByRole('button', { name: '🏳 기권' }).click();
|
|
await b.getByRole('button', { name: '기권하기' }).click();
|
|
const result = b.getByRole('dialog', { name: '게임 결과' });
|
|
await result.waitFor();
|
|
const ranks = await result.locator('strong', { hasText: /^\d+등$/ }).allInnerTexts();
|
|
const scores = await result.locator('.result-score').allInnerTexts();
|
|
if (ranks.length !== 2 || scores.length !== 2 || !scores.every((t) => /^\d+점$/.test(t))) {
|
|
throw new Error(`ranks ${JSON.stringify(ranks)} scores ${JSON.stringify(scores)}`);
|
|
}
|
|
// Fairness check: the computer's level and luck in this game, and the shuffles tried for its luck.
|
|
await result.getByText('공정성 확인').click();
|
|
const fair = await result.locator('details').innerText();
|
|
if (!/컴퓨터 1: 난이도 (4[1-9]|5\d|60)단계 \(어려움\) · 운 아주 좋음/.test(fair) || !fair.includes('미리 섞어 본 5가지')) throw new Error(`fairness: ${fair}`);
|
|
await result.getByRole('button', { name: '내 브라우저에서 직접 확인하기' }).click();
|
|
await result.getByText('✓ 일치해요.').waitFor();
|
|
await Bun.sleep(400); // let the sheet finish sliding in
|
|
await b.screenshot({ path: join(SHOTS, 'bots-5-yacht-result-phone.png') });
|
|
console.log(` yacht result: ${ranks.map((r, i) => `${r} ${scores[i]}`).join(', ')}`);
|
|
await result.getByRole('button', { name: '방 나가기' }).click();
|
|
await b.waitForURL(`${ORIGIN}/`);
|
|
});
|
|
|
|
await step('마피아(연습, PC): 컴퓨터 5명 → 인사 채팅 → 이름을 부르면 대답', async () => {
|
|
await a.goto(`${ORIGIN}/g/mafia`);
|
|
await a.getByRole('button', { name: '🤖 컴퓨터와 연습하기' }).click();
|
|
await a.waitForURL(/\/r\/\d{6}$/);
|
|
await a.getByLabel('자리').getByText('컴퓨터 5').waitFor();
|
|
await a.getByRole('button', { name: '▶ 게임 시작' }).click();
|
|
// Role card (8 s), then the greeting: computers say hello at their own pace.
|
|
const fromBots = a.locator('.mf-msgs .mf-msg:not(.sys)', { has: a.locator('strong', { hasText: /^컴퓨터 \d$/ }) });
|
|
await a.waitForFunction(() => document.querySelectorAll('.mf-msgs .mf-msg:not(.sys)').length >= 2, null, { timeout: 40_000 });
|
|
const hello = await fromBots.allInnerTexts();
|
|
if (hello.length < 2) throw new Error(`computers said ${hello.length} lines`);
|
|
await a.getByRole('tab', { name: /^전체 채팅/ }).click();
|
|
await a.getByLabel('전체 채팅 메시지').fill('컴퓨터 2 직업 뭐야?');
|
|
await a.getByLabel('전체 채팅 메시지').press('Enter');
|
|
// The answer to the question (one of the "what's your job" lines), not just any line of 컴퓨터 2.
|
|
const answer = a.locator('.mf-msgs .mf-msg', { has: a.locator('strong', { hasText: /^컴퓨터 2$/ }), hasText: /저 시민이에요|저는 시민이에요|나 시민ㅋㅋ|비밀ㅋㅋ|시민이지/ });
|
|
await answer.first().waitFor({ timeout: 20_000 });
|
|
console.log(` mafia: ${hello.slice(0, 3).join(' | ')} → ${await answer.first().innerText()}`);
|
|
await a.screenshot({ path: join(SHOTS, 'bots-6-mafia-pc.png') });
|
|
await a.getByRole('button', { name: '방 나가고 홈으로' }).click();
|
|
await a.getByRole('button', { name: '나가기', exact: true }).click();
|
|
await a.waitForURL(`${ORIGIN}/`);
|
|
});
|
|
|
|
await step('게스트 로그아웃 → 계정 삭제', async () => {
|
|
const id = userId('호랑이')!;
|
|
await a.getByRole('button', { name: '내 계정: 호랑이' }).click();
|
|
await a.getByRole('menuitem', { name: '🚪 로그아웃' }).click();
|
|
await a.getByText('게스트 계정은 로그아웃하면 이름과 기록이 모두 지워져요.').waitFor();
|
|
await a.getByRole('button', { name: '로그아웃', exact: true }).last().click();
|
|
await a.getByRole('link', { name: /로그인/ }).first().waitFor();
|
|
if (app.db.query('SELECT 1 FROM users WHERE id = ?').get(id)) throw new Error('guest row still there');
|
|
});
|
|
|
|
if (errors.length) throw new Error(`page errors: ${errors.join(' | ')}`);
|
|
} catch (e) {
|
|
failed = true;
|
|
console.error(e);
|
|
} finally {
|
|
await browser.close();
|
|
await app.stop();
|
|
}
|
|
console.log(failed ? 'E2E FAILED' : 'E2E PASSED');
|
|
process.exit(failed ? 1 : 0);
|