mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
import { describe, expect, it } from 'bun:test'
|
|
import { Store, type StoredSession } from '../../../store'
|
|
import type { SyncEvent } from '../../../sync/syncEngine'
|
|
import type { CliSocketWithData } from '../../socketTypes'
|
|
import { registerSessionHandlers } from './sessionHandlers'
|
|
|
|
class FakeSocket {
|
|
readonly roomEvents: Array<{ room: string; event: string; data: unknown }> = []
|
|
private readonly handlers = new Map<string, (data: unknown) => void>()
|
|
|
|
on(event: string, handler: (data: unknown) => void): this {
|
|
this.handlers.set(event, handler)
|
|
return this
|
|
}
|
|
|
|
to(room: string): { emit: (event: string, data: unknown) => void } {
|
|
return {
|
|
emit: (event: string, data: unknown) => {
|
|
this.roomEvents.push({ room, event, data })
|
|
}
|
|
}
|
|
}
|
|
|
|
trigger(event: string, data: unknown): void {
|
|
this.handlers.get(event)?.(data)
|
|
}
|
|
}
|
|
|
|
function redundantGoalStatusContent(message: string): unknown {
|
|
return {
|
|
role: 'agent',
|
|
content: {
|
|
id: `event-${message}`,
|
|
type: 'event',
|
|
data: { type: 'message', message }
|
|
}
|
|
}
|
|
}
|
|
|
|
describe('cli session handlers', () => {
|
|
it('drops redundant goal status events before persistence and broadcast', () => {
|
|
const store = new Store(':memory:')
|
|
const session = store.sessions.getOrCreateSession('goal-status-session', {}, null, 'default')
|
|
const socket = new FakeSocket()
|
|
const webEvents: SyncEvent[] = []
|
|
|
|
registerSessionHandlers(socket as unknown as CliSocketWithData, {
|
|
store,
|
|
resolveSessionAccess: () => ({ ok: true, value: session as StoredSession }),
|
|
emitAccessError: () => {
|
|
throw new Error('unexpected access error')
|
|
},
|
|
onWebappEvent: (event) => {
|
|
webEvents.push(event)
|
|
}
|
|
})
|
|
|
|
socket.trigger('message', {
|
|
sid: session.id,
|
|
message: redundantGoalStatusContent('Goal active · 8016 tokens')
|
|
})
|
|
|
|
expect(store.messages.getMessages(session.id)).toHaveLength(0)
|
|
expect(socket.roomEvents).toHaveLength(0)
|
|
expect(webEvents).toHaveLength(0)
|
|
})
|
|
})
|