|
|
@@ -1,10 +1,11 @@
|
|
|
// @vitest-environment jsdom
|
|
|
/**
|
|
|
* PlanChip over the `plan` projection: nothing renders while the capability
|
|
|
- * is absent or the effective target is the default mode; the chip renders
|
|
|
- * while the target is plan mode (pending follows the target — /plan shows it
|
|
|
- * immediately, /plan off hides it immediately); the chip button executes
|
|
|
- * /plan off and surfaces failures without hiding until the projection says so.
|
|
|
+ * is absent; with the capability present the chip renders in both states with
|
|
|
+ * aria-pressed following the effective target (pending folds — /plan shows
|
|
|
+ * pressed immediately, /plan off unpressed immediately); clicking executes
|
|
|
+ * the command toward the opposite target and surfaces direction-specific
|
|
|
+ * failures while the projection still owns the displayed state.
|
|
|
*/
|
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
|
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
|
@@ -17,78 +18,98 @@ afterEach(cleanup)
|
|
|
|
|
|
function setup(
|
|
|
plan: PlanProjection | undefined,
|
|
|
- exitPlanMode = vi.fn(() => Promise.resolve<string | null>(null)),
|
|
|
+ setPlanMode = vi.fn((_on: boolean) => Promise.resolve<string | null>(null)),
|
|
|
locked = false,
|
|
|
) {
|
|
|
const store = createSnapshotStore<{ value: PlanProjection | undefined }>({ value: plan })
|
|
|
const useProjection = (_key: string, selector?: (v: unknown) => unknown) =>
|
|
|
bindSnapshotSelector(store)(s => (selector ?? (v => v))(s.value))
|
|
|
- const props = { useProjection, locked, exitPlanMode } as unknown as PlanChipProps
|
|
|
+ const props = { useProjection, locked, setPlanMode } as unknown as PlanChipProps
|
|
|
const view = render(<PlanChip {...props} />)
|
|
|
- return { store, exitPlanMode, view }
|
|
|
+ return { store, setPlanMode, view }
|
|
|
}
|
|
|
|
|
|
-const chip = () => screen.getByRole('button', { name: 'Plan mode on, press to turn off' })
|
|
|
+const onChip = () => screen.getByRole('button', { name: 'Plan mode on, press to turn off' })
|
|
|
+const offChip = () => screen.getByRole('button', { name: 'Plan mode off, press to turn on' })
|
|
|
|
|
|
describe('PlanChip', () => {
|
|
|
- it('renders nothing for absent capability or the default mode', () => {
|
|
|
+ it('renders nothing while the capability is absent', () => {
|
|
|
const absent = setup(undefined)
|
|
|
expect(absent.view.container.innerHTML).toBe('')
|
|
|
- cleanup()
|
|
|
- const inactive = setup({ active: false, pending: false })
|
|
|
- expect(inactive.view.container.innerHTML).toBe('')
|
|
|
- cleanup()
|
|
|
- // Active with a pending exit: the target is default — chip already gone.
|
|
|
- const leaving = setup({ active: true, pending: true })
|
|
|
- expect(leaving.view.container.innerHTML).toBe('')
|
|
|
})
|
|
|
|
|
|
- it('renders while the effective target is plan mode, including the pending entry window', () => {
|
|
|
+ it('reflects the effective target as the pressed state, folding pending', () => {
|
|
|
+ setup({ active: false, pending: false })
|
|
|
+ expect(offChip().getAttribute('aria-pressed')).toBe('false')
|
|
|
+ cleanup()
|
|
|
setup({ active: true, pending: false })
|
|
|
- expect(chip()).toBeTruthy()
|
|
|
+ expect(onChip().getAttribute('aria-pressed')).toBe('true')
|
|
|
cleanup()
|
|
|
// /plan just ran (command/run folded, plan/mode not yet): target is plan.
|
|
|
setup({ active: false, pending: true })
|
|
|
- expect(chip()).toBeTruthy()
|
|
|
+ expect(onChip().getAttribute('aria-pressed')).toBe('true')
|
|
|
+ cleanup()
|
|
|
+ // Active with a pending exit: the target is default — already unpressed.
|
|
|
+ setup({ active: true, pending: true })
|
|
|
+ expect(offChip().getAttribute('aria-pressed')).toBe('false')
|
|
|
})
|
|
|
|
|
|
- it('the chip executes /plan off once and follows the projection down', async () => {
|
|
|
+ it('unpressed chip executes /plan (on) once and follows the projection up', async () => {
|
|
|
let resolve!: (value: string | null) => void
|
|
|
- const exitPlanMode = vi.fn(() => new Promise<string | null>((done) => { resolve = done }))
|
|
|
- const { store } = setup({ active: true, pending: false }, exitPlanMode)
|
|
|
- fireEvent.click(chip())
|
|
|
- expect(exitPlanMode).toHaveBeenCalledTimes(1)
|
|
|
+ const setPlanMode = vi.fn((_on: boolean) => new Promise<string | null>((done) => { resolve = done }))
|
|
|
+ const { store } = setup({ active: false, pending: false }, setPlanMode)
|
|
|
+ fireEvent.click(offChip())
|
|
|
+ expect(setPlanMode).toHaveBeenCalledTimes(1)
|
|
|
+ expect(setPlanMode).toHaveBeenLastCalledWith(true)
|
|
|
// Busy while its own call is in flight.
|
|
|
- fireEvent.click(chip())
|
|
|
- expect(exitPlanMode).toHaveBeenCalledTimes(1)
|
|
|
+ fireEvent.click(offChip())
|
|
|
+ expect(setPlanMode).toHaveBeenCalledTimes(1)
|
|
|
resolve(null)
|
|
|
- // The off command's run record folds: target flips, the chip unmounts.
|
|
|
+ // The command's run record folds: target flips, the chip presses.
|
|
|
+ store.set({ value: { active: false, pending: true } })
|
|
|
+ await waitFor(() => {
|
|
|
+ expect(onChip().getAttribute('aria-pressed')).toBe('true')
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it('pressed chip executes /plan off and follows the projection down', async () => {
|
|
|
+ const setPlanMode = vi.fn((_on: boolean) => Promise.resolve<string | null>(null))
|
|
|
+ const { store } = setup({ active: true, pending: false }, setPlanMode)
|
|
|
+ fireEvent.click(onChip())
|
|
|
+ expect(setPlanMode).toHaveBeenLastCalledWith(false)
|
|
|
store.set({ value: { active: true, pending: true } })
|
|
|
await waitFor(() => {
|
|
|
- expect(screen.queryByRole('button', { name: 'Plan mode on, press to turn off' })).toBeNull()
|
|
|
+ expect(offChip().getAttribute('aria-pressed')).toBe('false')
|
|
|
})
|
|
|
})
|
|
|
|
|
|
it('disables under the locked owner prop', () => {
|
|
|
setup({ active: true, pending: false }, vi.fn(), true)
|
|
|
- expect((chip() as HTMLButtonElement).disabled).toBe(true)
|
|
|
+ expect((onChip() as HTMLButtonElement).disabled).toBe(true)
|
|
|
})
|
|
|
|
|
|
- it('surfaces admission and transport failures while staying visible', async () => {
|
|
|
- const exitPlanMode = vi.fn()
|
|
|
+ it('surfaces direction-specific admission and transport failures while staying visible', async () => {
|
|
|
+ const exitFailing = vi.fn()
|
|
|
.mockResolvedValueOnce('host said no')
|
|
|
.mockRejectedValueOnce(new Error('network down'))
|
|
|
.mockRejectedValueOnce('socket closed')
|
|
|
- setup({ active: true, pending: false }, exitPlanMode)
|
|
|
- fireEvent.click(chip())
|
|
|
+ setup({ active: true, pending: false }, exitFailing)
|
|
|
+ fireEvent.click(onChip())
|
|
|
expect((await screen.findByText('退出 plan mode 失败')).getAttribute('title')).toBe('host said no')
|
|
|
- expect(chip()).toBeTruthy()
|
|
|
+ expect(onChip()).toBeTruthy()
|
|
|
|
|
|
- fireEvent.click(chip())
|
|
|
+ fireEvent.click(onChip())
|
|
|
expect(await screen.findByTitle('network down')).toBeTruthy()
|
|
|
|
|
|
- fireEvent.click(chip())
|
|
|
+ fireEvent.click(onChip())
|
|
|
expect(await screen.findByTitle('socket closed')).toBeTruthy()
|
|
|
+ cleanup()
|
|
|
+
|
|
|
+ const enterFailing = vi.fn().mockResolvedValueOnce('agent busy')
|
|
|
+ setup({ active: false, pending: false }, enterFailing)
|
|
|
+ fireEvent.click(offChip())
|
|
|
+ expect((await screen.findByText('进入 plan mode 失败')).getAttribute('title')).toBe('agent busy')
|
|
|
+ expect(offChip()).toBeTruthy()
|
|
|
})
|
|
|
|
|
|
it('ignores in-flight fulfillment and rejection after unmount', () => {
|
|
|
@@ -97,14 +118,14 @@ describe('PlanChip', () => {
|
|
|
{ active: true, pending: false },
|
|
|
vi.fn(() => new Promise<string | null>((done) => { resolve = done })),
|
|
|
)
|
|
|
- fireEvent.click(chip())
|
|
|
+ fireEvent.click(onChip())
|
|
|
successful.view.unmount()
|
|
|
expect(() => { resolve(null) }).not.toThrow()
|
|
|
|
|
|
let reject!: (reason: unknown) => void
|
|
|
- const exitPlanMode = vi.fn(() => new Promise<string | null>((_done, fail) => { reject = fail }))
|
|
|
- const { view } = setup({ active: true, pending: false }, exitPlanMode)
|
|
|
- fireEvent.click(chip())
|
|
|
+ const setPlanMode = vi.fn(() => new Promise<string | null>((_done, fail) => { reject = fail }))
|
|
|
+ const { view } = setup({ active: true, pending: false }, setPlanMode)
|
|
|
+ fireEvent.click(onChip())
|
|
|
view.unmount()
|
|
|
expect(() => { reject(new Error('late')) }).not.toThrow()
|
|
|
})
|