// 用真实编译器、HTTP 服务和浏览器验证随包小游戏;只管理本测试创建的进程。 const assert = require('node:assert/strict'); const fs = require('node:fs/promises'); const path = require('node:path'); const net = require('node:net'); const { spawn, execFile } = require('node:child_process'); const { promisify } = require('node:util'); const { createRequire } = require('node:module'); const { once } = require('node:events'); const run = promisify(execFile); async function homepage(server, url, errors, timeout) { const deadline = Date.now() + timeout; while (true) { if (server.exitCode !== null || server.signalCode !== null) throw new Error(errors()); const response = await fetch(url).catch(() => undefined); if (response) { assert.equal(response.status, 200); return response.text(); } if (Date.now() >= deadline) throw new Error('HTTP startup timed out: ' + errors()); await new Promise(resolve => setTimeout(resolve, 25)); } } async function main() { assert.ok(process.env.DEVER_TEST_BIN); assert.ok(process.env.DEVER_BROWSER_TOOLS); const tools = createRequire(path.join(process.env.DEVER_BROWSER_TOOLS, '..', 'package.json')); const { chromium: playwright } = tools('playwright-core'); const { default: chromium } = tools('@sparticuz/chromium'); const root = path.resolve(__dirname, '..'); const example = path.join(root, 'examples/next/text_game'); const work = await fs.mkdtemp(path.join(root, 'target/web-browser-')); const project = path.join(work, 'project'); const front = path.join(project, 'module/game/front'); await fs.cp(example, project, { recursive: true, filter: source => !['target', 'dist'].includes(path.basename(source)) }); const binary = path.join(work, 'game'); const listener = net.createServer(); listener.listen(0, '127.0.0.1'); await once(listener, 'listening'); const port = listener.address().port; await new Promise(resolve => listener.close(resolve)); const config = JSON.parse(await fs.readFile(path.join(project, 'config/setting.json'), 'utf8')); config.http.port = port; const settings = path.join(work, 'setting.json'); await fs.writeFile(settings, JSON.stringify(config)); await fs.copyFile(settings, path.join(project, 'config/setting.json')); await fs.mkdir(path.join(front, 'dist')); await fs.writeFile(path.join(front, 'dist/index.html'), 'stale output'); const url = 'http://127.0.0.1:' + port; const dev = spawn(process.env.DEVER_TEST_BIN, ['run', project, '--', 'api'], { detached: true, stdio: ['ignore', 'ignore', 'pipe'] }); let devErrors = ''; dev.stderr.on('data', data => devErrors += data); try { assert.equal(await homepage(dev, url, () => devErrors, 120000), await fs.readFile(path.join(front, 'src/index.html'), 'utf8')); assert.equal(await fs.readFile(path.join(front, 'dist/index.html'), 'utf8'), 'stale output'); } finally { if (dev.exitCode === null && dev.signalCode === null) { process.kill(-dev.pid, 'SIGTERM'); await once(dev, 'close'); } } await run(process.env.DEVER_TEST_BIN, ['build', project, '--profile', 'dev', '--output', binary]); assert.equal(await fs.readFile(path.join(front, 'dist/index.html'), 'utf8'), await fs.readFile(path.join(front, 'src/index.html'), 'utf8')); await fs.rename(front, path.join(work, 'offline-front')); const server = spawn(binary, ['--config', settings, 'api'], { stdio: ['ignore', 'pipe', 'pipe'] }); let log = ''; server.stderr.on('data', data => log += data); let browser; try { await homepage(server, url, () => log, 10000); browser = await playwright.launch({ executablePath: await chromium.executablePath(), args: chromium.args, headless: true }); const page = await browser.newPage(); const errors = []; page.on('pageerror', error => errors.push(error.message)); for (const width of [320, 900]) { await page.setViewportSize({ width, height: 800 }); await page.emulateMedia({ colorScheme: width === 320 ? 'dark' : 'light' }); await page.goto(url); await page.getByText('你醒来时,发现自己在一间上锁的房间里。', { exact: true }).waitFor(); await page.getByRole('button', { name: '打开门' }).click(); await page.getByText('门打不开,需要钥匙。', { exact: true }).waitFor(); await page.getByRole('button', { name: '寻找钥匙' }).click(); await page.getByText('你在盒子里找到了钥匙。', { exact: true }).waitFor(); await page.getByRole('button', { name: '打开门' }).click(); await page.getByText('门开了,你逃出了房间!', { exact: true }).waitFor(); assert.ok(await page.getByRole('button', { name: '寻找钥匙' }).isDisabled()); await page.getByRole('button', { name: '重新开始' }).click(); await page.getByRole('button', { name: '结束探索' }).click(); await page.getByText('你结束了这次探索。', { exact: true }).waitFor(); assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)); assert.deepEqual(errors, []); } console.log('game run(src), standalone build(dist), browser: 320/900 px, light/dark, locked/key/won/restart/quit passed'); } finally { if (browser) await browser.close(); if (server.exitCode === null) { server.kill('SIGTERM'); const timeout = setTimeout(() => server.kill('SIGKILL'), 5000); const [code] = await once(server, 'exit'); clearTimeout(timeout); assert.equal(code, 0, log); } } await fs.rm(work, { recursive: true }); } main().catch(error => { console.error(error); process.exitCode = 1; });