Files
botfights/server/src/engine/events.test.ts
T

59 lines
1.4 KiB
TypeScript
Raw Normal View History

import { describe, it, expect } from 'vitest'
import { fightEvents, type FightEvent } from './events.js'
describe('fightEvents', () => {
it('emits events to fight-specific listeners', () => {
const events: FightEvent[] = []
const unsub = fightEvents.on('fight-1', (e) => events.push(e))
fightEvents.emit({
fightId: 'fight-1',
type: 'test',
data: { msg: 'hello' },
timestamp: new Date().toISOString(),
})
expect(events).toHaveLength(1)
expect(events[0].data.msg).toBe('hello')
unsub()
})
it('cleanup removes all listeners for a fight', () => {
const events: FightEvent[] = []
fightEvents.on('fight-cleanup', (e) => events.push(e))
fightEvents.on('fight-cleanup', (e) => events.push(e))
fightEvents.cleanup('fight-cleanup')
fightEvents.emit({
fightId: 'fight-cleanup',
type: 'test',
data: {},
timestamp: new Date().toISOString(),
})
expect(events).toHaveLength(0)
})
it('global listeners receive all events', () => {
const events: FightEvent[] = []
const unsub = fightEvents.onAll((e) => events.push(e))
fightEvents.emit({
fightId: 'fight-global-1',
type: 'test',
data: {},
timestamp: new Date().toISOString(),
})
fightEvents.emit({
fightId: 'fight-global-2',
type: 'test',
data: {},
timestamp: new Date().toISOString(),
})
expect(events).toHaveLength(2)
unsub()
})
})