Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -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 () => {
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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');
|
||||
|
||||
178
backend/dist/tests/services/metadataService.spec.js
vendored
178
backend/dist/tests/services/metadataService.spec.js
vendored
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user