client-apply.spec.ts 5.0 KB

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