Files
bare-operating-system/packages/bare-os-coreutils/test/telnet-cli.test.mjs
T
Raven Scott 237e0ef63a Added missing locale command support:
packages/bare-os-coreutils/src/locale.js
registered in packages/bare-os-coreutils/lib/commands.mjs
Fixed trustctl generated runtime mismatch:

added preamble wiring in packages/bare-os-coreutils/build.mjs (trustctl: ['p2p-suite.js'])
Improved help behavior:

packages/bare-os-coreutils/src/ssh-keygen.js (help, -h, --help, -?)
packages/bare-os-coreutils/src/oidc-publish.js (help detection across args)
Improved procstat empty-state UX while preserving JSON output:

packages/bare-os-coreutils/src/procstat.js
adds sourcePath and additive hint when process table is empty
Enforced stricter network behavior:

packages/bare-os-coreutils/src/whois.js
prefers ctx.httpFetch, adds bounded timeout via AbortController
clearer timeout/policy/offline error mapping
packages/bare-os-coreutils/src/telnet.js
strict-close behavior by default
--strict-close / --no-strict-close
BARE_OS_TELNET_STRICT_CLOSE env override
Implemented expected-item UX/compat updates:

packages/bare-os-coreutils/src/iconv.js: added -l/--list, improved encoding-pair error messaging
packages/bare-os-coreutils/src/crontab.js: clearer -l empty-file message
packages/bare-os-coreutils/src/getconf.js: added PAGE_SIZE / PAGESIZE aliases
packages/bare-os-coreutils/src/nproc.js: clarified help text
packages/bare-os-coreutils/src/time.js: clarified output comments/behavior
2026-04-27 08:22:57 -04:00

132 lines
3.7 KiB
JavaScript

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')
})