import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { buildApp } from '../../src/app'; import { FastifyInstance } from 'fastify'; import * as metadataService from '../../src/services/metadataService'; describe('Metadata API', () => { let app: FastifyInstance; beforeEach(async () => { app = buildApp(); await app.ready(); }); afterEach(async () => { await app.close(); vi.restoreAllMocks(); }); describe('GET /api/metadata/search', () => { it('debería devolver múltiples resultados cuando se busca un juego existente', async () => { const mockResults = [ { source: 'igdb' as const, externalIds: { igdb: 1 }, name: 'The Legend of Zelda', title: 'The Legend of Zelda', slug: 'the-legend-of-zelda', releaseDate: '1986-02-21', genres: ['Adventure'], coverUrl: 'https://example.com/cover.jpg', platforms: undefined, }, { source: 'rawg' as const, externalIds: { rawg: 2 }, name: 'The Legend of Zelda: A Link to the Past', title: 'The Legend of Zelda: A Link to the Past', slug: 'the-legend-of-zelda-a-link-to-the-past', releaseDate: '1991-11-21', genres: ['Adventure'], coverUrl: 'https://example.com/cover2.jpg', platforms: undefined, }, ]; vi.spyOn(metadataService, 'searchGames').mockResolvedValue(mockResults); const res = await app.inject({ method: 'GET', url: '/api/metadata/search?q=zelda', }); expect(res.statusCode).toBe(200); const body = res.json(); expect(Array.isArray(body)).toBe(true); expect(body.length).toBeGreaterThan(0); expect(body[0].name).toContain('Zelda'); }); it('debería devolver lista vacía cuando no hay resultados', async () => { vi.spyOn(metadataService, 'searchGames').mockResolvedValue([]); const res = await app.inject({ method: 'GET', url: '/api/metadata/search?q=nonexistentgame12345', }); expect(res.statusCode).toBe(200); const body = res.json(); expect(Array.isArray(body)).toBe(true); expect(body.length).toBe(0); }); it('debería devolver 400 si falta el parámetro query', async () => { const res = await app.inject({ method: 'GET', url: '/api/metadata/search', }); expect(res.statusCode).toBe(400); expect(res.json()).toHaveProperty('error'); }); it('debería devolver 400 si el parámetro query está vacío', async () => { const res = await app.inject({ method: 'GET', url: '/api/metadata/search?q=', }); expect(res.statusCode).toBe(400); }); it('debería pasar el parámetro platform a searchGames si se proporciona', async () => { const searchSpy = vi.spyOn(metadataService, 'searchGames').mockResolvedValue([]); const res = await app.inject({ method: 'GET', url: '/api/metadata/search?q=mario&platform=Nintendo%2064', }); expect(res.statusCode).toBe(200); expect(searchSpy).toHaveBeenCalledWith({ title: 'mario', platform: 'Nintendo 64', year: undefined, }); }); it('debería pasar el parámetro year a searchGames si se proporciona', async () => { const searchSpy = vi.spyOn(metadataService, 'searchGames').mockResolvedValue([]); const res = await app.inject({ method: 'GET', url: '/api/metadata/search?q=mario&year=1990', }); expect(res.statusCode).toBe(200); expect(searchSpy).toHaveBeenCalledWith({ title: 'mario', platform: undefined, year: 1990, }); }); it('debería pasar todos los parámetros a searchGames', async () => { const searchSpy = vi.spyOn(metadataService, 'searchGames').mockResolvedValue([]); const res = await app.inject({ method: 'GET', url: '/api/metadata/search?q=mario&platform=NES&year=1985', }); expect(res.statusCode).toBe(200); expect(searchSpy).toHaveBeenCalledWith({ title: 'mario', platform: 'NES', year: 1985, }); }); }); }); /** * Metadatos: * Autor: GitHub Copilot * Última actualización: 2026-02-11 */