mirror of
https://github.com/mileswolfallen2/miles-web.git
synced 2026-12-05 16:20:44 +00:00
Add comprehensive test suite for application functionality
- Implement tests for HTTP API endpoints, ensuring correct responses and security headers. - Create configuration tests to validate loading of defaults and environment overrides. - Add tests for FEDL service to verify data handling and snapshot functionality. - Introduce GitHub service tests to check API data normalization and caching behavior. - Develop monitor service tests for network checks and uptime calculations. - Implement persistence tests to ensure database migrations are idempotent and data persists across restarts. - Add vault service tests for session management and brute-force protection mechanisms. - Include helper functions for test context creation and JSON response handling.
This commit is contained in:
1 parent
84b7a7e302
commit
c250653961
55 files changed
+8174
-226
No files matched your search
@@ -0,0 +1,50 @@
|
||||
export function classifyHttpStatus(code) {
|
||||
if (code >= 200 && code < 400) return 'operational';
|
||||
if (code >= 400 && code < 500) return 'degraded';
|
||||
if (code >= 500) return 'offline';
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
export async function checkHttp(
|
||||
url,
|
||||
{ timeoutMs = 5000, fetchFn = globalThis.fetch, now = () => Date.now() } = {}
|
||||
) {
|
||||
const started = now();
|
||||
try {
|
||||
const response = await fetchFn(url, {
|
||||
method: 'GET',
|
||||
redirect: 'follow',
|
||||
signal: AbortSignal.timeout(timeoutMs),
|
||||
headers: {
|
||||
'User-Agent': 'milesallen-status/1.0 (+https://milesallen.site)',
|
||||
Accept: 'text/html,application/xhtml+xml,*/*;q=0.8',
|
||||
'Accept-Encoding': 'gzip, deflate, br'
|
||||
}
|
||||
});
|
||||
const responseMs = Math.max(0, now() - started);
|
||||
try {
|
||||
await response.body?.cancel();
|
||||
} catch {
|
||||
// ignore body cancellation errors
|
||||
}
|
||||
return {
|
||||
status: classifyHttpStatus(response.status),
|
||||
responseMs,
|
||||
httpStatus: response.status,
|
||||
error: null
|
||||
};
|
||||
} catch (err) {
|
||||
const responseMs = Math.max(0, now() - started);
|
||||
const name = err?.name || '';
|
||||
let message = 'request failed';
|
||||
if (name === 'TimeoutError' || name === 'AbortError') message = 'timeout';
|
||||
else if (err?.cause?.code) message = String(err.cause.code);
|
||||
else if (err?.message) message = String(err.message);
|
||||
return {
|
||||
status: 'offline',
|
||||
responseMs,
|
||||
httpStatus: null,
|
||||
error: message.slice(0, 200)
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user