browser-plugin.client.spec.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * ui-plan browser half on a real SlotRegistry: 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 { SlotRegistry } from '@deepseek-ai/dsh-client-ui-renderer/client'
  11. import type { SessionId } from '@deepseek-ai/dsh-session/types'
  12. import { LocaleRuntime } 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(SlotRegistry).await()
  21. const slots = ctx.get('slots') as SlotRegistry
  22. slots.register({
  23. name: 'root',
  24. children: { 'conversation.input.plan': { kind: 'single', scope: 'session' } },
  25. } as never, () => null)
  26. const execute = vi.fn((_sessionId: SessionId, _line: string) =>
  27. Promise.resolve({ ok: true, value: { commandId: 'c1', result: { kind: 'success' as const } } }))
  28. const commandsRemote = { execute }
  29. ctx.provide('remote', { commands: commandsRemote })
  30. ctx.provide('remote.commands', commandsRemote)
  31. ctx.provide('locale', new LocaleRuntime(ctx))
  32. return { ctx, slots, execute }
  33. }
  34. describe('ui-plan browser apply', () => {
  35. it('declares every service it binds', () => {
  36. expect(inject).toEqual(['slots', 'remote', 'remote.commands', 'locale'])
  37. })
  38. it('node-half apply is an intentional no-op', () => {
  39. expect(() => { nodeApply() }).not.toThrow()
  40. })
  41. it('waits until conversation declares the plan seat', async () => {
  42. const ctx = new Context()
  43. await ctx.plugin(SlotRegistry).await()
  44. ctx.provide('remote', { commands: {} })
  45. ctx.provide('remote.commands', {})
  46. ctx.provide('locale', new LocaleRuntime(ctx))
  47. const fiber = ctx.plugin({ inject: [...inject], apply })
  48. await fiber.await()
  49. expect(ctx.slots.entries('conversation.input.plan')).toHaveLength(0)
  50. ctx.slots.register({
  51. name: 'root', children: { 'conversation.input.plan': { kind: 'single', scope: 'session' } },
  52. } as never, () => null)
  53. await Promise.resolve()
  54. expect(ctx.slots.entries('conversation.input.plan')).toHaveLength(1)
  55. })
  56. it('registers the chip, executes /plan off, and unregisters on teardown', async () => {
  57. const b = await bench()
  58. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  59. await fiber.await()
  60. const entry = b.slots.entries('conversation.input.plan')[0]!
  61. expect(entry.component).toBe(PlanChip)
  62. const injected = (entry.inject as unknown as (id: SessionId) => PlanChipInjected)(SID)
  63. await expect(injected.exitPlanMode()).resolves.toBeNull()
  64. expect(b.execute).toHaveBeenLastCalledWith(SID, '/plan off', [])
  65. // Business failure folds to the composer-visible line: the generated method
  66. // reports the RPC failure in its error branch.
  67. b.execute.mockResolvedValueOnce({
  68. ok: false,
  69. error: { code: 'session-not-found', message: 'gone', details: {} },
  70. } as never)
  71. await expect(injected.exitPlanMode()).resolves.toBe('gone (session-not-found)')
  72. // Unmatched admission (plan-mode not composed host-side) is also a failure line.
  73. b.execute.mockResolvedValueOnce({ ok: true, value: undefined } as never)
  74. await expect(injected.exitPlanMode()).resolves.toBe('unknown command: /plan off')
  75. await fiber.dispose()
  76. expect(b.slots.entries('conversation.input.plan')).toHaveLength(0)
  77. })
  78. })