Spaces:
Running
Running
File size: 11,560 Bytes
bdc8c47 35f09a0 bdc8c47 6aa3dd7 bdc8c47 6aa3dd7 bdc8c47 6aa3dd7 bdc8c47 6aa3dd7 35f09a0 6aa3dd7 35f09a0 6aa3dd7 35f09a0 6aa3dd7 35f09a0 6aa3dd7 35f09a0 6aa3dd7 35f09a0 6aa3dd7 35f09a0 6aa3dd7 35f09a0 6aa3dd7 bdc8c47 6aa3dd7 bdc8c47 7a224f7 |
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
import { Component, inject, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { MatProgressBarModule } from '@angular/material/progress-bar';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatExpansionModule } from '@angular/material/expansion';
import { MatListModule } from '@angular/material/list';
import { MatChipsModule } from '@angular/material/chips';
import { ApiService } from '../../services/api.service';
import { HttpClient } from '@angular/common/http';
interface TestResult {
name: string;
status: 'PASS' | 'FAIL' | 'RUNNING' | 'SKIPPED';
duration_ms?: number;
error?: string;
details?: string;
}
interface TestCategory {
name: string;
displayName: string;
tests: TestCase[];
selected: boolean;
expanded: boolean;
}
interface TestCase {
name: string;
category: string;
selected: boolean;
testFn: () => Promise<TestResult>;
}
@Component({
selector: 'app-test',
standalone: true,
imports: [
CommonModule,
FormsModule,
MatProgressBarModule,
MatCheckboxModule,
MatButtonModule,
MatIconModule,
MatExpansionModule,
MatListModule,
MatChipsModule
],
templateUrl: './test.component.html',
styleUrls: ['./test.component.scss']
})
export class TestComponent implements OnInit {
private apiService = inject(ApiService);
private http = inject(HttpClient);
running = false;
currentTest: string = '';
testResults: TestResult[] = [];
categories: TestCategory[] = [
{
name: 'auth',
displayName: 'Authentication Tests',
tests: [],
selected: false,
expanded: false
},
{
name: 'api',
displayName: 'API Endpoint Tests',
tests: [],
selected: false,
expanded: false
},
{
name: 'validation',
displayName: 'Validation Tests',
tests: [],
selected: false,
expanded: false
},
{
name: 'integration',
displayName: 'Integration Tests',
tests: [],
selected: false,
expanded: false
}
];
get allSelected(): boolean {
return this.categories.every(c => c.selected);
}
get selectedTests(): TestCase[] {
return this.categories
.filter(c => c.selected)
.flatMap(c => c.tests);
}
get totalTests(): number {
return this.categories.reduce((sum, c) => sum + c.tests.length, 0);
}
get passedTests(): number {
return this.testResults.filter(r => r.status === 'PASS').length;
}
get failedTests(): number {
return this.testResults.filter(r => r.status === 'FAIL').length;
}
get progress(): number {
if (this.testResults.length === 0) return 0;
return (this.testResults.length / this.selectedTests.length) * 100;
}
ngOnInit() {
this.initializeTests();
}
initializeTests() {
// Authentication Tests
this.addTest('auth', 'Login with valid credentials', async () => {
try {
const response = await this.http.post('/api/login', {
username: 'admin',
password: 'admin'
}).toPromise() as any;
return {
name: 'Login with valid credentials',
status: response?.token ? 'PASS' : 'FAIL',
duration_ms: 120
};
} catch (error) {
return {
name: 'Login with valid credentials',
status: 'FAIL',
error: 'Login failed',
duration_ms: 120
};
}
});
this.addTest('auth', 'Login with invalid credentials', async () => {
try {
await this.http.post('/api/login', {
username: 'admin',
password: 'wrong'
}).toPromise();
return {
name: 'Login with invalid credentials',
status: 'FAIL',
error: 'Expected 401 but got success',
duration_ms: 80
};
} catch (error: any) {
return {
name: 'Login with invalid credentials',
status: error.status === 401 ? 'PASS' : 'FAIL',
duration_ms: 80,
details: 'Correctly rejected invalid credentials'
};
}
});
// API Endpoint Tests
this.addTest('api', 'GET /api/environment', async () => {
const start = Date.now();
try {
const response = await this.apiService.getEnvironment().toPromise();
return {
name: 'GET /api/environment',
status: response?.work_mode ? 'PASS' : 'FAIL',
duration_ms: Date.now() - start
};
} catch (error) {
return {
name: 'GET /api/environment',
status: 'FAIL',
error: 'Failed to get environment',
duration_ms: Date.now() - start
};
}
});
this.addTest('api', 'GET /api/projects', async () => {
const start = Date.now();
try {
const response = await this.apiService.getProjects().toPromise();
return {
name: 'GET /api/projects',
status: Array.isArray(response) ? 'PASS' : 'FAIL',
duration_ms: Date.now() - start,
details: `Retrieved ${response?.length || 0} projects`
};
} catch (error) {
return {
name: 'GET /api/projects',
status: 'FAIL',
error: 'Failed to get projects',
duration_ms: Date.now() - start
};
}
});
this.addTest('api', 'GET /api/apis', async () => {
const start = Date.now();
try {
const response = await this.apiService.getAPIs().toPromise();
return {
name: 'GET /api/apis',
status: Array.isArray(response) ? 'PASS' : 'FAIL',
duration_ms: Date.now() - start,
details: `Retrieved ${response?.length || 0} APIs`
};
} catch (error) {
return {
name: 'GET /api/apis',
status: 'FAIL',
error: 'Failed to get APIs',
duration_ms: Date.now() - start
};
}
});
// Integration Tests
this.addTest('integration', 'Create and delete project', async () => {
const start = Date.now();
let projectId: number | undefined;
try {
// Create project
const createResponse = await this.apiService.createProject({
name: `test_project_${Date.now()}`,
caption: 'Test Project'
}).toPromise() as any;
if (!createResponse?.id) {
throw new Error('Project creation failed');
}
projectId = createResponse.id;
// Delete project
await this.apiService.deleteProject(projectId).toPromise();
return {
name: 'Create and delete project',
status: 'PASS',
duration_ms: Date.now() - start,
details: 'Project lifecycle test passed'
};
} catch (error: any) {
// Try to clean up if project was created
if (projectId !== undefined) {
try {
await this.apiService.deleteProject(projectId).toPromise();
} catch {}
}
return {
name: 'Create and delete project',
status: 'FAIL',
error: error.message || 'Test failed',
duration_ms: Date.now() - start
};
}
});
this.addTest('integration', 'API used in intent cannot be deleted', async () => {
const start = Date.now();
try {
// Try to delete an API that's used in intents
await this.apiService.deleteAPI('book_flight_api').toPromise();
return {
name: 'API used in intent cannot be deleted',
status: 'FAIL',
error: 'Expected error but API was deleted',
duration_ms: Date.now() - start
};
} catch (error: any) {
return {
name: 'API used in intent cannot be deleted',
status: error.status === 400 ? 'PASS' : 'FAIL',
duration_ms: Date.now() - start,
details: 'Correctly prevented deletion of API in use'
};
}
});
// Validation Tests
this.addTest('validation', 'Regex validation - valid pattern', async () => {
const start = Date.now();
try {
const response = await this.apiService.validateRegex('^[A-Z]{3}$', 'ABC').toPromise();
return {
name: 'Regex validation - valid pattern',
status: response.valid && response.matches ? 'PASS' : 'FAIL',
duration_ms: Date.now() - start
};
} catch (error) {
return {
name: 'Regex validation - valid pattern',
status: 'FAIL',
error: 'Validation failed',
duration_ms: Date.now() - start
};
}
});
this.addTest('validation', 'Regex validation - invalid pattern', async () => {
const start = Date.now();
try {
const response = await this.apiService.validateRegex('[invalid', 'test').toPromise();
return {
name: 'Regex validation - invalid pattern',
status: !response.valid ? 'PASS' : 'FAIL',
duration_ms: Date.now() - start,
details: 'Correctly identified invalid regex'
};
} catch (error) {
return {
name: 'Regex validation - invalid pattern',
status: 'PASS',
duration_ms: Date.now() - start,
details: 'Correctly rejected invalid regex'
};
}
});
// Update test counts
this.categories.forEach(cat => {
cat.displayName = `${cat.displayName} (${cat.tests.length} tests)`;
});
}
private addTest(category: string, name: string, testFn: () => Promise<TestResult>) {
const cat = this.categories.find(c => c.name === category);
if (cat) {
cat.tests.push({
name,
category,
selected: true,
testFn
});
}
}
toggleAll() {
const newState = !this.allSelected;
this.categories.forEach(c => c.selected = newState);
}
async runAllTests() {
this.categories.forEach(c => c.selected = true);
await this.runTests();
}
async runSelectedTests() {
await this.runTests();
}
async runTests() {
if (this.running) return;
this.running = true;
this.testResults = [];
this.currentTest = '';
const testsToRun = this.selectedTests;
for (const test of testsToRun) {
this.currentTest = test.name;
try {
const result = await test.testFn();
this.testResults.push(result);
} catch (error: any) {
this.testResults.push({
name: test.name,
status: 'FAIL',
error: error.message || 'Unexpected error'
});
}
// Small delay between tests
await new Promise(resolve => setTimeout(resolve, 100));
}
this.running = false;
this.currentTest = '';
}
stopTests() {
this.running = false;
this.currentTest = '';
}
getTestResult(testName: string): TestResult | undefined {
return this.testResults.find(r => r.name === testName);
}
getCategoryResults(category: TestCategory): { passed: number; failed: number; total: number } {
const categoryResults = this.testResults.filter(r =>
category.tests.some(t => t.name === r.name)
);
return {
passed: categoryResults.filter(r => r.status === 'PASS').length,
failed: categoryResults.filter(r => r.status === 'FAIL').length,
total: category.tests.length
};
}
} |