browser-plugin.spec.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. * declaration-aware activation, 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 '@deepseek-ai/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. ctx.provide('locale', new LocaleService(ctx))
  25. return { ctx, slots }
  26. }
  27. describe('apply', () => {
  28. it('declares the services it binds', () => {
  29. expect(inject).toEqual(['slots', 'locale'])
  30. })
  31. it('waits until a live entry declares the composer slot', async () => {
  32. const ctx = new Context()
  33. await ctx.plugin(SlotsService).await()
  34. ctx.provide('locale', new LocaleService(ctx))
  35. const fiber = ctx.plugin({ inject: [...inject], apply })
  36. await fiber.await()
  37. expect(ctx.slots.entries('conversation.composer')).toHaveLength(0)
  38. ctx.slots.register(
  39. { name: 'root', children: { 'conversation.composer': { kind: 'chain', scope: 'session' } } } as never,
  40. () => null,
  41. )
  42. await Promise.resolve()
  43. expect(ctx.slots.entries('conversation.composer')).toHaveLength(1)
  44. })
  45. it('registers the question entry: routing selector, no inject face', async () => {
  46. const { ctx, slots } = await bench()
  47. await ctx.plugin({ inject: [...inject], apply }).await()
  48. const entry = slots.entries('conversation.composer')[0]!
  49. expect(entry.component).toBe(QuestionComposer)
  50. // The whole behavior surface rides the matched carrier: no business face;
  51. // copy rides the standard locale seat.
  52. expect(entry.inject).toBeUndefined()
  53. expect(entry.locale).toBe('question')
  54. // The selector narrows the chain currency: question wait in → that wait; none → null.
  55. const select = entry.select as (owner: { interactions: readonly { kind: string }[] }) => unknown
  56. const question = { kind: 'question' }
  57. expect(select({ interactions: [{ kind: 'approval' }, question] })).toBe(question)
  58. expect(select({ interactions: [{ kind: 'approval' }] })).toBeNull()
  59. expect(select({ interactions: [] })).toBeNull()
  60. })
  61. it('teardown unregisters the slot entry', async () => {
  62. const { ctx, slots } = await bench()
  63. const fiber = ctx.plugin({ inject: [...inject], apply })
  64. await fiber.await()
  65. expect(slots.entries('conversation.composer')).toHaveLength(1)
  66. await fiber.dispose()
  67. expect(slots.entries('conversation.composer')).toHaveLength(0)
  68. })
  69. })