1
0

apply.client.spec.ts 5.2 KB

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