client-apply.spec.ts 4.8 KB

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