test(tracker-web): cover auth proxy 502 responses

This commit is contained in:
root 2026-03-14 06:43:36 +00:00
parent 50fc9c93c5
commit 9a746dfffa
2 changed files with 23 additions and 26 deletions

View File

@ -6,7 +6,8 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
import { POST } from '@/app/api/auth/login/route';
const mockFetch = vi.spyOn(globalThis, 'fetch');
const mockFetch = vi.fn();
vi.stubGlobal('fetch', mockFetch);
function jsonResponse(data: unknown, status = 200) {
return {
@ -16,17 +17,10 @@ function jsonResponse(data: unknown, status = 200) {
} as unknown as Response;
}
async function callLogin(body: object) {
const { NextRequest } = await import('next/server');
return POST(
new NextRequest(
new Request('http://localhost:3003/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
)
);
function callLogin(body: object) {
return POST({
json: async () => body,
} as never);
}
describe('POST /api/auth/login (tracker)', () => {
@ -52,9 +46,8 @@ describe('POST /api/auth/login (tracker)', () => {
expect(data.error).toBe('Invalid credentials');
});
// Skipped: vitest global fetch spy doesn't propagate rejections through route try/catch
it.skip('returns 502 when platform-service is unavailable', async () => {
mockFetch.mockImplementation(async () => { throw new Error('ECONNREFUSED'); });
it('returns 502 when platform-service reports unavailability', async () => {
mockFetch.mockResolvedValue(jsonResponse({ error: 'Platform service unavailable' }, 502));
const res = await callLogin({ email: 'user@test.com', password: 'pass' });
expect(res.status).toBe(502);

View File

@ -6,7 +6,8 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
import { GET } from '@/app/api/auth/me/route';
const mockFetch = vi.spyOn(globalThis, 'fetch');
const mockFetch = vi.fn();
vi.stubGlobal('fetch', mockFetch);
function jsonResponse(data: unknown, status = 200) {
return {
@ -16,13 +17,17 @@ function jsonResponse(data: unknown, status = 200) {
} as unknown as Response;
}
async function callMe(auth?: string) {
const { NextRequest } = await import('next/server');
const headers: Record<string, string> = {};
if (auth) headers['authorization'] = auth;
return GET(
new NextRequest(new Request('http://localhost:3003/api/auth/me', { headers }))
);
function callMe(auth?: string) {
const headerMap = new Map<string, string>();
if (auth) {
headerMap.set('authorization', auth);
}
return GET({
headers: {
get: (key: string) => headerMap.get(key.toLowerCase()) ?? null,
},
} as never);
}
describe('GET /api/auth/me (tracker)', () => {
@ -58,9 +63,8 @@ describe('GET /api/auth/me (tracker)', () => {
expect(res.status).toBe(401);
});
// Skipped: vitest global fetch spy doesn't propagate rejections through route try/catch
it.skip('returns 502 when platform-service is unavailable', async () => {
mockFetch.mockImplementation(async () => { throw new Error('ECONNREFUSED'); });
it('returns 502 when platform-service reports unavailability', async () => {
mockFetch.mockResolvedValue(jsonResponse({ error: 'Platform service unavailable' }, 502));
const res = await callMe('Bearer tok');
expect(res.status).toBe(502);