browser-plugin.spec.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // The composer slot exists only while its declaring entry is live.
  19. slots.register(
  20. { name: 'root', children: { 'conversation.composer': { kind: 'chain', scope: 'session' } } } as never,
  21. () => null,
  22. )
  23. // 'conversation' inject is an ordering edge (the declaring plugin provides
  24. // it after declaring the chain); the bench declares the chain itself.
  25. ctx.provide('conversation', {})
  26. return { ctx, slots }
  27. }
  28. describe('apply', () => {
  29. it('declares the services it binds', () => {
  30. expect(inject).toEqual(['slots', 'conversation'])
  31. })
  32. it('fails loud when no live entry has declared the composer slot', async () => {
  33. const ctx = new Context()
  34. await ctx.plugin(SlotsService).await()
  35. // Satisfy the ordering inject without declaring the chain: apply must
  36. // then hit the undeclared-slot throw, not sit waiting on the service.
  37. ctx.provide('conversation', {})
  38. await expect(ctx.plugin({ inject: [...inject], apply }))
  39. .rejects.toThrow(/slot "conversation.composer" is not declared/)
  40. })
  41. it('registers the question entry: routing selector, no inject face', async () => {
  42. const { ctx, slots } = await bench()
  43. await ctx.plugin({ inject: [...inject], apply }).await()
  44. const entry = slots.entries('conversation.composer')[0]!
  45. expect(entry.component).toBe(QuestionComposer)
  46. // The whole behavior surface rides the matched carrier: no business face.
  47. expect(entry.inject).toBeUndefined()
  48. // The selector narrows the chain currency: question wait in → that wait; none → null.
  49. const select = entry.select as (owner: { interactions: readonly { kind: string }[] }) => unknown
  50. const question = { kind: 'question' }
  51. expect(select({ interactions: [{ kind: 'approval' }, question] })).toBe(question)
  52. expect(select({ interactions: [{ kind: 'approval' }] })).toBeNull()
  53. expect(select({ interactions: [] })).toBeNull()
  54. })
  55. it('teardown unregisters the slot entry', async () => {
  56. const { ctx, slots } = await bench()
  57. const fiber = ctx.plugin({ inject: [...inject], apply })
  58. await fiber.await()
  59. expect(slots.entries('conversation.composer')).toHaveLength(1)
  60. await fiber.dispose()
  61. expect(slots.entries('conversation.composer')).toHaveLength(0)
  62. })
  63. })