plan-mode-control.spec.tsx 4.7 KB

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