browser-plugin.spec.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * ui-command browser half on a real cordis Context with fake slash/slots
  3. * faces and real session scopes: the plugin body mounts CommandService as
  4. * `command`, the popupSelect shell registers into conversation.input.overlay
  5. * once the conversation seam is up with a per-session inject (sessionId →
  6. * scope → popupFor; unknown id fails loud), both fold up on fiber disposal
  7. * (HMR safety), and the service satisfies the frozen CommandServiceContract.
  8. */
  9. import { Context } from 'cordis'
  10. import { describe, expect, it } from 'vitest'
  11. import { createScope, scopeOf } from '@deepseek-ai/dsh-client-runtime/client'
  12. import type { SessionId } from '@deepseek-ai/dsh-client-runtime/client'
  13. import type { SlashSource } from '@deepseek-ai/dsh-client-ui-slash/client'
  14. import type { CommandServiceContract } from '../src/client/contract.ts'
  15. import type { PopupSelectInjected } from '../src/client/PopupSelectView.tsx'
  16. import { LocaleService } from '@deepseek-ai/dsh-client-locale/client'
  17. import { apply, CommandService, inject } from '../src/client/index.ts'
  18. const sid = (k: string): SessionId => k as SessionId
  19. async function bench() {
  20. const ctx = new Context()
  21. const sources = new Map<string, SlashSource>()
  22. const overlays = new Map<string, { inject: unknown }>()
  23. ctx.provide('slash', {
  24. registerSource(src: SlashSource) {
  25. sources.set(`${src.trigger} ${src.name}`, src)
  26. return () => { sources.delete(`${src.trigger} ${src.name}`) }
  27. },
  28. })
  29. const scopes = new Map<SessionId, Context>()
  30. ctx.provide('sessions', {
  31. scope: (id: SessionId) => scopes.get(id),
  32. scopeOf: (c: Context) => scopeOf(c),
  33. })
  34. ctx.provide('connection', { api: { commands: { list: () => Promise.resolve({ result: { ok: true, value: { commands: [] } } }) } } })
  35. ctx.provide('slots', {
  36. register(options: { name: string; id?: string; inject?: unknown }) {
  37. const key = `${options.name}#${options.id ?? ''}`
  38. overlays.set(key, { inject: options.inject })
  39. return () => { overlays.delete(key) }
  40. },
  41. })
  42. ctx.provide('conversation', {})
  43. ctx.provide('locale', new LocaleService(ctx))
  44. const fiber = ctx.plugin({ inject: [...inject], apply })
  45. await fiber.await()
  46. const mint = (key: string) => {
  47. const handle = createScope(ctx, sid(key))
  48. scopes.set(sid(key), handle.ctx)
  49. return handle
  50. }
  51. return { ctx, fiber, sources, overlays, mint }
  52. }
  53. describe('apply', () => {
  54. it('declares the services it binds', () => {
  55. expect(inject).toEqual(['slash', 'sessions', 'connection', 'locale'])
  56. })
  57. it('mounts ctx.command, registers the source and the overlay entry, and folds up on disposal', async () => {
  58. const { ctx, fiber, sources, overlays } = await bench()
  59. const command = ctx.get('command')
  60. expect(command).toBeInstanceOf(CommandService)
  61. // Frozen-contract conformance (compile-time check rides the assignment).
  62. const contract: CommandServiceContract = command as CommandService
  63. expect(typeof contract.register).toBe('function')
  64. expect(typeof contract.popupFor).toBe('function')
  65. expect([...sources.keys()]).toEqual(['/ command'])
  66. expect([...overlays.keys()]).toEqual(['conversation.input.overlay#command-popup'])
  67. await fiber.dispose()
  68. expect(sources.size).toBe(0)
  69. expect(overlays.size).toBe(0)
  70. })
  71. it('the overlay inject resolves the per-session popup controller by sessionId and fails loud on an unknown id', async () => {
  72. const { ctx, overlays, mint } = await bench()
  73. const command = ctx.get('command') as CommandService
  74. const scope = mint('s1')
  75. const entry = overlays.get('conversation.input.overlay#command-popup')!
  76. const injectEntry = entry.inject as (sessionId: SessionId) => PopupSelectInjected
  77. expect(injectEntry(sid('s1')).popup).toBe(command.popupFor(scope.ctx))
  78. expect(() => injectEntry(sid('ghost'))).toThrow(/resolved no scope/)
  79. })
  80. })