fix: resolve critical app functionality issues

Complete implementation of fixes for queue processing, SSE connection display, service worker installation, and failing tests.

Key Changes:
- Fix queue processor startup with proper import and subscription mechanism
- Implement centralized API error handling middleware for proper HTTP status codes
- Enhance service worker configuration for PWA compliance and reliability
- Fix SSE connection display with reactive state management
- Add comprehensive test coverage and health check endpoints

Results:
- All 169 tests now passing (previously 16 failing)
- Queue items process immediately from pending to success/error states
- Real-time SSE connection status with auto-reconnection logic
- Proper PWA functionality with working service worker registration
- API endpoints return correct HTTP status codes (400/404/409) instead of 500 errors

This resolves the critical issues preventing core app functionality and enables proper production deployment.
This commit is contained in:
Giancarmine Salucci
2025-12-22 04:27:59 +01:00
parent b60f96a75e
commit 93aa25a31c
25 changed files with 3243 additions and 559 deletions

View File

@@ -6,9 +6,11 @@
* - GET /api/queue - List all queue items with optional status filtering
*/
import { json, error } from '@sveltejs/kit';
import { json } from '@sveltejs/kit';
import { queueManager } from '$lib/server/queue/QueueManager';
import { validateInstagramUrl } from '$lib/server/validation/instagram-url';
import { handleApiError } from '$lib/server/api/errorHandler';
import { ValidationError } from '$lib/server/api/errors';
import type { RequestHandler } from './$types';
/**
@@ -27,25 +29,25 @@ export const POST: RequestHandler = async ({ request }) => {
try {
body = await request.json();
} catch (jsonError) {
return error(400, { message: 'Invalid JSON in request body' });
throw new ValidationError('Invalid JSON in request body');
}
// Validate request body
if (!body || typeof body !== 'object') {
return error(400, { message: 'Request body must be JSON object' });
throw new ValidationError('Request body must be JSON object');
}
const { url } = body;
// Validate URL presence
if (!url || typeof url !== 'string') {
return error(400, { message: 'URL is required and must be a string' });
throw new ValidationError('URL is required and must be a string');
}
// Validate Instagram URL format using utility
const validation = validateInstagramUrl(url);
if (!validation.valid) {
return error(400, { message: validation.error || 'Invalid Instagram URL' });
throw new ValidationError(validation.error || 'Invalid Instagram URL');
}
// Enqueue the URL
@@ -59,9 +61,8 @@ export const POST: RequestHandler = async ({ request }) => {
enqueuedAt: queueItem.enqueuedAt
});
} catch (err) {
console.error('Failed to enqueue URL:', err);
return error(500, { message: 'Internal server error' });
} catch (error) {
return handleApiError(error);
}
};
@@ -89,10 +90,10 @@ export const GET: RequestHandler = async ({ url }) => {
if (limitParam) {
const parsedLimit = parseInt(limitParam, 10);
if (isNaN(parsedLimit) || parsedLimit < 1) {
return error(400, { message: 'Limit must be a positive integer' });
throw new ValidationError('Limit must be a positive integer');
}
if (parsedLimit > 200) {
return error(400, { message: 'Limit cannot exceed 200' });
throw new ValidationError('Limit cannot exceed 200');
}
limit = parsedLimit;
}
@@ -102,7 +103,7 @@ export const GET: RequestHandler = async ({ url }) => {
if (offsetParam) {
const parsedOffset = parseInt(offsetParam, 10);
if (isNaN(parsedOffset) || parsedOffset < 0) {
return error(400, { message: 'Offset must be a non-negative integer' });
throw new ValidationError('Offset must be a non-negative integer');
}
offset = parsedOffset;
}
@@ -110,9 +111,9 @@ export const GET: RequestHandler = async ({ url }) => {
// Validate status filter
const validStatuses = ['pending', 'in_progress', 'success', 'unhealthy', 'error'];
if (statusFilter && !validStatuses.includes(statusFilter)) {
return error(400, {
message: `Invalid status filter. Must be one of: ${validStatuses.join(', ')}`
});
throw new ValidationError(
`Invalid status filter. Must be one of: ${validStatuses.join(', ')}`
);
}
// Get all items
@@ -142,8 +143,7 @@ export const GET: RequestHandler = async ({ url }) => {
}
});
} catch (err) {
console.error('Failed to list queue items:', err);
return error(500, { message: 'Internal server error' });
} catch (error) {
return handleApiError(error);
}
};