client-apply.spec.ts 4.8 KB

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