|
| 1 | +import { CommandMode } from '../src/command'; |
1 | 2 | import ExplainCommand from '../src/commands/explain-command'; |
2 | | -import { ContextV2 } from '../src/context'; |
3 | | -import { HelpProvider } from '../src/help'; |
4 | 3 | import { ClientRequest, NavieOptions, default as navie } from '../src/navie'; |
5 | | -import { ProjectInfoProvider } from '../src/project-info'; |
6 | | -import { TestInvocationProvider } from '../src/test-invocation'; |
| 4 | + |
| 5 | +jest.mock('../src/commands/explain-command'); |
| 6 | +jest.mock('../src/services/openai-completion-service'); |
| 7 | + |
| 8 | +// eslint-disable-next-line @typescript-eslint/require-await |
| 9 | +ExplainCommand.prototype.execute = jest.fn().mockImplementation(async function* () { |
| 10 | + yield 'This is a test response'; |
| 11 | +}); |
7 | 12 |
|
8 | 13 | describe('Navie Function', () => { |
9 | 14 | let clientRequest: ClientRequest; |
10 | | - let contextProvider: ContextV2.ContextProvider; |
11 | | - let projectInfoProvider: ProjectInfoProvider; |
12 | | - let helpProvider: HelpProvider; |
13 | | - let testInvocationProvider: TestInvocationProvider; |
14 | | - let options: NavieOptions; |
15 | 15 |
|
16 | | - beforeEach(() => { |
17 | | - clientRequest = { question: '' }; |
18 | | - contextProvider = jest.fn().mockResolvedValue({}); |
19 | | - projectInfoProvider = jest.fn().mockResolvedValue({}); |
20 | | - helpProvider = jest.fn().mockResolvedValue({}); |
21 | | - testInvocationProvider = jest.fn().mockResolvedValue({}); |
22 | | - options = new NavieOptions(); |
23 | | - }); |
| 16 | + const createNavie = () => { |
| 17 | + const contextProvider = jest.fn().mockResolvedValue({}); |
| 18 | + const projectInfoProvider = jest.fn().mockResolvedValue({}); |
| 19 | + const helpProvider = jest.fn().mockResolvedValue({}); |
| 20 | + const testInvocationProvider = jest.fn().mockResolvedValue({}); |
| 21 | + const options = new NavieOptions(); |
24 | 22 |
|
25 | | - test('uses default command if question is empty', async () => { |
26 | | - const instance = navie( |
| 23 | + return navie( |
27 | 24 | clientRequest, |
28 | 25 | contextProvider, |
29 | 26 | projectInfoProvider, |
30 | 27 | helpProvider, |
31 | 28 | testInvocationProvider, |
32 | 29 | options |
33 | 30 | ); |
34 | | - const result = []; |
35 | | - for await (const chunk of instance.execute()) { |
36 | | - result.push(chunk); |
37 | | - } |
38 | | - expect(result).toBeTruthy(); |
39 | | - expect(clientRequest.question).toEqual('explain'); |
| 31 | + }; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + clientRequest = { question: '' }; |
40 | 35 | }); |
41 | | -}); |
42 | 36 |
|
43 | | -jest.mock('../src/commands/explain-command'); |
44 | | -// eslint-disable-next-line @typescript-eslint/require-await |
45 | | -ExplainCommand.prototype.execute = jest.fn().mockImplementation(async function* () { |
46 | | - yield 'This is a test response'; |
47 | | -}); |
| 37 | + describe('commandMode property', () => { |
| 38 | + test.each([ |
| 39 | + ['empty question', '', CommandMode.Explain], |
| 40 | + ['no command prefix', 'How does this work?', CommandMode.Explain], |
| 41 | + ['only whitespace', ' ', CommandMode.Explain], |
| 42 | + ['@welcome prefix', '@welcome', CommandMode.Welcome], |
| 43 | + ['@suggest prefix with text', '@suggest What should I do next?', CommandMode.Suggest], |
| 44 | + ['@suggest prefix alone', '@suggest', CommandMode.Suggest], |
| 45 | + ['@review prefix', '@review Check this code', CommandMode.Review], |
| 46 | + [ |
| 47 | + '@review prefix multiline', |
| 48 | + '@review\nCheck this implementation\nfor potential bugs', |
| 49 | + CommandMode.Review, |
| 50 | + ], |
| 51 | + ['@observe prefix', '@observe What changed?', CommandMode.Observe], |
| 52 | + ['@fix prefix', '@fix Resolve this issue', CommandMode.Fix], |
| 53 | + ['@context prefix', '@context Show me relevant files', CommandMode.Context], |
| 54 | + ])('should set commandMode correctly for %s', (description, question, expectedMode) => { |
| 55 | + clientRequest.question = question; |
| 56 | + const instance = createNavie(); |
| 57 | + expect(instance.commandMode).toEqual(expectedMode); |
| 58 | + }); |
| 59 | + }); |
48 | 60 |
|
49 | | -jest.mock('../src/services/openai-completion-service'); |
| 61 | + describe('execution', () => { |
| 62 | + test('executes successfully and normalizes empty question', async () => { |
| 63 | + clientRequest.question = ''; |
| 64 | + const instance = createNavie(); |
| 65 | + const result = []; |
| 66 | + for await (const chunk of instance.execute()) { |
| 67 | + result.push(chunk); |
| 68 | + } |
| 69 | + expect(result).toHaveLength(1); |
| 70 | + expect(result[0]).toBe('This is a test response'); |
| 71 | + expect(clientRequest.question).toBe('explain'); |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments