import test from 'brittle'
import { Router } from '../lib/router.js'
test('Router - routes to Keet', (t) => {
let received = null
const router = new Router({
onKeetMessage: (nick, text) => {
received = { nick, text }
}
})
const ok = router.routeToKeet('alice', 'hello')
t.ok(ok)
t.is(received.nick, 'alice')
t.is(received.text, 'hello')
})
test('Router - routes to IRC', (t) => {
let received = null
const router = new Router({
onIrcMessage: (name, text) => {
received = { name, text }
}
})
const ok = router.routeToIrc('Bob', 'hello from keet')
t.ok(ok)
t.is(received.name, 'Bob')
t.is(received.text, 'hello from keet')
})
test('Router - dedup prevents echo', (t) => {
let keetCount = 0
let ircCount = 0
const router = new Router({
onKeetMessage: () => keetCount++,
onIrcMessage: () => ircCount++
})
// Message from IRC → Keet
t.ok(router.routeToKeet('alice', 'hello'))
t.is(keetCount, 1)
// Same message from Keet → IRC should be deduped
t.absent(router.routeToIrc('alice', 'hello'))
t.is(ircCount, 0, 'deduped — same content')
})
test('Router - different messages not deduped', (t) => {
let count = 0
const router = new Router({
onKeetMessage: () => count++
})
router.routeToKeet('alice', 'message 1')
router.routeToKeet('alice', 'message 2')
t.is(count, 2)
})
test('Router - same text different authors not deduped', (t) => {
let count = 0
const router = new Router({
onKeetMessage: () => count++
})
router.routeToKeet('alice', 'hello')
router.routeToKeet('bob', 'hello')
t.is(count, 2)
})
test('Router - seenCount tracks entries', (t) => {
const router = new Router()
t.is(router.seenCount, 0)
router.routeToKeet('a', 'msg1')
router.routeToKeet('b', 'msg2')
t.is(router.seenCount, 2)
})
test('Router - clearSeen resets dedup', (t) => {
let count = 0
const router = new Router({
onKeetMessage: () => count++
})
router.routeToKeet('alice', 'hello')
t.is(count, 1)
router.clearSeen()
router.routeToKeet('alice', 'hello')
t.is(count, 2, 'after clear, same message routes again')
})
test('Router - TTL expiry allows re-routing', async (t) => {
let count = 0
const router = new Router({
ttl: 50, // 50ms TTL for testing
onKeetMessage: () => count++
})
router.routeToKeet('alice', 'hello')
t.is(count, 1)
// Immediately should be deduped
router.routeToKeet('alice', 'hello')
t.is(count, 1)
// After TTL, should route again
await new Promise(r => setTimeout(r, 60))
router.routeToKeet('alice', 'hello')
t.is(count, 2, 'after TTL, same message routes again')
})
test('Router - no callback does not throw', (t) => {
const router = new Router()
const ok = router.routeToKeet('alice', 'hello')
t.ok(ok, 'returns true even without callback')
})
test('Router - handles empty strings', (t) => {
let count = 0
const router = new Router({
onKeetMessage: () => count++
})
router.routeToKeet('', '')
t.is(count, 1)
})
test('Router - handles unicode', (t) => {
let received = null
const router = new Router({
onKeetMessage: (nick, text) => { received = text }
})
router.routeToKeet('ユーザー', '🎉 こんにちは')
t.is(received, '🎉 こんにちは')
})