browser-plugin.spec.ts 8.3 KB

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