wire-events.spec.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Wire-to-typed-event bridge (web input-triggers cut 1): host/commands-changed
  3. * → ctx 'commands/changed'; each established connection generation →
  4. * ctx 'connection/reset' (the forced cache-invalidation broadcast).
  5. */
  6. import { Context } from 'cordis'
  7. import { describe, expect, it } from 'vitest'
  8. import type { ConnectionHandle, ConnectionSinks } from '@deepseek-ai/dsh-client-connection/client'
  9. import * as RuntimeClient from '../src/client/index.ts'
  10. import { FakeApiClient } from './fake-api.ts'
  11. interface Bench {
  12. ctx: Context
  13. sinks: ConnectionSinks | undefined
  14. }
  15. async function mount(): Promise<Bench> {
  16. const ctx = new Context()
  17. const api = new FakeApiClient()
  18. const bench: Bench = { ctx, sinks: undefined }
  19. const handle: ConnectionHandle = {
  20. api,
  21. start: (sinks) => {
  22. bench.sinks = sinks
  23. return { stop: () => {} }
  24. },
  25. }
  26. ctx.reflect.provide('connection', handle)
  27. await ctx.plugin(RuntimeClient).await()
  28. return bench
  29. }
  30. describe('wire event bridge', () => {
  31. it('broadcasts commands/changed on a host/commands-changed frame, not on other host frames', async () => {
  32. const bench = await mount()
  33. let changed = 0
  34. bench.ctx.on('commands/changed', () => { changed++ })
  35. bench.sinks?.onHostEnvelope?.({ rpcId: 'r1' as never, payload: { type: 'host/commands-changed' } })
  36. expect(changed).toBe(1)
  37. bench.sinks?.onHostEnvelope?.({
  38. rpcId: 'r2' as never,
  39. payload: { type: 'host/session-status', sessionId: 's1' as never, running: true },
  40. })
  41. expect(changed).toBe(1)
  42. })
  43. it('broadcasts connection/reset on every established generation (reconnect invalidation)', async () => {
  44. const bench = await mount()
  45. let resets = 0
  46. bench.ctx.on('connection/reset', () => { resets++ })
  47. bench.sinks?.onConnected?.()
  48. bench.sinks?.onConnected?.() // second generation after a reconnect
  49. expect(resets).toBe(2)
  50. })
  51. })