Mercurial
comparison hg-web/e2e/app_e2e_test.js @ 223:0e7b9464248d hg-web
[hg-web] Add browser route regression coverage
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Sun, 02 Aug 2026 10:07:40 -0700 |
| parents | |
| children | 3007ef5fc0ed |
comparison
equal
deleted
inserted
replaced
| 222:a8d6435dc021 | 223:0e7b9464248d |
|---|---|
| 1 const assert = require('node:assert/strict'); | |
| 2 const fs = require('node:fs'); | |
| 3 const os = require('node:os'); | |
| 4 const path = require('node:path'); | |
| 5 const { spawn, spawnSync } = require('node:child_process'); | |
| 6 const { chromium } = require('playwright-core'); | |
| 7 | |
| 8 const BASE_URL = 'http://127.0.0.1:6970'; | |
| 9 const RUNFILES = process.env.JS_BINARY__RUNFILES; | |
| 10 const WORKSPACE = process.env.JS_BINARY__WORKSPACE; | |
| 11 | |
| 12 function run(command, args, options = {}) { | |
| 13 const result = spawnSync(command, args, { | |
| 14 encoding: 'utf8', | |
| 15 ...options, | |
| 16 }); | |
| 17 if (result.status !== 0) { | |
| 18 throw new Error( | |
| 19 `${command} ${args.join(' ')} failed\n${result.stdout || ''}${result.stderr || ''}`, | |
| 20 ); | |
| 21 } | |
| 22 return result.stdout.trim(); | |
| 23 } | |
| 24 | |
| 25 function mercurialEnvironment(home) { | |
| 26 return { | |
| 27 ...process.env, | |
| 28 HGPLAIN: '1', | |
| 29 HGRCPATH: '', | |
| 30 HOME: home, | |
| 31 }; | |
| 32 } | |
| 33 | |
| 34 function stopProcess(child) { | |
| 35 if (!child || child.exitCode !== null) return Promise.resolve(); | |
| 36 child.kill('SIGTERM'); | |
| 37 return new Promise(resolve => { | |
| 38 const timer = setTimeout(() => { | |
| 39 if (child.exitCode === null) child.kill('SIGKILL'); | |
| 40 }, 3000); | |
| 41 child.once('exit', () => { | |
| 42 clearTimeout(timer); | |
| 43 resolve(); | |
| 44 }); | |
| 45 }); | |
| 46 } | |
| 47 | |
| 48 async function waitForHttp(url, child, logs) { | |
| 49 const deadline = Date.now() + 15000; | |
| 50 while (Date.now() < deadline) { | |
| 51 if (child.exitCode !== null) { | |
| 52 throw new Error(`Server exited early with ${child.exitCode}\n${logs.join('')}`); | |
| 53 } | |
| 54 try { | |
| 55 const response = await fetch(url); | |
| 56 if (response.ok) return; | |
| 57 } catch { | |
| 58 // Keep waiting for startup. | |
| 59 } | |
| 60 await new Promise(resolve => setTimeout(resolve, 100)); | |
| 61 } | |
| 62 throw new Error(`Timed out waiting for ${url}\n${logs.join('')}`); | |
| 63 } | |
| 64 | |
| 65 function createFixtureRepository(root) { | |
| 66 const options = { env: mercurialEnvironment(root) }; | |
| 67 run('hg', ['init', root], options); | |
| 68 fs.mkdirSync(path.join(root, 'docs'), { recursive: true }); | |
| 69 fs.mkdirSync(path.join(root, 'src'), { recursive: true }); | |
| 70 fs.writeFileSync( | |
| 71 path.join(root, 'README.md'), | |
| 72 '# Fixture Repository\n\n<script>window.__hgWebXss = true</script>\n', | |
| 73 ); | |
| 74 fs.writeFileSync(path.join(root, 'docs', 'README.md'), '# Documentation\n\nNested README.\n'); | |
| 75 fs.writeFileSync(path.join(root, 'src', 'main.c'), 'int main(void) { return 0; }\n'); | |
| 76 run('hg', ['--repository', root, 'add'], options); | |
| 77 run('hg', ['--repository', root, 'commit', '-u', 'Test User <[email protected]>', '-m', 'Initial fixture'], options); | |
| 78 const firstNode = run('hg', ['--repository', root, 'log', '-r', '.', '-T', '{node}'], options); | |
| 79 | |
| 80 fs.appendFileSync(path.join(root, 'README.md'), '\nSecond revision.\n'); | |
| 81 fs.writeFileSync(path.join(root, 'new-file.txt'), 'new file\n'); | |
| 82 run('hg', ['add', 'new-file.txt'], { ...options, cwd: root }); | |
| 83 run('hg', ['--repository', root, 'commit', '-u', 'Test User <[email protected]>', '-m', 'Update fixture'], options); | |
| 84 const tipNode = run('hg', ['--repository', root, 'log', '-r', '.', '-T', '{node}'], options); | |
| 85 return { firstNode, tipNode }; | |
| 86 } | |
| 87 | |
| 88 async function assertJson(pathname, status = 200) { | |
| 89 const response = await fetch(`${BASE_URL}${pathname}`); | |
| 90 const body = await response.text(); | |
| 91 assert.equal(response.status, status, `${pathname} status: ${body}`); | |
| 92 return JSON.parse(body); | |
| 93 } | |
| 94 | |
| 95 async function assertPageHasNoBrowserErrors(browser, pathname, assertion) { | |
| 96 const page = await browser.newPage(); | |
| 97 const errors = []; | |
| 98 page.on('pageerror', error => errors.push(`pageerror: ${error.stack || error.message}`)); | |
| 99 page.on('console', message => { | |
| 100 if (message.type() === 'error') errors.push(`console: ${message.text()}`); | |
| 101 }); | |
| 102 page.on('requestfailed', request => { | |
| 103 errors.push(`requestfailed: ${request.url()} ${request.failure()?.errorText || ''}`); | |
| 104 }); | |
| 105 page.on('response', response => { | |
| 106 if (response.status() >= 400) { | |
| 107 errors.push(`response: ${response.status()} ${response.url()}`); | |
| 108 } | |
| 109 }); | |
| 110 | |
| 111 const response = await page.goto(`${BASE_URL}${pathname}`, { waitUntil: 'networkidle' }); | |
| 112 assert.equal(response.status(), 200, `${pathname} document status`); | |
| 113 await assertion(page); | |
| 114 if (errors.length > 0) { | |
| 115 throw new Error(`${pathname} browser errors\n${errors.join('\n')}`); | |
| 116 } | |
| 117 await page.close(); | |
| 118 } | |
| 119 | |
| 120 async function main() { | |
| 121 assert.ok(RUNFILES, 'rules_js runfiles path is required'); | |
| 122 assert.ok(WORKSPACE, 'rules_js workspace name is required'); | |
| 123 | |
| 124 const fixtureRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'hg-web-e2e-')); | |
| 125 const appLogs = []; | |
| 126 const hgLogs = []; | |
| 127 let hgServer; | |
| 128 let appServer; | |
| 129 let browser; | |
| 130 | |
| 131 try { | |
| 132 const { firstNode, tipNode } = createFixtureRepository(fixtureRoot); | |
| 133 const runfilesWorkspace = path.join(RUNFILES, WORKSPACE); | |
| 134 const serverBinary = path.join(runfilesWorkspace, 'hg-web', 'hg_web_server'); | |
| 135 const chromiumPath = path.resolve(process.env.CHROMIUM_PATH); | |
| 136 | |
| 137 assert.ok(fs.existsSync(serverBinary), `missing server binary: ${serverBinary}`); | |
| 138 assert.ok(fs.existsSync(chromiumPath), `missing Chromium binary: ${chromiumPath}`); | |
| 139 | |
| 140 hgServer = spawn( | |
| 141 'hg', | |
| 142 [ | |
| 143 '--repository', fixtureRoot, | |
| 144 'serve', | |
| 145 '--address', '127.0.0.1', | |
| 146 '--port', '4444', | |
| 147 '--accesslog', '-', | |
| 148 '--errorlog', '-', | |
| 149 ], | |
| 150 { | |
| 151 env: mercurialEnvironment(fixtureRoot), | |
| 152 stdio: ['ignore', 'pipe', 'pipe'], | |
| 153 }, | |
| 154 ); | |
| 155 hgServer.stdout.on('data', chunk => hgLogs.push(chunk.toString())); | |
| 156 hgServer.stderr.on('data', chunk => hgLogs.push(chunk.toString())); | |
| 157 | |
| 158 appServer = spawn(serverBinary, [], { | |
| 159 cwd: runfilesWorkspace, | |
| 160 stdio: ['ignore', 'pipe', 'pipe'], | |
| 161 }); | |
| 162 appServer.stdout.on('data', chunk => appLogs.push(chunk.toString())); | |
| 163 appServer.stderr.on('data', chunk => appLogs.push(chunk.toString())); | |
| 164 | |
| 165 await waitForHttp(`${BASE_URL}/`, appServer, appLogs); | |
| 166 if (hgServer.exitCode !== null) { | |
| 167 throw new Error(`Mercurial server exited early with ${hgServer.exitCode}\n${hgLogs.join('')}`); | |
| 168 } | |
| 169 await waitForHttp(`${BASE_URL}/api/repo/list`, hgServer, hgLogs); | |
| 170 | |
| 171 for (const pathname of ['/', '/directory', '/directory?path=docs', '/graph', `/changeset/${tipNode}`]) { | |
| 172 const response = await fetch(`${BASE_URL}${pathname}`); | |
| 173 assert.equal(response.status, 200, `${pathname} shell route`); | |
| 174 assert.match(await response.text(), /<title>Zenbu Repository<\/title>/); | |
| 175 } | |
| 176 for (const pathname of ['/page.js', '/index.css', '/base.css', '/pencil_lines.png', '/panda.png']) { | |
| 177 const response = await fetch(`${BASE_URL}${pathname}`); | |
| 178 assert.equal(response.status, 200, `${pathname} static asset`); | |
| 179 assert.ok((await response.arrayBuffer()).byteLength > 0, `${pathname} is non-empty`); | |
| 180 } | |
| 181 | |
| 182 const rootList = await assertJson('/api/repo/list'); | |
| 183 assert.ok(rootList.directories.some(entry => entry.basename === 'docs')); | |
| 184 assert.ok(rootList.files.some(entry => entry.basename === 'README.md')); | |
| 185 | |
| 186 const nestedList = await assertJson('/api/repo/list?path=docs'); | |
| 187 assert.ok(nestedList.files.some(entry => entry.basename === 'README.md')); | |
| 188 | |
| 189 const fileResponse = await fetch(`${BASE_URL}/api/repo/file?path=src%2Fmain.c`); | |
| 190 assert.equal(fileResponse.status, 200); | |
| 191 assert.match(await fileResponse.text(), /int main/); | |
| 192 | |
| 193 const readmeResponse = await fetch(`${BASE_URL}/api/repo/readme?path=docs`); | |
| 194 assert.equal(readmeResponse.status, 200); | |
| 195 assert.match(await readmeResponse.text(), /Nested README/); | |
| 196 assert.equal((await fetch(`${BASE_URL}/api/repo/readme?path=src`)).status, 204); | |
| 197 | |
| 198 const graph = await assertJson('/api/graph/tip?style=json'); | |
| 199 assert.equal(graph.node, tipNode); | |
| 200 assert.ok(graph.changesets.length >= 2); | |
| 201 | |
| 202 const changeset = await assertJson(`/api/changeset/${tipNode}`); | |
| 203 assert.equal(changeset.node, tipNode); | |
| 204 assert.equal(changeset.desc, 'Update fixture'); | |
| 205 assert.ok(changeset.files.some(entry => entry.file === 'new-file.txt' && entry.status === 'added')); | |
| 206 assert.ok(changeset.diff.length > 0); | |
| 207 | |
| 208 for (const pathname of [ | |
| 209 '/api/repo/list?path=..%2Fetc', | |
| 210 '/api/repo/file?path=..%2FREADME.md', | |
| 211 '/api/graph/not-a-node?style=json', | |
| 212 '/api/changeset/not-a-node', | |
| 213 ]) { | |
| 214 const response = await fetch(`${BASE_URL}${pathname}`); | |
| 215 assert.equal(response.status, 400, `${pathname} rejects invalid input`); | |
| 216 } | |
| 217 assert.equal((await fetch(`${BASE_URL}/missing-route`)).status, 404); | |
| 218 | |
| 219 const identify = run( | |
| 220 'hg', | |
| 221 ['identify', `${BASE_URL}/repo`], | |
| 222 { env: mercurialEnvironment(fixtureRoot) }, | |
| 223 ); | |
| 224 assert.match(identify, new RegExp(`^${tipNode.slice(0, 12)}`)); | |
| 225 | |
| 226 browser = await chromium.launch({ | |
| 227 executablePath: chromiumPath, | |
| 228 headless: true, | |
| 229 args: ['--no-sandbox'], | |
| 230 }); | |
| 231 | |
| 232 await assertPageHasNoBrowserErrors(browser, '/', async page => { | |
| 233 await page.getByRole('heading', { name: 'Zenbu Repository' }).waitFor(); | |
| 234 await page.getByText('Recent Commits').waitFor(); | |
| 235 await page.getByText('Repository Files').waitFor(); | |
| 236 assert.equal(await page.evaluate(() => window.__hgWebXss), undefined); | |
| 237 await page.locator('.theme-toggle').click(); | |
| 238 }); | |
| 239 | |
| 240 await assertPageHasNoBrowserErrors(browser, '/directory', async page => { | |
| 241 await page.getByText('Repository Files').waitFor(); | |
| 242 await page.getByRole('link', { name: 'README.md' }).first().click(); | |
| 243 await page.getByText('Fixture Repository').waitFor(); | |
| 244 assert.equal(await page.evaluate(() => window.__hgWebXss), undefined); | |
| 245 await page.keyboard.press('Escape'); | |
| 246 | |
| 247 let delayedReadmeRequested = false; | |
| 248 let markReadmeRequested; | |
| 249 const readmeRequested = new Promise(resolve => { | |
| 250 markReadmeRequested = resolve; | |
| 251 }); | |
| 252 await page.route(/\/api\/repo\/readme\?path=/, async route => { | |
| 253 delayedReadmeRequested = true; | |
| 254 markReadmeRequested(); | |
| 255 await new Promise(resolve => setTimeout(resolve, 500)); | |
| 256 await route.continue(); | |
| 257 }); | |
| 258 await page.getByRole('link', { name: 'docs' }).click(); | |
| 259 await Promise.race([ | |
| 260 readmeRequested, | |
| 261 new Promise((_, reject) => { | |
| 262 setTimeout(() => reject(new Error('Timed out waiting for delayed README request')), 5000); | |
| 263 }), | |
| 264 ]); | |
| 265 await page.getByRole('link', { name: 'root' }).click(); | |
| 266 await page.getByText('Fixture Repository').waitFor(); | |
| 267 await page.waitForTimeout(600); | |
| 268 assert.equal(await page.getByText('Documentation').count(), 0); | |
| 269 assert.equal(delayedReadmeRequested, true); | |
| 270 | |
| 271 await page.getByRole('link', { name: 'src' }).click(); | |
| 272 await page.getByRole('link', { name: 'main.c' }).click(); | |
| 273 await page.getByText('int main(void)').waitFor(); | |
| 274 await page.keyboard.press('Escape'); | |
| 275 }); | |
| 276 | |
| 277 await assertPageHasNoBrowserErrors(browser, '/directory?path=docs', async page => { | |
| 278 await page.getByText('Documentation').waitFor(); | |
| 279 await page.getByRole('link', { name: 'README.md' }).click(); | |
| 280 await page.getByText('Nested README.').waitFor(); | |
| 281 }); | |
| 282 | |
| 283 await assertPageHasNoBrowserErrors(browser, '/graph', async page => { | |
| 284 await page.getByText('Commit Graph').waitFor(); | |
| 285 await page.waitForFunction(() => new URL(window.location.href).searchParams.has('tip')); | |
| 286 const graphUrl = page.url(); | |
| 287 const rows = page.locator('.graph-row'); | |
| 288 await rows.first().click(); | |
| 289 await page.waitForURL(/\/changeset\/[0-9a-f]+$/); | |
| 290 await page.getByRole('heading', { name: 'Update fixture' }).waitFor(); | |
| 291 await page.getByRole('button', { name: 'Back', exact: true }).click(); | |
| 292 await page.waitForFunction(expected => window.location.href === expected, graphUrl); | |
| 293 await page.getByText('Commit Graph').waitFor(); | |
| 294 }); | |
| 295 | |
| 296 await assertPageHasNoBrowserErrors(browser, `/changeset/${tipNode}`, async page => { | |
| 297 await page.getByRole('heading', { name: 'Update fixture' }).waitFor(); | |
| 298 await page.locator('.changeset-files code').filter({ hasText: 'new-file.txt' }).waitFor(); | |
| 299 await page.getByText('added', { exact: true }).waitFor(); | |
| 300 await page.getByRole('region', { name: 'Changeset diff' }).waitFor(); | |
| 301 await page.getByRole('button', { name: firstNode.slice(0, 12) }).click(); | |
| 302 await page.waitForFunction( | |
| 303 expectedPath => window.location.pathname === expectedPath, | |
| 304 `/changeset/${firstNode}`, | |
| 305 ); | |
| 306 await page.getByRole('heading', { name: 'Initial fixture' }).waitFor(); | |
| 307 await page.getByRole('button', { name: 'Back', exact: true }).click(); | |
| 308 await page.getByRole('heading', { name: 'Update fixture' }).waitFor(); | |
| 309 await page.getByRole('button', { name: 'Back', exact: true }).click(); | |
| 310 await page.getByText('Commit Graph').waitFor(); | |
| 311 }); | |
| 312 | |
| 313 await assertPageHasNoBrowserErrors(browser, '/changeset/not-a-node', async page => { | |
| 314 await page.getByText('Recent Commits').waitFor(); | |
| 315 }); | |
| 316 | |
| 317 await stopProcess(hgServer); | |
| 318 const unavailable = await fetch(`${BASE_URL}/api/repo/list`); | |
| 319 assert.equal(unavailable.status, 502); | |
| 320 assert.equal((await fetch(`${BASE_URL}/`)).status, 200); | |
| 321 } finally { | |
| 322 if (browser) await browser.close(); | |
| 323 await stopProcess(appServer); | |
| 324 await stopProcess(hgServer); | |
| 325 fs.rmSync(fixtureRoot, { recursive: true, force: true }); | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 main().catch(error => { | |
| 330 console.error(error.stack || error); | |
| 331 process.exitCode = 1; | |
| 332 }); |