### 1. Middleware Tests Bypassing Authentication (12 test failures)
**Problem:**
The middleware tests were creating a MiddlewareManager with `NODE_ENV=test`, which caused all authentication middlewares to bypass their checks. This resulted in tests expecting 401 responses getting 200 instead.
### 2. Auth Helper Runtime Type Safety (2 test failures)
**Problem:**
The `validateAuthHeader` function assumed the input would always be a string, but the edge case tests were passing numbers and objects coerced with `as any`. This caused `authHeader.startsWith is not a function` errors.
**Root Cause:**
```typescript
export function validateAuthHeader(authHeader: string | undefined) {
if (!authHeader) { ... }
if (!authHeader.startsWith("Bearer ")) { ... } // ← Crashes if not a string!
}
```
**Solution:**
Added runtime type checking before using string methods:
```typescript
export function validateAuthHeader(authHeader: string | undefined) {
// Handle non-string inputs (runtime type safety)
if (typeof authHeader !== "string" || !authHeader) {
return {
success: false,
error: "Missing or invalid authorization header",
statusCode: 401,
};
}
if (!authHeader.startsWith("Bearer ")) { ... } // ← Safe now!
- [Middleware Tests](./MIDDLEWARE_TESTS.md) - Test suite overview
- [Test Router Refactor](./TEST_ROUTER_REFACTOR.md) - Test structure
---
## Conclusion
All 142 tests are now passing with proper authentication logic being tested. The fixes maintain backwards compatibility while improving test coverage and runtime safety.