browser-plugin.spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * ui-plan browser half on a real SlotsService: the plugin occupies the
  3. * conversation-declared `conversation.input.plan` single seat with the plan
  4. * 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 '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 { PlanChip } from '../src/client/PlanModeControl.tsx'
  13. import type { PlanChipInjected } from '../src/client/index.ts'
  14. import { apply, inject } from '../src/client/index.ts'
  15. import { apply as nodeApply } from '../src/index.ts'
  16. const SID = 's-plan' as SessionId
  17. async function bench() {
  18. const ctx = new Context()
  19. await ctx.plugin(SlotsService).await()
  20. const slots = ctx.get('slots') as SlotsService
  21. slots.register({
  22. name: 'root',
  23. children: { 'conversation.input.plan': { kind: 'single', scope: 'session' } },
  24. } as never, () => null)
  25. const execute = vi.fn((_payload: { sessionId: SessionId; line: string }) =>
  26. Promise.resolve({ result: { ok: true as const, value: { matched: true as const, commandId: 'c1' } } }))
  27. ctx.provide('connection', { api: { commands: { execute } } })
  28. ctx.provide('conversation', {})
  29. return { ctx, slots, execute }
  30. }
  31. describe('ui-plan browser apply', () => {
  32. it('declares every service it binds', () => {
  33. expect(inject).toEqual(['slots', 'connection', 'conversation'])
  34. })
  35. it('node-half apply is an intentional no-op', () => {
  36. expect(() => { nodeApply() }).not.toThrow()
  37. })
  38. it('fails loud when conversation did not declare the plan seat', async () => {
  39. const ctx = new Context()
  40. await ctx.plugin(SlotsService).await()
  41. ctx.provide('connection', {})
  42. ctx.provide('conversation', {})
  43. await expect(ctx.plugin({ inject: [...inject], apply }))
  44. .rejects.toThrow(/slot "conversation.input.plan" is not declared/)
  45. })
  46. it('registers the chip, executes /plan off, and unregisters on teardown', async () => {
  47. const b = await bench()
  48. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  49. await fiber.await()
  50. const entry = b.slots.entries('conversation.input.plan')[0]!
  51. expect(entry.component).toBe(PlanChip)
  52. const injected = (entry.inject as unknown as (id: SessionId) => PlanChipInjected)(SID)
  53. await expect(injected.exitPlanMode()).resolves.toBeNull()
  54. expect(b.execute).toHaveBeenLastCalledWith({ sessionId: SID, line: '/plan off' })
  55. // Business failure folds to the composer-visible line.
  56. b.execute.mockResolvedValueOnce({
  57. result: { ok: false as const, error: { code: 'session-not-found', message: 'gone', details: {} } },
  58. } as never)
  59. await expect(injected.exitPlanMode()).resolves.toBe('gone(session-not-found)')
  60. // Unmatched admission (plan-mode not composed host-side) is also a failure line.
  61. b.execute.mockResolvedValueOnce({
  62. result: { ok: true as const, value: { matched: false as const } },
  63. } as never)
  64. await expect(injected.exitPlanMode()).resolves.toBe('未知命令:/plan off')
  65. await fiber.dispose()
  66. expect(b.slots.entries('conversation.input.plan')).toHaveLength(0)
  67. })
  68. })