apply.spec.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * apply wiring on a real cordis Context + SlotsService: SlashService mounts
  3. * as ctx.slash once its sessions dependency is up; the MenuView overlay
  4. * registration waits on the conversation seam (ctx.inject scope), lands once
  5. * the declarer is up, resolves the per-session controller from the slot's
  6. * sessionId, and unregisters on fiber teardown.
  7. */
  8. import { Context } from 'cordis'
  9. import { describe, expect, it, vi } from 'vitest'
  10. import { LocaleService } from '@deepseek-ai/dsh-client-locale/client'
  11. import { createScope, scopeOf, SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
  12. import type { SessionId } from '@deepseek-ai/dsh-client-runtime/client'
  13. import { apply, inject, SlashService } from '@deepseek-ai/dsh-client-ui-slash/client'
  14. import type { MenuViewInjected } from '@deepseek-ai/dsh-client-ui-slash/client'
  15. const sid = (k: string): SessionId => k as SessionId
  16. async function bench() {
  17. const ctx = new Context()
  18. await ctx.plugin(SlotsService).await()
  19. const slots = ctx.get('slots') as SlotsService
  20. // Stand-in for the ui-conversation composer entry: declare the overlay
  21. // slot, then provide the conversation service (declaration precedes the
  22. // service exactly as the real apply orders them).
  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 LocaleService(ctx)
  34. ctx.provide('locale', locale)
  35. return { ctx, slots, locale }
  36. }
  37. describe('apply', () => {
  38. it('declares the sessions and locale dependencies (scope tree + localized menu copy)', () => {
  39. expect(inject).toEqual(['sessions', 'locale'])
  40. })
  41. it('registers the bilingual menu dictionaries (group titles by source name + the pending row)', async () => {
  42. const { ctx, locale } = await bench()
  43. await ctx.plugin({ inject: [...inject], apply }).await()
  44. const t = locale.bind('slash.menu')
  45. expect(t('command')).toBe('命令')
  46. locale.setLocale('en')
  47. expect(t('skill')).toBe('Skills')
  48. expect(t('subagent')).toBe('Subagents')
  49. expect(t('loading')).toBe('Loading…')
  50. })
  51. it('mounts ctx.slash once sessions is up, before any conversation service exists', async () => {
  52. const { ctx } = await bench()
  53. await ctx.plugin({ inject: [...inject], apply }).await()
  54. expect(ctx.get('slash')).toBeInstanceOf(SlashService)
  55. })
  56. it('registers MenuView into the overlay and resolves the per-session controller by slot sessionId', async () => {
  57. const { ctx, slots } = await bench()
  58. await ctx.plugin({ inject: [...inject], apply }).await()
  59. expect(slots.entries('conversation.input.overlay')).toHaveLength(0)
  60. ctx.provide('conversation', {})
  61. // The inject scope activates asynchronously on the service arrival.
  62. await vi.waitFor(() => { 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 slash = ctx.get('slash') as SlashService
  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 = slash.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. ctx.provide('conversation', {})
  90. await vi.waitFor(() => { expect(slots.entries('conversation.input.overlay')).toHaveLength(1) })
  91. await fiber.dispose()
  92. expect(slots.entries('conversation.input.overlay')).toHaveLength(0)
  93. expect(ctx.get('slash')).toBeUndefined()
  94. })
  95. })