feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
import { jest } from '@jest/globals';
|
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
// Mock config-manager
|
|
|
|
const mockGetMainProvider = jest.fn();
|
|
|
|
const mockGetMainModelId = jest.fn();
|
|
|
|
const mockGetResearchProvider = jest.fn();
|
|
|
|
const mockGetResearchModelId = jest.fn();
|
|
|
|
const mockGetFallbackProvider = jest.fn();
|
|
|
|
const mockGetFallbackModelId = jest.fn();
|
|
|
|
const mockGetParametersForRole = jest.fn();
|
2025-05-07 13:54:01 -04:00
|
|
|
const mockGetUserId = jest.fn();
|
|
|
|
|
|
|
|
// --- Mock MODEL_MAP Data ---
|
|
|
|
// Provide a simplified structure sufficient for cost calculation tests
|
|
|
|
const mockModelMap = {
|
|
|
|
anthropic: [
|
|
|
|
{
|
|
|
|
id: 'test-main-model',
|
|
|
|
cost_per_1m_tokens: { input: 3, output: 15, currency: 'USD' }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'test-fallback-model',
|
|
|
|
cost_per_1m_tokens: { input: 3, output: 15, currency: 'USD' }
|
|
|
|
}
|
|
|
|
],
|
|
|
|
perplexity: [
|
|
|
|
{
|
|
|
|
id: 'test-research-model',
|
|
|
|
cost_per_1m_tokens: { input: 1, output: 1, currency: 'USD' }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
// Add other providers/models if needed for specific tests
|
|
|
|
};
|
2025-04-30 22:02:02 -04:00
|
|
|
|
|
|
|
jest.unstable_mockModule('../../scripts/modules/config-manager.js', () => ({
|
|
|
|
getMainProvider: mockGetMainProvider,
|
|
|
|
getMainModelId: mockGetMainModelId,
|
|
|
|
getResearchProvider: mockGetResearchProvider,
|
|
|
|
getResearchModelId: mockGetResearchModelId,
|
|
|
|
getFallbackProvider: mockGetFallbackProvider,
|
|
|
|
getFallbackModelId: mockGetFallbackModelId,
|
2025-05-07 13:54:01 -04:00
|
|
|
getParametersForRole: mockGetParametersForRole,
|
|
|
|
getUserId: mockGetUserId,
|
|
|
|
MODEL_MAP: mockModelMap
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
}));
|
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
// Mock AI Provider Modules
|
|
|
|
const mockGenerateAnthropicText = jest.fn();
|
|
|
|
const mockStreamAnthropicText = jest.fn();
|
|
|
|
const mockGenerateAnthropicObject = jest.fn();
|
|
|
|
jest.unstable_mockModule('../../src/ai-providers/anthropic.js', () => ({
|
|
|
|
generateAnthropicText: mockGenerateAnthropicText,
|
|
|
|
streamAnthropicText: mockStreamAnthropicText,
|
|
|
|
generateAnthropicObject: mockGenerateAnthropicObject
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
}));
|
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
const mockGeneratePerplexityText = jest.fn();
|
|
|
|
const mockStreamPerplexityText = jest.fn();
|
|
|
|
const mockGeneratePerplexityObject = jest.fn();
|
|
|
|
jest.unstable_mockModule('../../src/ai-providers/perplexity.js', () => ({
|
|
|
|
generatePerplexityText: mockGeneratePerplexityText,
|
|
|
|
streamPerplexityText: mockStreamPerplexityText,
|
|
|
|
generatePerplexityObject: mockGeneratePerplexityObject
|
|
|
|
}));
|
|
|
|
|
|
|
|
// ... Mock other providers (google, openai, etc.) similarly ...
|
|
|
|
|
2025-05-01 17:11:51 -04:00
|
|
|
// Mock utils logger, API key resolver, AND findProjectRoot
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
const mockLog = jest.fn();
|
2025-04-30 22:02:02 -04:00
|
|
|
const mockResolveEnvVariable = jest.fn();
|
2025-05-01 17:11:51 -04:00
|
|
|
const mockFindProjectRoot = jest.fn();
|
2025-05-07 13:54:01 -04:00
|
|
|
const mockIsSilentMode = jest.fn();
|
|
|
|
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
jest.unstable_mockModule('../../scripts/modules/utils.js', () => ({
|
2025-04-30 22:02:02 -04:00
|
|
|
log: mockLog,
|
2025-05-01 17:11:51 -04:00
|
|
|
resolveEnvVariable: mockResolveEnvVariable,
|
2025-05-07 13:54:01 -04:00
|
|
|
findProjectRoot: mockFindProjectRoot,
|
|
|
|
isSilentMode: mockIsSilentMode
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
// Import the module to test (AFTER mocks)
|
|
|
|
const { generateTextService } = await import(
|
|
|
|
'../../scripts/modules/ai-services-unified.js'
|
|
|
|
);
|
|
|
|
|
|
|
|
describe('Unified AI Services', () => {
|
2025-05-01 17:11:51 -04:00
|
|
|
const fakeProjectRoot = '/fake/project/root'; // Define for reuse
|
|
|
|
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
// Clear mocks before each test
|
2025-04-30 22:02:02 -04:00
|
|
|
jest.clearAllMocks(); // Clears all mocks
|
|
|
|
|
|
|
|
// Set default mock behaviors
|
|
|
|
mockGetMainProvider.mockReturnValue('anthropic');
|
|
|
|
mockGetMainModelId.mockReturnValue('test-main-model');
|
|
|
|
mockGetResearchProvider.mockReturnValue('perplexity');
|
|
|
|
mockGetResearchModelId.mockReturnValue('test-research-model');
|
|
|
|
mockGetFallbackProvider.mockReturnValue('anthropic');
|
|
|
|
mockGetFallbackModelId.mockReturnValue('test-fallback-model');
|
|
|
|
mockGetParametersForRole.mockImplementation((role) => {
|
|
|
|
if (role === 'main') return { maxTokens: 100, temperature: 0.5 };
|
|
|
|
if (role === 'research') return { maxTokens: 200, temperature: 0.3 };
|
|
|
|
if (role === 'fallback') return { maxTokens: 150, temperature: 0.6 };
|
|
|
|
return { maxTokens: 100, temperature: 0.5 }; // Default
|
|
|
|
});
|
|
|
|
mockResolveEnvVariable.mockImplementation((key) => {
|
|
|
|
if (key === 'ANTHROPIC_API_KEY') return 'mock-anthropic-key';
|
|
|
|
if (key === 'PERPLEXITY_API_KEY') return 'mock-perplexity-key';
|
|
|
|
return null;
|
|
|
|
});
|
2025-05-01 17:11:51 -04:00
|
|
|
|
|
|
|
// Set a default behavior for the new mock
|
|
|
|
mockFindProjectRoot.mockReturnValue(fakeProjectRoot);
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('generateTextService', () => {
|
2025-04-30 22:02:02 -04:00
|
|
|
test('should use main provider/model and succeed', async () => {
|
|
|
|
mockGenerateAnthropicText.mockResolvedValue('Main provider response');
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
const params = {
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
role: 'main',
|
2025-04-30 22:02:02 -04:00
|
|
|
session: { env: {} },
|
|
|
|
systemPrompt: 'System',
|
|
|
|
prompt: 'Test'
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
};
|
2025-04-30 22:02:02 -04:00
|
|
|
const result = await generateTextService(params);
|
|
|
|
|
2025-05-07 13:54:01 -04:00
|
|
|
expect(result.mainResult).toBe('Main provider response');
|
|
|
|
expect(result).toHaveProperty('telemetryData');
|
2025-05-01 17:11:51 -04:00
|
|
|
expect(mockGetMainProvider).toHaveBeenCalledWith(fakeProjectRoot);
|
|
|
|
expect(mockGetMainModelId).toHaveBeenCalledWith(fakeProjectRoot);
|
|
|
|
expect(mockGetParametersForRole).toHaveBeenCalledWith(
|
|
|
|
'main',
|
|
|
|
fakeProjectRoot
|
|
|
|
);
|
2025-04-30 22:02:02 -04:00
|
|
|
expect(mockResolveEnvVariable).toHaveBeenCalledWith(
|
|
|
|
'ANTHROPIC_API_KEY',
|
2025-05-01 17:11:51 -04:00
|
|
|
params.session,
|
|
|
|
fakeProjectRoot
|
2025-04-30 22:02:02 -04:00
|
|
|
);
|
|
|
|
expect(mockGenerateAnthropicText).toHaveBeenCalledTimes(1);
|
|
|
|
expect(mockGenerateAnthropicText).toHaveBeenCalledWith({
|
|
|
|
apiKey: 'mock-anthropic-key',
|
|
|
|
modelId: 'test-main-model',
|
|
|
|
maxTokens: 100,
|
|
|
|
temperature: 0.5,
|
|
|
|
messages: [
|
|
|
|
{ role: 'system', content: 'System' },
|
|
|
|
{ role: 'user', content: 'Test' }
|
|
|
|
]
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
});
|
2025-04-30 22:02:02 -04:00
|
|
|
expect(mockGeneratePerplexityText).not.toHaveBeenCalled();
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
});
|
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
test('should fall back to fallback provider if main fails', async () => {
|
|
|
|
const mainError = new Error('Main provider failed');
|
|
|
|
mockGenerateAnthropicText
|
2025-05-01 17:11:51 -04:00
|
|
|
.mockRejectedValueOnce(mainError)
|
|
|
|
.mockResolvedValueOnce('Fallback provider response');
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
|
2025-05-01 17:11:51 -04:00
|
|
|
const explicitRoot = '/explicit/test/root';
|
|
|
|
const params = {
|
|
|
|
role: 'main',
|
|
|
|
prompt: 'Fallback test',
|
|
|
|
projectRoot: explicitRoot
|
|
|
|
};
|
2025-04-30 22:02:02 -04:00
|
|
|
const result = await generateTextService(params);
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
|
2025-05-07 13:54:01 -04:00
|
|
|
expect(result.mainResult).toBe('Fallback provider response');
|
|
|
|
expect(result).toHaveProperty('telemetryData');
|
2025-05-01 17:11:51 -04:00
|
|
|
expect(mockGetMainProvider).toHaveBeenCalledWith(explicitRoot);
|
|
|
|
expect(mockGetFallbackProvider).toHaveBeenCalledWith(explicitRoot);
|
|
|
|
expect(mockGetParametersForRole).toHaveBeenCalledWith(
|
|
|
|
'main',
|
|
|
|
explicitRoot
|
|
|
|
);
|
|
|
|
expect(mockGetParametersForRole).toHaveBeenCalledWith(
|
|
|
|
'fallback',
|
|
|
|
explicitRoot
|
|
|
|
);
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
|
2025-05-01 17:11:51 -04:00
|
|
|
expect(mockResolveEnvVariable).toHaveBeenCalledWith(
|
|
|
|
'ANTHROPIC_API_KEY',
|
|
|
|
undefined,
|
|
|
|
explicitRoot
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(mockGenerateAnthropicText).toHaveBeenCalledTimes(2);
|
|
|
|
expect(mockGeneratePerplexityText).not.toHaveBeenCalled();
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
expect(mockLog).toHaveBeenCalledWith(
|
|
|
|
'error',
|
2025-04-30 22:02:02 -04:00
|
|
|
expect.stringContaining('Service call failed for role main')
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
);
|
|
|
|
expect(mockLog).toHaveBeenCalledWith(
|
|
|
|
'info',
|
2025-04-30 22:02:02 -04:00
|
|
|
expect.stringContaining('New AI service call with role: fallback')
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
test('should fall back to research provider if main and fallback fail', async () => {
|
|
|
|
const mainError = new Error('Main failed');
|
|
|
|
const fallbackError = new Error('Fallback failed');
|
|
|
|
mockGenerateAnthropicText
|
|
|
|
.mockRejectedValueOnce(mainError)
|
|
|
|
.mockRejectedValueOnce(fallbackError);
|
|
|
|
mockGeneratePerplexityText.mockResolvedValue(
|
|
|
|
'Research provider response'
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
);
|
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
const params = { role: 'main', prompt: 'Research fallback test' };
|
|
|
|
const result = await generateTextService(params);
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
|
2025-05-07 13:54:01 -04:00
|
|
|
expect(result.mainResult).toBe('Research provider response');
|
|
|
|
expect(result).toHaveProperty('telemetryData');
|
2025-05-01 17:11:51 -04:00
|
|
|
expect(mockGetMainProvider).toHaveBeenCalledWith(fakeProjectRoot);
|
|
|
|
expect(mockGetFallbackProvider).toHaveBeenCalledWith(fakeProjectRoot);
|
|
|
|
expect(mockGetResearchProvider).toHaveBeenCalledWith(fakeProjectRoot);
|
|
|
|
expect(mockGetParametersForRole).toHaveBeenCalledWith(
|
|
|
|
'main',
|
|
|
|
fakeProjectRoot
|
|
|
|
);
|
|
|
|
expect(mockGetParametersForRole).toHaveBeenCalledWith(
|
|
|
|
'fallback',
|
|
|
|
fakeProjectRoot
|
|
|
|
);
|
|
|
|
expect(mockGetParametersForRole).toHaveBeenCalledWith(
|
|
|
|
'research',
|
|
|
|
fakeProjectRoot
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(mockResolveEnvVariable).toHaveBeenCalledWith(
|
|
|
|
'ANTHROPIC_API_KEY',
|
|
|
|
undefined,
|
|
|
|
fakeProjectRoot
|
|
|
|
);
|
|
|
|
expect(mockResolveEnvVariable).toHaveBeenCalledWith(
|
|
|
|
'ANTHROPIC_API_KEY',
|
|
|
|
undefined,
|
|
|
|
fakeProjectRoot
|
|
|
|
);
|
|
|
|
expect(mockResolveEnvVariable).toHaveBeenCalledWith(
|
|
|
|
'PERPLEXITY_API_KEY',
|
|
|
|
undefined,
|
|
|
|
fakeProjectRoot
|
|
|
|
);
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
|
2025-05-01 17:11:51 -04:00
|
|
|
expect(mockGenerateAnthropicText).toHaveBeenCalledTimes(2);
|
|
|
|
expect(mockGeneratePerplexityText).toHaveBeenCalledTimes(1);
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
expect(mockLog).toHaveBeenCalledWith(
|
|
|
|
'error',
|
2025-04-30 22:02:02 -04:00
|
|
|
expect.stringContaining('Service call failed for role fallback')
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
);
|
|
|
|
expect(mockLog).toHaveBeenCalledWith(
|
|
|
|
'info',
|
2025-04-30 22:02:02 -04:00
|
|
|
expect.stringContaining('New AI service call with role: research')
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
test('should throw error if all providers in sequence fail', async () => {
|
|
|
|
mockGenerateAnthropicText.mockRejectedValue(
|
|
|
|
new Error('Anthropic failed')
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
);
|
2025-04-30 22:02:02 -04:00
|
|
|
mockGeneratePerplexityText.mockRejectedValue(
|
|
|
|
new Error('Perplexity failed')
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
);
|
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
const params = { role: 'main', prompt: 'All fail test' };
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
await expect(generateTextService(params)).rejects.toThrow(
|
|
|
|
'Perplexity failed' // Error from the last attempt (research)
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
);
|
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
expect(mockGenerateAnthropicText).toHaveBeenCalledTimes(2); // main, fallback
|
|
|
|
expect(mockGeneratePerplexityText).toHaveBeenCalledTimes(1); // research
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
});
|
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
test('should handle retryable errors correctly', async () => {
|
|
|
|
const retryableError = new Error('Rate limit');
|
|
|
|
mockGenerateAnthropicText
|
|
|
|
.mockRejectedValueOnce(retryableError) // Fails once
|
|
|
|
.mockResolvedValue('Success after retry'); // Succeeds on retry
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
const params = { role: 'main', prompt: 'Retry success test' };
|
|
|
|
const result = await generateTextService(params);
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
|
2025-05-07 13:54:01 -04:00
|
|
|
expect(result.mainResult).toBe('Success after retry');
|
|
|
|
expect(result).toHaveProperty('telemetryData');
|
2025-04-30 22:02:02 -04:00
|
|
|
expect(mockGenerateAnthropicText).toHaveBeenCalledTimes(2); // Initial + 1 retry
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
expect(mockLog).toHaveBeenCalledWith(
|
|
|
|
'info',
|
2025-04-30 22:02:02 -04:00
|
|
|
expect.stringContaining('Retryable error detected. Retrying')
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2025-05-01 17:11:51 -04:00
|
|
|
test('should use default project root or handle null if findProjectRoot returns null', async () => {
|
|
|
|
mockFindProjectRoot.mockReturnValue(null); // Simulate not finding root
|
|
|
|
mockGenerateAnthropicText.mockResolvedValue('Response with no root');
|
|
|
|
|
|
|
|
const params = { role: 'main', prompt: 'No root test' }; // No explicit root passed
|
|
|
|
await generateTextService(params);
|
|
|
|
|
|
|
|
expect(mockGetMainProvider).toHaveBeenCalledWith(null);
|
|
|
|
expect(mockGetParametersForRole).toHaveBeenCalledWith('main', null);
|
|
|
|
expect(mockResolveEnvVariable).toHaveBeenCalledWith(
|
|
|
|
'ANTHROPIC_API_KEY',
|
|
|
|
undefined,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
expect(mockGenerateAnthropicText).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2025-04-30 22:02:02 -04:00
|
|
|
// Add more tests for edge cases:
|
|
|
|
// - Missing API keys (should throw from _resolveApiKey)
|
|
|
|
// - Unsupported provider configured (should skip and log)
|
|
|
|
// - Missing provider/model config for a role (should skip and log)
|
|
|
|
// - Missing prompt
|
|
|
|
// - Different initial roles (research, fallback)
|
|
|
|
// - generateObjectService (mock schema, check object result)
|
|
|
|
// - streamTextService (more complex to test, might need stream helpers)
|
feat(config): Implement new config system and resolve refactoring errors Introduced config-manager.js and new utilities (resolveEnvVariable, findProjectRoot). Removed old global CONFIG object from utils.js. Updated .taskmasterconfig, mcp.json, and .env.example. Added generateComplexityAnalysisPrompt to ui.js. Removed unused updateSubtaskById from task-manager.js. Resolved SyntaxError and ReferenceError issues across commands.js, ui.js, task-manager.js, and ai-services.js by replacing CONFIG references with config-manager getters (getDebugFlag, getProjectName, getDefaultSubtasks, isApiKeySet). Refactored 'models' command to use getConfig/writeConfig. Simplified version checking. This stabilizes the codebase after initial Task 61 refactoring, fixing CLI errors and enabling subsequent work on Subtasks 61.34 and 61.35.
2025-04-20 01:09:30 -04:00
|
|
|
});
|
|
|
|
});
|