plan-mode-control.client.spec.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // @vitest-environment jsdom
  2. /**
  3. * PlanChip over the `plan` projection: nothing renders while the capability
  4. * is absent or the effective target is the default mode; while plan mode is
  5. * the target, the chip executes /plan off and remains visible through failures
  6. * until the projection confirms the exit.
  7. */
  8. import { afterEach, describe, expect, it, vi } from 'vitest'
  9. import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
  10. import { createSnapshotStore } from '@deepseek-ai/dsh-client-store'
  11. import { bindSnapshotSelector, makeTranslate } from '@deepseek-ai/dsh-client-test-runtime'
  12. import type { PlanProjection } from '@deepseek-ai/dsh-plan-mode/client'
  13. import { PlanChip, type PlanChipProps } from '../src/client/PlanModeControl.tsx'
  14. import { zh as commonZh } from '@deepseek-ai/dsh-client-locale/src/locales/zh.ts'
  15. import { zh } from '../src/client/locales.ts'
  16. afterEach(cleanup)
  17. // The framework-injected t seat, stubbed over the zh dictionaries (the default locale).
  18. const t: PlanChipProps['t'] = makeTranslate(zh, commonZh)
  19. function setup(
  20. plan: PlanProjection | undefined,
  21. exitPlanMode = vi.fn(() => Promise.resolve<string | null>(null)),
  22. locked = false,
  23. ) {
  24. const store = createSnapshotStore<{ value: PlanProjection | undefined }>({ value: plan })
  25. const useProjection = (_key: string, selector?: (v: unknown) => unknown) =>
  26. bindSnapshotSelector(store)(s => (selector ?? (v => v))(s.value))
  27. const props = { useProjection, locked, exitPlanMode, t } as unknown as PlanChipProps
  28. const view = render(<PlanChip {...props} />)
  29. return { store, exitPlanMode, view }
  30. }
  31. const chip = () => screen.getByRole('button', { name: 'plan mode 已开启,按下关闭' })
  32. describe('PlanChip', () => {
  33. it('renders nothing for an absent capability or a default-mode target', () => {
  34. const absent = setup(undefined)
  35. expect(absent.view.container.innerHTML).toBe('')
  36. cleanup()
  37. const inactive = setup({ active: false, pending: false })
  38. expect(inactive.view.container.innerHTML).toBe('')
  39. cleanup()
  40. const leaving = setup({ active: true, pending: true })
  41. expect(leaving.view.container.innerHTML).toBe('')
  42. })
  43. it('renders the Plan status for active and pending-entry targets', () => {
  44. setup({ active: true, pending: false })
  45. expect(chip().textContent).toBe('Plan')
  46. cleanup()
  47. setup({ active: false, pending: true })
  48. expect(chip().textContent).toBe('Plan')
  49. })
  50. it('executes /plan off once and follows the projection down', async () => {
  51. let resolve!: (value: string | null) => void
  52. const exitPlanMode = vi.fn(() => new Promise<string | null>((done) => { resolve = done }))
  53. const { store } = setup({ active: true, pending: false }, exitPlanMode)
  54. fireEvent.click(chip())
  55. expect(exitPlanMode).toHaveBeenCalledTimes(1)
  56. fireEvent.click(chip())
  57. expect(exitPlanMode).toHaveBeenCalledTimes(1)
  58. resolve(null)
  59. store.set({ value: { active: true, pending: true } })
  60. await waitFor(() => {
  61. expect(screen.queryByRole('button', { name: 'plan mode 已开启,按下关闭' })).toBeNull()
  62. })
  63. })
  64. it('disables under the locked owner prop', () => {
  65. setup({ active: true, pending: false }, vi.fn(), true)
  66. expect((chip() as HTMLButtonElement).disabled).toBe(true)
  67. })
  68. it('surfaces admission and transport failures while staying visible', async () => {
  69. const exitPlanMode = vi.fn()
  70. .mockResolvedValueOnce('host said no')
  71. .mockRejectedValueOnce(new Error('network down'))
  72. .mockRejectedValueOnce('socket closed')
  73. setup({ active: true, pending: false }, exitPlanMode)
  74. fireEvent.click(chip())
  75. expect((await screen.findByText('退出 plan mode 失败')).getAttribute('title')).toBe('host said no')
  76. expect(chip()).toBeTruthy()
  77. fireEvent.click(chip())
  78. expect(await screen.findByTitle('network down')).toBeTruthy()
  79. fireEvent.click(chip())
  80. expect(await screen.findByTitle('socket closed')).toBeTruthy()
  81. })
  82. it('ignores in-flight fulfillment and rejection after unmount', () => {
  83. let resolve!: (value: string | null) => void
  84. const successful = setup(
  85. { active: true, pending: false },
  86. vi.fn(() => new Promise<string | null>((done) => { resolve = done })),
  87. )
  88. fireEvent.click(chip())
  89. successful.view.unmount()
  90. expect(() => { resolve(null) }).not.toThrow()
  91. let reject!: (reason: unknown) => void
  92. const exitPlanMode = vi.fn(() => new Promise<string | null>((_done, fail) => { reject = fail }))
  93. const { view } = setup({ active: true, pending: false }, exitPlanMode)
  94. fireEvent.click(chip())
  95. view.unmount()
  96. expect(() => { reject(new Error('late')) }).not.toThrow()
  97. })
  98. })