// #66 (spec #63 decision 6): the test vhost is the production shape of DESIGN 11.1 — TLS only, h2 // by ALPN, the client build at `/` (SPA fallback, index no-cache, hashed assets immutable), and the // tile gate's `internal` location still unreachable from outside. The certificate is generated per // run by tools/tlsdev.mjs. Here a Node HTTP/2 client (built-in `node:http2`, TESTING 10.4) is the // independent client; the headless-browser proof against the real build lives in `client/e2e/` // (`devbox run e2e`, npm deps outside tools/). import test from 'node:test' import assert from 'node:assert/strict' import fs from 'node:fs' import http2 from 'node:http2' import path from 'node:path' import { X509Certificate } from 'node:crypto' import { freePort, httpsBaseUrl, refuseLivePort } from './lib.mjs' import { makeStateDir, startServer, stopServer } from './withserver.mjs' import { startTestNginx, stopTestNginx } from './nginx.mjs' // This group proves nginx's config, not the app, and runs inside `devbox run test`, which must not // need the client's npm build. The stub has the exact shape DESIGN 11.1 pins (hashed asset under // /assets/ named by index.html); `client/e2e/h2.test.mjs` repeats the same assertions on the real // Vite output. const STUB_HASH = 'a1b2c3d4e5f6' function writeStubDist(dir) { const assetPath = `/assets/app-${STUB_HASH}.js` fs.mkdirSync(path.join(dir, 'assets'), { recursive: true }) const indexHtml = ` vhost stub

dist stub

` const asset = `// content-hashed stub asset (#66)\ndocument.documentElement.dataset.stub = 'loaded'\n` fs.writeFileSync(path.join(dir, 'index.html'), indexHtml) fs.writeFileSync(path.join(dir, assetPath.slice(1)), asset) return { dir, indexHtml, assetPath } } async function stack(fn) { const stateDir = makeStateDir() const dist = writeStubDist(`${stateDir}/dist`) const nginxPort = await freePort() refuseLivePort(nginxPort) const server = await startServer({ stateDir, publicOrigin: httpsBaseUrl(nginxPort) }) let nginx try { nginx = await startTestNginx({ backendPort: server.port, stateDir, port: nginxPort, distDir: dist.dir }) return await fn({ stateDir, server, nginx, dist }) } finally { await stopTestNginx(nginx) await stopServer(server) fs.rmSync(stateDir, { recursive: true, force: true }) } } // One HTTP/2 request; resolves with the negotiated protocol, the response and the body. async function h2Get(origin, urlPath) { const client = http2.connect(origin) try { await new Promise((resolve, reject) => { client.once('connect', resolve) client.once('error', reject) }) const alpn = client.alpnProtocol const res = await new Promise((resolve, reject) => { const req = client.request({ ':path': urlPath, ':method': 'GET' }) let headers = null let body = '' req.setEncoding('utf8') req.on('response', h => { headers = h }) req.on('data', c => { body += c }) req.on('end', () => resolve({ status: headers[':status'], headers, body })) req.on('error', reject) req.end() }) return { alpn, ...res } } finally { client.close() } } test('vhost: the generated certificate is self-signed and names loopback', async () => { await stack(async ({ nginx }) => { const cert = new X509Certificate(fs.readFileSync(nginx.certPath)) assert.equal(cert.subject, cert.issuer, 'the certificate is self-signed') assert.equal(cert.verify(cert.publicKey), true, 'its own signature verifies') assert.equal(cert.checkIP('127.0.0.1'), '127.0.0.1', 'the SAN covers the loopback address') assert.equal(cert.checkHost('localhost'), 'localhost', 'the SAN covers localhost') assert.ok(new Date(cert.validTo) > new Date(), 'the certificate is still valid') }) }) test('vhost: an HTTP/2 client negotiates h2 and serves the client index', async () => { await stack(async ({ nginx, dist }) => { const r = await h2Get(nginx.origin, '/') assert.equal(r.alpn, 'h2', 'TLS ALPN must select h2, not http/1.1') assert.equal(r.status, 200) assert.match(r.headers['content-type'], /^text\/html/) assert.equal(r.body, dist.indexHtml, 'index.html is served at /') assert.equal(r.headers['cache-control'], 'no-cache', 'index.html is revalidated, never cached hard') }) }) test('vhost: hashed assets are immutable, the SPA falls back, a missing asset is 404', async () => { await stack(async ({ nginx, dist }) => { const asset = await h2Get(nginx.origin, dist.assetPath) assert.equal(asset.status, 200) assert.equal(asset.alpn, 'h2') assert.match(asset.headers['content-type'], /^application\/javascript/) assert.equal(asset.headers['cache-control'], 'public, max-age=31536000, immutable') assert.match(asset.body, /content-hashed stub asset/) // A client-side route with no file behind it is index.html (SPA fallback), revalidated. const route = await h2Get(nginx.origin, '/scene/abc/tokens') assert.equal(route.status, 200) assert.equal(route.body, dist.indexHtml) assert.equal(route.headers['cache-control'], 'no-cache') // A stale index naming a deleted hash must fail, not boot the app. const missing = await h2Get(nginx.origin, '/assets/app-000000000000.js') assert.equal(missing.status, 404) }) }) test('vhost: direct /_tiles/ is 404 over TLS', async () => { await stack(async ({ nginx }) => { for (const p of ['/_tiles/', '/_tiles/scene1/v1/0_0.png']) { const r = await h2Get(nginx.origin, p) assert.equal(r.status, 404, `${p} is internal and must not be reachable from outside`) } // The backend still answers through the proxy, so the 404 is the location's, not a dead vhost. const health = await h2Get(nginx.origin, '/healthz') assert.equal(health.status, 200) assert.equal(health.body, '{"status":"ok"}') }) }) test('vhost: an HTTP/1.1 client without ALPN is still served over TLS', async () => { // Global fetch is Node's HTTP/1.1 client: it proves the TLS listener is not h2-only, so the // harnesses (fetch + WebSocket) keep working against the same vhost the browser uses. await stack(async ({ nginx, dist }) => { const r = await fetch(`${nginx.origin}/`) assert.equal(r.status, 200) assert.equal(await r.text(), dist.indexHtml) assert.equal(r.headers.get('cache-control'), 'no-cache') }) })