- Implemented AlertDialog component with overlay, content, header, footer, title, description, action, and cancel functionalities. - Created Badge component with variant support for different styles. - Developed Checkbox component with custom styling and indicator. - Added Dialog component with trigger, close, overlay, content, header, footer, title, and description. - Introduced Label component for form elements. - Built Select component with trigger, content, group, item, label, separator, and scroll buttons. - Created Sheet component with trigger, close, overlay, content, header, footer, title, and description. - Implemented Table component with header, body, footer, row, head, cell, and caption. - Added Textarea component with custom styling. - Established API service for game management with CRUD operations and metadata search functionalities. - Updated dependencies in package lock files.
151 lines
4.5 KiB
TypeScript
151 lines
4.5 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import type { Mock } from 'vitest';
|
|
|
|
vi.mock('../../src/services/fsScanner', () => ({
|
|
scanDirectory: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../../src/services/checksumService', () => ({
|
|
computeHashes: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../../src/plugins/prisma', () => ({
|
|
default: {
|
|
game: { findFirst: vi.fn(), create: vi.fn(), update: vi.fn() },
|
|
},
|
|
}));
|
|
|
|
import { importDirectory, createSlug } from '../../src/services/importService';
|
|
import { scanDirectory } from '../../src/services/fsScanner';
|
|
import { computeHashes } from '../../src/services/checksumService';
|
|
import prisma from '../../src/plugins/prisma';
|
|
|
|
describe('services/importService', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('exporta createSlug e importDirectory', () => {
|
|
expect(typeof createSlug).toBe('function');
|
|
expect(typeof importDirectory).toBe('function');
|
|
});
|
|
|
|
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' };
|
|
|
|
(scanDirectory as unknown as Mock).mockResolvedValue(files);
|
|
(computeHashes as unknown as Mock).mockResolvedValue(hashes);
|
|
|
|
(prisma.game.findFirst as unknown as Mock).mockResolvedValue(null);
|
|
(prisma.game.create as unknown as Mock).mockResolvedValue({
|
|
id: 77,
|
|
title: 'Sonic',
|
|
slug: 'sonic',
|
|
});
|
|
|
|
const summary = await importDirectory({ dir: '/roms', persist: true });
|
|
|
|
expect((scanDirectory as unknown as Mock).mock.calls[0][0]).toBe('/roms');
|
|
expect((computeHashes as unknown as Mock).mock.calls[0][0]).toBe('/roms/Sonic.bin');
|
|
|
|
expect((prisma.game.findFirst as unknown as Mock).mock.calls[0][0]).toEqual({
|
|
where: { source: 'rom', romChecksum: 'md5-abc' },
|
|
});
|
|
expect((prisma.game.create as unknown as Mock).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: expect.any(Date),
|
|
lastSeenAt: expect.any(Date),
|
|
},
|
|
});
|
|
|
|
expect(summary).toEqual({ processed: 1, createdCount: 1, upserted: 0 });
|
|
});
|
|
|
|
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' };
|
|
|
|
(scanDirectory as unknown as Mock).mockResolvedValue(files);
|
|
(computeHashes as unknown as Mock).mockResolvedValue(hashes);
|
|
|
|
(prisma.game.findFirst as unknown as Mock).mockResolvedValue({
|
|
id: 77,
|
|
title: 'Sonic',
|
|
slug: 'sonic',
|
|
});
|
|
(prisma.game.update as unknown as Mock).mockResolvedValue({
|
|
id: 77,
|
|
title: 'Sonic',
|
|
slug: 'sonic',
|
|
});
|
|
|
|
const summary = await importDirectory({ dir: '/roms', persist: true });
|
|
|
|
expect((prisma.game.update as unknown as Mock).mock.calls[0][0]).toEqual({
|
|
where: { id: 77 },
|
|
data: {
|
|
lastSeenAt: expect.any(Date),
|
|
romHashes: JSON.stringify(hashes),
|
|
},
|
|
});
|
|
|
|
expect(summary).toEqual({ processed: 1, createdCount: 0, upserted: 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' };
|
|
|
|
(scanDirectory as unknown as Mock).mockResolvedValue(files);
|
|
(computeHashes as unknown as Mock).mockResolvedValue(hashes);
|
|
|
|
const summary = await importDirectory({ dir: '/roms', persist: false });
|
|
|
|
expect(prisma.game.findFirst).not.toHaveBeenCalled();
|
|
expect(prisma.game.create).not.toHaveBeenCalled();
|
|
expect(prisma.game.update).not.toHaveBeenCalled();
|
|
|
|
expect(summary).toEqual({ processed: 1, createdCount: 0, upserted: 0 });
|
|
});
|
|
});
|