|
| 1 | +// browser.test.js |
| 2 | +import http from 'node:http' |
| 3 | +import { spawn } from 'node:child_process' |
| 4 | +import { once } from 'node:events' |
| 5 | +import fs from 'node:fs/promises' |
| 6 | + |
| 7 | +const PORT = 5173 |
| 8 | + |
| 9 | +function html(mode = '') { |
| 10 | + const forceFallback = mode === 'fallback' |
| 11 | + |
| 12 | + return /* html */ ` |
| 13 | +<!doctype html> |
| 14 | +<html> |
| 15 | +<body> |
| 16 | +<script> |
| 17 | +${ |
| 18 | + forceFallback |
| 19 | + ? ` |
| 20 | +delete globalThis.CompressionStream |
| 21 | +delete globalThis.DecompressionStream |
| 22 | +delete Uint8Array.prototype.toBase64 |
| 23 | +delete Uint8Array.fromBase64 |
| 24 | +` |
| 25 | + : '' |
| 26 | +} |
| 27 | +</script> |
| 28 | +
|
| 29 | +<script type="module"> |
| 30 | +import { zipurl, unzipurl } from './index.js?mode=${mode}' |
| 31 | +
|
| 32 | +const results = [] |
| 33 | +
|
| 34 | +async function test(name, fn) { |
| 35 | + try { |
| 36 | + await fn() |
| 37 | + results.push({ name, ok: true }) |
| 38 | + } catch (err) { |
| 39 | + results.push({ name, ok: false, error: err?.message || String(err) }) |
| 40 | + } |
| 41 | +} |
| 42 | +
|
| 43 | +// --- tests --- |
| 44 | +
|
| 45 | +await test('basic', async () => { |
| 46 | + const input = 'hello world' |
| 47 | + const out = await unzipurl(await zipurl(input)) |
| 48 | + if (out !== input) throw new Error('mismatch') |
| 49 | +}) |
| 50 | +
|
| 51 | +await test('unicode', async () => { |
| 52 | + const input = '你好 🌏🚀 café üñîçødê' |
| 53 | + const out = await unzipurl(await zipurl(input)) |
| 54 | + if (out !== input) throw new Error('mismatch') |
| 55 | +}) |
| 56 | +
|
| 57 | +await test('empty', async () => { |
| 58 | + const input = '' |
| 59 | + const out = await unzipurl(await zipurl(input)) |
| 60 | + if (out !== input) throw new Error('mismatch') |
| 61 | +}) |
| 62 | +
|
| 63 | +await test('long string', async () => { |
| 64 | + const input = 'abc123 '.repeat(10000) |
| 65 | + const out = await unzipurl(await zipurl(input)) |
| 66 | + if (out !== input) throw new Error('mismatch') |
| 67 | +}) |
| 68 | +
|
| 69 | +await test('url-safe', async () => { |
| 70 | + const z = await zipurl('test + / =') |
| 71 | + if (!/^[A-Za-z0-9\\-_]+$/.test(z)) { |
| 72 | + throw new Error('not url safe') |
| 73 | + } |
| 74 | +}) |
| 75 | +
|
| 76 | +await test('compression effectiveness', async () => { |
| 77 | + const input = 'aaaaaaabbbbbbbcccccccddddddd'.repeat(100) |
| 78 | + const z = await zipurl(input) |
| 79 | + if (z.length >= input.length) { |
| 80 | + throw new Error('not compressed') |
| 81 | + } |
| 82 | +}) |
| 83 | +
|
| 84 | +await test('invalid input throws', async () => { |
| 85 | + let threw = false |
| 86 | + try { |
| 87 | + await unzipurl('invalid!!base64') |
| 88 | + } catch { |
| 89 | + threw = true |
| 90 | + } |
| 91 | + if (!threw) throw new Error('did not throw') |
| 92 | +}) |
| 93 | +
|
| 94 | +await test('multiple roundtrips (stability)', async () => { |
| 95 | + for (let i = 0; i < 10; i++) { |
| 96 | + const input = 'loop-' + i + '-' + Math.random() |
| 97 | + const out = await unzipurl(await zipurl(input)) |
| 98 | + if (out !== input) throw new Error('mismatch at ' + i) |
| 99 | + } |
| 100 | +}) |
| 101 | +
|
| 102 | +// --- report --- |
| 103 | +
|
| 104 | +await fetch('/report?mode=${mode}', { |
| 105 | + method: 'POST', |
| 106 | + headers: { 'content-type': 'application/json' }, |
| 107 | + body: JSON.stringify(results) |
| 108 | +}) |
| 109 | +
|
| 110 | +window.close() |
| 111 | +</script> |
| 112 | +</body> |
| 113 | +</html> |
| 114 | +` |
| 115 | +} |
| 116 | + |
| 117 | +let phase = 'native' |
| 118 | +let failures = 0 |
| 119 | + |
| 120 | +const server = http.createServer(async (req, res) => { |
| 121 | + const url = new URL(req.url || '/', `http://localhost:${PORT}`) |
| 122 | + |
| 123 | + if (url.pathname === '/') { |
| 124 | + res.setHeader('content-type', 'text/html') |
| 125 | + res.end(html(phase)) |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + if (url.pathname === '/index.js') { |
| 130 | + const code = await fs.readFile(new URL('../index.js', import.meta.url)) |
| 131 | + res.setHeader('content-type', 'application/javascript') |
| 132 | + res.end(code) |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + if (url.pathname === '/report' && req.method === 'POST') { |
| 137 | + let body = '' |
| 138 | + for await (const chunk of req) body += chunk |
| 139 | + |
| 140 | + const results = JSON.parse(body) |
| 141 | + const mode = url.searchParams.get('mode') |
| 142 | + |
| 143 | + console.log(`\nBrowser tests (${mode}):\n`) |
| 144 | + |
| 145 | + let failed = 0 |
| 146 | + for (const r of results) { |
| 147 | + if (r.ok) { |
| 148 | + console.log('✓', r.name) |
| 149 | + } else { |
| 150 | + failed++ |
| 151 | + console.error('✗', r.name, '-', r.error) |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + failures += failed |
| 156 | + res.end('ok') |
| 157 | + |
| 158 | + if (phase === 'native') { |
| 159 | + phase = 'fallback' |
| 160 | + openBrowser() |
| 161 | + } else { |
| 162 | + server.close() |
| 163 | + setTimeout(() => process.exit(failures ? 1 : 0), 200) |
| 164 | + } |
| 165 | + |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + res.statusCode = 404 |
| 170 | + res.end() |
| 171 | +}) |
| 172 | + |
| 173 | +await new Promise((resolve) => server.listen(PORT, /** @type {any} */ (resolve))) |
| 174 | + |
| 175 | +function openBrowser() { |
| 176 | + const url = `http://localhost:${PORT}` |
| 177 | + |
| 178 | + const cmd = |
| 179 | + process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'cmd' : 'xdg-open' |
| 180 | + |
| 181 | + const args = process.platform === 'win32' ? ['/c', 'start', url] : [url] |
| 182 | + |
| 183 | + spawn(cmd, args, { stdio: 'ignore', detached: true }) |
| 184 | +} |
| 185 | + |
| 186 | +console.log('Running browser tests (native + fallback)...') |
| 187 | +openBrowser() |
| 188 | + |
| 189 | +await once(server, 'close') |
0 commit comments