client-apply.spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 type { SessionsService } from '../src/client/sessions/service.ts'
  12. import type { WorkspacesService } from '../src/client/workspaces/service.ts'
  13. import { FakeApiClient, ok } from './fake-api.ts'
  14. interface Bench {
  15. ctx: Context
  16. api: FakeApiClient
  17. sinks: ConnectionSinks | undefined
  18. stopped: number
  19. }
  20. async function mount(): Promise<Bench> {
  21. const ctx = new Context()
  22. const api = new FakeApiClient()
  23. const bench: Bench = { ctx, api, sinks: undefined, stopped: 0 }
  24. const handle: ConnectionHandle = {
  25. api,
  26. start: (sinks) => {
  27. bench.sinks = sinks
  28. return { stop: () => { bench.stopped += 1 } }
  29. },
  30. }
  31. ctx.reflect.provide('connection', handle)
  32. await ctx.plugin(RuntimeClient).await()
  33. return bench
  34. }
  35. async function flushMicrotasks(): Promise<void> {
  36. for (let i = 0; i < 12; i++) await Promise.resolve()
  37. }
  38. describe('runtime client apply', () => {
  39. it('mounts slots, Sessions, and Workspaces and fans host frames into both managers', async () => {
  40. const bench = await mount()
  41. expect(bench.ctx.get('slots') !== undefined).toBe(true)
  42. // The built-in 'root' declaration ships with this package's SlotsService
  43. // (the SlotMap 'root' merge lives here since the slot-parity rework).
  44. expect(bench.ctx.slots.spec('root')).toEqual({ kind: 'single', scope: 'root' })
  45. const sessions = bench.ctx.get('sessions')
  46. const workspaces = bench.ctx.get('workspaces')
  47. expect(sessions !== undefined).toBe(true)
  48. expect(workspaces !== undefined).toBe(true)
  49. if (workspaces === undefined) throw new Error('WorkspacesService missing after runtime apply')
  50. expect(bench.sinks).toBeDefined()
  51. // Frame sinks reach the object layer: a host session-added lands in the list store.
  52. bench.sinks?.onHostEnvelope?.({
  53. rpcId: 'r1' as never,
  54. payload: { type: 'host/session-added', blank: true, sessionId: 's-new' } as never,
  55. })
  56. await Promise.resolve()
  57. expect((sessions as { list: { getSnapshot(): { ids: string[] } } }).list.getSnapshot().ids).toContain('s-new')
  58. bench.sinks?.onHostEnvelope?.({
  59. rpcId: 'r-workspace' as never,
  60. payload: {
  61. type: 'host/workspace-changed',
  62. workspace: {
  63. workspaceId: 'w-new', path: '/w/new', title: 'new', sessionIds: [],
  64. createdAt: '2026-01-01T00:00:00.000Z', updatedAt: '2026-01-01T00:00:00.000Z',
  65. },
  66. } as never,
  67. })
  68. await Promise.resolve()
  69. expect(workspaces.list.getSnapshot().items[0]?.workspaceId).toBe('w-new')
  70. // Mux sink and onConnected route without throwing (manager semantics own the behavior).
  71. bench.sinks?.onMuxEnvelope?.({ rpcId: 'r2' as never, payload: { type: 'stream/error', message: 'x' } as never })
  72. bench.sinks?.onConnected?.()
  73. })
  74. it('selects the recent Workspace once when the first baselines have no current session', async () => {
  75. const bench = await mount()
  76. bench.api.onWorkspaceList = () => Promise.resolve(ok({
  77. items: [{
  78. workspaceId: 'w-recent', path: '/w/recent', title: 'recent', sessionIds: [],
  79. createdAt: '2026-01-01T00:00:00.000Z', updatedAt: '2026-01-01T00:00:00.000Z',
  80. }] as never[],
  81. }))
  82. bench.api.onList = () => Promise.resolve(ok({ items: [] }))
  83. bench.sinks?.onConnected?.()
  84. await flushMicrotasks()
  85. const sessions = bench.ctx.get('sessions') as SessionsService
  86. const workspaces = bench.ctx.get('workspaces') as WorkspacesService
  87. expect(bench.api.callsOf('session.create')).toEqual([{ workspaceId: 'w-recent' }])
  88. expect(sessions.list.getSnapshot().current).toBe('fk-new')
  89. sessions.clear()
  90. await workspaces.refresh()
  91. await flushMicrotasks()
  92. expect(sessions.list.getSnapshot().current).toBeUndefined()
  93. expect(bench.api.callsOf('session.create')).toHaveLength(1)
  94. })
  95. it('stops the stream loop when the plugin fiber unloads', async () => {
  96. const bench = await mount()
  97. const fiber = [...bench.ctx.registry.values()].find(f => f.name?.includes('client'))
  98. // Dispose the whole tree: the ctx.effect teardown must call loop.stop exactly once.
  99. await bench.ctx.fiber.dispose()
  100. expect(bench.stopped).toBe(1)
  101. void fiber
  102. })
  103. })