-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathjest.e2e.tools.setup.mjs
More file actions
148 lines (125 loc) · 4.9 KB
/
jest.e2e.tools.setup.mjs
File metadata and controls
148 lines (125 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Setup file for E2E tools tests
// This file runs before all tests and sets up the environment
// Load environment variables from .env files
import dotenv from 'dotenv';
dotenv.config();
dotenv.config({ path: '.env.test' });
// Set default environment variables for testing
process.env.NODE_ENV = process.env.NODE_ENV || 'test';
// GitHub API configuration
if (!process.env.GITHUB_TOKEN) {
console.warn('⚠️ GITHUB_TOKEN not set - using test token (some tests may be skipped)');
process.env.GITHUB_TOKEN = 'ghp_test_token_for_mocking';
}
if (!process.env.GITHUB_OWNER) {
console.warn('⚠️ GITHUB_OWNER not set - using test owner');
process.env.GITHUB_OWNER = 'test-owner';
}
if (!process.env.GITHUB_REPO) {
console.warn('⚠️ GITHUB_REPO not set - using test repo');
process.env.GITHUB_REPO = 'test-repo';
}
// AI API configuration
const aiKeys = [
'ANTHROPIC_API_KEY',
'OPENAI_API_KEY',
'GOOGLE_API_KEY',
'PERPLEXITY_API_KEY'
];
let hasAnyAIKey = false;
aiKeys.forEach(key => {
if (process.env[key]) {
hasAnyAIKey = true;
} else {
// Set test values for missing AI keys
process.env[key] = `test-${key.toLowerCase().replace('_', '-')}`;
}
});
if (!hasAnyAIKey && process.env.E2E_REAL_API === 'true') {
console.warn('⚠️ No AI API keys found - AI tool tests may be limited');
}
// AI model configuration
process.env.AI_MAIN_MODEL = process.env.AI_MAIN_MODEL || 'claude-3-5-sonnet-20241022';
process.env.AI_RESEARCH_MODEL = process.env.AI_RESEARCH_MODEL || 'perplexity-llama-3.1-sonar-large-128k-online';
process.env.AI_FALLBACK_MODEL = process.env.AI_FALLBACK_MODEL || 'gpt-4o';
process.env.AI_PRD_MODEL = process.env.AI_PRD_MODEL || 'claude-3-5-sonnet-20241022';
// Test configuration
process.env.CACHE_DIRECTORY = process.env.CACHE_DIRECTORY || './test-cache';
process.env.SYNC_ENABLED = process.env.SYNC_ENABLED || 'false';
process.env.WEBHOOK_SECRET = process.env.WEBHOOK_SECRET || 'test-webhook-secret';
process.env.WEBHOOK_PORT = process.env.WEBHOOK_PORT || '3001';
process.env.SSE_ENABLED = process.env.SSE_ENABLED || 'false';
// Logging configuration
process.env.LOG_LEVEL = process.env.LOG_LEVEL || 'warn';
// Test mode configuration
if (process.env.E2E_REAL_API === 'true') {
console.log('🔗 Running E2E tests with REAL API calls');
console.log('📋 GitHub:', process.env.GITHUB_OWNER + '/' + process.env.GITHUB_REPO);
console.log('🤖 AI Keys:', aiKeys.filter(key => process.env[key] && !process.env[key].startsWith('test-')).join(', ') || 'None (using test keys)');
} else {
console.log('🧪 Running E2E tests with MOCKED API calls');
}
// Global test timeout is set in jest config
// Handle unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
// Handle uncaught exceptions
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
});
// Cleanup function for test resources
global.testCleanup = {
resources: new Set(),
addResource(type, id) {
this.resources.add({ type, id });
},
async cleanup() {
// This would be used to clean up test resources in real API tests
if (process.env.E2E_REAL_API === 'true') {
console.log('🧹 Cleaning up test resources...');
// Implementation would depend on specific cleanup needs
}
this.resources.clear();
}
};
// Global test utilities
global.testUtils = {
// Generate unique test identifiers
generateTestId: () => `e2e-test-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
// Wait for condition
waitFor: async (condition, timeout = 10000, interval = 1000) => {
const start = Date.now();
while (Date.now() - start < timeout) {
if (await condition()) {
return true;
}
await new Promise(resolve => setTimeout(resolve, interval));
}
throw new Error(`Condition not met within ${timeout}ms`);
},
// Retry function with exponential backoff
retry: async (fn, maxAttempts = 3, baseDelay = 1000) => {
let lastError;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await fn();
} catch (error) {
lastError = error;
if (attempt === maxAttempts) break;
const delay = baseDelay * Math.pow(2, attempt - 1);
console.log(`Attempt ${attempt} failed, retrying in ${delay}ms...`);
await new Promise(resolve => setTimeout(resolve, delay));
}
}
throw lastError;
}
};
console.log('🚀 E2E Tools Test Environment Setup Complete');
console.log('📊 Test Configuration:');
console.log(' - Real API:', process.env.E2E_REAL_API === 'true' ? 'Yes' : 'No');
console.log(' - GitHub Token:', process.env.GITHUB_TOKEN ? 'Set' : 'Not Set');
console.log(' - AI Keys:', hasAnyAIKey ? 'Available' : 'Test Only');
console.log(' - Test Timeout:', '60 seconds');
console.log(' - Max Concurrency:', '1 (sequential)');
console.log('');