import { readFile } from 'node:fs/promises' import path from 'node:path' import { fileURLToPath } from 'node:url' import test from 'brittle' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor async function loadTelnetRun() { const runtime = await readFile(path.join(__dirname, '../lib/runtime.js'), 'utf8') const body = await readFile(path.join(__dirname, '../src/telnet.js'), 'utf8') return new AsyncFunction( 'ctx', 'argv', `${runtime}\n${body}\nif (typeof run === 'function') return await run(ctx, argv)\n` ) } function createSocketMock() { const handlers = new Map() return { writes: [], on(name, fn) { const arr = handlers.get(name) || [] arr.push(fn) handlers.set(name, arr) }, off(name, fn) { const arr = handlers.get(name) || [] handlers.set( name, arr.filter((x) => x !== fn) ) }, removeListener(name, fn) { this.off(name, fn) }, emit(name, v) { const arr = handlers.get(name) || [] for (const fn of arr) fn(v) }, write(buf) { this.writes.push(buf instanceof Uint8Array ? buf : new Uint8Array(buf)) }, end() {}, destroy() {}, setNoDelay() {}, setKeepAlive() {} } } test('telnet --help prints usage', async (t) => { const run = await loadTelnetRun() const logs = [] const ctx = { console: { log: (m) => logs.push(String(m)), error: () => {} }, exitCode: 0 } await run(ctx, ['telnet', '--help']) t.ok(logs.join('\n').includes('usage: telnet')) }) test('telnet rejects unknown option', async (t) => { const run = await loadTelnetRun() const errs = [] const ctx = { console: { log: () => {}, error: (m) => errs.push(String(m)) }, exitCode: 0 } await run(ctx, ['telnet', '--bad', 'localhost']) t.is(ctx.exitCode, 1) t.ok(errs.join('\n').includes('unknown option')) }) test('telnet connects through ctx.bareOsTelnetConnect', async (t) => { const run = await loadTelnetRun() const sock = createSocketMock() const logs = [] let connectCalls = 0 const stdout = { write() {} } const stdin = { isTTY: false, on() {}, resume() {} } const ctx = { console: { log: (m) => logs.push(String(m)), error: () => {} }, exitCode: 0, stdout, stdin, bareOsTelnetConnect(_host, _port, _opts) { connectCalls++ setTimeout(() => sock.emit('connect'), 0) setTimeout(() => sock.emit('close'), 1) return sock } } await run(ctx, ['telnet', '--no-strict-close', 'localhost', '2323']) t.is(connectCalls, 1) t.is(ctx.exitCode, 0) }) test('telnet falls back to bareOsSyscall socket bridge', async (t) => { const run = await loadTelnetRun() const stdout = { write() {} } const stdin = { isTTY: false, on() {}, resume() {} } /** @type {{ fd?: number, sent?: number }} */ const state = {} const ctx = { console: { log: () => {}, error: () => {} }, exitCode: 0, stdout, stdin, vfs: { env: {} }, async bareOsSyscall(name, args) { if (name === 'socket') { state.fd = 41 return { ok: true, fd: 41 } } if (name === 'connect') return { ok: true, fd: state.fd } if (name === 'send') { state.sent = (state.sent || 0) + 1 return { ok: true, bytesSent: 1 } } if (name === 'recv') { return { ok: true, bytesReceived: 0, buf: new Uint8Array(0), eof: true } } if (name === 'shutdown' || name === 'close') return { ok: true } return { ok: false, code: 'ENOSYS' } } } await run(ctx, ['telnet', '--no-strict-close', 'localhost', '2323']) t.is(ctx.exitCode, 0) t.is(String(ctx.vfs.env.BARE_OS_POSIX_SOCKET_FD_BRIDGE), '1') })