browser-plugin.spec.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * apply wiring on a real cordis Context + SlotsService: QuestionComposer
  3. * registered as the `question` entry of the conversation-declared composer
  4. * slot with ZERO business face (data and verbs ride the dispatched carrier),
  5. * load-order fail-loud, and fiber-teardown unregistration. Component and
  6. * domain-face behavior is covered props-direct in question-composer.spec.tsx;
  7. * no renderer machinery here.
  8. */
  9. import { Context } from 'cordis'
  10. import { describe, expect, it } from 'vitest'
  11. import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
  12. import { LocaleService } from '@deepseek-ai/dsh-client-locale/client'
  13. import { QuestionComposer } from '../src/client/QuestionComposer.tsx'
  14. import { apply, inject } from '../src/client/index.ts'
  15. async function bench() {
  16. const ctx = new Context()
  17. await ctx.plugin(SlotsService).await()
  18. const slots = ctx.get('slots') as SlotsService
  19. // The composer slot exists only while its declaring entry is live.
  20. slots.register(
  21. { name: 'root', children: { 'conversation.composer': { kind: 'chain', scope: 'session' } } } as never,
  22. () => null,
  23. )
  24. // 'conversation' inject is an ordering edge (the declaring plugin provides
  25. // it after declaring the chain); the bench declares the chain itself.
  26. ctx.provide('conversation', {})
  27. ctx.provide('locale', new LocaleService(ctx))
  28. return { ctx, slots }
  29. }
  30. describe('apply', () => {
  31. it('declares the services it binds', () => {
  32. expect(inject).toEqual(['slots', 'conversation', 'locale'])
  33. })
  34. it('fails loud when no live entry has declared the composer slot', async () => {
  35. const ctx = new Context()
  36. await ctx.plugin(SlotsService).await()
  37. // Satisfy the ordering inject without declaring the chain: apply must
  38. // then hit the undeclared-slot throw, not sit waiting on the service.
  39. ctx.provide('conversation', {})
  40. ctx.provide('locale', new LocaleService(ctx))
  41. await expect(ctx.plugin({ inject: [...inject], apply }))
  42. .rejects.toThrow(/slot "conversation.composer" is not declared/)
  43. })
  44. it('registers the question entry: routing selector, no inject face', async () => {
  45. const { ctx, slots } = await bench()
  46. await ctx.plugin({ inject: [...inject], apply }).await()
  47. const entry = slots.entries('conversation.composer')[0]!
  48. expect(entry.component).toBe(QuestionComposer)
  49. // The whole behavior surface rides the matched carrier: no business face;
  50. // copy rides the standard locale seat.
  51. expect(entry.inject).toBeUndefined()
  52. expect(entry.locale).toBe('question')
  53. // The selector narrows the chain currency: question wait in → that wait; none → null.
  54. const select = entry.select as (owner: { interactions: readonly { kind: string }[] }) => unknown
  55. const question = { kind: 'question' }
  56. expect(select({ interactions: [{ kind: 'approval' }, question] })).toBe(question)
  57. expect(select({ interactions: [{ kind: 'approval' }] })).toBeNull()
  58. expect(select({ interactions: [] })).toBeNull()
  59. })
  60. it('teardown unregisters the slot entry', async () => {
  61. const { ctx, slots } = await bench()
  62. const fiber = ctx.plugin({ inject: [...inject], apply })
  63. await fiber.await()
  64. expect(slots.entries('conversation.composer')).toHaveLength(1)
  65. await fiber.dispose()
  66. expect(slots.entries('conversation.composer')).toHaveLength(0)
  67. })
  68. })