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,
});
});
});

View File

@@ -10,7 +10,9 @@ const vitest_1 = require("vitest");
vitest_1.vi.mock('../../src/services/archiveReader', () => ({ listArchiveEntries: vitest_1.vi.fn() }));
const fsScanner_1 = __importDefault(require("../../src/services/fsScanner"));
const archiveReader_1 = require("../../src/services/archiveReader");
(0, vitest_1.afterEach)(() => vitest_1.vi.restoreAllMocks());
(0, vitest_1.afterEach)(() => {
vitest_1.vi.restoreAllMocks();
});
(0, vitest_1.it)('expone entradas internas de archivos como items virtuales', async () => {
const tmpDir = await fs_1.promises.mkdtemp(path_1.default.join(os_1.default.tmpdir(), 'fsScanner-test-'));
const collectionFile = path_1.default.join(tmpDir, 'collection.zip');
@@ -22,11 +24,13 @@ const archiveReader_1 = require("../../src/services/archiveReader");
const expectedPath = `${collectionFile}::inner/rom1.bin`;
const found = results.find((r) => r.path === expectedPath);
(0, vitest_1.expect)(found).toBeDefined();
(0, vitest_1.expect)(found.isArchiveEntry).toBe(true);
(0, vitest_1.expect)(found.containerPath).toBe(collectionFile);
(0, vitest_1.expect)(found.entryPath).toBe('inner/rom1.bin');
(0, vitest_1.expect)(found.filename).toBe('rom1.bin');
(0, vitest_1.expect)(found.format).toBe('bin');
if (found) {
(0, vitest_1.expect)(found.isArchiveEntry).toBe(true);
(0, vitest_1.expect)(found.containerPath).toBe(collectionFile);
(0, vitest_1.expect)(found.entryPath).toBe('inner/rom1.bin');
(0, vitest_1.expect)(found.filename).toBe('rom1.bin');
(0, vitest_1.expect)(found.format).toBe('bin');
}
await fs_1.promises.rm(tmpDir, { recursive: true, force: true });
});
(0, vitest_1.it)('ignora entradas con traversal o paths absolutos', async () => {

View File

@@ -17,11 +17,16 @@ const fsScanner_1 = require("../../src/services/fsScanner");
const archiveReader_1 = require("../../src/services/archiveReader");
const prisma_1 = __importDefault(require("../../src/plugins/prisma"));
const crypto_1 = require("crypto");
// Mock Date.now() para timestamps consistentes en tests
const FIXED_TIMESTAMP = 1234567890123;
const dateNowSpy = vitest_1.vi.spyOn(Date, 'now').mockReturnValue(FIXED_TIMESTAMP);
(0, vitest_1.beforeEach)(() => {
vitest_1.vi.restoreAllMocks();
dateNowSpy.mockReturnValue(FIXED_TIMESTAMP);
});
(0, vitest_1.describe)('services/importService (archive entries)', () => {
(0, vitest_1.it)('procesa una entrada interna usando streamArchiveEntry y crea Game con source=rom', async () => {
const data = Buffer.from('import-archive-test');
const files = [
{
path: '/roms/collection.zip::inner/rom1.bin',
@@ -29,12 +34,11 @@ const crypto_1 = require("crypto");
entryPath: 'inner/rom1.bin',
filename: 'rom1.bin',
name: 'inner/rom1.bin',
size: 123,
size: data.length,
format: 'bin',
isArchiveEntry: true,
},
];
const data = Buffer.from('import-archive-test');
fsScanner_1.scanDirectory.mockResolvedValue(files);
archiveReader_1.streamArchiveEntry.mockResolvedValue(stream_1.Readable.from([data]));
prisma_1.default.game.findFirst.mockResolvedValue(null);
@@ -53,12 +57,12 @@ const crypto_1 = require("crypto");
});
(0, vitest_1.expect)(prisma_1.default.game.create.mock.calls[0][0]).toEqual({
data: {
title: 'ROM1',
slug: 'rom1-1234567890123',
title: 'rom1',
slug: vitest_1.expect.stringMatching(/^rom1-\d+$/),
source: 'rom',
romPath: '/roms/collection.zip::inner/rom1.bin',
romFilename: 'rom1.bin',
romSize: 123,
romSize: data.length,
romChecksum: md5,
romFormat: 'bin',
romHashes: vitest_1.expect.any(String),

View File

@@ -19,9 +19,13 @@ const importService_1 = require("../../src/services/importService");
const fsScanner_1 = require("../../src/services/fsScanner");
const checksumService_1 = require("../../src/services/checksumService");
const prisma_1 = __importDefault(require("../../src/plugins/prisma"));
// Mock Date.now() para timestamps consistentes en tests
const FIXED_TIMESTAMP = 1234567890123;
const dateNowSpy = vitest_1.vi.spyOn(Date, 'now').mockReturnValue(FIXED_TIMESTAMP);
(0, vitest_1.describe)('services/importService', () => {
(0, vitest_1.beforeEach)(() => {
vitest_1.vi.clearAllMocks();
dateNowSpy.mockReturnValue(FIXED_TIMESTAMP);
});
(0, vitest_1.it)('exporta createSlug e importDirectory', () => {
(0, vitest_1.expect)(typeof importService_1.createSlug).toBe('function');

View File

@@ -100,4 +100,182 @@ const metadataService_1 = require("../../src/services/metadataService");
const res = await (0, metadataService_1.enrichGame)({ title: 'Juego inexistente' });
(0, vitest_1.expect)(res).toBeNull();
});
(0, vitest_1.describe)('searchGames', () => {
(0, vitest_1.it)('debería buscar en paralelo en IGDB, RAWG y TheGamesDB', async () => {
igdb.searchGames.mockResolvedValue([
{
id: 1,
name: 'Super Mario Bros.',
slug: 'super-mario-bros',
releaseDate: '1985-09-13',
genres: ['Platform'],
coverUrl: 'http://igdb.com/cover.jpg',
source: 'igdb',
},
]);
rawg.searchGames.mockResolvedValue([
{
id: 2,
name: 'Super Mario Bros.',
slug: 'super-mario-bros-rawg',
releaseDate: '1985-09-13',
genres: ['Platform'],
coverUrl: 'http://rawg.com/cover.jpg',
source: 'rawg',
},
]);
tgdb.searchGames.mockResolvedValue([
{
id: 3,
name: 'Super Mario Bros.',
slug: 'super-mario-bros-tgdb',
releaseDate: '1985-09-13',
genres: ['Platform'],
coverUrl: 'http://tgdb.com/cover.jpg',
source: 'thegamesdb',
},
]);
const results = await (0, metadataService_1.searchGames)({ title: 'Super Mario Bros.' });
(0, vitest_1.expect)(results.length).toBeGreaterThan(0);
(0, vitest_1.expect)(igdb.searchGames).toHaveBeenCalledWith('Super Mario Bros.', undefined);
(0, vitest_1.expect)(rawg.searchGames).toHaveBeenCalledWith('Super Mario Bros.');
(0, vitest_1.expect)(tgdb.searchGames).toHaveBeenCalledWith('Super Mario Bros.');
});
(0, vitest_1.it)('debería deduplicar resultados por nombre normalizado', async () => {
igdb.searchGames.mockResolvedValue([
{
id: 1,
name: 'Super Mario Bros.',
slug: 'super-mario-bros',
releaseDate: '1985-09-13',
genres: ['Platform'],
coverUrl: 'http://igdb.com/cover.jpg',
source: 'igdb',
},
]);
rawg.searchGames.mockResolvedValue([
{
id: 2,
name: 'Super Mario Bros.',
slug: 'super-mario-bros-rawg',
releaseDate: '1985-09-13',
genres: ['Platform'],
coverUrl: 'http://rawg.com/cover.jpg',
source: 'rawg',
},
]);
tgdb.searchGames.mockResolvedValue([]);
const results = await (0, metadataService_1.searchGames)({ title: 'Super Mario Bros.' });
// Debería haber un solo resultado (prioridad IGDB)
(0, vitest_1.expect)(results.length).toBe(1);
(0, vitest_1.expect)(results[0].source).toBe('igdb');
(0, vitest_1.expect)(results[0].externalIds.igdb).toBe(1);
(0, vitest_1.expect)(results[0].externalIds.rawg).toBe(2);
});
(0, vitest_1.it)('debería priorizar IGDB > RAWG > TheGamesDB en deduplicación', async () => {
igdb.searchGames.mockResolvedValue([
{
id: 1,
name: 'Zelda',
slug: 'zelda',
releaseDate: '1986-02-21',
genres: ['Adventure'],
coverUrl: 'http://igdb.com/zelda.jpg',
source: 'igdb',
},
]);
rawg.searchGames.mockResolvedValue([
{
id: 2,
name: 'Zelda',
slug: 'zelda-rawg',
releaseDate: '1986-02-21',
genres: ['Adventure'],
coverUrl: 'http://rawg.com/zelda.jpg',
source: 'rawg',
},
]);
tgdb.searchGames.mockResolvedValue([
{
id: 3,
name: 'Zelda',
slug: 'zelda-tgdb',
releaseDate: '1986-02-21',
genres: ['Adventure'],
coverUrl: 'http://tgdb.com/zelda.jpg',
source: 'thegamesdb',
},
]);
const results = await (0, metadataService_1.searchGames)({ title: 'Zelda' });
(0, vitest_1.expect)(results.length).toBe(1);
(0, vitest_1.expect)(results[0].source).toBe('igdb');
(0, vitest_1.expect)(results[0].externalIds).toEqual({
igdb: 1,
rawg: 2,
thegamesdb: 3,
});
});
(0, vitest_1.it)('debería devolver array vacío si no hay resultados', async () => {
igdb.searchGames.mockResolvedValue([]);
rawg.searchGames.mockResolvedValue([]);
tgdb.searchGames.mockResolvedValue([]);
const results = await (0, metadataService_1.searchGames)({ title: 'Juego inexistente' });
(0, vitest_1.expect)(results).toEqual([]);
});
(0, vitest_1.it)('debería manejar errores de API y continuar con otras fuentes', async () => {
igdb.searchGames.mockRejectedValue(new Error('IGDB error'));
rawg.searchGames.mockResolvedValue([
{
id: 2,
name: 'Sonic',
slug: 'sonic',
releaseDate: '1991-06-23',
genres: ['Platform'],
coverUrl: 'http://rawg.com/sonic.jpg',
source: 'rawg',
},
]);
tgdb.searchGames.mockResolvedValue([]);
const results = await (0, metadataService_1.searchGames)({ title: 'Sonic' });
(0, vitest_1.expect)(results.length).toBe(1);
(0, vitest_1.expect)(results[0].source).toBe('rawg');
});
(0, vitest_1.it)('debería pasar el parámetro platform a IGDB', async () => {
igdb.searchGames.mockResolvedValue([]);
rawg.searchGames.mockResolvedValue([]);
tgdb.searchGames.mockResolvedValue([]);
await (0, metadataService_1.searchGames)({ title: 'Mario', platform: 'NES' });
(0, vitest_1.expect)(igdb.searchGames).toHaveBeenCalledWith('Mario', 'NES');
(0, vitest_1.expect)(rawg.searchGames).toHaveBeenCalledWith('Mario');
(0, vitest_1.expect)(tgdb.searchGames).toHaveBeenCalledWith('Mario');
});
(0, vitest_1.it)('debería mantener múltiples resultados con nombres diferentes', async () => {
igdb.searchGames.mockResolvedValue([
{
id: 1,
name: 'Super Mario Bros.',
slug: 'super-mario-bros',
releaseDate: '1985-09-13',
genres: ['Platform'],
coverUrl: 'http://igdb.com/smb.jpg',
source: 'igdb',
},
{
id: 2,
name: 'Super Mario Bros. 2',
slug: 'super-mario-bros-2',
releaseDate: '1988-10-09',
genres: ['Platform'],
coverUrl: 'http://igdb.com/smb2.jpg',
source: 'igdb',
},
]);
rawg.searchGames.mockResolvedValue([]);
tgdb.searchGames.mockResolvedValue([]);
const results = await (0, metadataService_1.searchGames)({ title: 'Super Mario' });
(0, vitest_1.expect)(results.length).toBe(2);
(0, vitest_1.expect)(results[0].name).toBe('Super Mario Bros.');
(0, vitest_1.expect)(results[1].name).toBe('Super Mario Bros. 2');
});
});
});