Files
freno-dev/src/server/fetch-utils.test.ts
Michael Freno 898c891bd5 chore: remediate pygienium audit findings
Dead code: 77 verified-unused exports, files (BackArrow, MenuBars,
cookies.ts, db/create.ts, schemas/comment.ts, security-headers.ts) and
12 unused dependencies removed
Comments: ~370 RESTATE comments stripped across 53 files; 2 verbose
blocks tightened; dead commented-out config removed
Complexity: bulkUpsert extracted into 11 per-entity helpers (CCN 156->~10);
login formHandler split into 3 submitters (CCN 63->~5); account page render
split into 8 section components (CCN 40); updatePost SQL builder rebuilt;
assert*Owned consolidated behind generic assertOwnedBy
Defensive guards: 4 redundant rethrow/nullish guards removed
2026-08-11 13:36:18 -04:00

116 lines
3.1 KiB
TypeScript

// Manual test file for fetch-utils error handling
import {
fetchWithTimeout,
checkResponse,
NetworkError,
TimeoutError,
APIError,
fetchWithRetry
} from "./fetch-utils";
async function testTimeoutError() {
console.log("\n=== Testing Timeout Error ===");
try {
await fetchWithTimeout("https://httpbin.org/delay/10", { timeout: 1 });
console.log("❌ Should have thrown TimeoutError");
} catch (error) {
if (error instanceof TimeoutError) {
console.log("✅ TimeoutError caught correctly");
console.log(` Message: ${error.message}`);
console.log(` Timeout: ${error.timeoutMs}ms`);
} else {
console.log("❌ Wrong error type:", error);
}
}
}
async function testNetworkError() {
console.log("\n=== Testing Network Error ===");
try {
await fetchWithTimeout(
"https://invalid-domain-that-does-not-exist-12345.com"
);
console.log("❌ Should have thrown NetworkError");
} catch (error) {
if (error instanceof NetworkError) {
console.log("✅ NetworkError caught correctly");
console.log(` Message: ${error.message}`);
} else {
console.log("❌ Wrong error type:", error);
}
}
}
async function testAPIError() {
console.log("\n=== Testing API Error ===");
try {
const response = await fetchWithTimeout("https://httpbin.org/status/404");
await checkResponse(response);
console.log("❌ Should have thrown APIError");
} catch (error) {
if (error instanceof APIError) {
console.log("✅ APIError caught correctly");
console.log(` Message: ${error.message}`);
console.log(` Status: ${error.status}`);
} else {
console.log("❌ Wrong error type:", error);
}
}
}
async function testSuccessfulRequest() {
console.log("\n=== Testing Successful Request ===");
try {
const response = await fetchWithTimeout("https://httpbin.org/get", {
timeout: 10000
});
await checkResponse(response);
const data = await response.json();
console.log("✅ Successful request");
console.log(` URL: ${data.url}`);
} catch (error) {
console.log("❌ Should not have thrown error:", error);
}
}
async function testRetryLogic() {
console.log("\n=== Testing Retry Logic ===");
let attempts = 0;
try {
await fetchWithRetry(
async () => {
attempts++;
console.log(` Attempt ${attempts}`);
throw new NetworkError("Simulated network error");
},
{
maxRetries: 2,
retryDelay: 100
}
);
console.log("❌ Should have thrown error after retries");
} catch (error) {
if (error instanceof NetworkError && attempts === 3) {
console.log("✅ Retry logic worked correctly");
console.log(` Total attempts: ${attempts}`);
} else {
console.log("❌ Wrong behavior:", { error, attempts });
}
}
}
async function runTests() {
console.log("Starting fetch-utils tests...\n");
await testTimeoutError();
await testNetworkError();
await testAPIError();
await testSuccessfulRequest();
await testRetryLogic();
console.log("\n=== All tests completed ===\n");
}
runTests();