apply.client.spec.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * apply wiring on a real cordis Context + SlotRegistry: InputTriggerService mounts
  3. * as ctx.inputTriggers once its sessions dependency is up; the MenuView overlay
  4. * registration follows the slot declaration, resolves the per-session controller from the slot's
  5. * sessionId, and unregisters on fiber teardown.
  6. */
  7. import { Context } from '@deepseek-ai/cordis'
  8. import { describe, expect, it } from 'vitest'
  9. import { LocaleRuntime } from '@deepseek-ai/dsh-client-locale/client'
  10. import { usePinnedBrowserLanguages } from '@deepseek-ai/dsh-client-test-runtime'
  11. import { createScope, scopeOf, SlotRegistry } from '@deepseek-ai/dsh-client-runtime/client'
  12. import type { SessionId } from '@deepseek-ai/dsh-client-runtime/client'
  13. import { apply, inject, InputTriggerService } from '@deepseek-ai/dsh-client-ui-input-trigger/client'
  14. import type { MenuViewInjected } from '@deepseek-ai/dsh-client-ui-input-trigger/client'
  15. // The service reads its initial locale from the browser; these specs assert
  16. // the shipped Chinese copy, so they state the browser they assume.
  17. usePinnedBrowserLanguages('zh-CN')
  18. const sid = (k: string): SessionId => k as SessionId
  19. async function bench() {
  20. const ctx = new Context()
  21. await ctx.plugin(SlotRegistry).await()
  22. const slots = ctx.get('slots') as SlotRegistry
  23. // Stand-in for the ui-conversation composer entry: declare the overlay
  24. // slot without providing ConversationController, which is not its lifecycle
  25. // signal.
  26. slots.register(
  27. { name: 'root', children: { 'conversation.input.overlay': { kind: 'list', scope: 'session' } } } as never,
  28. () => null,
  29. )
  30. // Sessions face: mint one real scope for session 'a' and resolve it by id.
  31. const scope = createScope(ctx, sid('a'))
  32. ctx.provide('sessions', {
  33. scope: (id: SessionId) => (id === sid('a') ? scope.ctx : undefined),
  34. scopeOf: (c: Context) => scopeOf(c),
  35. })
  36. const locale = new LocaleRuntime(ctx)
  37. ctx.provide('locale', locale)
  38. return { ctx, slots, locale }
  39. }
  40. describe('apply', () => {
  41. it('declares the sessions and locale dependencies (scope tree + localized menu copy)', () => {
  42. expect(inject).toEqual(['sessions', 'locale'])
  43. })
  44. it('registers the bilingual menu dictionaries (group titles by source name + the pending row)', async () => {
  45. const { ctx, locale } = await bench()
  46. await ctx.plugin({ inject: [...inject], apply }).await()
  47. const t = locale.bind('slash.menu')
  48. expect(t('command')).toBe('命令')
  49. locale.setLocale('en')
  50. expect(t('skill')).toBe('Skills')
  51. expect(t('subagent')).toBe('Subagents')
  52. expect(t('loading')).toBe('Loading…')
  53. })
  54. it('mounts ctx.inputTriggers once sessions is up, before any conversation service exists', async () => {
  55. const { ctx } = await bench()
  56. await ctx.plugin({ inject: [...inject], apply }).await()
  57. expect(ctx.get('inputTriggers')).toBeInstanceOf(InputTriggerService)
  58. })
  59. it('registers MenuView into the overlay and resolves the per-session controller by slot sessionId', async () => {
  60. const { ctx, slots } = await bench()
  61. await ctx.plugin({ inject: [...inject], apply }).await()
  62. expect(slots.entries('conversation.input.overlay')).toHaveLength(1)
  63. const entries = slots.entries('conversation.input.overlay')
  64. expect(entries[0]!.options.id).toBe('slash-menu')
  65. // Copy rides the standard locale seat, not the business face.
  66. expect(entries[0]!.locale).toBe('slash.menu')
  67. const inputTriggers = ctx.get('inputTriggers') as InputTriggerService
  68. // StoredEntry.inject is declaration-typed ((...args: never[]) shape);
  69. // the erased registration widens it past a direct cast, so hop unknown.
  70. const injectEntry = entries[0]!.inject as unknown as (sessionId: SessionId) => MenuViewInjected
  71. const injected = injectEntry(sid('a'))
  72. const controller = inputTriggers.sessionOf(
  73. (ctx.get('sessions') as { scope(id: SessionId): Context }).scope(sid('a')),
  74. )
  75. expect(injected.menu).toBe(controller.menu)
  76. // The pick face routes into the controller pipeline (closed menu → no-op).
  77. injected.onPick('command', 0)
  78. expect(controller.menu.getSnapshot().open).toBe(false)
  79. // The dismiss face routes into the controller too (closed menu → no-op).
  80. injected.onDismiss()
  81. expect(controller.menu.getSnapshot().open).toBe(false)
  82. // An unknown session id fails loud (no silent scope miss).
  83. expect(() => injectEntry(sid('ghost'))).toThrow(/resolved no scope/)
  84. })
  85. it('fiber teardown removes the overlay entry', async () => {
  86. const { ctx, slots } = await bench()
  87. const fiber = ctx.plugin({ inject: [...inject], apply })
  88. await fiber.await()
  89. expect(slots.entries('conversation.input.overlay')).toHaveLength(1)
  90. await fiber.dispose()
  91. expect(slots.entries('conversation.input.overlay')).toHaveLength(0)
  92. expect(ctx.get('inputTriggers')).toBeUndefined()
  93. })
  94. })