132 lines
5.7 KiB
JavaScript
132 lines
5.7 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const vitest_1 = require("vitest");
|
|
vitest_1.vi.mock('../../src/services/fsScanner', () => ({
|
|
scanDirectory: vitest_1.vi.fn(),
|
|
}));
|
|
vitest_1.vi.mock('../../src/services/checksumService', () => ({
|
|
computeHashes: vitest_1.vi.fn(),
|
|
}));
|
|
vitest_1.vi.mock('../../src/plugins/prisma', () => ({
|
|
default: {
|
|
game: { findFirst: vitest_1.vi.fn(), create: vitest_1.vi.fn(), update: vitest_1.vi.fn() },
|
|
},
|
|
}));
|
|
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');
|
|
(0, vitest_1.expect)(typeof importService_1.importDirectory).toBe('function');
|
|
});
|
|
(0, vitest_1.it)('cuando hay un archivo y persist:true crea Game con source=rom y devuelve resumen', async () => {
|
|
const files = [
|
|
{
|
|
path: '/roms/Sonic.bin',
|
|
filename: 'Sonic.bin',
|
|
name: 'Sonic.bin',
|
|
size: 123,
|
|
format: 'bin',
|
|
isArchive: false,
|
|
},
|
|
];
|
|
const hashes = { size: 123, md5: 'md5-abc', sha1: 'sha1-abc', crc32: 'abcd' };
|
|
fsScanner_1.scanDirectory.mockResolvedValue(files);
|
|
checksumService_1.computeHashes.mockResolvedValue(hashes);
|
|
prisma_1.default.game.findFirst.mockResolvedValue(null);
|
|
prisma_1.default.game.create.mockResolvedValue({
|
|
id: 77,
|
|
title: 'Sonic',
|
|
slug: 'sonic',
|
|
});
|
|
const summary = await (0, importService_1.importDirectory)({ dir: '/roms', persist: true });
|
|
(0, vitest_1.expect)(fsScanner_1.scanDirectory.mock.calls[0][0]).toBe('/roms');
|
|
(0, vitest_1.expect)(checksumService_1.computeHashes.mock.calls[0][0]).toBe('/roms/Sonic.bin');
|
|
(0, vitest_1.expect)(prisma_1.default.game.findFirst.mock.calls[0][0]).toEqual({
|
|
where: { source: 'rom', romChecksum: 'md5-abc' },
|
|
});
|
|
(0, vitest_1.expect)(prisma_1.default.game.create.mock.calls[0][0]).toEqual({
|
|
data: {
|
|
title: 'Sonic',
|
|
slug: 'sonic-1234567890123',
|
|
source: 'rom',
|
|
romPath: '/roms/Sonic.bin',
|
|
romFilename: 'Sonic.bin',
|
|
romSize: 123,
|
|
romChecksum: 'md5-abc',
|
|
romFormat: 'bin',
|
|
romHashes: JSON.stringify(hashes),
|
|
addedAt: vitest_1.expect.any(Date),
|
|
lastSeenAt: vitest_1.expect.any(Date),
|
|
},
|
|
});
|
|
(0, vitest_1.expect)(summary).toEqual({ processed: 1, createdCount: 1, upserted: 0 });
|
|
});
|
|
(0, vitest_1.it)('cuando el juego ya existe (mismo checksum), actualiza lastSeenAt', async () => {
|
|
const files = [
|
|
{
|
|
path: '/roms/Sonic.bin',
|
|
filename: 'Sonic.bin',
|
|
name: 'Sonic.bin',
|
|
size: 123,
|
|
format: 'bin',
|
|
isArchive: false,
|
|
},
|
|
];
|
|
const hashes = { size: 123, md5: 'md5-abc', sha1: 'sha1-abc', crc32: 'abcd' };
|
|
fsScanner_1.scanDirectory.mockResolvedValue(files);
|
|
checksumService_1.computeHashes.mockResolvedValue(hashes);
|
|
prisma_1.default.game.findFirst.mockResolvedValue({
|
|
id: 77,
|
|
title: 'Sonic',
|
|
slug: 'sonic',
|
|
});
|
|
prisma_1.default.game.update.mockResolvedValue({
|
|
id: 77,
|
|
title: 'Sonic',
|
|
slug: 'sonic',
|
|
});
|
|
const summary = await (0, importService_1.importDirectory)({ dir: '/roms', persist: true });
|
|
(0, vitest_1.expect)(prisma_1.default.game.update.mock.calls[0][0]).toEqual({
|
|
where: { id: 77 },
|
|
data: {
|
|
lastSeenAt: vitest_1.expect.any(Date),
|
|
romHashes: JSON.stringify(hashes),
|
|
},
|
|
});
|
|
(0, vitest_1.expect)(summary).toEqual({ processed: 1, createdCount: 0, upserted: 1 });
|
|
});
|
|
(0, vitest_1.it)('cuando persist=false no guarda nada en la base de datos', async () => {
|
|
const files = [
|
|
{
|
|
path: '/roms/Sonic.bin',
|
|
filename: 'Sonic.bin',
|
|
name: 'Sonic.bin',
|
|
size: 123,
|
|
format: 'bin',
|
|
isArchive: false,
|
|
},
|
|
];
|
|
const hashes = { size: 123, md5: 'md5-abc', sha1: 'sha1-abc', crc32: 'abcd' };
|
|
fsScanner_1.scanDirectory.mockResolvedValue(files);
|
|
checksumService_1.computeHashes.mockResolvedValue(hashes);
|
|
const summary = await (0, importService_1.importDirectory)({ dir: '/roms', persist: false });
|
|
(0, vitest_1.expect)(prisma_1.default.game.findFirst).not.toHaveBeenCalled();
|
|
(0, vitest_1.expect)(prisma_1.default.game.create).not.toHaveBeenCalled();
|
|
(0, vitest_1.expect)(prisma_1.default.game.update).not.toHaveBeenCalled();
|
|
(0, vitest_1.expect)(summary).toEqual({ processed: 1, createdCount: 0, upserted: 0 });
|
|
});
|
|
});
|