Files
breeze/tests/site_assets_test.mjs
waozi ef0de86e02
Cloudflare Pages / Build and verify the site (push) Successful in 3m29s
Integrate Breeze desktop, Android, and app updates
Use the verified dependency lock across native hosts and bundled apps. Preserve saved styles, local app reactivation, and offline packages while integrating the latest practice, Habits, Lumi, and figure preview updates.
2026-11-20 02:24:34 -03:00

286 lines
14 KiB
JavaScript

#!/usr/bin/env node
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { mkdtempSync, mkdirSync, readFileSync, writeFileSync, readdirSync, rmSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { spawnSync } from 'node:child_process';
import * as zlib from 'node:zlib';
import { Script } from 'node:vm';
const { gunzipSync, zstdDecompressSync } = zlib;
const root = resolve(import.meta.dirname, '..');
const temporary = mkdtempSync(join(tmpdir(), 'inbe-site-assets-'));
const source = readFileSync(join(root, 'site/static/_worker.js'));
const { default: worker } = await import('data:text/javascript;base64,' + source.toString('base64'));
const limit = 4096;
const random = Buffer.concat(Array.from({ length: 400 }, (_, i) =>
createHash('sha256').update(String(i)).digest()));
const payloads = new Map();
const requests = [];
const assets = {
async fetch(request) {
requests.push(request);
const path = join(temporary, new URL(request.url).pathname);
if (!existsSync(path)) {
return new Response('Not found', { status: 404 });
}
const headers = { 'Content-Type': path.endsWith('.json')
? 'application/json' : 'application/octet-stream', 'Cache-Control': 'public, max-age=0' };
return new Response(request.method === 'HEAD' ? null : readFileSync(path), { headers });
},
};
async function serve(path, options = {}) {
return worker.fetch(new Request('https://inbe.example' + path + '?v=test', options), { ASSETS: assets });
}
async function verifyHostRouting() {
const staticAssets = {
fetch: request => new Response(request.method === 'HEAD' ? null : 'page fixture', {
status: new URL(request.url).pathname === '/missing-page' ? 404 : 200,
headers: { 'Content-Type': 'text/html; charset=utf-8' },
}),
};
const at = (host, path, method) => worker.fetch(new Request(`https://${host}${path}`, { method }), { ASSETS: staticAssets });
for (const host of ['inbe.waozi.xyz', 'breeze.waozi.xyz', 'www.innerbreeze.org']) {
for (const path of ['/privacy?from=A%2FB&hl=en', '//other.example/path?q=1', '/build/telegram/?r=abcdef']) {
const response = await at(host, path, 'HEAD');
assert.equal(response.status, 308);
assert.equal(response.headers.get('Location'), `https://innerbreeze.org${path}`);
}
}
for (const path of ['/build/web/?legacy-origin=1', '/build/web/index.js', '/apt/inbe.sources', '/web-assets/dl/audio.zip']) {
assert.equal((await at('breeze.waozi.xyz', path)).status, 200);
}
for (const path of ['/', '/privacy', '/install-apt', '/legacy-converter/']) {
assert.equal((await at('innerbreeze.org', path)).headers.get('X-Robots-Tag'), null);
}
for (const [host, path] of [['innerbreeze.org', '/build/web/'], ['innerbreeze.org', '/delete-account'],
['innerbreeze.org', '/web-assets/canvas_index'], ['innerbreeze.org', '/web-assets/canvas_index.html'],
['innerbreeze.org', '/404'],
['breeze.waozi.xyz', '/build/web/?legacy-origin=1'], ['preview.inbe.pages.dev', '/']]) {
assert.equal((await at(host, path)).headers.get('X-Robots-Tag'), 'noindex, follow');
}
const missing = await at('innerbreeze.org', '/missing-page');
assert.equal(missing.status, 404);
assert.equal(missing.headers.get('X-Robots-Tag'), 'noindex, follow');
console.log('Site hosts: exact path/query redirects, origin-bound app data routes, APT and noindex passed');
}
function verifyTelegramBootstrap() {
const generator = readFileSync(join(root, 'site/build.sh'), 'utf8');
const start = generator.indexOf('write_telegram_web_app_html() {');
const end = generator.indexOf('\nrequire_output() {', start);
assert.ok(start >= 0 && end > start, 'Mini App generator function missing');
const input = join(temporary, 'bootstrap-input.html');
const output = join(temporary, 'bootstrap-output.html');
writeFileSync(input, `<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<style>body { margin: 0; }</style>
</head>
<body>
<script>
window.originalBootstrapRan = true;
</script>
</body>
</html>
`);
const environment = { ...process.env };
for (const name of ['DISPLAY', 'WAYLAND_DISPLAY', 'XAUTHORITY', 'DBUS_SESSION_BUS_ADDRESS']) {
delete environment[name];
}
environment.YUE_DESKTOP_RECOVERY = '0';
const generated = spawnSync('sh', ['-c',
generator.slice(start, end) + '\nwrite_telegram_web_app_html "$1" "$2"\n',
'telegram-bootstrap-test', input, output], { encoding: 'utf8', env: environment });
assert.equal(generated.status, 0, generated.stderr);
const html = readFileSync(output, 'utf8');
const scripts = [...html.matchAll(/<script\b([^>]*)>([\s\S]*?)<\/script>/g)]
.filter(match => !/\bsrc\s*=/.test(match[1]) && match[2].trim())
.map(match => new Script(match[2], { filename: 'generated-miniapp-bootstrap.js' }));
assert.equal(scripts.length, 2);
assert.ok(html.indexOf('telegram-web-app.js') < html.indexOf('telegram-bridge.js'));
assert.ok(html.indexOf('telegram-bridge.js') < html.indexOf('window.originalBootstrapRan'));
assert.ok(html.includes('viewport-fit=cover'));
assert.equal(generated.stderr, '', 'Generator emitted an escaping diagnostic');
for (const mode of ['absent', 'minimal', 'default-theme', 'provided-theme']) {
const calls = [];
const events = new Map();
const app = {
ready() {
calls.push(['ready']);
},
expand() {
calls.push(['expand']);
},
};
if (mode === 'default-theme' || mode === 'provided-theme') {
app.disableVerticalSwipes = () => calls.push(['disableVerticalSwipes']);
app.setBackgroundColor = color => calls.push(['background', color]);
app.setHeaderColor = color => calls.push(['header', color]);
app.themeParams = mode === 'provided-theme' ? { bg_color: '#abcdef' } : {};
}
const context = {
window: mode === 'absent' ? {} : { Telegram: { WebApp: app } },
document: {
addEventListener(name, handler, options) {
assert.ok(!events.has(name));
events.set(name, { handler, options });
},
},
};
for (const script of scripts) {
script.runInNewContext(context, { timeout: 1000 });
}
assert.equal(context.window.originalBootstrapRan, true);
const expected = mode === 'absent' ? [] : [['ready'], ['expand']];
if (mode === 'default-theme' || mode === 'provided-theme') {
const color = mode === 'provided-theme' ? '#abcdef' : '#181818';
expected.push(['disableVerticalSwipes'], ['background', color], ['header', color]);
}
assert.deepEqual(calls, expected);
assert.equal(events.size, 2);
assert.equal(events.get('touchmove').options.passive, false);
for (const name of ['touchmove', 'gesturestart']) {
let prevented = false;
events.get(name).handler({ preventDefault() { prevented = true; } });
assert.equal(prevented, true);
}
}
console.log('Mini App generated bootstrap: syntax, SDK absence/older features, theme and gestures passed');
}
async function verifyWasmNegotiation() {
const partLimit = 96 * 1024;
const block = Array.from({ length: 3 }, (_, frame) => {
const noise = Buffer.concat(Array.from({ length: 1920 }, (_, i) =>
createHash('sha256').update(`${frame}-${i}`).digest()));
const repeat = noise.subarray(0, 36 * 1024);
return Buffer.concat([repeat, repeat, noise.subarray(repeat.length)]);
});
const original = Buffer.concat(block);
for (const app of ['web', 'telegram']) {
const directory = join(temporary, 'build', app);
rmSync(directory, { recursive: true, force: true });
mkdirSync(directory, { recursive: true });
writeFileSync(join(directory, 'index.wasm'), original);
}
const result = spawnSync('python3', [join(root, 'scripts/prepare-site-assets.py'), temporary,
'--max-file-bytes', String(partLimit)], { encoding: 'utf8' });
assert.equal(result.status, 0, result.stdout + result.stderr);
const library = spawnSync('python3', ['-c', `import runpy, sys
stage = runpy.run_path(sys.argv[1])
print('available' if stage['load_zstd']() else 'missing')`,
join(root, 'scripts/prepare-site-assets.py')], { encoding: 'utf8' });
assert.equal(library.status, 0, library.stdout + library.stderr);
for (const app of ['web', 'telegram']) {
const path = `/build/${app}/index.wasm`;
const directory = join(temporary, 'build', app);
const gzip = await serve(path, { headers: { 'Accept-Encoding': 'gzip, zstd;q=0' } });
assert.equal(gzip.headers.get('Content-Encoding'), 'gzip');
assert.ok(gzip.headers.get('Vary').toLowerCase().includes('accept-encoding'));
assert.deepEqual(gunzipSync(Buffer.from(await gzip.arrayBuffer())), original);
const zstdManifest = join(directory, 'index.wasm.zstd.parts.json');
if (library.stdout.trim() === 'missing') {
assert.ok(!existsSync(zstdManifest));
assert.equal((await serve(path, { method: 'HEAD', headers: { 'Accept-Encoding': 'zstd, gzip' } }))
.headers.get('Content-Encoding'), 'gzip');
continue;
}
const requestForZstd = new Request('https://inbe.example' + path + '?v=test', {
headers: { 'Accept-Encoding': 'gzip, br', Range: 'bytes=0-9', 'If-None-Match': gzip.headers.get('ETag') },
});
requestForZstd.cf = { clientAcceptEncoding: 'gzip;q=0.8, ZSTD;q=0.9' };
const zstd = await worker.fetch(requestForZstd, { ASSETS: assets });
assert.equal(zstd.status, 200);
assert.equal(zstd.headers.get('Content-Encoding'), 'zstd');
assert.equal(zstd.headers.get('Content-Type'), 'application/wasm');
assert.match(zstd.headers.get('Cache-Control'), /no-transform/);
const encoded = Buffer.from(await zstd.arrayBuffer());
assert.equal(zstd.headers.get('Content-Length'), String(encoded.length));
assert.equal(zstd.headers.get('ETag'), '"' + createHash('sha256').update(encoded).digest('hex') + '"');
assert.ok(encoded.length < +JSON.parse(readFileSync(join(directory, 'index.wasm.parts.json'))).size);
if (typeof zstdDecompressSync === 'function') {
assert.deepEqual(zstdDecompressSync(encoded), original);
}
const manifest = JSON.parse(readFileSync(zstdManifest));
assert.ok(manifest.parts.length > 1);
assert.ok(manifest.parts.every(part => readFileSync(join(directory, part)).byteLength <= partLimit));
const head = await serve(path, { method: 'HEAD', headers: { 'Accept-Encoding': 'zstd, gzip' } });
assert.equal(head.headers.get('ETag'), zstd.headers.get('ETag'));
assert.equal((await head.arrayBuffer()).byteLength, 0);
const cached = await serve(path, { headers: { 'Accept-Encoding': 'gzip, zstd', 'If-None-Match': zstd.headers.get('ETag') } });
assert.equal(cached.status, 304);
const gzipPreferred = await serve(path, { method: 'HEAD', headers: { 'Accept-Encoding': 'zstd;q=0.5, gzip' } });
assert.equal(gzipPreferred.headers.get('Content-Encoding'), 'gzip');
writeFileSync(zstdManifest, '{');
const fallback = await serve(path, { method: 'HEAD', headers: { 'Accept-Encoding': 'zstd, gzip' } });
assert.equal(fallback.headers.get('ETag'), gzip.headers.get('ETag'));
}
console.log('Wasm variants: Zstd framing, negotiation, gzip fallback, asset limit and validators passed');
}
try {
await verifyHostRouting();
verifyTelegramBootstrap();
for (const app of ['web', 'telegram']) {
const directory = join(temporary, 'build', app);
mkdirSync(directory, { recursive: true });
for (const [name, bytes] of [['index.data', random], ['index.wasm', Buffer.concat([random, random])]]) {
writeFileSync(join(directory, name), bytes);
payloads.set('/build/' + app + '/' + name, bytes);
}
}
const result = spawnSync('python3', [join(root, 'scripts/prepare-site-assets.py'), temporary,
'--max-file-bytes', String(limit)], { encoding: 'utf8' });
assert.equal(result.status, 0, result.stdout + result.stderr);
for (const [path, original] of payloads) {
const response = await serve(path, { headers: { Range: 'bytes=0-9' } });
assert.equal(response.status, 200);
assert.equal(response.headers.get('Content-Encoding'), 'gzip');
assert.equal(response.headers.get('Content-Type'), path.endsWith('.wasm')
? 'application/wasm' : 'application/octet-stream');
assert.deepEqual(gunzipSync(Buffer.from(await response.arrayBuffer())), original);
const head = await serve(path, { method: 'HEAD' });
assert.equal((await head.arrayBuffer()).byteLength, 0);
const cached = await serve(path, { headers: { 'If-None-Match': response.headers.get('ETag') } });
assert.equal(cached.status, 304);
assert.equal((await cached.arrayBuffer()).byteLength, 0);
for (const part of readdirSync(join(temporary, 'build', path.split('/')[2]))) {
assert.ok(readFileSync(join(temporary, 'build', path.split('/')[2], part)).byteLength <= limit);
}
}
assert.ok(requests.filter(r => r.url.includes('.part-')).every(r => !r.headers.has('Range')));
const manifestPath = join(temporary, 'build/web/index.data.parts.json');
const manifest = JSON.parse(readFileSync(manifestPath));
rmSync(join(temporary, 'build/web', manifest.parts[1]));
const missing = await serve('/build/web/index.data');
await assert.rejects(missing.arrayBuffer(), /Missing browser asset part/);
const directory = join(temporary, 'build/web');
rmSync(directory, { recursive: true });
mkdirSync(directory);
writeFileSync(join(directory, 'index.data'), Buffer.alloc(8192, 65));
writeFileSync(join(directory, 'index.wasm'), Buffer.alloc(8192, 66));
const compressed = spawnSync('python3', [join(root, 'scripts/prepare-site-assets.py'), temporary,
'--max-file-bytes', String(limit)], { encoding: 'utf8' });
assert.equal(compressed.status, 0, compressed.stderr);
assert.deepEqual(gunzipSync(Buffer.from(await (await serve('/build/web/index.data')).arrayBuffer())), Buffer.alloc(8192, 65));
const wasm = await serve('/build/web/index.wasm');
assert.equal(wasm.headers.get('Content-Type'), 'application/wasm');
assert.deepEqual(gunzipSync(Buffer.from(await wasm.arrayBuffer())), Buffer.alloc(8192, 66));
writeFileSync(join(directory, 'index.wasm'), Buffer.from('small wasm'));
rmSync(join(directory, 'index.wasm.gz'));
assert.equal(await (await serve('/build/web/index.wasm')).text(), 'small wasm');
console.log('Site assets: both apps, data/Wasm, streaming gzip, cache, HEAD, ranges and missing pieces passed');
await verifyWasmNegotiation();
} finally {
rmSync(temporary, { recursive: true, force: true });
}