mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
801 lines
29 KiB
TypeScript
801 lines
29 KiB
TypeScript
import { fireEvent, render as renderRaw, screen, waitFor, within } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import type { ReactElement } from 'react'
|
|
import type { FormInfo } from '@opencode-manager/shared/opencode'
|
|
import { KeyboardShortcutsProvider, useShortcutActions } from '@/contexts/KeyboardShortcutsContext'
|
|
import { FormPrompt } from './FormPrompt'
|
|
|
|
vi.mock('@/hooks/useSettings', () => ({
|
|
useSettings: () => ({ preferences: undefined }),
|
|
}))
|
|
|
|
function render(ui: ReactElement) {
|
|
return renderRaw(ui, { wrapper: KeyboardShortcutsProvider })
|
|
}
|
|
|
|
function AbortLayer({ onAbort }: { onAbort: () => void }) {
|
|
useShortcutActions({ abort: onAbort })
|
|
return null
|
|
}
|
|
|
|
function getHeaderDismissButton() {
|
|
return screen.getAllByRole('button', { name: 'Dismiss' })[0]
|
|
}
|
|
|
|
const selectForm: FormInfo = {
|
|
id: 'form-1',
|
|
sessionID: 'session-1',
|
|
title: 'Deploy to prod?',
|
|
fields: [
|
|
{
|
|
key: 'q0',
|
|
title: 'Environment',
|
|
type: 'string',
|
|
options: [
|
|
{ value: 'staging', label: 'Staging' },
|
|
{ value: 'production', label: 'Production' },
|
|
],
|
|
},
|
|
],
|
|
}
|
|
|
|
describe('FormPrompt', () => {
|
|
beforeEach(() => {
|
|
sessionStorage.clear()
|
|
})
|
|
|
|
afterEach(() => {
|
|
sessionStorage.clear()
|
|
})
|
|
|
|
it('submits the selected option keyed by field key', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
render(<FormPrompt form={selectForm} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
await userEvent.click(screen.getByText('Production'))
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
|
|
await waitFor(() => expect(onReply).toHaveBeenCalledWith('form-1', { q0: 'production' }))
|
|
})
|
|
|
|
it('collects multiselect, boolean, and number answers', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const form: FormInfo = {
|
|
id: 'form-2',
|
|
sessionID: 'session-1',
|
|
title: 'Configure',
|
|
fields: [
|
|
{
|
|
key: 'features',
|
|
type: 'multiselect',
|
|
options: [
|
|
{ value: 'a', label: 'Alpha' },
|
|
{ value: 'b', label: 'Beta' },
|
|
],
|
|
},
|
|
{ key: 'enabled', type: 'boolean' },
|
|
{ key: 'count', type: 'integer', minimum: 1 },
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
await userEvent.click(screen.getByText('Alpha'))
|
|
await userEvent.click(screen.getByText('No'))
|
|
const numberInput = screen.getByRole('spinbutton')
|
|
await userEvent.clear(numberInput)
|
|
await userEvent.type(numberInput, '3')
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
|
|
await waitFor(() =>
|
|
expect(onReply).toHaveBeenCalledWith('form-2', { features: ['a'], enabled: false, count: 3 }),
|
|
)
|
|
})
|
|
|
|
it('cancels the form', async () => {
|
|
const onCancel = vi.fn().mockResolvedValue(undefined)
|
|
render(<FormPrompt form={selectForm} onReply={vi.fn()} onCancel={onCancel} />)
|
|
|
|
await userEvent.click(getHeaderDismissButton())
|
|
|
|
await waitFor(() => expect(onCancel).toHaveBeenCalledWith('form-1'))
|
|
})
|
|
|
|
describe('field defaults', () => {
|
|
it('initializes a required numeric default and allows submission as displayed', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const form: FormInfo = {
|
|
id: 'form-numeric-default',
|
|
sessionID: 'session-1',
|
|
title: 'Configure',
|
|
fields: [{ key: 'count', title: 'Count', type: 'integer', required: true, default: 3 }],
|
|
}
|
|
render(<FormPrompt form={form} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
const input = screen.getByLabelText(/Count/) as HTMLInputElement
|
|
expect(input.value).toBe('3')
|
|
const submit = screen.getByRole('button', { name: 'Submit' })
|
|
expect(submit).toBeEnabled()
|
|
|
|
await userEvent.click(submit)
|
|
await waitFor(() => expect(onReply).toHaveBeenCalledWith('form-numeric-default', { count: 3 }))
|
|
})
|
|
|
|
it('keeps a cleared numeric default cleared and re-enables gating', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const form: FormInfo = {
|
|
id: 'form-numeric-clear',
|
|
sessionID: 'session-1',
|
|
title: 'Configure',
|
|
fields: [{ key: 'count', title: 'Count', type: 'integer', required: true, default: 3 }],
|
|
}
|
|
render(<FormPrompt form={form} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
const input = screen.getByLabelText(/Count/) as HTMLInputElement
|
|
const submit = screen.getByRole('button', { name: 'Submit' })
|
|
|
|
await userEvent.clear(input)
|
|
expect(input.value).toBe('')
|
|
expect(submit).toBeDisabled()
|
|
|
|
await userEvent.type(input, '5')
|
|
expect(submit).toBeEnabled()
|
|
await userEvent.click(submit)
|
|
await waitFor(() => expect(onReply).toHaveBeenCalledWith('form-numeric-clear', { count: 5 }))
|
|
})
|
|
|
|
it('initializes string, boolean, and multiselect defaults', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const form: FormInfo = {
|
|
id: 'form-mixed-defaults',
|
|
sessionID: 'session-1',
|
|
title: 'Configure',
|
|
fields: [
|
|
{ key: 'name', title: 'Name', type: 'string', required: true, default: 'Ada' },
|
|
{ key: 'enabled', title: 'Enabled', type: 'boolean', required: true, default: false },
|
|
{
|
|
key: 'features',
|
|
title: 'Features',
|
|
type: 'multiselect',
|
|
required: true,
|
|
options: [
|
|
{ value: 'a', label: 'Alpha' },
|
|
{ value: 'b', label: 'Beta' },
|
|
],
|
|
default: ['a'],
|
|
},
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
expect((screen.getByLabelText(/Name/) as HTMLTextAreaElement).value).toBe('Ada')
|
|
expect(screen.getByRole('button', { name: 'No' })).toHaveAttribute('aria-pressed', 'true')
|
|
expect(screen.getByRole('button', { name: 'Alpha' })).toHaveAttribute('aria-pressed', 'true')
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
await waitFor(() =>
|
|
expect(onReply).toHaveBeenCalledWith('form-mixed-defaults', {
|
|
name: 'Ada',
|
|
enabled: false,
|
|
features: ['a'],
|
|
}),
|
|
)
|
|
})
|
|
|
|
it('keeps a predefined string default as an explicit selection', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const form: FormInfo = {
|
|
id: 'form-custom-string-default-option',
|
|
sessionID: 'session-1',
|
|
title: 'Choose',
|
|
fields: [
|
|
{
|
|
key: 'answer',
|
|
title: 'Answer',
|
|
type: 'string',
|
|
required: true,
|
|
options: [{ value: 'Yes', label: 'Yes' }],
|
|
custom: true,
|
|
default: 'Yes',
|
|
},
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
expect(screen.getByRole('button', { name: 'Yes' })).toHaveAttribute('aria-pressed', 'true')
|
|
expect(screen.queryByRole('button', { name: 'Other...' })).not.toHaveAttribute('aria-pressed', 'true')
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
await waitFor(() =>
|
|
expect(onReply).toHaveBeenCalledWith('form-custom-string-default-option', { answer: 'Yes' }),
|
|
)
|
|
})
|
|
|
|
it('shows a custom string default and preserves it when Other opens', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const form: FormInfo = {
|
|
id: 'form-custom-string-default',
|
|
sessionID: 'session-1',
|
|
title: 'Choose',
|
|
fields: [
|
|
{
|
|
key: 'answer',
|
|
title: 'Answer',
|
|
type: 'string',
|
|
required: true,
|
|
options: [{ value: 'Yes', label: 'Yes' }],
|
|
custom: true,
|
|
default: 'Yesterday',
|
|
},
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
expect(screen.getByText('Yesterday')).toBeInTheDocument()
|
|
expect(screen.getByRole('button', { name: 'Submit' })).toBeEnabled()
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Other...' }))
|
|
const customInput = screen.getByLabelText('Other answer for Answer') as HTMLTextAreaElement
|
|
expect(customInput.value).toBe('Yesterday')
|
|
|
|
await userEvent.clear(customInput)
|
|
await userEvent.type(customInput, 'Tomorrow')
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
await waitFor(() =>
|
|
expect(onReply).toHaveBeenCalledWith('form-custom-string-default', { answer: 'Tomorrow' }),
|
|
)
|
|
})
|
|
|
|
it('shows custom multiselect defaults, removes them, and appends Other without stale entries', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const form: FormInfo = {
|
|
id: 'form-custom-multiselect-default',
|
|
sessionID: 'session-1',
|
|
title: 'Choose',
|
|
fields: [
|
|
{
|
|
key: 'features',
|
|
title: 'Features',
|
|
type: 'multiselect',
|
|
required: true,
|
|
options: [
|
|
{ value: 'Yes', label: 'Yes' },
|
|
{ value: 'No', label: 'No' },
|
|
],
|
|
custom: true,
|
|
default: ['Yes', 'Yesterday', 'Tomorrow'],
|
|
},
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
expect(screen.getByRole('button', { name: 'Yes' })).toHaveAttribute('aria-pressed', 'true')
|
|
expect(screen.getByRole('button', { name: 'Yesterday' })).toBeInTheDocument()
|
|
expect(screen.getByRole('button', { name: 'Tomorrow' })).toBeInTheDocument()
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Yesterday' }))
|
|
expect(screen.queryByRole('button', { name: 'Yesterday' })).not.toBeInTheDocument()
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Other...' }))
|
|
const customInput = screen.getByLabelText('Other answer for Features')
|
|
await userEvent.type(customInput, 'Later')
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
await waitFor(() =>
|
|
expect(onReply).toHaveBeenCalledWith('form-custom-multiselect-default', {
|
|
features: ['Yes', 'Tomorrow', 'Later'],
|
|
}),
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('custom answers', () => {
|
|
it('submits typed custom text for a string field without pressing Enter', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const form: FormInfo = {
|
|
id: 'form-custom-string',
|
|
sessionID: 'session-1',
|
|
title: 'Choose',
|
|
fields: [
|
|
{
|
|
key: 'environment',
|
|
title: 'Environment',
|
|
type: 'string',
|
|
required: true,
|
|
options: [{ value: 'staging', label: 'Staging' }],
|
|
custom: true,
|
|
},
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Other...' }))
|
|
await userEvent.type(screen.getByLabelText('Other answer for Environment'), 'custom-value')
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
|
|
await waitFor(() =>
|
|
expect(onReply).toHaveBeenCalledWith('form-custom-string', { environment: 'custom-value' }),
|
|
)
|
|
})
|
|
|
|
it('submits a multiselect custom answer alongside predefined selections without Enter', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const form: FormInfo = {
|
|
id: 'form-custom-multiselect',
|
|
sessionID: 'session-1',
|
|
title: 'Choose',
|
|
fields: [
|
|
{
|
|
key: 'features',
|
|
title: 'Features',
|
|
type: 'multiselect',
|
|
required: true,
|
|
options: [
|
|
{ value: 'a', label: 'Alpha' },
|
|
{ value: 'b', label: 'Beta' },
|
|
],
|
|
custom: true,
|
|
},
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Alpha' }))
|
|
await userEvent.click(screen.getByRole('button', { name: 'Other...' }))
|
|
const customInput = screen.getByLabelText('Other answer for Features')
|
|
await userEvent.type(customInput, 'gamma')
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
|
|
await waitFor(() =>
|
|
expect(onReply).toHaveBeenCalledWith('form-custom-multiselect', { features: ['a', 'gamma'] }),
|
|
)
|
|
})
|
|
|
|
it('replaces an edited multiselect custom value instead of appending stale entries', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const form: FormInfo = {
|
|
id: 'form-custom-edit',
|
|
sessionID: 'session-1',
|
|
title: 'Choose',
|
|
fields: [
|
|
{
|
|
key: 'features',
|
|
title: 'Features',
|
|
type: 'multiselect',
|
|
required: true,
|
|
options: [{ value: 'a', label: 'Alpha' }],
|
|
custom: true,
|
|
},
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Alpha' }))
|
|
await userEvent.click(screen.getByRole('button', { name: 'Other...' }))
|
|
const customInput = screen.getByLabelText('Other answer for Features')
|
|
await userEvent.type(customInput, 'gamma')
|
|
await userEvent.clear(customInput)
|
|
await userEvent.type(customInput, 'delta')
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
|
|
await waitFor(() =>
|
|
expect(onReply).toHaveBeenCalledWith('form-custom-edit', { features: ['a', 'delta'] }),
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('custom option prefix collisions', () => {
|
|
it('keeps custom text visible and submits it when a predefined option is a prefix of the answer', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const form: FormInfo = {
|
|
id: 'form-custom-prefix-string',
|
|
sessionID: 'session-1',
|
|
title: 'Choose',
|
|
fields: [
|
|
{
|
|
key: 'answer',
|
|
title: 'Answer',
|
|
type: 'string',
|
|
required: true,
|
|
options: [{ value: 'Yes', label: 'Yes' }],
|
|
custom: true,
|
|
},
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Other...' }))
|
|
const customInput = screen.getByLabelText('Other answer for Answer')
|
|
await userEvent.type(customInput, 'Yesterday')
|
|
|
|
expect((customInput as HTMLTextAreaElement).value).toBe('Yesterday')
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
|
|
await waitFor(() =>
|
|
expect(onReply).toHaveBeenCalledWith('form-custom-prefix-string', { answer: 'Yesterday' }),
|
|
)
|
|
})
|
|
|
|
it('does not turn a custom value colliding with a predefined option into a phantom selection', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const form: FormInfo = {
|
|
id: 'form-custom-prefix-multiselect',
|
|
sessionID: 'session-1',
|
|
title: 'Choose',
|
|
fields: [
|
|
{
|
|
key: 'features',
|
|
title: 'Features',
|
|
type: 'multiselect',
|
|
required: true,
|
|
options: [
|
|
{ value: 'Yes', label: 'Yes' },
|
|
{ value: 'No', label: 'No' },
|
|
],
|
|
custom: true,
|
|
},
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'No' }))
|
|
await userEvent.click(screen.getByRole('button', { name: 'Other...' }))
|
|
const customInput = screen.getByLabelText('Other answer for Features')
|
|
await userEvent.type(customInput, 'Yesterday')
|
|
|
|
expect((customInput as HTMLTextAreaElement).value).toBe('Yesterday')
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
|
|
await waitFor(() =>
|
|
expect(onReply).toHaveBeenCalledWith('form-custom-prefix-multiselect', {
|
|
features: ['No', 'Yesterday'],
|
|
}),
|
|
)
|
|
})
|
|
|
|
it('preserves custom text across collapse and reopen, replacing it on edit', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const form: FormInfo = {
|
|
id: 'form-custom-reopen',
|
|
sessionID: 'session-1',
|
|
title: 'Choose',
|
|
fields: [
|
|
{
|
|
key: 'answer',
|
|
title: 'Answer',
|
|
type: 'string',
|
|
required: true,
|
|
options: [{ value: 'Yes', label: 'Yes' }],
|
|
custom: true,
|
|
},
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Other...' }))
|
|
await userEvent.type(screen.getByLabelText('Other answer for Answer'), 'Yesterday')
|
|
await userEvent.keyboard('{Enter}')
|
|
|
|
expect(screen.getByText('Yesterday')).toBeInTheDocument()
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Other...' }))
|
|
const reopened = screen.getByLabelText('Other answer for Answer') as HTMLTextAreaElement
|
|
expect(reopened.value).toBe('Yesterday')
|
|
|
|
await userEvent.clear(reopened)
|
|
await userEvent.type(reopened, 'Tomorrow')
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
|
|
await waitFor(() =>
|
|
expect(onReply).toHaveBeenCalledWith('form-custom-reopen', { answer: 'Tomorrow' }),
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('accessibility', () => {
|
|
it('exposes labels, group names, selected state, and named dismiss controls', async () => {
|
|
const form: FormInfo = {
|
|
id: 'form-a11y',
|
|
sessionID: 'session-1',
|
|
title: 'Configure',
|
|
fields: [
|
|
{ key: 'q0', title: 'Count', type: 'integer', required: true, default: 3 },
|
|
{ key: 'q1', title: 'Notes', type: 'string' },
|
|
{ key: 'q2', title: 'Enabled', type: 'boolean' },
|
|
{
|
|
key: 'q3',
|
|
title: 'Features',
|
|
type: 'multiselect',
|
|
options: [{ value: 'a', label: 'Alpha' }],
|
|
},
|
|
{
|
|
key: 'q4',
|
|
title: 'Extras',
|
|
type: 'multiselect',
|
|
options: [{ value: 'a', label: 'Alpha' }],
|
|
},
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={vi.fn()} onCancel={vi.fn()} />)
|
|
|
|
expect(screen.getByLabelText(/Count/)).toHaveAttribute('type', 'number')
|
|
expect(screen.getByLabelText(/Notes/).tagName).toBe('TEXTAREA')
|
|
expect(screen.getByRole('group', { name: /Enabled/ })).toBeInTheDocument()
|
|
const featuresGroup = screen.getByRole('group', { name: /Features/ })
|
|
const extrasGroup = screen.getByRole('group', { name: /Extras/ })
|
|
expect(featuresGroup).toBeInTheDocument()
|
|
expect(extrasGroup).toBeInTheDocument()
|
|
|
|
const featuresAlpha = within(featuresGroup).getByRole('button', { name: 'Alpha' })
|
|
expect(featuresAlpha).toHaveAttribute('aria-pressed', 'false')
|
|
await userEvent.click(featuresAlpha)
|
|
expect(featuresAlpha).toHaveAttribute('aria-pressed', 'true')
|
|
|
|
expect(screen.getByRole('button', { name: 'Dismiss form' })).toBeInTheDocument()
|
|
expect(screen.getAllByRole('button', { name: 'Dismiss' })).toHaveLength(2)
|
|
})
|
|
})
|
|
|
|
describe('mobile sheet controls', () => {
|
|
it('minimizes from the grab handle', async () => {
|
|
const onMinimize = vi.fn()
|
|
render(<FormPrompt form={selectForm} onReply={vi.fn()} onCancel={vi.fn()} onMinimize={onMinimize} />)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Drag to minimize' }))
|
|
|
|
expect(onMinimize).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('minimizes from the mobile header control', async () => {
|
|
const onMinimize = vi.fn()
|
|
render(<FormPrompt form={selectForm} onReply={vi.fn()} onCancel={vi.fn()} onMinimize={onMinimize} />)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Minimize form' }))
|
|
|
|
expect(onMinimize).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('dismisses from the mobile header link', async () => {
|
|
const onCancel = vi.fn().mockResolvedValue(undefined)
|
|
render(<FormPrompt form={selectForm} onReply={vi.fn()} onCancel={onCancel} />)
|
|
|
|
await userEvent.click(getHeaderDismissButton())
|
|
|
|
await waitFor(() => expect(onCancel).toHaveBeenCalledWith('form-1'))
|
|
})
|
|
})
|
|
|
|
describe('side question button', () => {
|
|
it('opens the side question from the footer button', async () => {
|
|
const onAskSideQuestion = vi.fn()
|
|
render(
|
|
<FormPrompt
|
|
form={selectForm}
|
|
onReply={vi.fn()}
|
|
onCancel={vi.fn()}
|
|
onAskSideQuestion={onAskSideQuestion}
|
|
/>
|
|
)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Ask a side question' }))
|
|
|
|
expect(onAskSideQuestion).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('omits the side question button when no handler is provided', () => {
|
|
render(<FormPrompt form={selectForm} onReply={vi.fn()} onCancel={vi.fn()} />)
|
|
|
|
expect(screen.queryByRole('button', { name: 'Ask a side question' })).not.toBeInTheDocument()
|
|
})
|
|
})
|
|
|
|
describe('conditional and external fields', () => {
|
|
const conditionalForm: FormInfo = {
|
|
id: 'form-conditional',
|
|
sessionID: 'session-1',
|
|
title: 'Configure',
|
|
fields: [
|
|
{
|
|
key: 'deploymentType',
|
|
title: 'Deployment',
|
|
type: 'string',
|
|
options: [
|
|
{ value: 'github.com', label: 'GitHub.com' },
|
|
{ value: 'enterprise', label: 'GitHub Enterprise' },
|
|
],
|
|
default: 'github.com',
|
|
},
|
|
{
|
|
key: 'enterpriseUrl',
|
|
title: 'Enterprise URL',
|
|
type: 'string',
|
|
required: true,
|
|
when: [{ key: 'deploymentType', op: 'eq', value: 'enterprise' }],
|
|
},
|
|
],
|
|
}
|
|
|
|
it('submits while a conditional required field stays hidden', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
render(<FormPrompt form={conditionalForm} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
expect(screen.queryByLabelText(/Enterprise URL/)).not.toBeInTheDocument()
|
|
const submit = screen.getByRole('button', { name: 'Submit' })
|
|
expect(submit).toBeEnabled()
|
|
|
|
await userEvent.click(submit)
|
|
await waitFor(() =>
|
|
expect(onReply).toHaveBeenCalledWith('form-conditional', { deploymentType: 'github.com' }),
|
|
)
|
|
})
|
|
|
|
it('reveals and requires the conditional field once its condition matches', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
render(<FormPrompt form={conditionalForm} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
await userEvent.click(screen.getByText('GitHub Enterprise'))
|
|
|
|
const submit = screen.getByRole('button', { name: 'Submit' })
|
|
expect(submit).toBeDisabled()
|
|
|
|
await userEvent.type(screen.getByLabelText(/Enterprise URL/), 'company.ghe.com')
|
|
expect(submit).toBeEnabled()
|
|
|
|
await userEvent.click(submit)
|
|
await waitFor(() =>
|
|
expect(onReply).toHaveBeenCalledWith('form-conditional', {
|
|
deploymentType: 'enterprise',
|
|
enterpriseUrl: 'company.ghe.com',
|
|
}),
|
|
)
|
|
})
|
|
|
|
it('renders an external field as a link card', () => {
|
|
const form: FormInfo = {
|
|
id: 'form-external',
|
|
sessionID: 'session-1',
|
|
title: 'Approve',
|
|
fields: [
|
|
{
|
|
key: 'approve',
|
|
type: 'external',
|
|
url: 'https://example.com/approve',
|
|
title: 'Approve access',
|
|
description: 'Do it in the provider dashboard',
|
|
},
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={vi.fn()} onCancel={vi.fn()} />)
|
|
|
|
const link = screen.getByRole('link', { name: /Approve access/ })
|
|
expect(link).toHaveAttribute('href', 'https://example.com/approve')
|
|
expect(link).toHaveAttribute('target', '_blank')
|
|
expect(link).toHaveAttribute('rel', 'noopener noreferrer')
|
|
expect(screen.getByText('Do it in the provider dashboard')).toBeInTheDocument()
|
|
})
|
|
|
|
it('does not render an external field with a non-http url', () => {
|
|
const form: FormInfo = {
|
|
id: 'form-external-unsafe',
|
|
sessionID: 'session-1',
|
|
title: 'Approve',
|
|
fields: [
|
|
{
|
|
key: 'approve',
|
|
type: 'external',
|
|
url: 'javascript:alert(1)',
|
|
title: 'Approve access',
|
|
},
|
|
],
|
|
}
|
|
render(<FormPrompt form={form} onReply={vi.fn()} onCancel={vi.fn()} />)
|
|
|
|
expect(screen.queryByRole('link', { name: /Approve access/ })).not.toBeInTheDocument()
|
|
})
|
|
})
|
|
|
|
describe('draft persistence', () => {
|
|
const draftKey = 'opencode-manager:form-draft:form-1'
|
|
|
|
it('restores answers after unmount and remount', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const first = render(<FormPrompt form={selectForm} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Production' }))
|
|
first.unmount()
|
|
|
|
render(<FormPrompt form={selectForm} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
expect(screen.getByRole('button', { name: 'Production' })).toHaveAttribute('aria-pressed', 'true')
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
await waitFor(() => expect(onReply).toHaveBeenCalledWith('form-1', { q0: 'production' }))
|
|
})
|
|
|
|
it('clears the draft after a successful submit', async () => {
|
|
const onReply = vi.fn().mockResolvedValue(undefined)
|
|
const first = render(<FormPrompt form={selectForm} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Production' }))
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }))
|
|
await waitFor(() => expect(sessionStorage.getItem(draftKey)).toBeNull())
|
|
first.unmount()
|
|
|
|
render(<FormPrompt form={selectForm} onReply={onReply} onCancel={vi.fn()} />)
|
|
|
|
expect(screen.getByRole('button', { name: 'Production' })).toHaveAttribute('aria-pressed', 'false')
|
|
})
|
|
|
|
it('clears the draft after a successful dismiss', async () => {
|
|
const onCancel = vi.fn().mockResolvedValue(undefined)
|
|
render(<FormPrompt form={selectForm} onReply={vi.fn()} onCancel={onCancel} />)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Production' }))
|
|
await userEvent.click(getHeaderDismissButton())
|
|
|
|
await waitFor(() => expect(onCancel).toHaveBeenCalledWith('form-1'))
|
|
await waitFor(() => expect(sessionStorage.getItem(draftKey)).toBeNull())
|
|
})
|
|
|
|
it('falls back to defaults when the stored draft is corrupt', () => {
|
|
const form: FormInfo = {
|
|
id: 'form-corrupt',
|
|
sessionID: 'session-1',
|
|
title: 'Choose',
|
|
fields: [
|
|
{
|
|
key: 'q0',
|
|
title: 'Environment',
|
|
type: 'string',
|
|
options: [
|
|
{ value: 'staging', label: 'Staging' },
|
|
{ value: 'production', label: 'Production' },
|
|
],
|
|
default: 'staging',
|
|
},
|
|
],
|
|
}
|
|
sessionStorage.setItem('opencode-manager:form-draft:form-corrupt', '{not json')
|
|
|
|
render(<FormPrompt form={form} onReply={vi.fn()} onCancel={vi.fn()} />)
|
|
|
|
expect(screen.getByRole('button', { name: 'Staging' })).toHaveAttribute('aria-pressed', 'true')
|
|
expect(screen.getByRole('button', { name: 'Production' })).toHaveAttribute('aria-pressed', 'false')
|
|
})
|
|
})
|
|
|
|
describe('keyboard shortcuts', () => {
|
|
it('minimizes once on Escape while expanded without triggering a registered session abort', async () => {
|
|
const sessionAbort = vi.fn()
|
|
const onMinimize = vi.fn()
|
|
render(
|
|
<>
|
|
<AbortLayer onAbort={sessionAbort} />
|
|
<FormPrompt form={selectForm} onReply={vi.fn()} onCancel={vi.fn()} onMinimize={onMinimize} />
|
|
</>
|
|
)
|
|
|
|
await userEvent.keyboard('{Escape}')
|
|
|
|
expect(onMinimize).toHaveBeenCalledTimes(1)
|
|
expect(sessionAbort).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('ignores Escape from inside a dialog but minimizes from the document body', () => {
|
|
const onMinimize = vi.fn()
|
|
render(
|
|
<>
|
|
<div role="dialog" data-state="open">
|
|
<button type="button">Dialog action</button>
|
|
</div>
|
|
<FormPrompt form={selectForm} onReply={vi.fn()} onCancel={vi.fn()} onMinimize={onMinimize} />
|
|
</>
|
|
)
|
|
|
|
fireEvent.keyDown(screen.getByRole('button', { name: 'Dialog action' }), { key: 'Escape' })
|
|
expect(onMinimize).not.toHaveBeenCalled()
|
|
|
|
fireEvent.keyDown(document.body, { key: 'Escape' })
|
|
expect(onMinimize).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|
|
})
|