summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/consumer/build.zig20
-rw-r--r--test/consumer/build.zig.zon10
-rw-r--r--test/consumer/main.zig82
-rw-r--r--test/web/catalog.py235
-rw-r--r--test/web/catalog_ui.mjs208
-rw-r--r--test/web/host_fs.py167
-rw-r--r--test/web/parallel_reads.mjs44
-rw-r--r--test/web/prefix.py73
8 files changed, 839 insertions, 0 deletions
diff --git a/test/consumer/build.zig b/test/consumer/build.zig
new file mode 100644
index 0000000..9fc2c77
--- /dev/null
+++ b/test/consumer/build.zig
@@ -0,0 +1,20 @@
+//! A package that depends on cloud9 as any other does: cloud9's `test`
+//! step runs `zig build test` here, so its build API is used the way a
+//! consumer uses it (modules by name, no option).
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+ const target = b.standardTargetOptions(.{});
+ const optimize = b.standardOptimizeOption(.{});
+ const cloud9 = b.dependency("cloud9", .{});
+ const tests = b.addTest(.{ .root_module = b.createModule(.{
+ .root_source_file = b.path("main.zig"),
+ .target = target,
+ .optimize = optimize,
+ .imports = &.{
+ .{ .name = "cloud9", .module = cloud9.module("cloud9") },
+ .{ .name = "9proc", .module = cloud9.module("9proc") },
+ },
+ }) });
+ b.step("test", "a client and a server on cloud9 talk over a socket pair").dependOn(&b.addRunArtifact(tests).step);
+}
diff --git a/test/consumer/build.zig.zon b/test/consumer/build.zig.zon
new file mode 100644
index 0000000..088a635
--- /dev/null
+++ b/test/consumer/build.zig.zon
@@ -0,0 +1,10 @@
+.{
+ .name = .cloud9_consumer,
+ .version = "0.0.0",
+ .dependencies = .{
+ .cloud9 = .{ .path = "../.." },
+ },
+ .minimum_zig_version = "0.17.0",
+ .paths = .{""},
+ .fingerprint = 0x7babe157bcd22cee,
+}
diff --git a/test/consumer/main.zig b/test/consumer/main.zig
new file mode 100644
index 0000000..3dc555d
--- /dev/null
+++ b/test/consumer/main.zig
@@ -0,0 +1,82 @@
+//! cloud9 from a package that depends on it: a client and a server, each
+//! a cloud9 session over its own end of a socket pair, and 9proc.
+const std = @import("std");
+const c9 = @import("cloud9");
+const testing = std.testing;
+const File = std.Io.File;
+
+test "a client and a server talk 9P over a socket pair" {
+ if (@import("builtin").os.tag != .linux) return error.SkipZigTest;
+ const linux = std.os.linux;
+ var fds: [2]linux.fd_t = undefined;
+ try testing.expectEqual(0, linux.socketpair(linux.AF.UNIX, linux.SOCK.STREAM, 0, &fds));
+ const near: File = .{ .handle = fds[0], .flags = .{ .nonblocking = false } };
+ const far: File = .{ .handle = fds[1], .flags = .{ .nonblocking = false } };
+ defer near.close(testing.io);
+ defer far.close(testing.io);
+
+ var bufs: [4][8192]u8 = undefined;
+ var client: c9.Client = .init(.{ .in = &bufs[0], .out = &bufs[1] });
+ var server: c9.Server = .init(.{ .in = &bufs[2], .out = &bufs[3] });
+ const root: c9.Qid = .{ .type = c9.qtdir, .version = 0, .path = 1 };
+ const hello = "hello from the server";
+
+ _ = try client.submit(.{ .version = .{} });
+ try send(near, &client);
+ const version = (try request(far, &server)).msg.tversion;
+ try server.negotiate(version.msize, version.version);
+ server.release();
+ try send(far, &server);
+ try testing.expectEqualStrings("9P2000", (try result(near, &client)).result.version.version);
+
+ _ = try client.submit(.{ .attach = .{ .fid = 0, .uname = "glenda" } });
+ try send(near, &client);
+ const attach = try request(far, &server);
+ try testing.expectEqualStrings("glenda", attach.msg.tattach.uname);
+ try server.reply(attach.tag, .{ .rattach = .{ .qid = root } });
+ server.release();
+ try send(far, &server);
+ try testing.expectEqual(root.path, (try result(near, &client)).result.attach.path);
+
+ _ = try client.submit(.{ .read = .{ .fid = 0, .offset = 0, .count = 64 } });
+ try send(near, &client);
+ const read = try request(far, &server);
+ try server.reply(read.tag, .{ .rread = .{ .data = hello } });
+ server.release();
+ try send(far, &server);
+ try testing.expectEqualStrings(hello, (try result(near, &client)).result.read);
+}
+
+test "9proc comes from the same dependency, on the same cloud9" {
+ try @import("9proc").core.validName("pane");
+ try testing.expectError(error.BadName, @import("9proc").core.validName("a/b"));
+}
+
+/// Writes what `end` (a client or a server) has to say on `file`.
+fn send(file: File, end: anytype) !void {
+ const bytes = end.output();
+ try file.writeStreamingAll(testing.io, bytes);
+ end.wrote(bytes.len);
+}
+
+/// Reads from `file` into `server` until it holds a whole request.
+fn request(file: File, server: *c9.Server) !c9.Decoded {
+ while (true) {
+ if (try server.receive()) |got| return got;
+ try recv(file, server);
+ }
+}
+
+/// Reads from `file` into `client` until it holds a whole answer.
+fn result(file: File, client: *c9.Client) !c9.Client.Done {
+ while (true) {
+ if (client.take()) |done| return done;
+ try recv(file, client);
+ }
+}
+
+fn recv(file: File, end: anytype) !void {
+ var buf: [8192]u8 = undefined;
+ const n = try file.readStreaming(testing.io, &.{&buf});
+ try testing.expectEqual(n, end.push(buf[0..n]));
+}
diff --git a/test/web/catalog.py b/test/web/catalog.py
new file mode 100644
index 0000000..87773d8
--- /dev/null
+++ b/test/web/catalog.py
@@ -0,0 +1,235 @@
+#!/usr/bin/env python3
+"""Black-box catalogue checks: real HTTP, 9P, child processes and files."""
+from http.client import HTTPConnection, HTTPException
+import importlib.util
+import json
+import os
+from pathlib import Path
+import socket
+import shutil
+import struct
+import subprocess
+import sys
+import tempfile
+import time
+
+
+def port():
+ with socket.socket() as s:
+ s.bind(('127.0.0.1', 0))
+ return s.getsockname()[1]
+
+
+def eventually(f, timeout=15):
+ end = time.monotonic() + timeout
+ while time.monotonic() < end:
+ try:
+ value = f()
+ if value:
+ return value
+ except (OSError, HTTPException):
+ pass
+ time.sleep(.1)
+ raise AssertionError('timed out: ' + repr(f))
+
+
+def http(p, path='/', method='GET', body=None, headers=None):
+ c = HTTPConnection('127.0.0.1', p, timeout=5)
+ try:
+ c.request(method, path, body, headers or {})
+ r = c.getresponse()
+ return r.status, dict(r.getheaders()), r.read()
+ finally:
+ c.close()
+
+
+def string(s):
+ b = s.encode()
+ return struct.pack('<H', len(b)) + b
+
+
+class Nine:
+ def __init__(self, path):
+ self.s = socket.socket(socket.AF_UNIX)
+ self.s.settimeout(5)
+ self.s.connect(path)
+ self.rpc(100, struct.pack('<I', 65536) + string('9P2000'), 65535)
+ self.rpc(104, struct.pack('<II', 1, 0xffffffff) + string('user') + string(''))
+
+ def recv(self, n):
+ b = b''
+ while len(b) < n:
+ chunk = self.s.recv(n-len(b))
+ if not chunk:
+ raise AssertionError('9P disconnected')
+ b += chunk
+ return b
+
+ def rpc(self, t, body, tag=1):
+ self.s.sendall(struct.pack('<IBH', len(body)+7, t, tag) + body)
+ size, rt, rtag = struct.unpack('<IBH', self.recv(7))
+ result = self.recv(size-7)
+ assert rt != 107, result
+ assert rt == t+1 and rtag == tag, (rt, rtag)
+ return result
+
+ def walk(self, path):
+ names = path.split('/')
+ r = self.rpc(110, struct.pack('<IIH', 1, 2, len(names)) + b''.join(map(string, names)))
+ assert struct.unpack('<H', r[:2])[0] == len(names), path
+
+ def read(self, path):
+ self.walk(path)
+ self.rpc(112, struct.pack('<IB', 2, 0))
+ r = self.rpc(116, struct.pack('<IQI', 2, 0, 32000))
+ self.rpc(120, struct.pack('<I', 2))
+ return r[4:]
+
+ def write(self, path, text):
+ self.walk(path)
+ self.rpc(112, struct.pack('<IB', 2, 0x11))
+ b = text.encode()
+ self.rpc(118, struct.pack('<IQI', 2, 0, len(b)) + b)
+ self.rpc(120, struct.pack('<I', 2))
+
+ def list(self, path):
+ self.walk(path)
+ self.rpc(112, struct.pack('<IB', 2, 0))
+ offset, names = 0, []
+ while True:
+ data = self.rpc(116, struct.pack('<IQI', 2, offset, 160))[4:]
+ if not data: break
+ offset += len(data)
+ while data:
+ size = struct.unpack('<H', data[:2])[0] + 2
+ nlen = struct.unpack('<H', data[41:43])[0]
+ names.append(data[43:43+nlen].decode())
+ data = data[size:]
+ self.rpc(120, struct.pack('<I', 2))
+ return names
+
+
+def main():
+ binary = str(Path(sys.argv[1] if len(sys.argv) > 1 else 'zig-out/bin/9web').resolve())
+ spec = importlib.util.spec_from_file_location('discover', Path(__file__).resolve().parents[2] / 'web/discover.py')
+ discover = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(discover)
+ assert discover.recognize(['python3', '-m', 'http.server', '8000'])
+ assert discover.recognize(['node', '/project/app.js', 'vite']) is None
+ assert discover.recognize(['miniserve', '--tls-cert', 'cert'])[1] == 'https'
+ with tempfile.TemporaryDirectory(prefix='9web-catalog-') as tmp:
+ root = Path(tmp)
+ public = root/'public'
+ public.mkdir()
+ (public/'index.html').write_text('<h1>catalog fixture</h1>')
+ (public/'style.css').write_text('body{color:red}')
+ (public/'pretty.html').write_text('pretty page')
+ (public/'nested').mkdir()
+ (public/'nested/index.html').write_text('nested page')
+ (root/'secret').write_text('must not serve')
+ (public/'escape').symlink_to(root/'secret')
+ (public/'escape-dir').symlink_to(root, target_is_directory=True)
+ (root/'source.txt').write_text('source')
+ dashboard, site_port, python_port = port(), port(), port()
+ cfg = dict(sites=[dict(name='fixture', title='Fixture', cwd=tmp, root=str(public), port=site_port,
+ build=['sh','-c','echo build-output; if test -f slow; then sleep 30 & echo $! > descendant; wait; fi; sleep 0.2; cp source.txt public/result.txt'], inputs=['source.txt'])])
+ (root/'catalog.json').write_text(json.dumps(cfg))
+ log = open(root/'server.log', 'wb')
+ proc = subprocess.Popen([binary, '--listen', f'0.0.0.0:{dashboard}', '--catalog', str(root/'catalog.json'), '--catalog-socket', str(root/'catalog.sock'), '--state-dir', str(root/'state')], stdout=log, stderr=log)
+ py = subprocess.Popen([sys.executable, '-m', 'http.server', str(python_port), '--bind', '127.0.0.1', '--directory', str(public)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+ try:
+ eventually(lambda: http(dashboard)[0] == 200)
+ assert http(site_port)[2] == b'<h1>catalog fixture</h1>'
+ assert http(site_port, '/style.css')[1]['content-type'] == 'text/css'
+ assert http(site_port, '/pretty')[2] == b'pretty page'
+ assert http(site_port, '/nested')[0] == 301
+ assert http(site_port, '/nested/')[2] == b'nested page'
+ assert http(site_port, '/', 'HEAD')[2] == b''
+ assert http(site_port, '/style.css', headers={'Range':'bytes=0-3'})[2] == b'body'
+ assert http(dashboard, '/sites/fixture/')[2] == b'<h1>catalog fixture</h1>'
+ assert http(dashboard, '/sites/fixture/pretty')[2] == b'pretty page'
+ assert http(dashboard, '/sites/fixture/nested?x=1')[1]['location'] == '/sites/fixture/nested/?x=1'
+ assert http(dashboard, '/sites/fixture/style.css', headers={'Range':'bytes=-2'})[0] == 206
+ assert http(dashboard, '/sites/fixture/style.css', headers={'Range':'bytes=999999-'})[0] == 416
+ for path in ['/../secret', '/%2e%2e/secret', '/escape', '/escape-dir/secret', '/.git/config', '/%2fetc/passwd']:
+ assert http(site_port, path)[0] in (400,403,404), path
+ assert http(dashboard, '/sites/fixture' + path)[0] in (400,403,404), path
+ assert http(dashboard, headers={'Host':'attacker.invalid'})[0] == 403
+ ctl = '/_cloud9/sites/fixture/ctl'
+ assert http(dashboard, ctl, 'POST', 'build', {'Origin':'https://attacker.invalid'})[0] == 403
+ assert http(dashboard, ctl, 'POST', 'bogus')[0] == 400
+ nine = Nine(str(root/'catalog.sock'))
+ assert nine.list('sites') == ['fixture']
+ assert nine.list('sites/fixture') == ['ctl', 'events', 'log', 'public', 'status']
+ assert nine.list('sites/fixture/public') == ['index.html', 'nested', 'pretty.html', 'style.css']
+ assert nine.read('sites/fixture/public/index.html').startswith(b'<h1>')
+ assert b'build' in nine.read('sites/fixture/ctl')
+ nine.write('sites/fixture/ctl', 'build\n')
+ eventually(lambda: (public/'result.txt').exists())
+ eventually(lambda: json.loads(nine.read('sites/fixture/status'))['generation'] == 1)
+ assert b'build-output' in nine.read('sites/fixture/log')
+ assert http(dashboard, ctl, 'POST', 'watch on')[0] == 202
+ eventually(lambda: json.loads(nine.read('sites/fixture/status'))['generation'] >= 2)
+ (root/'source.txt').write_text('changed source')
+ eventually(lambda: (public/'result.txt').read_text() == 'changed source')
+ assert http(dashboard, ctl, 'POST', 'watch off')[0] == 202
+ def discovered():
+ data = json.loads(http(dashboard, '/_cloud9/catalog.json')[2])
+ return any(x['port'] == python_port and x['service'] == 'python http.server' for x in data['listeners'])
+ eventually(discovered)
+ # A parked event reader must not stall HTTP or other 9P clients.
+ events = Nine(str(root/'catalog.sock'))
+ events.walk('sites/fixture/events')
+ events.rpc(112, struct.pack('<IB', 2, 0))
+ first = events.rpc(116, struct.pack('<IQI', 2, 0, 2048))
+ assert json.loads(first[4:])['revision'] > 0
+ pending = struct.pack('<IQI', 2, len(first)-4, 2048)
+ events.s.sendall(struct.pack('<IBH', len(pending)+7, 116, 2) + pending)
+ assert http(site_port)[0] == 200
+ (root/'slow').write_text('yes')
+ nine.write('sites/fixture/ctl', 'build')
+ size, typ, tag = struct.unpack('<IBH', events.recv(7))
+ assert (typ, tag) == (117, 2)
+ assert json.loads(events.recv(size-7)[4:])['revision'] > json.loads(first[4:])['revision']
+ eventually(lambda: (root/'descendant').exists())
+ nine.write('sites/fixture/ctl', 'cancel')
+ eventually(lambda: json.loads(nine.read('sites/fixture/status'))['phase'] == 'canceled')
+ descendant = int((root/'descendant').read_text())
+ def stopped():
+ try: return Path(f'/proc/{descendant}/stat').read_text().split(') ')[1].startswith('Z')
+ except FileNotFoundError: return True
+ eventually(stopped)
+ events.s.close()
+ (root/'slow').unlink()
+ # A failed compile leaves readable existing output and reports failure.
+ (root/'source.txt').unlink()
+ nine.write('sites/fixture/ctl', 'build')
+ eventually(lambda: json.loads(nine.read('sites/fixture/status'))['phase'] == 'failed')
+ assert http(site_port)[0] == 200
+ # A published generation replacement must not splice an open fid.
+ saved = (public/'style.css').read_bytes()
+ nine.walk('sites/fixture/public/style.css')
+ nine.rpc(112, struct.pack('<IB', 2, 0))
+ assert nine.rpc(116, struct.pack('<IQI', 2, 0, 2))[4:] == saved[:2]
+ public.rename(root/'previous-public')
+ shutil.copytree(root/'previous-public', public, symlinks=True)
+ (public/'style.css').write_bytes(b'new generation')
+ assert nine.rpc(116, struct.pack('<IQI', 2, 2, 30000))[4:] == saved[2:]
+ nine.rpc(120, struct.pack('<I', 2))
+ assert http(dashboard, '/sites/fixture/style.css')[2] == b'new generation'
+ nine.s.close()
+ print('catalog: HTTP serving, paths, MIME, HEAD/range, controls, watch, failure, cancellation, 9P events and passive discovery passed')
+ except BaseException:
+ log.flush()
+ print((root/'server.log').read_text(), file=sys.stderr)
+ raise
+ finally:
+ proc.terminate(); py.terminate()
+ for p in (proc, py):
+ try: p.wait(timeout=5)
+ except subprocess.TimeoutExpired: p.kill(); p.wait()
+ log.close()
+
+
+if __name__ == '__main__':
+ main()
diff --git a/test/web/catalog_ui.mjs b/test/web/catalog_ui.mjs
new file mode 100644
index 0000000..32fb2ad
--- /dev/null
+++ b/test/web/catalog_ui.mjs
@@ -0,0 +1,208 @@
+// Real Chromium regression checks for the integrated 9web views. No npm deps.
+import assert from 'node:assert/strict';
+import { spawn } from 'node:child_process';
+import { once } from 'node:events';
+import fs from 'node:fs/promises';
+import path from 'node:path';
+import os from 'node:os';
+import net from 'node:net';
+
+const binary = path.resolve(process.argv[2] || 'zig-out/bin/9web');
+const work = await fs.mkdtemp(path.join(os.tmpdir(), '9web-ui-'));
+const children = [], exceptions = [], pending = new Map();
+let socket, serial = 0;
+function start(command, args, env = process.env) {
+ const p = spawn(command, args, { env, stdio: ['ignore', 'pipe', 'pipe'] });
+ p.output = ''; p.stdout.on('data', b => { p.output += b; }); p.stderr.on('data', b => { p.output += b; });
+ p.on('error', e => { p.output += e.message; }); children.push(p); return p;
+}
+async function port() {
+ const s = net.createServer(); s.listen(0, '127.0.0.1'); await once(s, 'listening');
+ const n = s.address().port; await new Promise(resolve => s.close(resolve)); return n;
+}
+async function wait(fn, message, timeout = 15000) {
+ const until = Date.now() + timeout;
+ while (Date.now() < until) { try { const value = await fn(); if (value) return value; } catch {} await new Promise(r => setTimeout(r, 50)); }
+ throw new Error('timeout: ' + message);
+}
+function cdp(method, params = {}) {
+ return new Promise((resolve, reject) => {
+ const id = ++serial;
+ const timer = setTimeout(() => { pending.delete(id); reject(new Error(`CDP timeout: ${method}`)); }, 10000);
+ pending.set(id, { resolve, reject, timer }); socket.send(JSON.stringify({ id, method, params }));
+ });
+}
+async function evaluate(expression) {
+ const r = await cdp('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true });
+ if (r.exceptionDetails) throw new Error(JSON.stringify(r.exceptionDetails));
+ return r.result.value;
+}
+async function click(selector) { await evaluate(`document.querySelector(${JSON.stringify(selector)}).click()`); }
+async function view(name) {
+ await click(`#view-${name}`);
+ await wait(() => evaluate(`!document.getElementById('${name}-view').hidden`), name + ' view');
+}
+async function readySites() { await wait(() => evaluate(`document.querySelectorAll('#sites-entries tr').length === 1`), 'site row'); }
+
+try {
+ const root = path.join(work, 'public'); await fs.mkdir(root);
+ await fs.writeFile(path.join(root, 'index.html'), '<h1>Fixture</h1>');
+ await fs.writeFile(path.join(root, 'large.bin'), Buffer.alloc(512000, 0xa5));
+ await fs.writeFile(path.join(work, 'large.txt'), 'x'.repeat(180000));
+ const dashboard = await port(), debugPort = await port();
+ const runtime = path.join(work, 'runtime');
+ await fs.mkdir(path.join(runtime, '9p'), { recursive: true });
+ start(path.resolve('zig-out/bin/9proc-demo'), ['--unix', path.join(runtime, '9p', 'fixture')]);
+ await wait(() => fs.stat(path.join(runtime, '9p', 'fixture')), '9P fixture posted');
+ await fs.writeFile(path.join(work, 'config.json'), JSON.stringify({ filesystem: '/', sites: [{ name: 'fixture', title: 'Fixture notes', cwd: work, root, build: ['sh', '-c', 'echo integrated-build'], inputs: ['public/index.html'] }] }));
+ const startWeb = () => start(path.resolve('zig-out/bin/9ns'), ['--mntgen', '--', binary, '--listen', `127.0.0.1:${dashboard}`, '--catalog', path.join(work, 'config.json'), '--catalog-socket', path.join(work, 'catalog.sock'), '--state-dir', path.join(work, 'state')], { ...process.env, XDG_RUNTIME_DIR: runtime });
+ const web = startWeb();
+ const url = `http://127.0.0.1:${dashboard}`;
+ await wait(async () => (await fetch(url)).ok, 'server ready');
+ const concurrent = start(process.execPath, ['test/web/parallel_reads.mjs', url + '/sites/fixture/', root]);
+ const [parallelCode] = await once(concurrent, 'exit');
+ assert.equal(parallelCode, 0, concurrent.output);
+ assert.equal((await (await fetch(url + '/_cloud9/config.json')).json()).catalog, true);
+ assert.equal((await fetch(url + '/_cloud9/catalog.js')).status, 404, 'standalone JS removed');
+ start(process.env.CLOUD9_CHROME || 'google-chrome-stable', ['--headless=new', '--no-sandbox', '--disable-dev-shm-usage', '--no-first-run', '--no-default-browser-check', `--user-data-dir=${work}/chrome`, `--remote-debugging-port=${debugPort}`, 'about:blank']);
+ const target = await wait(async () => (await fetch(`http://127.0.0.1:${debugPort}/json/new?about:blank`, { method: 'PUT' })).json(), 'browser ready');
+ socket = new WebSocket(target.webSocketDebuggerUrl); await once(socket, 'open');
+ socket.addEventListener('message', ({ data }) => {
+ const m = JSON.parse(data);
+ if (m.id) {
+ const p = pending.get(m.id); if (!p) return; clearTimeout(p.timer); pending.delete(m.id);
+ if (m.error) p.reject(new Error(JSON.stringify(m.error))); else p.resolve(m.result);
+ } else if (m.method === 'Runtime.exceptionThrown') exceptions.push(m.params.exceptionDetails);
+ });
+ await cdp('Runtime.enable'); await cdp('Page.enable');
+ await cdp('Emulation.setDeviceMetricsOverride', { width: 1200, height: 850, deviceScaleFactor: 1, mobile: false });
+ await cdp('Page.navigate', { url });
+ await wait(() => evaluate(`document.getElementById('status')?.textContent.includes('entries')`), 'files loaded');
+ assert.equal(await evaluate(`document.querySelectorAll('header').length`), 1);
+ assert.equal(await evaluate(`document.querySelector('#sidebar')`), null);
+ assert.equal(await evaluate(`document.querySelector('.brand').textContent`), '9web');
+ assert.equal(await evaluate(`document.querySelector('footer').textContent`), 'served by 9web');
+ assert.equal(await evaluate(`document.title`), '/ — 9web');
+ assert.equal(await evaluate(`Boolean(document.querySelector('#entries a[data-path="/etc"]'))`), true);
+ assert.equal(await evaluate(`getComputedStyle(document.documentElement).backgroundColor`), 'rgb(255, 255, 255)');
+ assert.equal(await evaluate(`document.getElementById('view-files').getAttribute('aria-current')`), 'page');
+ // Browse a real mounted 9P tree, including finite zero-stat-length files.
+ async function browse(path) {
+ await evaluate(`document.getElementById('path').value = ${JSON.stringify(path)}; document.getElementById('path-form').requestSubmit()`);
+ await wait(() => evaluate(`document.getElementById('status').textContent === 'File loaded.' || document.getElementById('status').textContent.includes('entries')`), 'mounted path loaded');
+ }
+ await browse('/mnt/9p/fixture/build');
+ assert.equal(await evaluate(`Boolean(document.querySelector('#entries a[data-path="/mnt/9p/fixture/build/zig_version"]'))`), true);
+ await browse('/mnt/9p/fixture/runtime/pid');
+ assert.match(await evaluate(`document.getElementById('editor').value`), /^\d+/);
+ await browse('/mnt/9p/fixture/runtime/ctl');
+ assert.equal(await evaluate(`document.getElementById('editor').value`), '');
+ await browse(path.join(work, 'large.txt'));
+ assert.equal(await evaluate(`document.getElementById('editor').value.length`), 180000, 'full-msize browser reads across several frames');
+ await browse('/_9web/sites/fixture');
+ await click('#entries a[data-path="/_9web/sites/fixture/events"][data-follow]');
+ await wait(() => evaluate(`document.getElementById('stream').textContent.includes('revision')`), 'direct follow of blocking events');
+ assert.equal(await evaluate(`document.getElementById('go').disabled`), false, 'stream does not block navigation');
+ await click('#follow');
+ assert.equal(await evaluate(`document.getElementById('status').textContent`), 'Stream stopped.');
+ await browse('/');
+ await fs.mkdir('.zig-cache/catalog-ui', { recursive: true });
+ await fs.writeFile('.zig-cache/catalog-ui/files.png', Buffer.from((await cdp('Page.captureScreenshot', { format: 'png' })).data, 'base64'));
+ await view('sites'); await readySites();
+ assert.equal(await evaluate(`document.title`), 'sites — 9web');
+ assert.equal(await evaluate(`document.querySelector('#sites-entries .site-name').href`), `${url}/sites/fixture/`);
+ assert.equal(await evaluate(`document.querySelectorAll('link[rel=stylesheet]').length`), 1);
+ assert.equal(await evaluate(`document.querySelector('link[rel=stylesheet]').getAttribute('href')`), '/_cloud9/style.css');
+ // Build button writes through the same /fs tree as the normal file browser.
+ await click('#sites-entries button');
+ await wait(() => evaluate(`document.querySelector('#sites-entries .site-phase').textContent.includes('exit 0')`), 'build complete');
+ await evaluate(`document.querySelector('#sites-entries button').focus()`);
+ await click('#sites-reload');
+ await evaluate(`document.querySelector('#sites-entries button').focus()`);
+ await wait(() => evaluate(`!document.getElementById('sites-reload').disabled`), 'refresh complete');
+ assert.equal(await evaluate(`document.activeElement === document.querySelector('#sites-entries button')`), true, 'refresh preserves focused control');
+ await click('#sites-entries a[data-path$="/log"]');
+ await wait(() => evaluate(`document.getElementById('editor').value.includes('integrated-build')`), 'log in file viewer');
+ assert.equal(await evaluate(`document.getElementById('files-view').hidden`), false);
+ // Switching views preserves the existing file editor and its unsaved contents.
+ await evaluate(`document.getElementById('editor').value = 'unsaved note'; document.getElementById('editor').dispatchEvent(new Event('input'))`);
+ await view('sites'); await view('services');
+ await wait(() => evaluate(`document.getElementById('services-status').textContent.includes('HTTP services')`), 'services loaded');
+ await evaluate('history.back()');
+ await wait(() => evaluate(`!document.getElementById('sites-view').hidden`), 'back to sites');
+ await evaluate('history.back()');
+ await wait(() => evaluate(`!document.getElementById('files-view').hidden`), 'back to files');
+ assert.equal(await evaluate(`document.getElementById('editor').value`), 'unsaved note');
+ assert.equal(await evaluate(`document.getElementById('edit-status').textContent`), 'Unsaved changes.');
+ // Reload/deep-link into a tab; no synthetic 9P path is reserved for the tab.
+ await evaluate(`window.onbeforeunload = null; window.confirm = () => true`);
+ // A fresh target avoids bypassing the intentional beforeunload prompt.
+ const target2 = await (await fetch(`http://127.0.0.1:${debugPort}/json/new?${encodeURIComponent(url + '/?view=sites')}`, { method: 'PUT' })).json();
+ socket.close(); socket = new WebSocket(target2.webSocketDebuggerUrl); await once(socket, 'open');
+ socket.addEventListener('message', ({ data }) => {
+ const m = JSON.parse(data); if (!m.id) return;
+ const p = pending.get(m.id); if (!p) return; clearTimeout(p.timer); pending.delete(m.id);
+ if (m.error) p.reject(new Error(JSON.stringify(m.error))); else p.resolve(m.result);
+ });
+ await cdp('Runtime.enable'); await cdp('Page.enable'); await readySites();
+ assert.equal(await evaluate(`document.getElementById('view-sites').getAttribute('aria-current')`), 'page');
+ await cdp('Emulation.setDeviceMetricsOverride', { width: 1200, height: 850, deviceScaleFactor: 1, mobile: false });
+ await fs.mkdir('.zig-cache/catalog-ui', { recursive: true });
+ await fs.writeFile('.zig-cache/catalog-ui/sites.png', Buffer.from((await cdp('Page.captureScreenshot', { format: 'png' })).data, 'base64'));
+ await view('services');
+ await wait(() => evaluate(`document.getElementById('services-status').textContent.includes('HTTP services')`), 'services ready');
+ await fs.writeFile('.zig-cache/catalog-ui/services.png', Buffer.from((await cdp('Page.captureScreenshot', { format: 'png' })).data, 'base64'));
+ await view('sites');
+ await cdp('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true });
+ assert.equal(await evaluate(`document.documentElement.scrollWidth <= innerWidth`), true, 'mobile page does not overflow');
+ await fs.writeFile('.zig-cache/catalog-ui/mobile.png', Buffer.from((await cdp('Page.captureScreenshot', { format: 'png' })).data, 'base64'));
+ assert.deepEqual(exceptions, []);
+ // A live port remains exclusive, but TIME_WAIT must not break a restart.
+ const competitor = start(binary, ['--listen', `127.0.0.1:${dashboard}`]);
+ await wait(() => competitor.exitCode !== null, 'competing listener refused');
+ assert.notEqual(competitor.exitCode, 0);
+ assert.match(competitor.output, /address already in use/);
+ web.kill('SIGTERM'); await once(web, 'exit');
+ await fs.unlink(path.join(work, 'catalog.sock'));
+ startWeb();
+ await wait(async () => (await fetch(url)).ok && (await fetch(`${url}/sites/fixture/`)).ok, 'immediate restart with recent HTTP traffic');
+ // Opt-in, read-only smoke navigation against an installed workstation.
+ if (process.env.NINE_WEB_LIVE) {
+ for (const pathname of ['/mnt/9p', '/mnt/9p/9web', '/mnt/9p/9web/sites/entomology', '/mnt/9p/agents', '/mnt/9p/agents/codex/history', '/mnt/9p/zmx/daybreak/command', '/mnt/9p/zmx/daybreak/cwd', '/usr/bin']) {
+ await cdp('Page.navigate', { url: process.env.NINE_WEB_LIVE + pathname });
+ await wait(() => evaluate(`document.getElementById('path')?.value === ${JSON.stringify(pathname)} && (document.getElementById('status').textContent === 'File loaded.' || document.getElementById('status').textContent.includes('entries'))`), 'live navigation: ' + pathname, 30000).catch(async error => {
+ throw new Error(error.message + ': ' + await evaluate(`document.getElementById('status')?.textContent`));
+ });
+ console.log('live browser navigation passed:', pathname);
+ }
+ for (const site of ['entomology', 'cafe', 'standards', 'pardes', 'cafe-hub']) {
+ const target = `${process.env.NINE_WEB_LIVE}/sites/${site}/`;
+ await cdp('Page.navigate', { url: target });
+ await wait(() => evaluate(`location.href === ${JSON.stringify(target)} && document.readyState === 'complete' && document.body.textContent.length > 50`), 'live website: ' + site, 60000);
+ const images = await evaluate(`Array.from(document.images).filter(i => new URL(i.src).origin === location.origin && i.getAttribute('loading') !== 'lazy').map(i => ({src:i.src, ok:i.complete && i.naturalWidth > 0}))`);
+ assert.deepEqual(images.filter(i => !i.ok), [], `${site}: failed same-origin images`);
+ const unprefixed = await evaluate(`Array.from(document.querySelectorAll('a[href],img[src],script[src],link[href]')).map(e => e.getAttribute('href') || e.getAttribute('src')).filter(u => u?.startsWith('/') && !u.startsWith('//') && !u.startsWith('/sites/${site}/'))`);
+ assert.deepEqual(unprefixed, [], `${site}: unresolved absolute URLs`);
+ console.log('live prefixed website passed:', site, `(${images.length} local images)`);
+ if (site === 'cafe') {
+ await cdp('Page.navigate', { url: target + 'blog/' });
+ await wait(() => evaluate(`location.pathname === '/sites/cafe/blog/' && document.readyState === 'complete' && document.querySelectorAll('a[href^="/sites/cafe/blog/"]').length > 0`), 'cafe blog navigation', 30000);
+ const article = await evaluate(`Array.from(document.querySelectorAll('a[href^="/sites/cafe/blog/"]')).map(a=>a.href).find(h=>new URL(h).pathname !== '/sites/cafe/blog/')`);
+ assert.ok(article);
+ await cdp('Page.navigate', { url: article });
+ await wait(() => evaluate(`location.href === ${JSON.stringify(article)} && document.readyState === 'complete' && document.body.textContent.length > 100`), 'cafe pretty article', 30000);
+ console.log('live cafe blog and pretty article passed:', article);
+ }
+ }
+ }
+ console.log('catalog_ui: shared shell/style, tab history, file/log navigation, build control, focus, dirty edits and mobile layout passed');
+} catch (e) {
+ for (const child of children) console.error(child.output.slice(-5000));
+ throw e;
+} finally {
+ socket?.close();
+ for (const p of children.reverse()) {
+ if (p.exitCode === null) { p.kill('SIGTERM'); await Promise.race([once(p, 'exit'), new Promise(r => setTimeout(r, 3000))]); if (p.exitCode === null) p.kill('SIGKILL'); }
+ }
+ await fs.rm(work, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
+}
diff --git a/test/web/host_fs.py b/test/web/host_fs.py
new file mode 100644
index 0000000..ea39cc9
--- /dev/null
+++ b/test/web/host_fs.py
@@ -0,0 +1,167 @@
+#!/usr/bin/env python3
+"""Exercise host export; mutations are confined to our temporary directory."""
+from catalog import Nine, eventually, http, port, string
+import json
+import os
+from pathlib import Path
+import socket
+import struct
+import subprocess
+import sys
+import tempfile
+
+
+def main():
+ binary = str(Path(sys.argv[1]).resolve())
+ ns = Path('zig-out/bin/9ns').resolve()
+ with tempfile.TemporaryDirectory(prefix='9web-host-') as tmp:
+ root = Path(tmp)
+ p = port()
+ public = root/'public'
+ public.mkdir()
+ (public/'index.html').write_text('site still works')
+ (root/'source').write_text('original data')
+ (root/'alias').symlink_to(root, target_is_directory=True)
+ (root/'dangling').symlink_to(root/'missing')
+ os.mkfifo(root/'fifo')
+ large = root/'large'
+ large.mkdir()
+ for i in range(1500):
+ (large/f'entry-{i:04}').touch()
+ config = {'filesystem': '/', 'sites': [{'name': 'fixture', 'title': 'Fixture', 'root': str(public), 'cwd': tmp, 'port': port(), 'build': ['sh', '-c', 'echo host-build']}]}
+ (root/'config.json').write_text(json.dumps(config))
+ runtime = root/'runtime'
+ registry = runtime/'9p'
+ registry.mkdir(parents=True)
+ demo = subprocess.Popen([str(Path('zig-out/bin/9proc-demo').resolve()), '--unix', str(registry/'fixture')], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+ eventually(lambda: (registry/'fixture').exists())
+ event_config = {'sites': [{**config['sites'][0], 'port': port()}]}
+ (root/'events.json').write_text(json.dumps(event_config))
+ event_server = subprocess.Popen([binary, '--listen', f'127.0.0.1:{port()}', '--catalog', str(root/'events.json'), '--catalog-socket', str(registry/'events'), '--state-dir', str(root/'events-state')], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+ eventually(lambda: (registry/'events').exists())
+ args = [binary, '--listen', f'127.0.0.1:{p}', '--catalog', str(root/'config.json'), '--catalog-socket', str(root/'cat.sock'), '--state-dir', str(root/'state')]
+ # Match the deployed wrapper. No host mount is changed.
+ if not ns.exists():
+ raise RuntimeError('build 9ns first: zig build 9ns')
+ proc = subprocess.Popen([str(ns), '--mntgen', '--', *args], env={**os.environ, 'XDG_RUNTIME_DIR': str(runtime)}, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+ try:
+ eventually(lambda: http(p)[0] == 200)
+ entries = json.loads(http(p, '/fs/')[2])
+ assert {'home', 'etc', 'usr', 'mnt', '_9web'} <= {x['name'] for x in entries}, entries
+ assert http(p, '/fs/etc/hostname')[2] == Path('/etc/hostname').read_bytes()
+ assert http(p, '/fs'+tmp+'/alias/source')[2] == b'original data'
+ assert 'dangling' in {x['name'] for x in json.loads(http(p, '/fs'+tmp)[2])}
+ assert http(p, '/fs'+tmp+'/fifo')[0] >= 400
+ listing = http(p, '/fs'+tmp+'/large')
+ assert listing[0] == 200 and len(json.loads(listing[2])) == 1500
+ assert http(p, '/fs/dev/null')[0] >= 400
+ assert http(p, '/fs/mnt/9p')[0] == 200
+ assert 'fixture' in {x['name'] for x in json.loads(http(p, '/fs/mnt/9p')[2])}
+ assert http(p, '/fs/mnt/9p/fixture/build/zig_version')[0] == 200
+ assert b'0.17' in http(p, '/fs/mnt/9p/fixture/build/zig_version')[2]
+ mounted = '/fs/mnt/9p/fixture'
+ assert http(p, mounted+'/scratch/upload', 'PUT', 'via FUSE')[0] == 201
+ assert http(p, mounted+'/scratch/upload')[2] == b'via FUSE'
+ control = http(p, mounted+'/runtime/ctl', 'PUT', 'echo mount-control')
+ assert control[0] == 200, control
+ assert b'mount-control' in http(p, mounted+'/runtime/ctl')[2]
+ finite = Nine(str(root/'cat.sock'))
+ finite.walk('mnt/9p/fixture/runtime/ctl')
+ finite.rpc(112, struct.pack('<IB', 2, 0))
+ data = finite.rpc(116, struct.pack('<IQI', 2, 0, 4096))[4:]
+ assert b'mount-control' in data
+ assert finite.rpc(116, struct.pack('<IQI', 2, len(data), 4096))[4:] == b''
+ finite.s.close()
+ assert http(p, mounted+'/scratch/upload', 'DELETE')[0] == 204
+ # A blocking read through FUSE must not stall the shared backend.
+ events = Nine(str(root/'cat.sock'))
+ events.walk('mnt/9p/events/sites/fixture/events')
+ events.rpc(112, struct.pack('<IB', 2, 0))
+ assert json.loads(events.rpc(116, struct.pack('<IQI', 2, 0, 4096))[4:])['phase'] == 'idle'
+ request = struct.pack('<IQI', 2, 0, 4096)
+ events.s.sendall(struct.pack('<IBH', len(request)+7, 116, 2) + request)
+ events.rpc(124, struct.pack('<I', 1), 3)
+ # Also wake/retry this parked request from an unrelated catalogue change.
+ assert http(p, '/fs/_9web/sites/fixture/ctl', 'PUT', 'build')[0] == 200
+ assert http(p, '/fs/mnt/9p/events/sites/fixture/ctl', 'PUT', 'build')[0] == 200
+ size, typ, tag = struct.unpack('<IBH', events.recv(7))
+ assert (typ, tag) == (117, 2)
+ assert json.loads(events.recv(size-7)[4:])['revision'] > 1
+ events.s.close()
+ eventually(lambda: json.loads(http(p, '/fs/mnt/9p/events/sites/fixture/status')[2])['generation'] == 1)
+ # Repeated flush+clunk must release the underlying blocking reads,
+ # rather than exhaust the bounded task table or steal later events.
+ cancel = Nine(str(root/'cat.sock'))
+ for _ in range(40):
+ cancel.walk('mnt/9p/events/sites/fixture/events')
+ cancel.rpc(112, struct.pack('<IB', 2, 0))
+ cancel.rpc(116, struct.pack('<IQI', 2, 0, 4096))
+ cancel.s.sendall(struct.pack('<IBH', len(request)+7, 116, 2) + request)
+ cancel.s.sendall(struct.pack('<IBHH', 9, 108, 3, 2))
+ # An interrupted old read may be answered before Rflush.
+ while True:
+ n, kind, tag = struct.unpack('<IBH', cancel.recv(7))
+ cancel.recv(n-7)
+ if (kind, tag) == (109, 3):
+ break
+ assert tag == 2 and kind in (107, 117), (kind, tag)
+ cancel.rpc(120, struct.pack('<I', 2), 4)
+ cancel.s.close()
+ # Directory and upload/edit/append/delete use the same HTTP/9P path.
+ target = '/fs'+tmp+'/created'
+ assert http(p, target+'/', 'PUT')[0] == 201
+ assert http(p, target+'/file', 'PUT', 'abcdef')[0] == 201
+ assert http(p, target+'/file', 'PUT', 'xy')[0] == 200
+ assert (root/'created/file').read_bytes() == b'xy'
+ assert http(p, target+'/file?append=1', 'PUT', 'z')[0] == 200
+ assert http(p, target+'/file')[2] == b'xyz'
+ assert http(p, target+'/file', headers={'Range': 'bytes=1-2'})[2] == b'yz'
+ assert http(p, target+'/file', 'HEAD')[1]['content-length'] == '3'
+ # The local namespace is also accessible through raw 9P.
+ nine = Nine(str(root/'cat.sock'))
+ rel = tmp.lstrip('/')
+ assert nine.read(rel+'/source') == b'original data'
+ nine.write(rel+'/created/file', 'short')
+ assert (root/'created/file').read_text() == 'short'
+ # Twstat rename on the backing filesystem (not a UI copy/delete).
+ nine.walk(rel+'/created/file')
+ stat = struct.pack('<HI', 0xffff, 0xffffffff) + b'\xff'*13 + struct.pack('<IIIQ', 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffffffffffff) + string('renamed') + string('')*3
+ stat = struct.pack('<H', len(stat)) + stat
+ nine.rpc(126, struct.pack('<IH', 2, len(stat)) + stat)
+ nine.rpc(120, struct.pack('<I', 2))
+ assert (root/'created/renamed').read_text() == 'short'
+ # Virtual tree stays read-only apart from ctl.
+ assert http(p, '/fs/_9web/new', 'PUT', 'bad')[0] == 403
+ assert http(p, '/fs/_9web/sites/fixture/public/index.html', 'PUT', 'bad')[0] == 403
+ assert http(p, '/fs/_9web/sites/fixture/ctl', 'PUT', 'build')[0] == 200
+ eventually(lambda: b'host-build' in http(p, '/fs/_9web/sites/fixture/log')[2])
+ assert http(config['sites'][0]['port'])[2] == b'site still works'
+ assert http(p, '/_cloud9/catalog.json')[0] == 200
+ assert json.loads(http(p, '/_cloud9/catalog.json')[2])['namespace'] == '/mnt/9p/9web'
+ assert http(p, '/fs/mnt/9p/9web/sites/fixture/public/index.html')[2] == b'site still works'
+ assert http(p, '/sites/fixture/')[2] == b'site still works'
+ pure = Nine(str(registry/'9web'))
+ assert pure.list('sites') == ['fixture']
+ assert pure.read('sites/fixture/public/index.html') == b'site still works'
+ pure.s.close()
+ assert http(p, target+'/renamed', 'DELETE')[0] == 204
+ assert http(p, target, 'DELETE')[0] == 204
+ nine.s.close()
+ print('host_fs: root, symlinks, special-file refusal, /mnt/9p, upload/edit/append/rename/delete, raw 9P and catalogue controls passed')
+ finally:
+ proc.terminate()
+ try:
+ output = proc.communicate(timeout=5)[0]
+ except subprocess.TimeoutExpired:
+ proc.kill()
+ output = proc.communicate()[0]
+ if sys.exc_info()[0]:
+ print(output.decode(errors='replace'), file=sys.stderr)
+ demo.terminate()
+ demo.wait(timeout=5)
+ event_server.terminate()
+ event_server.wait(timeout=5)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/test/web/parallel_reads.mjs b/test/web/parallel_reads.mjs
new file mode 100644
index 0000000..a95976b
--- /dev/null
+++ b/test/web/parallel_reads.mjs
@@ -0,0 +1,44 @@
+// Read-only stress check: concurrent multi-frame website reads must be exact.
+import assert from 'node:assert/strict';
+import fs from 'node:fs/promises';
+import path from 'node:path';
+import { createHash } from 'node:crypto';
+
+const [base, root] = process.argv.slice(2);
+if (!base || !root) throw new Error('usage: node parallel_reads.mjs URL DIRECTORY');
+const hash = bytes => createHash('sha256').update(bytes).digest('hex');
+const fileUrl = name => base.replace(/\/$/, '') + '/' + name.split(path.sep).map(encodeURIComponent).join('/');
+const files = [];
+async function collect(dir, rel = '') {
+ for (const entry of await fs.readdir(dir, { withFileTypes: true })) {
+ const name = path.join(rel, entry.name), full = path.join(dir, entry.name);
+ if (entry.isDirectory()) await collect(full, name);
+ else if (entry.isFile()) {
+ const bytes = await fs.readFile(full);
+ if (bytes.length >= 65536) files.push({ name, size: bytes.length, hash: hash(bytes) });
+ }
+ }
+}
+await collect(root);
+assert.ok(files.length > 0, 'need at least one multi-frame file');
+for (let round = 0; round < 5; round++) {
+ const reads = Array.from({ length: 16 }, async (_, i) => {
+ const file = files[(round * 16 + i) % files.length];
+ const response = await fetch(fileUrl(file.name));
+ assert.equal(response.status, 200, file.name);
+ const bytes = Buffer.from(await response.arrayBuffer());
+ assert.equal(bytes.length, file.size, `truncated: ${file.name}`);
+ assert.equal(hash(bytes), file.hash, `corrupt: ${file.name}`);
+ });
+ // Mix synchronous stats with reads, and abandon owned responses midway.
+ const canceled = Array.from({ length: 8 }, async (_, i) => {
+ const response = await fetch(fileUrl(files[i % files.length].name));
+ assert.equal(response.status, 200);
+ const reader = response.body.getReader();
+ await reader.read();
+ await reader.cancel();
+ });
+ const head = fetch(fileUrl(files[0].name), { method: 'HEAD' }).then(r => assert.equal(r.status, 200));
+ await Promise.all([...reads, ...canceled, head]);
+}
+console.log(`parallel_reads: 80 exact multi-frame responses, 16 concurrent, 40 cancellations and mixed HEAD (${files.length} source files)`);
diff --git a/test/web/prefix.py b/test/web/prefix.py
new file mode 100644
index 0000000..68fe974
--- /dev/null
+++ b/test/web/prefix.py
@@ -0,0 +1,73 @@
+#!/usr/bin/env python3
+import hashlib
+import importlib.util
+from pathlib import Path
+import tempfile
+
+spec = importlib.util.spec_from_file_location('prefix', Path(__file__).resolve().parents[2] / 'web/prefix.py')
+p = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(p)
+prefix = '/sites/fixture'
+original = '''<!DOCTYPE html><html><head><style>
+/* url(/comment) */
+a[href="/blog/"]{background:url('/assets/a.png');content:"/ordinary"}
+@font-face{src:url(/fonts/a.woff2)}
+@import "/main.css";
+.svg{background:url("data:image/svg+xml,%3Csvg viewBox='0 0 10 10'%3E")}
+</style></head><body>
+<a href="/blog/post?q=1&amp;x=2#part">go</a><img src="/img.png" srcset="/one.png 1x, /two.png 2x">
+<a href="//elsewhere/foo">external</a><a href="https://other/x">external</a>
+<a href="#part">fragment</a><img src="data:image/png;base64,abcd">
+<a href="../page.html">relative</a><div style="background:url(/img.png)"></div>
+<code>href="/example"</code><!-- <a href="/comment"> -->
+<script>const s = '/assets/unmodified.js'; fetch('/api/pokes');</script>
+<script type="application/json">{"url":"/unmodified"}</script>
+</body></html>'''
+rewritten = p.html(original, prefix)
+assert 'srcset=" /sites/fixture/a.png 1x, /sites/fixture/b.png 2x"' in p.html('<img srcset=" /a.png 1x, /b.png 2x">', prefix)
+assert 'url(&quot;/sites/fixture/a.png&quot;)' in p.html('<div style="background:url(&quot;/a.png&quot;)"></div>', prefix)
+assert p.srcset('data:image/svg+xml,/untouched 1x, /a.png 2x', prefix) == 'data:image/svg+xml,/untouched 1x, /sites/fixture/a.png 2x'
+for piece in ['href="/sites/fixture/blog/post?q=1&amp;x=2#part"',
+ 'srcset="/sites/fixture/one.png 1x, /sites/fixture/two.png 2x"',
+ 'url(/sites/fixture/fonts/a.woff2)', 'a[href="/sites/fixture/blog/"]',
+ '@import "/sites/fixture/main.css"']:
+ assert piece in rewritten, piece
+for piece in ['/* url(/comment) */', 'content:"/ordinary"', '<code>href="/example"</code>',
+ "fetch('/api/pokes')", "const s = '/assets/unmodified.js'", '{"url":"/unmodified"}',
+ '<!-- <a href="/comment"> -->', 'href="../page.html"', 'href="#part"',
+ 'href="//elsewhere/foo"', 'href="https://other/x"', 'src="data:image/png;base64,abcd"',
+ 'url("data:image/svg+xml,%3Csvg viewBox=\'0 0 10 10\'%3E")']:
+ assert piece in rewritten, piece
+for invalid in ['', '/', '//host', '/a/../b', '/a?b', '/a#b', 'http://a']:
+ try:
+ p.check_prefix(invalid)
+ raise AssertionError(invalid)
+ except ValueError:
+ pass
+with tempfile.TemporaryDirectory() as tmp:
+ root = Path(tmp)
+ source, out = root/'normal', root/'preview'
+ source.mkdir()
+ (source/'index.html').write_text(original)
+ (source/'app.js').write_text("fetch('/api/pokes')")
+ def hashes():
+ return {str(f.relative_to(source)): hashlib.sha256(f.read_bytes()).hexdigest() for f in source.rglob('*') if f.is_file()}
+ before = hashes()
+ p.publish(source, out, prefix)
+ assert hashes() == before
+ assert out.is_symlink() and (out/'index.html').read_text() == rewritten
+ assert (out/'app.js').read_bytes() == (source/'app.js').read_bytes()
+ previous = out.resolve()
+ (source/'app.js').unlink()
+ p.publish(source, out, prefix)
+ assert out.resolve() != previous and previous.exists()
+ assert not (out/'app.js').exists(), 'stale files must not leak into next generation'
+ (source/'escape').symlink_to('/etc/hostname')
+ current = out.resolve()
+ try:
+ p.publish(source, out, prefix)
+ raise AssertionError('symlink accepted')
+ except ValueError:
+ pass
+ assert out.resolve() == current, 'failed generation changed published output'
+print('prefix: scoped HTML/CSS, unchanged JS/text/source, atomic publication and failure preservation passed')