browser-plugin.spec.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 { QuestionComposer } from '../src/client/QuestionComposer.tsx'
  13. import { apply, inject } from '../src/client/index.ts'
  14. async function bench() {
  15. const ctx = new Context()
  16. await ctx.plugin(SlotsService).await()
  17. const slots = ctx.get('slots') as SlotsService
  18. // Stand-in for ui-conversation's conversation entry: the composer slot only
  19. // exists while a live entry declares it in children (declaration account:
  20. // design §2.2).
  21. slots.register(
  22. { name: 'root', children: { 'conversation.composer': { kind: 'chain', scope: 'session' } } } as never,
  23. () => null,
  24. )
  25. // 'conversation' inject is an ordering edge (the declaring plugin provides
  26. // it after declaring the chain); the bench declares the chain itself.
  27. ctx.provide('conversation', {})
  28. return { ctx, slots }
  29. }
  30. describe('apply', () => {
  31. it('declares the services it binds', () => {
  32. expect(inject).toEqual(['slots', 'conversation'])
  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. await expect(ctx.plugin({ inject: [...inject], apply }))
  41. .rejects.toThrow(/slot "conversation.composer" is not declared/)
  42. })
  43. it('registers the question entry: routing selector, no inject face', async () => {
  44. const { ctx, slots } = await bench()
  45. await ctx.plugin({ inject: [...inject], apply }).await()
  46. const entry = slots.entries('conversation.composer')[0]!
  47. expect(entry.component).toBe(QuestionComposer)
  48. // The whole behavior surface rides the matched carrier: no business face.
  49. expect(entry.inject).toBeUndefined()
  50. // The selector narrows the chain currency: question wait in → that wait; none → null.
  51. const select = entry.select as (owner: { interactions: readonly { kind: string }[] }) => unknown
  52. const question = { kind: 'question' }
  53. expect(select({ interactions: [{ kind: 'approval' }, question] })).toBe(question)
  54. expect(select({ interactions: [{ kind: 'approval' }] })).toBeNull()
  55. expect(select({ interactions: [] })).toBeNull()
  56. })
  57. it('teardown unregisters the slot entry', async () => {
  58. const { ctx, slots } = await bench()
  59. const fiber = ctx.plugin({ inject: [...inject], apply })
  60. await fiber.await()
  61. expect(slots.entries('conversation.composer')).toHaveLength(1)
  62. await fiber.dispose()
  63. expect(slots.entries('conversation.composer')).toHaveLength(0)
  64. })
  65. })