Refactor code structure for improved readability and maintainability
Some checks failed
CI / lint (push) Failing after 10s
CI / test-backend (push) Has been skipped
CI / test-frontend (push) Has been skipped
CI / test-e2e (push) Has been skipped

This commit is contained in:
2026-03-22 11:34:38 +01:00
parent 5eaf320fc5
commit 2667e11284
46 changed files with 4949 additions and 157 deletions

View File

@@ -220,6 +220,132 @@ const prisma_1 = require("../../src/plugins/prisma");
(0, vitest_1.expect)(res.statusCode).toBe(404);
});
});
(0, vitest_1.describe)('POST /api/games/from-metadata', () => {
(0, vitest_1.it)('debería crear un juego a partir de metadatos', async () => {
const payload = {
metadata: {
source: 'igdb',
externalIds: { igdb: 1234 },
name: 'Super Mario Bros.',
slug: 'super-mario-bros',
releaseDate: '1985-09-13T00:00:00.000Z',
genres: ['Platform'],
coverUrl: 'https://example.com/cover.jpg',
},
};
const res = await app.inject({
method: 'POST',
url: '/api/games/from-metadata',
payload,
});
(0, vitest_1.expect)(res.statusCode).toBe(201);
const body = res.json();
(0, vitest_1.expect)(body).toHaveProperty('id');
(0, vitest_1.expect)(body.title).toBe('Super Mario Bros.');
(0, vitest_1.expect)(body.source).toBe('igdb');
(0, vitest_1.expect)(body.sourceId).toBe('1234');
});
(0, vitest_1.it)('debería crear un juego con overrides', async () => {
const platform = await prisma_1.prisma.platform.create({
data: { name: 'Nintendo Entertainment System', slug: 'nes' },
});
const payload = {
metadata: {
source: 'igdb',
externalIds: { igdb: 1234 },
name: 'Super Mario Bros.',
slug: 'super-mario-bros',
releaseDate: '1985-09-13T00:00:00.000Z',
genres: ['Platform'],
coverUrl: 'https://example.com/cover.jpg',
},
overrides: {
platformId: platform.id,
description: 'Descripción personalizada',
priceCents: 10000,
currency: 'USD',
store: 'eBay',
date: '2025-01-15',
condition: 'CIB',
},
};
const res = await app.inject({
method: 'POST',
url: '/api/games/from-metadata',
payload,
});
(0, vitest_1.expect)(res.statusCode).toBe(201);
const body = res.json();
(0, vitest_1.expect)(body.title).toBe('Super Mario Bros.');
(0, vitest_1.expect)(body.description).toBe('Descripción personalizada');
(0, vitest_1.expect)(body.gamePlatforms).toHaveLength(1);
(0, vitest_1.expect)(body.gamePlatforms[0].platformId).toBe(platform.id);
(0, vitest_1.expect)(body.purchases).toHaveLength(1);
(0, vitest_1.expect)(body.purchases[0].priceCents).toBe(10000);
});
(0, vitest_1.it)('debería devolver 400 si falta el campo metadata', async () => {
const res = await app.inject({
method: 'POST',
url: '/api/games/from-metadata',
payload: {},
});
(0, vitest_1.expect)(res.statusCode).toBe(400);
});
(0, vitest_1.it)('debería devolver 400 si metadata.name está vacío', async () => {
const res = await app.inject({
method: 'POST',
url: '/api/games/from-metadata',
payload: {
metadata: {
source: 'igdb',
externalIds: { igdb: 1234 },
name: '',
slug: 'super-mario-bros',
},
},
});
(0, vitest_1.expect)(res.statusCode).toBe(400);
});
(0, vitest_1.it)('debería usar el externalId principal como sourceId', async () => {
const payload = {
metadata: {
source: 'rawg',
externalIds: { rawg: 5678, igdb: 1234 },
name: 'Zelda',
slug: 'zelda',
},
};
const res = await app.inject({
method: 'POST',
url: '/api/games/from-metadata',
payload,
});
(0, vitest_1.expect)(res.statusCode).toBe(201);
const body = res.json();
(0, vitest_1.expect)(body.source).toBe('rawg');
(0, vitest_1.expect)(body.sourceId).toBe('5678');
});
(0, vitest_1.it)('debería manejar metadata sin externalIds', async () => {
const payload = {
metadata: {
source: 'manual',
externalIds: {},
name: 'Custom Game',
slug: 'custom-game',
},
};
const res = await app.inject({
method: 'POST',
url: '/api/games/from-metadata',
payload,
});
(0, vitest_1.expect)(res.statusCode).toBe(201);
const body = res.json();
(0, vitest_1.expect)(body.title).toBe('Custom Game');
(0, vitest_1.expect)(body.source).toBe('manual');
(0, vitest_1.expect)(body.sourceId).toBeNull();
});
});
});
/**
* Metadatos:

View File

@@ -47,19 +47,32 @@ const metadataService = __importStar(require("../../src/services/metadataService
vitest_1.vi.restoreAllMocks();
});
(0, vitest_1.describe)('GET /api/metadata/search', () => {
(0, vitest_1.it)('debería devolver resultados cuando se busca un juego existente', async () => {
(0, vitest_1.it)('debería devolver múltiples resultados cuando se busca un juego existente', async () => {
const mockResults = [
{
source: 'igdb',
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',
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,
},
];
vitest_1.vi.spyOn(metadataService, 'enrichGame').mockResolvedValue(mockResults[0]);
vitest_1.vi.spyOn(metadataService, 'searchGames').mockResolvedValue(mockResults);
const res = await app.inject({
method: 'GET',
url: '/api/metadata/search?q=zelda',
@@ -68,10 +81,10 @@ const metadataService = __importStar(require("../../src/services/metadataService
const body = res.json();
(0, vitest_1.expect)(Array.isArray(body)).toBe(true);
(0, vitest_1.expect)(body.length).toBeGreaterThan(0);
(0, vitest_1.expect)(body[0].title).toContain('Zelda');
(0, vitest_1.expect)(body[0].name).toContain('Zelda');
});
(0, vitest_1.it)('debería devolver lista vacía cuando no hay resultados', async () => {
vitest_1.vi.spyOn(metadataService, 'enrichGame').mockResolvedValue(null);
vitest_1.vi.spyOn(metadataService, 'searchGames').mockResolvedValue([]);
const res = await app.inject({
method: 'GET',
url: '/api/metadata/search?q=nonexistentgame12345',
@@ -96,16 +109,43 @@ const metadataService = __importStar(require("../../src/services/metadataService
});
(0, vitest_1.expect)(res.statusCode).toBe(400);
});
(0, vitest_1.it)('debería pasar el parámetro platform a enrichGame si se proporciona', async () => {
const enrichSpy = vitest_1.vi.spyOn(metadataService, 'enrichGame').mockResolvedValue(null);
(0, vitest_1.it)('debería pasar el parámetro platform a searchGames si se proporciona', async () => {
const searchSpy = vitest_1.vi.spyOn(metadataService, 'searchGames').mockResolvedValue([]);
const res = await app.inject({
method: 'GET',
url: '/api/metadata/search?q=mario&platform=Nintendo%2064',
});
(0, vitest_1.expect)(res.statusCode).toBe(200);
(0, vitest_1.expect)(enrichSpy).toHaveBeenCalledWith({
(0, vitest_1.expect)(searchSpy).toHaveBeenCalledWith({
title: 'mario',
platform: 'Nintendo 64',
year: undefined,
});
});
(0, vitest_1.it)('debería pasar el parámetro year a searchGames si se proporciona', async () => {
const searchSpy = vitest_1.vi.spyOn(metadataService, 'searchGames').mockResolvedValue([]);
const res = await app.inject({
method: 'GET',
url: '/api/metadata/search?q=mario&year=1990',
});
(0, vitest_1.expect)(res.statusCode).toBe(200);
(0, vitest_1.expect)(searchSpy).toHaveBeenCalledWith({
title: 'mario',
platform: undefined,
year: 1990,
});
});
(0, vitest_1.it)('debería pasar todos los parámetros a searchGames', async () => {
const searchSpy = vitest_1.vi.spyOn(metadataService, 'searchGames').mockResolvedValue([]);
const res = await app.inject({
method: 'GET',
url: '/api/metadata/search?q=mario&platform=NES&year=1985',
});
(0, vitest_1.expect)(res.statusCode).toBe(200);
(0, vitest_1.expect)(searchSpy).toHaveBeenCalledWith({
title: 'mario',
platform: 'NES',
year: 1985,
});
});
});