browser-plugin.spec.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. * toggle chip; the injected face executes /plan or /plan off by direction and
  5. * folds admission outcomes into null (admitted) or a user-visible failure
  6. * line; teardown 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 by direction, 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.setPlanMode(false)).resolves.toBeNull()
  54. expect(b.execute).toHaveBeenLastCalledWith({ sessionId: SID, line: '/plan off' })
  55. await expect(injected.setPlanMode(true)).resolves.toBeNull()
  56. expect(b.execute).toHaveBeenLastCalledWith({ sessionId: SID, line: '/plan' })
  57. // Business failure folds to the composer-visible line.
  58. b.execute.mockResolvedValueOnce({
  59. result: { ok: false as const, error: { code: 'session-not-found', message: 'gone', details: {} } },
  60. } as never)
  61. await expect(injected.setPlanMode(false)).resolves.toBe('gone(session-not-found)')
  62. // Unmatched admission (plan-mode not composed host-side) is also a failure line.
  63. b.execute.mockResolvedValueOnce({
  64. result: { ok: true as const, value: { matched: false as const } },
  65. } as never)
  66. await expect(injected.setPlanMode(true)).resolves.toBe('未知命令:/plan')
  67. await fiber.dispose()
  68. expect(b.slots.entries('conversation.input.plan')).toHaveLength(0)
  69. })
  70. })