browser-plugin.spec.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * ui-plan browser half on a real SlotsService: the plugin occupies the
  3. * conversation-declared `conversation.input.plan` single seat with the active
  4. * plan status chip; the injected face executes /plan off and folds admission
  5. * outcomes into null (admitted) or a user-visible failure line; teardown
  6. * empties the seat (HMR safety).
  7. */
  8. import { Context } from '@deepseek-ai/cordis'
  9. import { describe, expect, it, vi } from 'vitest'
  10. import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
  11. import type { SessionId } from '@deepseek-ai/dsh-client-runtime/client'
  12. import { LocaleService } from '@deepseek-ai/dsh-client-locale/client'
  13. import { PlanChip } from '../src/client/PlanModeControl.tsx'
  14. import type { PlanChipInjected } from '../src/client/index.ts'
  15. import { apply, inject } from '../src/client/index.ts'
  16. import { apply as nodeApply } from '../src/index.ts'
  17. const SID = 's-plan' as SessionId
  18. async function bench() {
  19. const ctx = new Context()
  20. await ctx.plugin(SlotsService).await()
  21. const slots = ctx.get('slots') as SlotsService
  22. slots.register({
  23. name: 'root',
  24. children: { 'conversation.input.plan': { kind: 'single', scope: 'session' } },
  25. } as never, () => null)
  26. const execute = vi.fn((_payload: { sessionId: SessionId; line: string }) =>
  27. Promise.resolve({ result: { ok: true as const, value: { matched: true as const, commandId: 'c1' } } }))
  28. ctx.provide('connection', { api: { commands: { execute } } })
  29. ctx.provide('locale', new LocaleService(ctx))
  30. return { ctx, slots, execute }
  31. }
  32. describe('ui-plan browser apply', () => {
  33. it('declares every service it binds', () => {
  34. expect(inject).toEqual(['slots', 'connection', 'locale'])
  35. })
  36. it('node-half apply is an intentional no-op', () => {
  37. expect(() => { nodeApply() }).not.toThrow()
  38. })
  39. it('waits until conversation declares the plan seat', async () => {
  40. const ctx = new Context()
  41. await ctx.plugin(SlotsService).await()
  42. ctx.provide('connection', {})
  43. ctx.provide('locale', new LocaleService(ctx))
  44. const fiber = ctx.plugin({ inject: [...inject], apply })
  45. await fiber.await()
  46. expect(ctx.slots.entries('conversation.input.plan')).toHaveLength(0)
  47. ctx.slots.register({
  48. name: 'root', children: { 'conversation.input.plan': { kind: 'single', scope: 'session' } },
  49. } as never, () => null)
  50. await Promise.resolve()
  51. expect(ctx.slots.entries('conversation.input.plan')).toHaveLength(1)
  52. })
  53. it('registers the chip, executes /plan off, and unregisters on teardown', async () => {
  54. const b = await bench()
  55. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  56. await fiber.await()
  57. const entry = b.slots.entries('conversation.input.plan')[0]!
  58. expect(entry.component).toBe(PlanChip)
  59. const injected = (entry.inject as unknown as (id: SessionId) => PlanChipInjected)(SID)
  60. await expect(injected.exitPlanMode()).resolves.toBeNull()
  61. expect(b.execute).toHaveBeenLastCalledWith({ sessionId: SID, line: '/plan off' })
  62. // Business failure folds to the composer-visible line.
  63. b.execute.mockResolvedValueOnce({
  64. result: { ok: false as const, error: { code: 'session-not-found', message: 'gone', details: {} } },
  65. } as never)
  66. await expect(injected.exitPlanMode()).resolves.toBe('gone (session-not-found)')
  67. // Unmatched admission (plan-mode not composed host-side) is also a failure line.
  68. b.execute.mockResolvedValueOnce({
  69. result: { ok: true as const, value: { matched: false as const } },
  70. } as never)
  71. await expect(injected.exitPlanMode()).resolves.toBe('unknown command: /plan off')
  72. await fiber.dispose()
  73. expect(b.slots.entries('conversation.input.plan')).toHaveLength(0)
  74. })
  75. })