client-apply.spec.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Runtime plugin browser-half apply: slots + object services mounting over the
  3. * connection handle, stream-loop sink wiring into the object layer, and the
  4. * fiber-scoped loop teardown.
  5. */
  6. import { Context } from 'cordis'
  7. import { describe, expect, it } from 'vitest'
  8. import type { ConnectionHandle } from '@deepseek-ai/dsh-client-connection/client'
  9. import type { ConnectionSinks } from '@deepseek-ai/dsh-client-connection/client'
  10. import * as RuntimeClient from '../src/client/index.ts'
  11. import { FakeApiClient } from './fake-api.ts'
  12. interface Bench {
  13. ctx: Context
  14. api: FakeApiClient
  15. sinks: ConnectionSinks | undefined
  16. stopped: number
  17. }
  18. async function mount(): Promise<Bench> {
  19. const ctx = new Context()
  20. const api = new FakeApiClient()
  21. const bench: Bench = { ctx, api, sinks: undefined, stopped: 0 }
  22. const handle: ConnectionHandle = {
  23. api,
  24. start: (sinks) => {
  25. bench.sinks = sinks
  26. return { stop: () => { bench.stopped += 1 } }
  27. },
  28. }
  29. ctx.reflect.provide('connection', handle)
  30. await ctx.plugin(RuntimeClient).await()
  31. return bench
  32. }
  33. describe('runtime client apply', () => {
  34. it('mounts slots, Sessions, and Workspaces and fans host frames into both managers', async () => {
  35. const bench = await mount()
  36. expect(bench.ctx.get('slots') !== undefined).toBe(true)
  37. // The built-in 'root' declaration ships with this package's SlotsService
  38. // (the SlotMap 'root' merge lives here since the slot-parity rework).
  39. expect(bench.ctx.slots.spec('root')).toEqual({ kind: 'single', scope: 'root' })
  40. const sessions = bench.ctx.get('sessions')
  41. const workspaces = bench.ctx.get('workspaces')
  42. expect(sessions !== undefined).toBe(true)
  43. expect(workspaces !== undefined).toBe(true)
  44. if (workspaces === undefined) throw new Error('WorkspacesService missing after runtime apply')
  45. expect(bench.sinks).toBeDefined()
  46. // Frame sinks reach the object layer: a host session-added lands in the list store.
  47. bench.sinks?.onHostEnvelope?.({
  48. rpcId: 'r1' as never,
  49. payload: { type: 'host/session-added', sessionId: 's-new' } as never,
  50. })
  51. await Promise.resolve()
  52. expect((sessions as { list: { getSnapshot(): { ids: string[] } } }).list.getSnapshot().ids).toContain('s-new')
  53. bench.sinks?.onHostEnvelope?.({
  54. rpcId: 'r-workspace' as never,
  55. payload: {
  56. type: 'host/workspace-changed',
  57. workspace: {
  58. workspaceId: 'w-new', path: '/w/new', title: 'new', sessionIds: [],
  59. createdAt: '2026-01-01T00:00:00.000Z', updatedAt: '2026-01-01T00:00:00.000Z',
  60. },
  61. } as never,
  62. })
  63. await Promise.resolve()
  64. expect(workspaces.list.getSnapshot().items[0]?.workspaceId).toBe('w-new')
  65. // Mux sink and onConnected route without throwing (manager semantics own the behavior).
  66. bench.sinks?.onMuxEnvelope?.({ rpcId: 'r2' as never, payload: { type: 'stream/error', message: 'x' } as never })
  67. bench.sinks?.onConnected?.()
  68. })
  69. it('stops the stream loop when the plugin fiber unloads', async () => {
  70. const bench = await mount()
  71. const fiber = [...bench.ctx.registry.values()].find(f => f.name?.includes('client'))
  72. // Dispose the whole tree: the ctx.effect teardown must call loop.stop exactly once.
  73. await bench.ctx.fiber.dispose()
  74. expect(bench.stopped).toBe(1)
  75. void fiber
  76. })
  77. })