Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -252,6 +252,149 @@ describe('Games API', () => {
|
||||
expect(res.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/games/from-metadata', () => {
|
||||
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,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(201);
|
||||
const body = res.json();
|
||||
expect(body).toHaveProperty('id');
|
||||
expect(body.title).toBe('Super Mario Bros.');
|
||||
expect(body.source).toBe('igdb');
|
||||
expect(body.sourceId).toBe('1234');
|
||||
});
|
||||
|
||||
it('debería crear un juego con overrides', async () => {
|
||||
const platform = await 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,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(201);
|
||||
const body = res.json();
|
||||
expect(body.title).toBe('Super Mario Bros.');
|
||||
expect(body.description).toBe('Descripción personalizada');
|
||||
expect(body.gamePlatforms).toHaveLength(1);
|
||||
expect(body.gamePlatforms[0].platformId).toBe(platform.id);
|
||||
expect(body.purchases).toHaveLength(1);
|
||||
expect(body.purchases[0].priceCents).toBe(10000);
|
||||
});
|
||||
|
||||
it('debería devolver 400 si falta el campo metadata', async () => {
|
||||
const res = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/games/from-metadata',
|
||||
payload: {},
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
});
|
||||
|
||||
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',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(201);
|
||||
const body = res.json();
|
||||
expect(body.source).toBe('rawg');
|
||||
expect(body.sourceId).toBe('5678');
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(201);
|
||||
const body = res.json();
|
||||
expect(body.title).toBe('Custom Game');
|
||||
expect(body.source).toBe('manual');
|
||||
expect(body.sourceId).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user