browser-plugin.spec.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * ui-model browser half on a real cordis Context with fake command/slots/
  3. * connection faces and real session scopes: the plugin mounts ModelService
  4. * as `models`, the /model contribution and the conversation.input.model
  5. * seat both register, and BOTH entries resolve the SAME per-session
  6. * directory through the service — a selection submitted through the seat's
  7. * inject face is the current the popup's next options pass marks active
  8. * (and the reverse), the one-shared-state contract of the dual entry.
  9. * Scope disposal drops the directory (HMR safety).
  10. */
  11. import { Context } from 'cordis'
  12. import { describe, expect, it } from 'vitest'
  13. import { createScope } from '@deepseek-ai/dsh-client-runtime/client'
  14. import type { SessionId } from '@deepseek-ai/dsh-client-runtime/client'
  15. import type { ModelTarget } from '@deepseek-ai/dsh-client-connection/client'
  16. import type { CommandContribution, SelectOption } from '@deepseek-ai/dsh-client-ui-command/client'
  17. import type { ModelSelectInjected } from '../src/client/slots.ts'
  18. import { apply, inject } from '../src/client/index.ts'
  19. const sid = (k: string): SessionId => k as SessionId
  20. const GROUPS = [{
  21. id: 'deepseek',
  22. name: 'DeepSeek',
  23. models: [
  24. {
  25. id: 'deepseek-v4-flash',
  26. name: 'DeepSeek-V4-Flash',
  27. reasoning: {
  28. efforts: [
  29. { id: 'off', name: 'Off' },
  30. { id: 'high', name: 'High' },
  31. { id: 'max', name: 'Max' },
  32. ],
  33. defaultEffort: 'high',
  34. },
  35. },
  36. {
  37. id: 'deepseek-v4-pro',
  38. name: 'DeepSeek-V4-Pro',
  39. reasoning: {
  40. efforts: [
  41. { id: 'off', name: 'Off' },
  42. { id: 'high', name: 'High' },
  43. { id: 'max', name: 'Max' },
  44. ],
  45. defaultEffort: 'high',
  46. },
  47. },
  48. ],
  49. }]
  50. /** Boot the plugin over fake faces + a stateful fake host (current moves on selectModel). */
  51. async function bench() {
  52. const ctx = new Context()
  53. let current: ModelTarget = { provider: 'deepseek', model: 'deepseek-v4-flash' }
  54. const calls = { models: 0, select: 0 }
  55. ctx.provide('connection', { api: { sessions: {
  56. models: () => {
  57. calls.models += 1
  58. return Promise.resolve({ result: { ok: true as const, value: { current, groups: GROUPS, failures: [] } } })
  59. },
  60. selectModel: (payload: { provider: string; model: string; reasoningEffort?: string }) => {
  61. calls.select += 1
  62. current = {
  63. provider: payload.provider,
  64. model: payload.model,
  65. ...payload.reasoningEffort === undefined
  66. ? {}
  67. : { reasoningEffort: payload.reasoningEffort },
  68. }
  69. return Promise.resolve({ result: { ok: true as const, value: { selected: current } } })
  70. },
  71. } } })
  72. let contribution: CommandContribution | undefined
  73. ctx.provide('command', {
  74. register(c: CommandContribution) {
  75. contribution = c
  76. return () => { contribution = undefined }
  77. },
  78. })
  79. const seats = new Map<string, { inject: ((sessionId: SessionId) => ModelSelectInjected) | undefined }>()
  80. ctx.provide('slots', {
  81. register(options: { name: string; inject?: (sessionId: SessionId) => ModelSelectInjected }) {
  82. seats.set(options.name, { inject: options.inject })
  83. return () => { seats.delete(options.name) }
  84. },
  85. })
  86. ctx.provide('conversation', {})
  87. const scopes = new Map<SessionId, Context>()
  88. ctx.provide('sessions', { scope: (id: SessionId) => scopes.get(id) })
  89. const fiber = ctx.plugin({ inject: [...inject], apply })
  90. await fiber.await()
  91. await ctx.plugin(function probe() {}).await()
  92. const mint = (key: string) => {
  93. const handle = createScope(ctx, sid(key))
  94. scopes.set(sid(key), handle.ctx)
  95. return handle
  96. }
  97. return {
  98. ctx, fiber, mint, calls,
  99. contribution: () => contribution!,
  100. seat: () => seats.get('conversation.input.model')!,
  101. hostCurrent: () => current,
  102. setHostCurrent: (target: ModelTarget) => { current = target },
  103. }
  104. }
  105. const projection = (id: string) => ({ sessionId: sid(id) })
  106. describe('ui-model dual entry', () => {
  107. it('registers the /model contribution and the composer model seat', async () => {
  108. const b = await bench()
  109. expect(b.contribution().name).toBe('model')
  110. expect(b.contribution().ui.kind).toBe('popupSelect')
  111. expect(b.seat().inject).toBeTypeOf('function')
  112. })
  113. it('popup options mark the host current active with the provider group in the detail', async () => {
  114. const b = await bench()
  115. b.mint('s1')
  116. const options = await b.contribution().ui.options(projection('s1'), new AbortController().signal)
  117. expect(options.map((o: SelectOption) => o.label)).toEqual(['DeepSeek-V4-Flash', 'DeepSeek-V4-Pro'])
  118. expect(options[0]).toMatchObject({ active: true, detail: 'DeepSeek' })
  119. expect(options[1]?.active).toBeUndefined()
  120. })
  121. it('a seat selection is the current the popup marks active next — one shared state', async () => {
  122. const b = await bench()
  123. b.mint('s1')
  124. const seatFace = b.seat().inject!(sid('s1'))
  125. // Switch through the SEAT entry.
  126. expect(await seatFace.select({
  127. provider: 'deepseek',
  128. model: 'deepseek-v4-pro',
  129. reasoningEffort: 'max',
  130. })).toBe(true)
  131. expect(b.hostCurrent()).toEqual({
  132. provider: 'deepseek',
  133. model: 'deepseek-v4-pro',
  134. reasoningEffort: 'max',
  135. })
  136. expect(seatFace.directory.getSnapshot().current).toEqual({
  137. provider: 'deepseek',
  138. model: 'deepseek-v4-pro',
  139. reasoningEffort: 'max',
  140. })
  141. // The POPUP's next options pass reflects it without a seat-side reload.
  142. const options = await b.contribution().ui.options(projection('s1'), new AbortController().signal)
  143. expect(options.find((o: SelectOption) => o.label === 'DeepSeek-V4-Pro')).toMatchObject({ active: true })
  144. })
  145. it('a popup selection lands on the seat store — the reverse direction of the same state', async () => {
  146. const b = await bench()
  147. b.mint('s1')
  148. const seatFace = b.seat().inject!(sid('s1'))
  149. const options = await b.contribution().ui.options(projection('s1'), new AbortController().signal)
  150. const pro = options.find((o: SelectOption) => o.label === 'DeepSeek-V4-Pro')!
  151. await b.contribution().ui.onSelect(pro, projection('s1'))
  152. expect(seatFace.directory.getSnapshot().current).toEqual({
  153. provider: 'deepseek',
  154. model: 'deepseek-v4-pro',
  155. reasoningEffort: 'high',
  156. })
  157. })
  158. it('both entries share one directory instance per session, isolated across sessions', async () => {
  159. const b = await bench()
  160. b.mint('a')
  161. b.mint('b')
  162. const faceA = b.seat().inject!(sid('a'))
  163. const faceA2 = b.seat().inject!(sid('a'))
  164. const faceB = b.seat().inject!(sid('b'))
  165. expect(faceA.directory).toBe(faceA2.directory)
  166. expect(faceA.directory).not.toBe(faceB.directory)
  167. // The service face resolves the same instance the seat inject handed out.
  168. expect(b.ctx.models.directoryFor(sid('a')).store).toBe(faceA.directory)
  169. })
  170. it('drops an unconsumed local selection and restores the Host target after reconnect', async () => {
  171. const b = await bench()
  172. b.mint('s1')
  173. const face = b.seat().inject!(sid('s1'))
  174. await face.select({ provider: 'deepseek', model: 'deepseek-v4-pro' })
  175. b.setHostCurrent({ provider: 'deepseek', model: 'deepseek-v4-flash' })
  176. b.ctx.emit('connection/reset')
  177. expect(face.directory.getSnapshot()).toMatchObject({ current: null, status: 'loading' })
  178. await Promise.resolve()
  179. expect(face.directory.getSnapshot()).toMatchObject({
  180. current: { provider: 'deepseek', model: 'deepseek-v4-flash' },
  181. status: 'ready',
  182. })
  183. })
  184. it('scope disposal drops the directory; a reborn scope gets a fresh one', async () => {
  185. const b = await bench()
  186. const first = b.mint('s1')
  187. const face1 = b.seat().inject!(sid('s1'))
  188. await first.fiber.dispose()
  189. b.mint('s1')
  190. const face2 = b.seat().inject!(sid('s1'))
  191. expect(face2.directory).not.toBe(face1.directory)
  192. })
  193. it('an unknown session fails loud at the seat inject', async () => {
  194. const b = await bench()
  195. expect(() => b.seat().inject!(sid('ghost'))).toThrow(/resolved no scope/)
  196. })
  197. })