|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 3 | +import { GET } from './route'; |
| 4 | +import { GET as getStreakSvg } from '../route'; |
| 5 | +import { NextResponse } from 'next/server'; |
| 6 | + |
| 7 | +// Mock the core route |
| 8 | +vi.mock('../route', () => ({ |
| 9 | + GET: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +// Mock resvg-js |
| 13 | +vi.mock('@resvg/resvg-js', () => { |
| 14 | + return { |
| 15 | + Resvg: class { |
| 16 | + constructor() { |
| 17 | + if ((global as any).throwResvgError) { |
| 18 | + throw new Error('Resvg crash'); |
| 19 | + } |
| 20 | + } |
| 21 | + render() { |
| 22 | + return { |
| 23 | + asPng: () => Buffer.from('mock-png-data'), |
| 24 | + }; |
| 25 | + } |
| 26 | + }, |
| 27 | + }; |
| 28 | +}); |
| 29 | + |
| 30 | +describe('PNG Route', () => { |
| 31 | + beforeEach(() => { |
| 32 | + vi.clearAllMocks(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('converts SVG to PNG successfully', async () => { |
| 36 | + const mockRequest = new Request('http://localhost:3000/api/streak/png?user=testuser'); |
| 37 | + |
| 38 | + // Setup mock SVG response |
| 39 | + const mockSvgResponse = new NextResponse('<svg>test</svg>', { |
| 40 | + status: 200, |
| 41 | + headers: { |
| 42 | + 'Content-Type': 'image/svg+xml', |
| 43 | + 'Cache-Control': 'public, max-age=3600', |
| 44 | + }, |
| 45 | + }); |
| 46 | + vi.mocked(getStreakSvg).mockResolvedValue(mockSvgResponse); |
| 47 | + |
| 48 | + const response = await GET(mockRequest); |
| 49 | + |
| 50 | + expect(response.status).toBe(200); |
| 51 | + expect(response.headers.get('Content-Type')).toBe('image/png'); |
| 52 | + expect(response.headers.get('Cache-Control')).toBe('public, max-age=3600'); |
| 53 | + |
| 54 | + // Check if body contains buffer |
| 55 | + const buffer = Buffer.from(await response.arrayBuffer()); |
| 56 | + expect(buffer.toString()).toBe('mock-png-data'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns errors from the base route directly', async () => { |
| 60 | + const mockRequest = new Request('http://localhost:3000/api/streak/png'); |
| 61 | + |
| 62 | + const mockErrorResponse = NextResponse.json({ error: 'Invalid parameters' }, { status: 400 }); |
| 63 | + vi.mocked(getStreakSvg).mockResolvedValue(mockErrorResponse); |
| 64 | + |
| 65 | + const response = await GET(mockRequest); |
| 66 | + |
| 67 | + expect(response.status).toBe(400); |
| 68 | + const data = await response.json(); |
| 69 | + expect(data.error).toBe('Invalid parameters'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('handles SVG conversion errors', async () => { |
| 73 | + const mockRequest = new Request('http://localhost:3000/api/streak/png?user=testuser'); |
| 74 | + |
| 75 | + const mockSvgResponse = new NextResponse('<svg>invalid</svg>', { |
| 76 | + status: 200, |
| 77 | + headers: { |
| 78 | + 'Content-Type': 'image/svg+xml', |
| 79 | + }, |
| 80 | + }); |
| 81 | + vi.mocked(getStreakSvg).mockResolvedValue(mockSvgResponse); |
| 82 | + |
| 83 | + // Force an error in Resvg |
| 84 | + (global as any).throwResvgError = true; |
| 85 | + |
| 86 | + const response = await GET(mockRequest); |
| 87 | + |
| 88 | + (global as any).throwResvgError = false; |
| 89 | + |
| 90 | + expect(response.status).toBe(500); |
| 91 | + const data = await response.json(); |
| 92 | + expect(data.error).toBe('Failed to convert SVG to PNG'); |
| 93 | + }); |
| 94 | +}); |
0 commit comments