feat: run tests in background with progress polling

Improve the testing dashboard to run pytest in the background:

- Add background task execution using FastAPI's BackgroundTasks
- Create test_runner_tasks.py following existing background task pattern
- API now returns immediately after starting the test run
- Frontend polls for status every 2 seconds until completion
- Show running indicator with elapsed time counter
- Resume polling if user navigates away and returns while tests running
- Tests continue running even if user closes the page

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-12 23:20:26 +01:00
parent 6e3ae4bebb
commit 0e6c9e3eea
5 changed files with 228 additions and 41 deletions

View File

@@ -20,6 +20,10 @@ function testingDashboard() {
collecting: false,
error: null,
successMessage: null,
activeRunId: null,
pollInterval: null,
elapsedTime: 0,
elapsedTimer: null,
// Statistics
stats: {
@@ -46,6 +50,30 @@ function testingDashboard() {
testingDashboardLog.info('Initializing testing dashboard');
await this.loadStats();
await this.loadRuns();
// Check if there's a running test and resume polling
await this.checkForRunningTests();
},
async checkForRunningTests() {
// Check if there's already a test running
const runningRun = this.runs.find(r => r.status === 'running');
if (runningRun) {
testingDashboardLog.info('Found running test:', runningRun.id);
this.running = true;
this.activeRunId = runningRun.id;
// Calculate elapsed time from when the run started
const startTime = new Date(runningRun.timestamp);
this.elapsedTime = Math.floor((Date.now() - startTime.getTime()) / 1000);
// Start elapsed time counter
this.elapsedTimer = setInterval(() => {
this.elapsedTime++;
}, 1000);
// Start polling for status
this.pollInterval = setInterval(() => this.pollRunStatus(), 2000);
}
},
async loadStats() {
@@ -84,45 +112,89 @@ function testingDashboard() {
this.running = true;
this.error = null;
this.successMessage = null;
this.elapsedTime = 0;
testingDashboardLog.info('Running tests:', testPath);
testingDashboardLog.info('Starting tests:', testPath);
try {
// Start the test run (returns immediately)
const result = await apiClient.post('/admin/tests/run', {
test_path: testPath
});
testingDashboardLog.info('Test run completed:', result);
testingDashboardLog.info('Test run started:', result);
this.activeRunId = result.id;
// Format success message
const status = result.status === 'passed' ? 'All tests passed!' : 'Tests completed with failures.';
this.successMessage = `${status} ${result.passed}/${result.total_tests} passed (${result.pass_rate.toFixed(1)}%) in ${this.formatDuration(result.duration_seconds)}`;
// Start elapsed time counter
this.elapsedTimer = setInterval(() => {
this.elapsedTime++;
}, 1000);
// Reload stats and runs
await this.loadStats();
await this.loadRuns();
// Start polling for status
this.pollInterval = setInterval(() => this.pollRunStatus(), 2000);
// Show toast notification
Utils.showToast(this.successMessage, result.status === 'passed' ? 'success' : 'warning');
Utils.showToast('Test run started...', 'info');
// Clear success message after 10 seconds
setTimeout(() => {
this.successMessage = null;
}, 10000);
} catch (err) {
testingDashboardLog.error('Failed to run tests:', err);
testingDashboardLog.error('Failed to start tests:', err);
this.error = err.message;
Utils.showToast('Failed to run tests: ' + err.message, 'error');
this.running = false;
Utils.showToast('Failed to start tests: ' + err.message, 'error');
// Redirect to login if unauthorized
if (err.message.includes('Unauthorized')) {
window.location.href = '/admin/login';
}
} finally {
this.running = false;
}
},
async pollRunStatus() {
if (!this.activeRunId) return;
try {
const run = await apiClient.get(`/admin/tests/runs/${this.activeRunId}`);
if (run.status !== 'running') {
// Test run completed
this.stopPolling();
testingDashboardLog.info('Test run completed:', run);
// Format success message
const status = run.status === 'passed' ? 'All tests passed!' : 'Tests completed with failures.';
this.successMessage = `${status} ${run.passed}/${run.total_tests} passed (${run.pass_rate.toFixed(1)}%) in ${this.formatDuration(run.duration_seconds)}`;
// Reload stats and runs
await this.loadStats();
await this.loadRuns();
// Show toast notification
Utils.showToast(this.successMessage, run.status === 'passed' ? 'success' : 'warning');
// Clear success message after 10 seconds
setTimeout(() => {
this.successMessage = null;
}, 10000);
}
} catch (err) {
testingDashboardLog.error('Failed to poll run status:', err);
// Don't stop polling on error, might be transient
}
},
stopPolling() {
if (this.pollInterval) {
clearInterval(this.pollInterval);
this.pollInterval = null;
}
if (this.elapsedTimer) {
clearInterval(this.elapsedTimer);
this.elapsedTimer = null;
}
this.running = false;
this.activeRunId = null;
},
async collectTests() {
this.collecting = true;
this.error = null;