import { describe, expect, it } from 'vitest'; import { assertUserInteractionRequest, parseUserInteractionAnswer, projectUserInteraction, renderUserInteraction, } from '../src/built/user-interaction.js'; describe('UserInteraction module', () => { it('renders title, description, tip and confirmation actions as canonical segments', () => { const view = projectUserInteraction({ type: 'confirm', title: '删除项目', description: '此操作会删除所有未发布内容。', tip: '已发布内容不受影响。', }); expect(renderUserInteraction(view)).toEqual([ { type: 'markdown', data: { content: '### 删除项目\n\n此操作会删除所有未发布内容。\n\n> 💡 已发布内容不受影响。\n> 💡 请选择确认或取消。', }, }, { type: 'keyboard', data: { rows: [[ expect.objectContaining({ label: '确认', payload: 'yes', mode: 'callback' }), expect.objectContaining({ label: '取消', payload: 'no', mode: 'callback' }), ]], fallback: { hint: '也可以直接回复对应内容。', map: { '1': 'yes', '2': 'no' }, }, }, }, ]); }); it('carries an approval purpose onto the keyboard and leaves ordinary prompts unmarked', () => { const request = { type: 'select' as const, title: '操作确认', description: '删除文件 a.txt', options: [{ label: '拒绝', value: 'reject' }, { label: '允许一次', value: 'approve-once' }], }; const approval = renderUserInteraction(projectUserInteraction({ ...request, purpose: 'approval' })) as { type: string; data: Record }[]; expect(approval.find(segment => segment.type === 'keyboard')?.data.purpose).toBe('approval'); // Only the keyboard carries it: the markdown body stays the same as an unmarked prompt. const plain = renderUserInteraction(projectUserInteraction(request)) as { type: string; data: Record }[]; expect(approval[0]).toEqual(plain[0]); expect(plain.find(segment => segment.type === 'keyboard')?.data).not.toHaveProperty('purpose'); expect(projectUserInteraction({ type: 'confirm', title: '再来一局?' })).not.toHaveProperty('purpose'); }); it('projects sequence progress and option descriptions', () => { const view = projectUserInteraction({ type: 'select', title: '部署环境', description: '请选择本次发布目标。', options: [ { label: '开发', value: 'dev', description: '允许调试' }, { label: '生产', value: 'prod', description: '对外服务' }, ], }, { title: '发布向导', description: '完成以下配置。', tip: '可以随时取消。', index: 2, total: 3, }); expect(view.title).toBe('发布向导'); expect(view.description).toContain('**2/3 · 部署环境**'); expect(view.description).toContain('2. 生产 — 对外服务'); expect(view.actions).toHaveLength(2); }); it('validates typed answers instead of settling with NaN or undefined', () => { expect(parseUserInteractionAnswer({ type: 'number', title: '年龄', min: 1 }, 'abc')) .toMatchObject({ ok: false }); expect(parseUserInteractionAnswer({ type: 'number', title: '年龄', min: 1 }, '18')) .toEqual({ ok: true, value: 18 }); expect(parseUserInteractionAnswer({ type: 'select', title: '环境', options: [{ label: '开发', value: 'dev' }, { label: '生产', value: 'prod' }], }, '生产')).toEqual({ ok: true, value: 'prod' }); expect(parseUserInteractionAnswer({ type: 'confirm', title: '确认' }, '随便说点什么')) .toMatchObject({ ok: false }); }); it('allows duplicate labels because options remain addressable by index', () => { const request = { type: 'select' as const, title: '环境', options: [{ label: '生产', value: 1 }, { label: '生产', value: 2 }], }; expect(() => assertUserInteractionRequest(request)).not.toThrow(); expect(parseUserInteractionAnswer(request, '1')).toEqual({ ok: true, value: 1 }); expect(parseUserInteractionAnswer(request, '2')).toEqual({ ok: true, value: 2 }); }); it('rejects impossible definitions before waiting for input', () => { expect(() => assertUserInteractionRequest({ type: 'select', title: '环境', options: [] })) .toThrow('options must not be empty'); expect(() => assertUserInteractionRequest({ type: 'number', title: '数量', min: 10, max: 1 })) .toThrow('range is invalid'); }); it('accepts plain-text aliases for select options and lists them in the prompt', () => { const request = { type: 'select' as const, title: '操作确认', options: [ { label: '拒绝', value: 'reject', aliases: ['n', '否'] }, { label: '允许一次', value: 'approve-once', aliases: ['y', '同意'], description: '仅执行当前操作' }, ], }; expect(parseUserInteractionAnswer(request, ' Y ')).toEqual({ ok: true, value: 'approve-once' }); expect(parseUserInteractionAnswer(request, '同意')).toEqual({ ok: true, value: 'approve-once' }); expect(parseUserInteractionAnswer(request, 'n')).toEqual({ ok: true, value: 'reject' }); expect(parseUserInteractionAnswer(request, '拒绝')).toEqual({ ok: true, value: 'reject' }); expect(parseUserInteractionAnswer(request, 'maybe')).toMatchObject({ ok: false }); const rendered = renderUserInteraction(projectUserInteraction(request)); expect(rendered).toHaveLength(1); expect(JSON.stringify(rendered)).toContain('2. 允许一次(y / 同意) — 仅执行当前操作'); }); it('passes every select option to the endpoint and never promises buttons in the text (#1250)', () => { const options = Array.from({ length: 20 }, (_, index) => ({ label: `晴天 ${index + 1}`, value: index + 1 })); const view = projectUserInteraction({ type: 'select', title: '选择歌曲', options }); expect(view.actions).toHaveLength(20); expect(view.tip).toBe('回复序号或选项名称,或回复「取消」退出。'); expect(view.tip).not.toContain('按钮'); const rendered = renderUserInteraction(view) as { type: string; data: { rows?: unknown[][] } }[]; expect(rendered[1]?.type).toBe('keyboard'); expect(rendered[1]?.data.rows?.flat()).toHaveLength(20); }); });