|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Qwen Team |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest'; |
| 8 | +import { planCommand } from './planCommand.js'; |
| 9 | +import { type CommandContext } from './types.js'; |
| 10 | +import { createMockCommandContext } from '../../test-utils/mockCommandContext.js'; |
| 11 | +import { ApprovalMode } from '@qwen-code/qwen-code-core'; |
| 12 | + |
| 13 | +describe('planCommand', () => { |
| 14 | + let mockContext: CommandContext; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + mockContext = createMockCommandContext({ |
| 18 | + services: { |
| 19 | + config: { |
| 20 | + getApprovalMode: vi.fn().mockReturnValue(ApprovalMode.DEFAULT), |
| 21 | + getPrePlanMode: vi.fn().mockReturnValue(ApprovalMode.DEFAULT), |
| 22 | + setApprovalMode: vi.fn(), |
| 23 | + } as unknown as import('@qwen-code/qwen-code-core').Config, |
| 24 | + }, |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should switch to plan mode if not in plan mode', async () => { |
| 29 | + if (!planCommand.action) { |
| 30 | + throw new Error('The plan command must have an action.'); |
| 31 | + } |
| 32 | + |
| 33 | + const result = await planCommand.action(mockContext, ''); |
| 34 | + |
| 35 | + expect(mockContext.services.config?.setApprovalMode).toHaveBeenCalledWith( |
| 36 | + ApprovalMode.PLAN, |
| 37 | + ); |
| 38 | + expect(result).toEqual({ |
| 39 | + type: 'message', |
| 40 | + messageType: 'info', |
| 41 | + content: |
| 42 | + 'Enabled plan mode. The agent will analyze and plan without executing tools.', |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should return submit prompt if arguments are provided when switching to plan mode', async () => { |
| 47 | + if (!planCommand.action) { |
| 48 | + throw new Error('The plan command must have an action.'); |
| 49 | + } |
| 50 | + |
| 51 | + const result = await planCommand.action(mockContext, 'refactor the code'); |
| 52 | + |
| 53 | + expect(mockContext.services.config?.setApprovalMode).toHaveBeenCalledWith( |
| 54 | + ApprovalMode.PLAN, |
| 55 | + ); |
| 56 | + expect(result).toEqual({ |
| 57 | + type: 'submit_prompt', |
| 58 | + content: [{ text: 'refactor the code' }], |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should return already in plan mode if mode is already plan', async () => { |
| 63 | + if (!planCommand.action) { |
| 64 | + throw new Error('The plan command must have an action.'); |
| 65 | + } |
| 66 | + |
| 67 | + (mockContext.services.config?.getApprovalMode as Mock).mockReturnValue( |
| 68 | + ApprovalMode.PLAN, |
| 69 | + ); |
| 70 | + |
| 71 | + const result = await planCommand.action(mockContext, ''); |
| 72 | + |
| 73 | + expect(mockContext.services.config?.setApprovalMode).not.toHaveBeenCalled(); |
| 74 | + expect(result).toEqual({ |
| 75 | + type: 'message', |
| 76 | + messageType: 'info', |
| 77 | + content: 'Already in plan mode. Use "/plan exit" to exit plan mode.', |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should return submit prompt if arguments are provided and already in plan mode', async () => { |
| 82 | + if (!planCommand.action) { |
| 83 | + throw new Error('The plan command must have an action.'); |
| 84 | + } |
| 85 | + |
| 86 | + (mockContext.services.config?.getApprovalMode as Mock).mockReturnValue( |
| 87 | + ApprovalMode.PLAN, |
| 88 | + ); |
| 89 | + |
| 90 | + const result = await planCommand.action(mockContext, 'keep planning'); |
| 91 | + |
| 92 | + expect(mockContext.services.config?.setApprovalMode).not.toHaveBeenCalled(); |
| 93 | + expect(result).toEqual({ |
| 94 | + type: 'submit_prompt', |
| 95 | + content: [{ text: 'keep planning' }], |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should exit plan mode when exit argument is passed', async () => { |
| 100 | + if (!planCommand.action) { |
| 101 | + throw new Error('The plan command must have an action.'); |
| 102 | + } |
| 103 | + |
| 104 | + (mockContext.services.config?.getApprovalMode as Mock).mockReturnValue( |
| 105 | + ApprovalMode.PLAN, |
| 106 | + ); |
| 107 | + |
| 108 | + const result = await planCommand.action(mockContext, 'exit'); |
| 109 | + |
| 110 | + expect(mockContext.services.config?.setApprovalMode).toHaveBeenCalledWith( |
| 111 | + ApprovalMode.DEFAULT, |
| 112 | + ); |
| 113 | + expect(result).toEqual({ |
| 114 | + type: 'message', |
| 115 | + messageType: 'info', |
| 116 | + content: 'Exited plan mode. Previous approval mode restored.', |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should restore pre-plan mode when executing from plan mode', async () => { |
| 121 | + if (!planCommand.action) { |
| 122 | + throw new Error('The plan command must have an action.'); |
| 123 | + } |
| 124 | + |
| 125 | + (mockContext.services.config?.getApprovalMode as Mock).mockReturnValue( |
| 126 | + ApprovalMode.PLAN, |
| 127 | + ); |
| 128 | + (mockContext.services.config?.getPrePlanMode as Mock).mockReturnValue( |
| 129 | + ApprovalMode.AUTO_EDIT, |
| 130 | + ); |
| 131 | + |
| 132 | + const result = await planCommand.action(mockContext, 'exit'); |
| 133 | + |
| 134 | + expect(mockContext.services.config?.setApprovalMode).toHaveBeenCalledWith( |
| 135 | + ApprovalMode.AUTO_EDIT, |
| 136 | + ); |
| 137 | + expect(result).toEqual({ |
| 138 | + type: 'message', |
| 139 | + messageType: 'info', |
| 140 | + content: 'Exited plan mode. Previous approval mode restored.', |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should return error when execute is used but not in plan mode', async () => { |
| 145 | + if (!planCommand.action) { |
| 146 | + throw new Error('The plan command must have an action.'); |
| 147 | + } |
| 148 | + |
| 149 | + // Default mock returns ApprovalMode.DEFAULT (not PLAN) |
| 150 | + const result = await planCommand.action(mockContext, 'exit'); |
| 151 | + |
| 152 | + expect(mockContext.services.config?.setApprovalMode).not.toHaveBeenCalled(); |
| 153 | + expect(result).toEqual({ |
| 154 | + type: 'message', |
| 155 | + messageType: 'error', |
| 156 | + content: 'Not in plan mode. Use "/plan" to enter plan mode first.', |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments