browser-plugin.spec.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 { apply, CommandService, inject } from '../src/client/index.ts'
  17. const sid = (k: string): SessionId => k as SessionId
  18. async function bench() {
  19. const ctx = new Context()
  20. const sources = new Map<string, SlashSource>()
  21. const overlays = new Map<string, { inject: unknown }>()
  22. ctx.provide('slash', {
  23. registerSource(src: SlashSource) {
  24. sources.set(`${src.trigger} ${src.name}`, src)
  25. return () => { sources.delete(`${src.trigger} ${src.name}`) }
  26. },
  27. })
  28. const scopes = new Map<SessionId, Context>()
  29. ctx.provide('sessions', {
  30. scope: (id: SessionId) => scopes.get(id),
  31. scopeOf: (c: Context) => scopeOf(c),
  32. })
  33. ctx.provide('connection', { api: { commands: { list: () => Promise.resolve({ result: { ok: true, value: { commands: [] } } }) } } })
  34. ctx.provide('slots', {
  35. register(options: { name: string; id?: string; inject?: unknown }) {
  36. const key = `${options.name}#${options.id ?? ''}`
  37. overlays.set(key, { inject: options.inject })
  38. return () => { overlays.delete(key) }
  39. },
  40. })
  41. ctx.provide('conversation', {})
  42. const fiber = ctx.plugin({ inject: [...inject], apply })
  43. await fiber.await()
  44. const mint = (key: string) => {
  45. const handle = createScope(ctx, sid(key))
  46. scopes.set(sid(key), handle.ctx)
  47. return handle
  48. }
  49. return { ctx, fiber, sources, overlays, mint }
  50. }
  51. describe('apply', () => {
  52. it('declares the services it binds', () => {
  53. expect(inject).toEqual(['slash', 'sessions', 'connection'])
  54. })
  55. it('mounts ctx.command, registers the source and the overlay entry, and folds up on disposal', async () => {
  56. const { ctx, fiber, sources, overlays } = await bench()
  57. const command = ctx.get('command')
  58. expect(command).toBeInstanceOf(CommandService)
  59. // Frozen-contract conformance (compile-time check rides the assignment).
  60. const contract: CommandServiceContract = command as CommandService
  61. expect(typeof contract.register).toBe('function')
  62. expect(typeof contract.popupFor).toBe('function')
  63. expect([...sources.keys()]).toEqual(['/ command'])
  64. expect([...overlays.keys()]).toEqual(['conversation.input.overlay#command-popup'])
  65. await fiber.dispose()
  66. expect(sources.size).toBe(0)
  67. expect(overlays.size).toBe(0)
  68. })
  69. it('the overlay inject resolves the per-session popup controller by sessionId and fails loud on an unknown id', async () => {
  70. const { ctx, overlays, mint } = await bench()
  71. const command = ctx.get('command') as CommandService
  72. const scope = mint('s1')
  73. const entry = overlays.get('conversation.input.overlay#command-popup')!
  74. const injectEntry = entry.inject as (sessionId: SessionId) => PopupSelectInjected
  75. expect(injectEntry(sid('s1')).popup).toBe(command.popupFor(scope.ctx))
  76. expect(() => injectEntry(sid('ghost'))).toThrow(/resolved no scope/)
  77. })
  78. })