const request = require('supertest'); const express = require('express'); const helmet = require('helmet'); // --- Mock axios before requiring the router --- jest.mock('axios'); const axios = require('axios'); const MOCK_SVG = `SimpleIcons`; beforeEach(() => { axios.get.mockResolvedValue({ status: 200, data: MOCK_SVG }); }); afterEach(() => { jest.restoreAllMocks(); }); // Fresh app for each test to avoid middleware leakage function createApp() { const badgeRouter = require('../src/api/badge'); const app = express(); app.use(helmet({ crossOriginResourcePolicy: false })); app.use(badgeRouter); return app; } describe('GET /badge', () => { // --- Basic happy-path tests --- it('should return SVG with default params', async () => { const res = await request(createApp()).get('/badge'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(res.headers['content-type']).toMatch(/image\/svg\+xml/); expect(body).toContain(' { const res = await request(createApp()).get('/badge?icon=github&label=GitHub'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain('GitHub'); expect(body).toContain(' { const res = await request(createApp()).get('/badge?icon=github'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain(' { const res = await request(createApp()).get('/badge?bgicon=none'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain('fill="none"'); }); it('should set label background to transparent', async () => { const res = await request(createApp()).get('/badge?bglabel=none'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain('fill="none"'); }); it('should apply custom bgicon and bglabel colours', async () => { const res = await request(createApp()).get('/badge?bgicon=red&bglabel=blue'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain('fill="red"'); expect(body).toContain('fill="blue"'); }); it('should apply custom text colour', async () => { const res = await request(createApp()).get('/badge?color=white'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain('fill="white"'); }); // --- Styles --- it('should use flat style with rounded corners (rx=3)', async () => { const res = await request(createApp()).get('/badge?style=flat'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain('rx="3"'); }); it('should use rect style with square corners (rx=0)', async () => { const res = await request(createApp()).get('/badge?style=rect'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain('rx="0"'); }); it('should fall back to rect for invalid style', async () => { const res = await request(createApp()).get('/badge?style=invalid'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain('rx="0"'); }); // --- labelpos --- it('should handle labelpos left', async () => { const res = await request(createApp()).get('/badge?labelpos=left'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain(' { const res = await request(createApp()).get('/badge?labelpos=above'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain(' { const res = await request(createApp()).get('/badge?labelpos=below'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain(' { const res = await request(createApp()).get('/badge?labelpos=diagonal'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain(' { const res = await request(createApp()).get('/badge?fontweight=bold'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain('font-weight="bold"'); }); it('should handle numeric fontweight 600', async () => { const res = await request(createApp()).get('/badge?fontweight=600'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain('font-weight="600"'); }); it('should fall back to normal for invalid fontweight', async () => { const res = await request(createApp()).get('/badge?fontweight=ultra'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain('font-weight="normal"'); }); // --- size --- it('should handle minimum valid size (8)', async () => { const res = await request(createApp()).get('/badge?size=8'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain(' { const res = await request(createApp()).get('/badge?size=128'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain(' { const res = await request(createApp()).get('/badge?size=abc'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain(' { const res = await request(createApp()).get('/badge?size=-5'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain(' { const res = await request(createApp()).get('/badge?size=9999'); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).toContain(' { const res = await request(createApp()).get('/badge?label='); const body = Buffer.from(res.body).toString(); expect(res.statusCode).toBe(200); expect(body).not.toContain('