import test from 'brittle' import { readFile } from 'node:fs/promises' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' const __dirname = dirname(fileURLToPath(import.meta.url)) async function load() { const parts = await Promise.all([ readFile(join(__dirname, '../lib/irc-config.js'), 'utf8'), readFile(join(__dirname, '../lib/irc-dial.js'), 'utf8') ]) const hook = {} new Function( 'exports', parts.join('\n') + ` exports.bareIrcDial = bareIrcDial; exports.bareIrcOpenTls = bareIrcOpenTls; exports.bareIrcTlsLib = bareIrcTlsLib; ` )(hook) return hook } test('bareIrcDial uses ctx.bare.bareTls.connect when syscall is missing', async (t) => { const h = await load() const seen = [] const sock = { kind: 'tls' } const ctx = { bare: { bareTls: { connect(opts) { seen.push(opts) return sock } } } } const out = h.bareIrcDial(ctx, { host: 'irc.libera.chat', port: 6697, tls: true }) t.is(out, sock) t.is(seen.length, 1) t.is(seen[0].host, 'irc.libera.chat') t.is(seen[0].port, 6697) }) test('bareIrcDial wraps TCP with TLSSocket when connect is absent', async (t) => { const h = await load() function FakeSock(tcp, opts) { this.tcp = tcp this.opts = opts } const tcp = { kind: 'tcp' } const ctx = { bareOsTelnetConnect() { return tcp }, bare: { bareTls: { Socket: FakeSock } } } const out = h.bareIrcDial(ctx, { host: 'irc.libera.chat', port: 6697, tls: true }) t.ok(out instanceof FakeSock) t.is(out.tcp, tcp) t.is(out.opts.host, 'irc.libera.chat') }) test('bareIrcDial prefers ctx.bareOsTlsConnect when present', async (t) => { const h = await load() const sock = { kind: 'sys' } const ctx = { bareOsTlsConnect(host, port) { return { sock, host, port } }, bare: { bareTls: { connect() { throw new Error('should not use ctx.bare fallback') } } } } const out = h.bareIrcDial(ctx, { host: 'irc.libera.chat', port: 6697, tls: true }) t.is(out.sock, sock) t.is(out.port, 6697) }) test('bareIrcDial still errors when no TLS surface exists', async (t) => { const h = await load() t.exception( () => h.bareIrcDial({}, { host: 'irc.libera.chat', port: 6697, tls: true }), /no TLS connector/ ) })