From 87e55372c023da4daf263a53c5212523b33eed28 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Tue, 6 Oct 2026 22:29:45 -0400 Subject: [PATCH] section 4: Phase 4: Prompt and multi-select dialog helpers --- ocm-cli/src/tui-dialogs.ts | 62 +++++++++++- ocm-cli/test/tui-dialogs.test.ts | 167 ++++++++++++++++++++++++++++++- 2 files changed, 225 insertions(+), 4 deletions(-) diff --git a/ocm-cli/src/tui-dialogs.ts b/ocm-cli/src/tui-dialogs.ts index 723efced..aa03813a 100644 --- a/ocm-cli/src/tui-dialogs.ts +++ b/ocm-cli/src/tui-dialogs.ts @@ -1,4 +1,6 @@ -import type { Context } from '@opencode/plugin/tui/context' +import type { Context, DialogPromptOptions } from '@opencode/plugin/tui/context' + +const DONE = -1 export function confirmDialog(context: Context, props: { title: string; message: string }): Promise { return context.ui.dialog.confirm(props).then((value) => value === true) @@ -11,3 +13,61 @@ export function selectDialog( ): Promise { return context.ui.dialog.select({ title, options }) } + +export function promptDialog(context: Context, props: DialogPromptOptions): Promise { + return context.ui.dialog.prompt(props).then((value) => { + const trimmed = value?.trim() + return trimmed ? trimmed : undefined + }) +} + +export async function multiSelectDialog( + context: Context, + props: { + title: string + options: { title: string; description?: string; value: Value }[] + min: number + max: number + }, +): Promise { + const selected = new Set() + let current: number | undefined + + for (;;) { + const options = [ + { + title: `Done (${selected.size} selected)`, + value: DONE, + disabled: selected.size < props.min, + }, + ...props.options.map((option, index) => ({ + title: `${selected.has(index) ? '[x]' : '[ ]'} ${option.title}`, + description: option.description, + value: index, + disabled: !selected.has(index) && selected.size >= props.max, + })), + ] + + const choice = await context.ui.dialog.select({ title: props.title, options, current }) + + if (choice === undefined) return undefined + if (choice === DONE) { + return props.options.filter((_, index) => selected.has(index)).map((option) => option.value) + } + + current = choice + if (selected.has(choice)) { + selected.delete(choice) + } else { + selected.add(choice) + } + } +} + +export function slashArgument(input: string | undefined, name: string): string { + const trimmed = (input ?? '').trim() + const prefix = `/${name}` + if (trimmed === prefix) return '' + if (trimmed.startsWith(`${prefix} `)) return trimmed.slice(prefix.length).trim() + return trimmed +} diff --git a/ocm-cli/test/tui-dialogs.test.ts b/ocm-cli/test/tui-dialogs.test.ts index 87727d2a..2c1648a6 100644 --- a/ocm-cli/test/tui-dialogs.test.ts +++ b/ocm-cli/test/tui-dialogs.test.ts @@ -1,14 +1,15 @@ import { describe, it, expect, vi } from 'vitest' import type { Context } from '@opencode/plugin/tui/context' -import { confirmDialog, selectDialog } from '../src/tui-dialogs.js' +import { confirmDialog, multiSelectDialog, promptDialog, selectDialog, slashArgument } from '../src/tui-dialogs.js' function createFakeContext() { const confirm = vi.fn() const select = vi.fn() + const prompt = vi.fn() const context = { - ui: { dialog: { confirm, select } }, + ui: { dialog: { confirm, select, prompt } }, } as unknown as Context - return { context, confirm, select } + return { context, confirm, select, prompt } } describe('confirmDialog', () => { @@ -55,3 +56,163 @@ describe('selectDialog', () => { expect(await selectDialog(context, 'Pick', [{ title: 'X', value: 42 }])).toBeUndefined() }) }) + +describe('promptDialog', () => { + it('forwards the options and resolves the trimmed value', async () => { + const { context, prompt } = createFakeContext() + prompt.mockResolvedValue(' fix it ') + + const props = { title: 'Goal', description: 'What do you want?', placeholder: 'Describe it', value: 'seed' } + + expect(await promptDialog(context, props)).toBe('fix it') + expect(prompt).toHaveBeenCalledWith(props) + }) + + it('resolves undefined when the dialog is dismissed', async () => { + const { context, prompt } = createFakeContext() + prompt.mockResolvedValue(undefined) + + expect(await promptDialog(context, { title: 'Goal' })).toBeUndefined() + }) + + it('resolves undefined when the value is blank', async () => { + const { context, prompt } = createFakeContext() + prompt.mockResolvedValue(' ') + + expect(await promptDialog(context, { title: 'Goal' })).toBeUndefined() + }) +}) + +describe('multiSelectDialog', () => { + const options = [ + { title: 'A', value: 'a' }, + { title: 'B', value: 'b' }, + { title: 'C', value: 'c' }, + ] + + it('resolves selected values in option order', async () => { + const { context, select } = createFakeContext() + select.mockResolvedValueOnce(0).mockResolvedValueOnce(1).mockResolvedValueOnce(-1) + + expect(await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 })).toEqual(['a', 'b']) + expect(select).toHaveBeenCalledTimes(3) + }) + + it('disables the done entry while fewer than min are selected', async () => { + const { context, select } = createFakeContext() + select.mockResolvedValueOnce(0).mockResolvedValueOnce(-1) + + await multiSelectDialog(context, { title: 'Pick', options, min: 2, max: 3 }) + + expect(select.mock.calls[0][0].options[0]).toEqual({ title: 'Done (0 selected)', value: -1, disabled: true }) + expect(select.mock.calls[1][0].options[0]).toEqual({ title: 'Done (1 selected)', value: -1, disabled: true }) + }) + + it('enables the done entry once min are selected', async () => { + const { context, select } = createFakeContext() + select.mockResolvedValueOnce(0).mockResolvedValueOnce(-1) + + await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 }) + + expect(select.mock.calls[1][0].options[0]).toEqual({ title: 'Done (1 selected)', value: -1, disabled: false }) + }) + + it('disables unselected options once max are selected', async () => { + const { context, select } = createFakeContext() + select.mockResolvedValueOnce(0).mockResolvedValueOnce(-1) + + await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 1 }) + + const second = select.mock.calls[1][0].options + expect(second[1]).toMatchObject({ title: '[x] A', value: 0, disabled: false }) + expect(second[2]).toMatchObject({ title: '[ ] B', value: 1, disabled: true }) + expect(second[3]).toMatchObject({ title: '[ ] C', value: 2, disabled: true }) + }) + + it('marks selected options with an x marker', async () => { + const { context, select } = createFakeContext() + select.mockResolvedValueOnce(1).mockResolvedValueOnce(-1) + + await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 }) + + const first = select.mock.calls[0][0].options + expect(first[1]).toMatchObject({ title: '[ ] A' }) + expect(first[2]).toMatchObject({ title: '[ ] B' }) + const second = select.mock.calls[1][0].options + expect(second[1]).toMatchObject({ title: '[ ] A' }) + expect(second[2]).toMatchObject({ title: '[x] B' }) + }) + + it('keeps the cursor on the last toggled entry', async () => { + const { context, select } = createFakeContext() + select.mockResolvedValueOnce(2).mockResolvedValueOnce(0).mockResolvedValueOnce(-1) + + await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 }) + + expect(select.mock.calls[0][0].current).toBeUndefined() + expect(select.mock.calls[1][0].current).toBe(2) + expect(select.mock.calls[2][0].current).toBe(0) + }) + + it('toggles a selected option off', async () => { + const { context, select } = createFakeContext() + select.mockResolvedValueOnce(0).mockResolvedValueOnce(1).mockResolvedValueOnce(0).mockResolvedValueOnce(-1) + + expect(await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 })).toEqual(['b']) + }) + + it('resolves undefined when dismissed before any selection', async () => { + const { context, select } = createFakeContext() + select.mockResolvedValueOnce(undefined) + + expect(await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 })).toBeUndefined() + }) + + it('resolves undefined when dismissed after a selection', async () => { + const { context, select } = createFakeContext() + select.mockResolvedValueOnce(0).mockResolvedValueOnce(undefined) + + expect(await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 })).toBeUndefined() + }) + + it('passes option descriptions through', async () => { + const { context, select } = createFakeContext() + select.mockResolvedValueOnce(-1) + + await multiSelectDialog(context, { + title: 'Pick', + options: [{ title: 'A', description: 'first', value: 'a' }], + min: 0, + max: 1, + }) + + expect(select.mock.calls[0][0].options[1]).toEqual({ + title: '[ ] A', + description: 'first', + value: 0, + disabled: false, + }) + }) +}) + +describe('slashArgument', () => { + it('resolves an empty string for undefined input', () => { + expect(slashArgument(undefined, 'goal')).toBe('') + }) + + it('strips a leading slash token and trims the rest', () => { + expect(slashArgument('/goal fix it ', 'goal')).toBe('fix it') + }) + + it('returns the trimmed argument when no slash token is present', () => { + expect(slashArgument(' fix it ', 'goal')).toBe('fix it') + }) + + it('only strips a whole-token match', () => { + expect(slashArgument('/goalie x', 'goal')).toBe('/goalie x') + }) + + it('resolves an empty string when only the slash token is present', () => { + expect(slashArgument('/goal', 'goal')).toBe('') + }) +})