browser-plugin.client.spec.ts 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * ui-commands browser half on a real cordis Context with fake slash/slots
  3. * faces and real session scopes: the plugin body mounts CommandUiRuntime as
  4. * `command`, the popupSelect shell registers into conversation.input.overlay
  5. * through slot declaration injection 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 CommandUiContract.
  8. */
  9. import { Context } from '@deepseek-ai/cordis'
  10. import { describe, expect, it } from 'vitest'
  11. import { createScope, scopeOf } from '@deepseek-ai/dsh-api-session-controller/client'
  12. import { SlotRegistry } from '@deepseek-ai/dsh-client-ui-renderer/client'
  13. import type { SessionId } from '@deepseek-ai/dsh-session/types'
  14. import type { InputTriggerSource } from '@deepseek-ai/dsh-client-ui-input-trigger/client'
  15. import type { CommandUiContract } from '../src/client/contract.ts'
  16. import type { PopupSelectInjected } from '../src/client/PopupSelectView.tsx'
  17. import { LocaleRuntime } from '@deepseek-ai/dsh-client-locale/client'
  18. import { apply, CommandUiRuntime, inject } from '../src/client/index.ts'
  19. const sid = (k: string): SessionId => k as SessionId
  20. async function bench() {
  21. const ctx = new Context()
  22. const sources = new Map<string, InputTriggerSource>()
  23. ctx.provide('inputTriggers', {
  24. registerSource(src: InputTriggerSource) {
  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. const commandsRemote = { list: () => Promise.resolve([]) }
  35. // The service subscribes its cache-invalidation events on construction, so
  36. // the Remote face needs `$on` even where this spec dispatches none.
  37. ctx.provide('remote', { commands: commandsRemote, $on: () => () => {} })
  38. ctx.provide('remote.commands', commandsRemote)
  39. await ctx.plugin(SlotRegistry).await()
  40. ctx.slots.register({
  41. name: 'root', children: { 'conversation.input.overlay': { kind: 'list', scope: 'session' } },
  42. } as never, (() => null) as never)
  43. ctx.provide('locale', new LocaleRuntime(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, slots: ctx.slots, mint }
  52. }
  53. describe('apply', () => {
  54. it('declares the services it binds', () => {
  55. expect(inject).toEqual(['inputTriggers', 'sessions', 'remote', 'remote.commands', 'locale'])
  56. })
  57. it('mounts ctx.commandUi, registers the source and the overlay entry, and folds up on disposal', async () => {
  58. const { ctx, fiber, sources, slots } = await bench()
  59. const command = ctx.get('commandUi')
  60. expect(command).toBeInstanceOf(CommandUiRuntime)
  61. // Frozen-contract conformance (compile-time check rides the assignment).
  62. const contract: CommandUiContract = command as CommandUiRuntime
  63. expect(typeof contract.register).toBe('function')
  64. expect(typeof contract.popupFor).toBe('function')
  65. expect([...sources.keys()]).toEqual(['/ command'])
  66. expect(slots.entries('conversation.input.overlay').map(entry => entry.options.id)).toEqual(['command-popup'])
  67. await fiber.dispose()
  68. expect(sources.size).toBe(0)
  69. expect(slots.entries('conversation.input.overlay')).toHaveLength(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, slots, mint } = await bench()
  73. const command = ctx.get('commandUi') as CommandUiRuntime
  74. const scope = mint('s1')
  75. const entry = slots.entries('conversation.input.overlay')[0]!
  76. const injectEntry = entry.inject as unknown 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. })