Skip to content

Commit f2a00a7

Browse files
fuzzie360claude
andcommitted
test: Add browser smoke test and deployment comparison, run in CI
The site had no automated checks at all, and its two recent breakages were both invisible to a build: the create-react-app service worker served the app shell for /api/ (gpujs/gpu.js#852), and the Vite build rendered a blank page until a missing `global` shim was found. Both show up immediately when something actually loads the pages. - scripts/smoke-test.mjs loads every route in headless Chrome and fails on uncaught errors, failed requests, an unrendered page or broken images, and checks /api/ is still served rather than swallowed by the SPA. Console errors are reported but tolerated, since software rendering on CI is chatty about WebGL. - scripts/compare.mjs diffs the rendered text, links and images of two deployments, for changes that should not alter the site at all. Asset content hashes are normalised and inlined assets are counted rather than compared. - CI builds, asserts the static files in public/ survived (CNAME, 404.html and api/ in particular), serves the build and smoke tests it. puppeteer-core is used rather than puppeteer so nothing downloads a browser; CI uses the runner's Chrome, and CHROME_PATH overrides it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent f139ae4 commit f2a00a7

7 files changed

Lines changed: 634 additions & 15 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
workflow_dispatch:
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
timeout-minutes: 15
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- uses: actions/setup-node@v4
17+
with:
18+
node-version: 22
19+
cache: yarn
20+
21+
- name: Install packages
22+
run: yarn install --frozen-lockfile
23+
24+
- name: Build
25+
run: yarn build
26+
27+
- name: Check the static files survived the build
28+
# everything in public/ ships verbatim; CNAME keeps the custom domain,
29+
# 404.html forwards deep links to the hash routes, and api/ is the
30+
# gpu.js API reference served at https://gpu.rocks/api/
31+
run: |
32+
for file in index.html CNAME 404.html api/index.html manifest.json service-worker.js; do
33+
if [ ! -f "dist/$file" ]; then
34+
echo "::error::dist/$file is missing from the build"
35+
exit 1
36+
fi
37+
echo "ok dist/$file"
38+
done
39+
40+
- name: Serve the build
41+
run: |
42+
yarn preview --port 4173 &
43+
for _ in $(seq 1 30); do
44+
curl -sf http://localhost:4173/ >/dev/null && exit 0
45+
sleep 1
46+
done
47+
echo "::error::preview server never came up"
48+
exit 1
49+
50+
- name: Smoke test every route in a browser
51+
run: yarn test:smoke http://localhost:4173

‎README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ yarn preview # serve the production build locally
4343
yarn deploy # build and publish dist/ to the gh-pages branch
4444
```
4545

46+
Two checks run against a real browser (they use `puppeteer-core`, which does
47+
not download anything — set `CHROME_PATH` if Chrome is somewhere unusual):
48+
49+
```sh
50+
yarn test:smoke # every route rendered, /api/ still served
51+
yarn test:compare https://gpu.rocks http://localhost:4173 # diff two deployments
52+
```
53+
54+
`test:smoke` runs in CI on every push and pull request. It exists because a
55+
build can succeed and still ship a blank site — the move off create-react-app
56+
did exactly that until a missing `global` shim was found. `test:compare` is a
57+
manual tool for changes that should *not* alter the site, such as a build tool
58+
swap or a dependency bump; it diffs the rendered text, links and images of two
59+
deployments.
60+
4661
Everything in `public/` is copied verbatim into the build — that includes
4762
`api/` (the generated gpu.js API reference served at https://gpu.rocks/api/),
4863
`404.html` (forwards deep links to the hash routes), and `CNAME`.

‎package.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"build": "vite build",
2828
"preview": "vite preview",
2929
"predeploy": "yarn build",
30-
"deploy": "gh-pages -d dist"
30+
"deploy": "gh-pages -d dist",
31+
"test:smoke": "node scripts/smoke-test.mjs",
32+
"test:compare": "node scripts/compare.mjs"
3133
},
3234
"browserslist": {
3335
"production": [
@@ -44,6 +46,7 @@
4446
"devDependencies": {
4547
"@vitejs/plugin-react": "^4.3.4",
4648
"gh-pages": "^6.3.0",
49+
"puppeteer-core": "^23.11.1",
4750
"sass": "^1.83.4",
4851
"vite": "^6.0.11"
4952
},

‎scripts/browser.mjs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { existsSync } from 'fs';
2+
import puppeteer from 'puppeteer-core';
3+
4+
// puppeteer-core does not download a browser, so find one that is already here.
5+
// CHROME_PATH wins; otherwise try the usual locations on CI runners and macOS.
6+
const CANDIDATES = [
7+
process.env.CHROME_PATH,
8+
'/usr/bin/google-chrome',
9+
'/usr/bin/google-chrome-stable',
10+
'/usr/bin/chromium-browser',
11+
'/usr/bin/chromium',
12+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
13+
'/Applications/Chromium.app/Contents/MacOS/Chromium',
14+
].filter(Boolean);
15+
16+
export function findBrowser() {
17+
const found = CANDIDATES.find(path => existsSync(path));
18+
if (!found) {
19+
throw new Error(
20+
`No Chrome or Chromium found. Looked in:\n ${CANDIDATES.join('\n ')}\n` +
21+
'Set CHROME_PATH to a browser executable.'
22+
);
23+
}
24+
return found;
25+
}
26+
27+
export function launch() {
28+
return puppeteer.launch({
29+
executablePath: findBrowser(),
30+
headless: true,
31+
args: [
32+
'--no-sandbox',
33+
'--disable-dev-shm-usage',
34+
// CI runners have no GPU; let WebGL fall back to software rendering so
35+
// the benchmark page still renders
36+
'--enable-unsafe-swiftshader',
37+
],
38+
});
39+
}
40+
41+
// the hash routes the site serves, plus the static API reference that must not
42+
// be swallowed by the SPA (gpujs/gpu.js#852)
43+
export const ROUTES = ['/#/', '/#/benchmark', '/#/install', '/#/examples'];

‎scripts/compare.mjs‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Renders every route on two deployments and diffs what came out.
3+
*
4+
* Useful when a change should not alter the site — a build tool swap, a
5+
* dependency bump — where "it still builds" says nothing about the result.
6+
* Asset filenames are normalised, since content hashes legitimately differ.
7+
*
8+
* Usage: node scripts/compare.mjs <baseUrlA> <baseUrlB>
9+
* e.g. node scripts/compare.mjs https://gpu.rocks http://localhost:4173
10+
*/
11+
import { launch, ROUTES } from './browser.mjs';
12+
13+
const [a, b] = process.argv.slice(2).map(url => url && url.replace(/\/$/, ''));
14+
if (!a || !b) {
15+
console.error('Usage: node scripts/compare.mjs <baseUrlA> <baseUrlB>');
16+
process.exit(2);
17+
}
18+
19+
const browser = await launch();
20+
21+
// strip the content hash bundlers add, so image-1a2b3c4d.png === image.abcd1234.png
22+
const normaliseAsset = src => src.split('/').pop()
23+
.replace(/[-.][A-Za-z0-9_-]{8,}\.(png|jpe?g|gif|svg|webp)$/i, '.$1');
24+
25+
async function snapshot(base, route) {
26+
const page = await browser.newPage();
27+
await page.goto(base + route, { waitUntil: 'networkidle2', timeout: 60000 });
28+
await new Promise(resolve => setTimeout(resolve, 2500));
29+
const data = await page.evaluate(() => ({
30+
text: document.body.innerText.replace(/\s+/g, ' ').trim(),
31+
links: Array.from(document.querySelectorAll('a')).map(el => el.getAttribute('href')).filter(Boolean).sort(),
32+
images: Array.from(document.images).map(img => img.src),
33+
}));
34+
await page.close();
35+
return {
36+
...data,
37+
// data: URIs mean the bundler inlined a small asset; count them rather than
38+
// comparing their contents
39+
images: data.images.map(src => src.startsWith('data:') ? '<inlined>' : normaliseAsset(src)).sort(),
40+
};
41+
}
42+
43+
let differences = 0;
44+
for (const route of ROUTES) {
45+
const [x, y] = [await snapshot(a, route), await snapshot(b, route)];
46+
const compare = (name, left, right) => {
47+
const same = JSON.stringify(left) === JSON.stringify(right);
48+
if (!same) differences++;
49+
return { name, same, left, right };
50+
};
51+
const results = [
52+
compare('text', x.text, y.text),
53+
compare('links', x.links, y.links),
54+
compare('images', x.images, y.images),
55+
];
56+
57+
console.log(route);
58+
for (const r of results) {
59+
const size = Array.isArray(r.left) ? `${r.left.length} vs ${r.right.length}` : `${r.left.length} vs ${r.right.length} chars`;
60+
console.log(` ${r.name}: ${r.same ? 'identical' : 'DIFFERS'} (${size})`);
61+
if (r.same) continue;
62+
if (Array.isArray(r.left)) {
63+
const onlyA = r.left.filter(v => !r.right.includes(v));
64+
const onlyB = r.right.filter(v => !r.left.includes(v));
65+
if (onlyA.length) console.log(` only in A: ${onlyA.slice(0, 8).join(', ')}`);
66+
if (onlyB.length) console.log(` only in B: ${onlyB.slice(0, 8).join(', ')}`);
67+
} else {
68+
const words = (p, q) => p.split(' ').filter(w => !q.includes(w)).slice(0, 20).join(' ');
69+
console.log(` only in A: "${words(r.left, r.right).slice(0, 150)}"`);
70+
console.log(` only in B: "${words(r.right, r.left).slice(0, 150)}"`);
71+
}
72+
}
73+
}
74+
75+
await browser.close();
76+
console.log(differences ? `\n${differences} difference(s)` : '\nA and B render identically');
77+
process.exit(differences ? 1 : 0);

‎scripts/smoke-test.mjs‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Loads every route in a real browser and checks the page actually rendered.
3+
*
4+
* A production build can succeed and still ship a blank site — migrating off
5+
* create-react-app hit exactly that, where a missing `global` shim left every
6+
* route throwing before React mounted. `vite build` cannot catch that; this can.
7+
*
8+
* Usage: node scripts/smoke-test.mjs [baseUrl] (default http://localhost:4173)
9+
* CHROME_PATH=/path/to/chrome node scripts/smoke-test.mjs https://gpu.rocks
10+
*/
11+
import { launch, ROUTES } from './browser.mjs';
12+
13+
const base = (process.argv[2] || 'http://localhost:4173').replace(/\/$/, '');
14+
const MIN_NODES = 50;
15+
16+
const browser = await launch();
17+
let failures = 0;
18+
19+
for (const route of ROUTES) {
20+
const page = await browser.newPage();
21+
// uncaught exceptions and dead requests are always failures; console.error is
22+
// reported but tolerated, since software rendering on CI is chatty about WebGL
23+
const fatal = [];
24+
const noise = [];
25+
page.on('pageerror', e => fatal.push(String(e).split('\n')[0]));
26+
page.on('requestfailed', r => fatal.push(`request failed: ${r.url().slice(0, 100)}`));
27+
page.on('console', m => { if (m.type() === 'error') noise.push(m.text().slice(0, 140)); });
28+
29+
await page.goto(base + route, { waitUntil: 'networkidle2', timeout: 60000 });
30+
await new Promise(resolve => setTimeout(resolve, 1500));
31+
32+
const rendered = await page.evaluate(() => ({
33+
nodes: document.querySelectorAll('#root *').length,
34+
brokenImages: Array.from(document.images).filter(i => !i.complete || i.naturalWidth === 0).length,
35+
excerpt: document.body.innerText.replace(/\s+/g, ' ').trim().slice(0, 90),
36+
}));
37+
await page.close();
38+
39+
const problems = [
40+
...fatal,
41+
rendered.nodes < MIN_NODES ? `only ${rendered.nodes} nodes rendered` : null,
42+
rendered.brokenImages ? `${rendered.brokenImages} broken images` : null,
43+
].filter(Boolean);
44+
45+
if (problems.length) failures++;
46+
console.log(`${problems.length ? 'FAIL' : 'PASS'} ${route.padEnd(14)} nodes=${rendered.nodes}`);
47+
console.log(` "${rendered.excerpt}"`);
48+
problems.forEach(p => console.log(` ✗ ${p}`));
49+
noise.slice(0, 3).forEach(n => console.log(` (console) ${n}`));
50+
}
51+
52+
// the API reference is a static file living under public/api
53+
const page = await browser.newPage();
54+
const response = await page.goto(`${base}/api/`, { waitUntil: 'domcontentloaded', timeout: 30000 });
55+
const title = await page.title();
56+
const apiOk = response.ok() && /^gpu\.js \d+\.\d+\.\d+/.test(title);
57+
if (!apiOk) failures++;
58+
console.log(`${apiOk ? 'PASS' : 'FAIL'} ${'/api/'.padEnd(14)} "${title}"`);
59+
if (!apiOk) console.log(' ✗ the API reference is not being served — is the SPA swallowing it?');
60+
61+
await browser.close();
62+
console.log(failures ? `\n${failures} check(s) failed` : '\nall checks passed');
63+
process.exit(failures ? 1 : 0);

0 commit comments

Comments
 (0)