Files
bare-operating-system/packages/bare-os-coreutils/test/irc-parse.test.mjs
T
2026-08-18 18:11:34 -04:00

63 lines
2.0 KiB
JavaScript

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 src = await readFile(join(__dirname, '../lib/irc/irc-parse.js'), 'utf8')
const hook = {}
new Function(
'exports',
src +
'\nexports.bareIrcParseMessage = bareIrcParseMessage;\nexports.bareIrcSerializeMessage = bareIrcSerializeMessage;\nexports.bareIrcCasemap = bareIrcCasemap;\nexports.bareIrcEqual = bareIrcEqual;\nexports.bareIrcCreateLineBuffer = bareIrcCreateLineBuffer;\n'
)(hook)
return hook
}
test('parse spec golden PRIVMSG with tags', async (t) => {
const h = await load()
const m = h.bareIrcParseMessage(
"@id=234AB :dan!d@localhost PRIVMSG #chan :Hey what's up!"
)
t.is(m.tags.id, '234AB')
t.is(m.nick, 'dan')
t.is(m.user, 'd')
t.is(m.host, 'localhost')
t.is(m.command, 'PRIVMSG')
t.alike(m.params, ['#chan', "Hey what's up!"])
})
test('trailing smiley and empty LIST', async (t) => {
const h = await load()
const smile = h.bareIrcParseMessage(':dan!d@localhost PRIVMSG #chan ::-)')
t.alike(smile.params, ['#chan', ':-)'])
const empty = h.bareIrcParseMessage(':irc.example.com CAP * LIST :')
t.alike(empty.params, ['*', 'LIST', ''])
})
test('serialize then parse round-trip', async (t) => {
const h = await load()
const line = h.bareIrcSerializeMessage({
command: 'PRIVMSG',
params: ['#chan', 'hello world']
})
t.ok(line.endsWith('\r\n'))
const m = h.bareIrcParseMessage(line)
t.alike(m.params, ['#chan', 'hello world'])
})
test('rfc1459 casemap', async (t) => {
const h = await load()
t.ok(h.bareIrcEqual('Dan', 'dan', 'rfc1459'))
t.ok(h.bareIrcEqual('[foo]', '{foo}', 'rfc1459'))
t.absent(h.bareIrcEqual('[foo]', '{foo}', 'ascii'))
})
test('line buffer splits CRLF and lone LF', async (t) => {
const h = await load()
const b = h.bareIrcCreateLineBuffer()
t.alike(b.push('PING :x\r\nPONG :y\n'), ['PING :x', 'PONG :y'])
})