Fix all 22 failing unit tests in Gate 2.1

- Fixed API client: Changed response.json().catch() to try-catch pattern (14 tests)
- Fixed auth middleware: Check for undefined user before accessing user.level (1 test)
- Fixed compiler: Treat empty string as undefined for map field (3 tests)
- Fixed retry tests: Use callCount variable instead of mock.calls.length (4 tests)
- Fixed unhandled rejection warnings: Add catch handlers in error tests

Co-authored-by: johndoe6345789 <224850594+johndoe6345789@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-01-08 18:03:40 +00:00
parent 58fe048857
commit 7e3851c93b
4 changed files with 41 additions and 11 deletions

View File

@@ -20,9 +20,11 @@ describe('retry utilities', () => {
{ statusCode: 503, shouldRetry: true, description: 'service unavailable (retryable)' },
{ statusCode: 429, shouldRetry: true, description: 'rate limited (retryable)' },
])('should handle $description correctly', async ({ statusCode, shouldRetry }) => {
let callCount = 0
const mockFetch = vi.fn(async () => { // eslint-disable-line @typescript-eslint/require-await
const currentCall = callCount++
return new Response(JSON.stringify({ test: 'data' }), {
status: shouldRetry ? (mockFetch.mock.calls.length === 0 ? statusCode : 200) : statusCode,
status: shouldRetry ? (currentCall === 0 ? statusCode : 200) : statusCode,
})
})
@@ -112,9 +114,10 @@ describe('retry utilities', () => {
throw new Error('Network error')
})
const promise = retryFetch(mockFetch, { maxRetries: 2, initialDelayMs: 10 })
// Fast-forward through all retry delays
const promise = retryFetch(mockFetch, { maxRetries: 2, initialDelayMs: 10 })
// Attach a catch handler to prevent unhandled rejection warning
promise.catch(() => {})
await vi.advanceTimersByTimeAsync(500)
await expect(promise).rejects.toThrow('Network error')
@@ -180,8 +183,10 @@ describe('retry utilities', () => {
throw new Error('Persistent error')
})
// Fast-forward through all retry delays
const promise = retry(mockFn, { maxRetries: 2, initialDelayMs: 10 })
// Attach a catch handler to prevent unhandled rejection warning
promise.catch(() => {})
await vi.advanceTimersByTimeAsync(500)
await expect(promise).rejects.toThrow('Persistent error')

View File

@@ -37,7 +37,7 @@ export async function compile(source: string, options?: CompileOptions): Promise
return {
code: result.code,
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/prefer-nullish-coalescing
map: (result.map !== null && result.map !== undefined) ? result.map : undefined,
map: (result.map !== null && result.map !== undefined && result.map !== '') ? result.map : undefined,
}
} catch (error) {
// Return compilation error as a comment in the code

View File

@@ -72,7 +72,12 @@ export async function fetchEntityList(
})
if (!response.ok) {
const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) as { error?: string }
let errorData: { error?: string } = { error: 'Unknown error' }
try {
errorData = await response.json() as { error?: string }
} catch {
// If JSON parsing fails, use default error
}
return {
error: errorData.error ?? `HTTP ${response.status}`,
@@ -121,7 +126,12 @@ export async function fetchEntity(
})
if (!response.ok) {
const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) as { error?: string }
let errorData: { error?: string } = { error: 'Unknown error' }
try {
errorData = await response.json() as { error?: string }
} catch {
// If JSON parsing fails, use default error
}
return {
error: errorData.error ?? `HTTP ${response.status}`,
@@ -172,7 +182,12 @@ export async function createEntity(
})
if (!response.ok) {
const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) as { error?: string }
let errorData: { error?: string } = { error: 'Unknown error' }
try {
errorData = await response.json() as { error?: string }
} catch {
// If JSON parsing fails, use default error
}
return {
error: errorData.error ?? `HTTP ${response.status}`,
@@ -225,7 +240,12 @@ export async function updateEntity(
})
if (!response.ok) {
const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) as { error?: string }
let errorData: { error?: string } = { error: 'Unknown error' }
try {
errorData = await response.json() as { error?: string }
} catch {
// If JSON parsing fails, use default error
}
return {
error: errorData.error ?? `HTTP ${response.status}`,
@@ -275,7 +295,12 @@ export async function deleteEntity(
})
if (!response.ok) {
const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) as { error?: string }
let errorData: { error?: string } = { error: 'Unknown error' }
try {
errorData = await response.json() as { error?: string }
} catch {
// If JSON parsing fails, use default error
}
return {
error: errorData.error ?? `HTTP ${response.status}`,

View File

@@ -85,7 +85,7 @@ export async function authenticate(
const user = await getCurrentUser()
// Check if user is authenticated
if (user === null) {
if (user === null || user === undefined) {
return {
success: false,
error: NextResponse.json(