102 lines
3.1 KiB
TypeScript
102 lines
3.1 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { proxyMediaRequest, getMediaContentType, getMediaCacheControl } from './MediaProxyAdapter';
|
|
|
|
// Mock fetch globally
|
|
const mockFetch = vi.fn();
|
|
global.fetch = mockFetch;
|
|
|
|
describe('MediaProxyAdapter', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe('proxyMediaRequest', () => {
|
|
it('should successfully proxy media and return ArrayBuffer', async () => {
|
|
const mockBuffer = new ArrayBuffer(8);
|
|
const mockResponse = {
|
|
ok: true,
|
|
arrayBuffer: vi.fn().mockResolvedValue(mockBuffer),
|
|
};
|
|
|
|
mockFetch.mockResolvedValue(mockResponse);
|
|
|
|
const result = await proxyMediaRequest('/media/avatar/123');
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBe(mockBuffer);
|
|
expect(mockFetch).toHaveBeenCalledWith(
|
|
'http://localhost:3000/media/avatar/123',
|
|
{ method: 'GET' }
|
|
);
|
|
});
|
|
|
|
it('should handle 404 errors', async () => {
|
|
const mockResponse = {
|
|
ok: false,
|
|
status: 404,
|
|
statusText: 'Not Found',
|
|
};
|
|
|
|
mockFetch.mockResolvedValue(mockResponse);
|
|
|
|
const result = await proxyMediaRequest('/media/avatar/999');
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
const error = result.getError();
|
|
expect(error.type).toBe('notFound');
|
|
expect(error.message).toContain('Media not found');
|
|
});
|
|
|
|
it('should handle other HTTP errors', async () => {
|
|
const mockResponse = {
|
|
ok: false,
|
|
status: 500,
|
|
statusText: 'Internal Server Error',
|
|
};
|
|
|
|
mockFetch.mockResolvedValue(mockResponse);
|
|
|
|
const result = await proxyMediaRequest('/media/avatar/123');
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
const error = result.getError();
|
|
expect(error.type).toBe('serverError');
|
|
expect(error.message).toContain('HTTP 500');
|
|
});
|
|
|
|
it('should handle network errors', async () => {
|
|
mockFetch.mockRejectedValue(new Error('Network error'));
|
|
|
|
const result = await proxyMediaRequest('/media/avatar/123');
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
const error = result.getError();
|
|
expect(error.type).toBe('networkError');
|
|
expect(error.message).toContain('Failed to fetch media');
|
|
});
|
|
|
|
it('should use custom API base URL from environment', () => {
|
|
process.env.API_BASE_URL = 'https://api.example.com';
|
|
|
|
// Just verify the function exists and can be called
|
|
expect(typeof proxyMediaRequest).toBe('function');
|
|
|
|
// Reset
|
|
delete process.env.API_BASE_URL;
|
|
});
|
|
});
|
|
|
|
describe('getMediaContentType', () => {
|
|
it('should return image/png for all media paths', () => {
|
|
expect(getMediaContentType('/media/avatar/123')).toBe('image/png');
|
|
expect(getMediaContentType('/media/teams/456/logo')).toBe('image/png');
|
|
expect(getMediaContentType('/media/leagues/789/cover')).toBe('image/png');
|
|
});
|
|
});
|
|
|
|
describe('getMediaCacheControl', () => {
|
|
it('should return public cache control with max-age', () => {
|
|
expect(getMediaCacheControl()).toBe('public, max-age=3600');
|
|
});
|
|
});
|
|
}); |