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'; 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) { function jsonResponse(data: unknown, status = 200) {
return { return {
@ -16,17 +17,10 @@ function jsonResponse(data: unknown, status = 200) {
} as unknown as Response; } as unknown as Response;
} }
async function callLogin(body: object) { function callLogin(body: object) {
const { NextRequest } = await import('next/server'); return POST({
return POST( json: async () => body,
new NextRequest( } as never);
new Request('http://localhost:3003/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
)
);
} }
describe('POST /api/auth/login (tracker)', () => { describe('POST /api/auth/login (tracker)', () => {
@ -52,9 +46,8 @@ describe('POST /api/auth/login (tracker)', () => {
expect(data.error).toBe('Invalid credentials'); expect(data.error).toBe('Invalid credentials');
}); });
// Skipped: vitest global fetch spy doesn't propagate rejections through route try/catch it('returns 502 when platform-service reports unavailability', async () => {
it.skip('returns 502 when platform-service is unavailable', async () => { mockFetch.mockResolvedValue(jsonResponse({ error: 'Platform service unavailable' }, 502));
mockFetch.mockImplementation(async () => { throw new Error('ECONNREFUSED'); });
const res = await callLogin({ email: 'user@test.com', password: 'pass' }); const res = await callLogin({ email: 'user@test.com', password: 'pass' });
expect(res.status).toBe(502); 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'; 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) { function jsonResponse(data: unknown, status = 200) {
return { return {
@ -16,13 +17,17 @@ function jsonResponse(data: unknown, status = 200) {
} as unknown as Response; } as unknown as Response;
} }
async function callMe(auth?: string) { function callMe(auth?: string) {
const { NextRequest } = await import('next/server'); const headerMap = new Map<string, string>();
const headers: Record<string, string> = {}; if (auth) {
if (auth) headers['authorization'] = auth; headerMap.set('authorization', auth);
return GET( }
new NextRequest(new Request('http://localhost:3003/api/auth/me', { headers }))
); return GET({
headers: {
get: (key: string) => headerMap.get(key.toLowerCase()) ?? null,
},
} as never);
} }
describe('GET /api/auth/me (tracker)', () => { describe('GET /api/auth/me (tracker)', () => {
@ -58,9 +63,8 @@ describe('GET /api/auth/me (tracker)', () => {
expect(res.status).toBe(401); expect(res.status).toBe(401);
}); });
// Skipped: vitest global fetch spy doesn't propagate rejections through route try/catch it('returns 502 when platform-service reports unavailability', async () => {
it.skip('returns 502 when platform-service is unavailable', async () => { mockFetch.mockResolvedValue(jsonResponse({ error: 'Platform service unavailable' }, 502));
mockFetch.mockImplementation(async () => { throw new Error('ECONNREFUSED'); });
const res = await callMe('Bearer tok'); const res = await callMe('Bearer tok');
expect(res.status).toBe(502); expect(res.status).toBe(502);