import assert from 'node:assert/strict'; import { execFile } from 'node:child_process'; import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import test from 'node:test'; import { install, RUNTIME_FILES } from '../installer.js'; const exec = (file, args) => new Promise((resolve, reject) => execFile(file, args, error => error ? reject(error) : resolve())); const response = body => async () => new Response(body, { status: 200 }); async function archive(files) { const temp = await mkdtemp(join(tmpdir(), 'unfugwart-test-')); try { const root = join(temp, 'Unfugwart-deadbeef'); await mkdir(root); for (const [name, value] of Object.entries(files)) { const destination = join(root, name); await mkdir(join(destination, '..'), { recursive: true }); await writeFile(destination, value); } const output = join(temp, 'archive.tar'); await exec('tar', ['-cf', output, '-C', temp, ...Object.keys(files).map(name => `Unfugwart-deadbeef/${name}`)]); return await readFile(output); } finally { await rm(temp, { recursive: true, force: true }); } } async function traversalArchive() { const temp = await mkdtemp(join(tmpdir(), 'unfugwart-test-')); try { const root = join(temp, 'Unfugwart-deadbeef'); await mkdir(root); for (const [name, value] of Object.entries(runtime)) await writeFile(join(root, name), value); await writeFile(join(root, 'extra.js'), 'forbidden'); const output = join(temp, 'archive.tar'); await exec('tar', ['-cf', output, '--transform=s|Unfugwart-deadbeef/extra.js|Unfugwart-deadbeef/../escape.js|', '-C', temp, ...Object.keys(runtime).map(name => `Unfugwart-deadbeef/${name}`), 'Unfugwart-deadbeef/extra.js']); return await readFile(output); } finally { await rm(temp, { recursive: true, force: true }); } } async function target() { const root = await mkdtemp(join(tmpdir(), 'unfugwart-target-')); await writeFile(join(root, 'bot.js'), 'old bot'); await writeFile(join(root, 'config.json'), 'private bytes'); return root; } const runtime = Object.fromEntries(RUNTIME_FILES.map(file => [file, `new ${file}`])); async function rejectsWithoutReplacing(body) { const root = await target(); try { await assert.rejects(install({ root, fetchFn: response(body), runNpm: async () => assert.fail('npm must not run') })); assert.equal(await readFile(join(root, 'bot.js'), 'utf8'), 'old bot'); assert.equal(await readFile(join(root, 'config.json'), 'utf8'), 'private bytes'); } finally { await rm(root, { recursive: true, force: true }); } } test('installer accepts full repository archive without directory entries and preserves local files', async () => { const root = await target(); try { let npmRan = false; const repo = { ...runtime, README: 'docs', 'test/example.js': 'test', 'scripts/build.js': 'script', 'settings.json': 'template', 'config.example.json': 'example', installer: 'source', '.gitignore': 'ignored' }; await install({ root, fetchFn: response(await archive(repo)), runNpm: async () => { npmRan = true; } }); assert.equal(await readFile(join(root, 'bot.js'), 'utf8'), 'new bot.js'); assert.equal(await readFile(join(root, 'config.json'), 'utf8'), 'private bytes'); await assert.rejects(readFile(join(root, 'README'))); assert.equal(npmRan, true); } finally { await rm(root, { recursive: true, force: true }); } }); test('installer rejects malformed, traversal, incomplete, and config archives before replacement', async () => { await rejectsWithoutReplacing(Buffer.from('not a tar')); await rejectsWithoutReplacing(await archive({ ...runtime, 'config.json': 'forbidden' })); const { 'package-lock.json': ignored, ...missing } = runtime; await rejectsWithoutReplacing(await archive(missing)); await rejectsWithoutReplacing(await traversalArchive()); }); test('installer rejects HTTP failures and oversized downloads', async () => { const root = await target(); try { await assert.rejects(install({ root, fetchFn: async () => new Response('', { status: 500 }) })); await assert.rejects(install({ root, fetchFn: async () => new Response('', { headers: { 'content-length': String(33 * 1024 * 1024) } }) })); assert.equal(await readFile(join(root, 'bot.js'), 'utf8'), 'old bot'); } finally { await rm(root, { recursive: true, force: true }); } }); test('runtime files include bot static relative import closure', async () => { const root = new URL('../', import.meta.url); const visited = new Set(); const pending = ['bot.js']; while (pending.length) { const file = pending.pop(); if (visited.has(file)) continue; visited.add(file); const source = await readFile(new URL(file, root), 'utf8'); for (const [, specifier] of source.matchAll(/\bimport\s+(?:[^'";]*?\s+from\s+)?['"](\.[^'"]*)['"]/g)) { const dependency = new URL(specifier, new URL(file, root)); assert.equal(dependency.origin, new URL(root).origin); pending.push(dependency.pathname.slice(new URL(root).pathname.length)); } } assert.deepEqual([...visited].sort(), [...visited].filter(file => RUNTIME_FILES.includes(file)).sort()); }); test('settings embeds installer source in a valid install command', async () => { const settings = JSON.parse(await readFile(new URL('../settings.json', import.meta.url))); const command = settings.install[0].commands[0]; const encoded = command.match(/base64,([A-Za-z0-9+/=]+)'/)[1]; assert.equal(Buffer.from(encoded, 'base64').toString(), await readFile(new URL('../installer.js', import.meta.url), 'utf8')); assert.match(command, /^node --input-type=module --eval /); assert.equal(settings.install[0].type, 'command'); assert.deepEqual(settings.requirements.binaries, ['node', 'npm']); assert.equal(settings.run.command, 'node ./${bot-js-file}'); });