|
| 1 | +import { toolAgent } from 'mycoder-agent'; |
| 2 | +import { beforeEach, afterEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { getConfig } from '../../src/settings/config.js'; |
| 5 | + |
| 6 | +// Mock dependencies |
| 7 | +vi.mock('../../src/settings/config.js', () => ({ |
| 8 | + getConfig: vi.fn(), |
| 9 | + updateConfig: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('mycoder-agent', () => ({ |
| 13 | + Logger: vi.fn().mockImplementation(() => ({ |
| 14 | + info: vi.fn(), |
| 15 | + warn: vi.fn(), |
| 16 | + error: vi.fn(), |
| 17 | + log: vi.fn(), |
| 18 | + debug: vi.fn(), |
| 19 | + })), |
| 20 | + toolAgent: vi.fn().mockResolvedValue({ result: 'Success' }), |
| 21 | + getTools: vi.fn().mockReturnValue([]), |
| 22 | + getAnthropicApiKeyError: vi.fn(), |
| 23 | + userPrompt: vi.fn(), |
| 24 | + LogLevel: { |
| 25 | + debug: 0, |
| 26 | + verbose: 1, |
| 27 | + info: 2, |
| 28 | + warn: 3, |
| 29 | + error: 4, |
| 30 | + }, |
| 31 | + subAgentTool: { logPrefix: '' }, |
| 32 | + errorToString: vi.fn(), |
| 33 | + getModel: vi.fn(), |
| 34 | + DEFAULT_CONFIG: {}, |
| 35 | + TokenTracker: vi.fn().mockImplementation(() => ({ |
| 36 | + logLevel: 2, |
| 37 | + toString: () => 'token usage', |
| 38 | + })), |
| 39 | +})); |
| 40 | + |
| 41 | +describe('Config Defaults for CLI Options', () => { |
| 42 | + beforeEach(() => { |
| 43 | + // Mock process.env |
| 44 | + process.env.ANTHROPIC_API_KEY = 'test-key'; |
| 45 | + |
| 46 | + // Reset mocks before each test |
| 47 | + vi.resetAllMocks(); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + vi.resetAllMocks(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should use config values for headless, userSession, and pageFilter when not provided in args', async () => { |
| 55 | + // Setup mock config with default values |
| 56 | + vi.mocked(getConfig).mockReturnValue({ |
| 57 | + githubMode: false, |
| 58 | + headless: true, |
| 59 | + userSession: false, |
| 60 | + pageFilter: 'none', |
| 61 | + modelProvider: 'anthropic', |
| 62 | + modelName: 'claude-3-7-sonnet-20250219', |
| 63 | + ollamaBaseUrl: 'http://localhost:11434/api', |
| 64 | + }); |
| 65 | + |
| 66 | + // Create minimal args (no headless, userSession, or pageFilter specified) |
| 67 | + const args = { |
| 68 | + headless: undefined, |
| 69 | + userSession: undefined, |
| 70 | + pageFilter: undefined, |
| 71 | + }; |
| 72 | + |
| 73 | + // Get config from getConfig |
| 74 | + const config = getConfig(); |
| 75 | + |
| 76 | + // Simulate how $default.ts uses these values |
| 77 | + const options = { |
| 78 | + headless: args.headless ?? config.headless, |
| 79 | + userSession: args.userSession ?? config.userSession, |
| 80 | + pageFilter: args.pageFilter ?? config.pageFilter, |
| 81 | + }; |
| 82 | + |
| 83 | + // Verify the correct values are used (from config) |
| 84 | + expect(options).toEqual({ |
| 85 | + headless: true, // Default from config |
| 86 | + userSession: false, // Default from config |
| 87 | + pageFilter: 'none', // Default from config |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should use command line args for headless, userSession, and pageFilter when provided', async () => { |
| 92 | + // Setup mock config with default values |
| 93 | + vi.mocked(getConfig).mockReturnValue({ |
| 94 | + githubMode: false, |
| 95 | + headless: true, // Default is true |
| 96 | + userSession: false, // Default is false |
| 97 | + pageFilter: 'none', // Default is none |
| 98 | + modelProvider: 'anthropic', |
| 99 | + modelName: 'claude-3-7-sonnet-20250219', |
| 100 | + ollamaBaseUrl: 'http://localhost:11434/api', |
| 101 | + }); |
| 102 | + |
| 103 | + // Create args with explicit values (overriding defaults) |
| 104 | + const args = { |
| 105 | + headless: false, // Override config default |
| 106 | + userSession: true, // Override config default |
| 107 | + pageFilter: 'readability', // Override config default |
| 108 | + }; |
| 109 | + |
| 110 | + // Get config from getConfig |
| 111 | + const config = getConfig(); |
| 112 | + |
| 113 | + // Simulate how $default.ts uses these values |
| 114 | + const options = { |
| 115 | + headless: args.headless ?? config.headless, |
| 116 | + userSession: args.userSession ?? config.userSession, |
| 117 | + pageFilter: args.pageFilter ?? config.pageFilter, |
| 118 | + }; |
| 119 | + |
| 120 | + // Verify the correct values are used (from command line args) |
| 121 | + expect(options).toEqual({ |
| 122 | + headless: false, // Overridden by command line |
| 123 | + userSession: true, // Overridden by command line |
| 124 | + pageFilter: 'readability', // Overridden by command line |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should test the actual toolAgent call with config defaults', async () => { |
| 129 | + // Setup mock config with default values |
| 130 | + vi.mocked(getConfig).mockReturnValue({ |
| 131 | + githubMode: false, |
| 132 | + headless: true, |
| 133 | + userSession: false, |
| 134 | + pageFilter: 'none', |
| 135 | + modelProvider: 'anthropic', |
| 136 | + modelName: 'claude-3-7-sonnet-20250219', |
| 137 | + ollamaBaseUrl: 'http://localhost:11434/api', |
| 138 | + }); |
| 139 | + |
| 140 | + // Create minimal args (no headless, userSession, or pageFilter specified) |
| 141 | + const args = { |
| 142 | + headless: undefined, |
| 143 | + userSession: undefined, |
| 144 | + pageFilter: undefined, |
| 145 | + }; |
| 146 | + |
| 147 | + // Get config from getConfig |
| 148 | + const config = getConfig(); |
| 149 | + |
| 150 | + // Call toolAgent with the config values |
| 151 | + await toolAgent( |
| 152 | + 'test prompt', |
| 153 | + [], |
| 154 | + {}, |
| 155 | + { |
| 156 | + headless: args.headless ?? config.headless, |
| 157 | + userSession: args.userSession ?? config.userSession, |
| 158 | + pageFilter: args.pageFilter ?? config.pageFilter, |
| 159 | + workingDirectory: '.', |
| 160 | + githubMode: config.githubMode, |
| 161 | + }, |
| 162 | + ); |
| 163 | + |
| 164 | + // Verify toolAgent was called with the correct config values from defaults |
| 165 | + expect(toolAgent).toHaveBeenCalledWith( |
| 166 | + 'test prompt', |
| 167 | + expect.any(Array), |
| 168 | + expect.any(Object), |
| 169 | + expect.objectContaining({ |
| 170 | + headless: true, // Default from config |
| 171 | + userSession: false, // Default from config |
| 172 | + pageFilter: 'none', // Default from config |
| 173 | + }), |
| 174 | + ); |
| 175 | + }); |
| 176 | + |
| 177 | + it('should test the actual toolAgent call with command line args', async () => { |
| 178 | + // Setup mock config with default values |
| 179 | + vi.mocked(getConfig).mockReturnValue({ |
| 180 | + githubMode: false, |
| 181 | + headless: true, // Default is true |
| 182 | + userSession: false, // Default is false |
| 183 | + pageFilter: 'none', // Default is none |
| 184 | + modelProvider: 'anthropic', |
| 185 | + modelName: 'claude-3-7-sonnet-20250219', |
| 186 | + ollamaBaseUrl: 'http://localhost:11434/api', |
| 187 | + }); |
| 188 | + |
| 189 | + // Create args with explicit values (overriding defaults) |
| 190 | + const args = { |
| 191 | + headless: false, // Override config default |
| 192 | + userSession: true, // Override config default |
| 193 | + pageFilter: 'readability', // Override config default |
| 194 | + }; |
| 195 | + |
| 196 | + // Get config from getConfig |
| 197 | + const config = getConfig(); |
| 198 | + |
| 199 | + // Call toolAgent with the command line args |
| 200 | + await toolAgent( |
| 201 | + 'test prompt', |
| 202 | + [], |
| 203 | + {}, |
| 204 | + { |
| 205 | + headless: args.headless ?? config.headless, |
| 206 | + userSession: args.userSession ?? config.userSession, |
| 207 | + pageFilter: args.pageFilter ?? config.pageFilter, |
| 208 | + workingDirectory: '.', |
| 209 | + githubMode: config.githubMode, |
| 210 | + }, |
| 211 | + ); |
| 212 | + |
| 213 | + // Verify toolAgent was called with the command line args (overriding defaults) |
| 214 | + expect(toolAgent).toHaveBeenCalledWith( |
| 215 | + 'test prompt', |
| 216 | + expect.any(Array), |
| 217 | + expect.any(Object), |
| 218 | + expect.objectContaining({ |
| 219 | + headless: false, // Overridden by command line |
| 220 | + userSession: true, // Overridden by command line |
| 221 | + pageFilter: 'readability', // Overridden by command line |
| 222 | + }), |
| 223 | + ); |
| 224 | + }); |
| 225 | +}); |
0 commit comments