deckhearth/test/api/cards-pricing.test.js

73 lines
1.8 KiB
JavaScript
Raw Normal View History

import { beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('../../lib/pricing/pricing-service.js', () => ({
getCardPricing: vi.fn(),
}));
import { getCardPricing } from '../../lib/pricing/pricing-service.js';
import handler from '../../pages/api/cards/[id]/pricing.js';
function createRes() {
const res = {
statusCode: 200,
body: null,
status(code) {
res.statusCode = code;
return res;
},
json(data) {
res.body = data;
return res;
},
};
return res;
}
describe('GET /api/cards/[id]/pricing', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('returns 400 for invalid card id', async () => {
const req = { method: 'GET', query: { id: 'abc' } };
const res = createRes();
await handler(req, res);
expect(res.statusCode).toBe(400);
expect(getCardPricing).not.toHaveBeenCalled();
});
it('returns 404 when card is missing', async () => {
getCardPricing.mockResolvedValue(null);
const req = { method: 'GET', query: { id: '99' } };
const res = createRes();
await handler(req, res);
expect(res.statusCode).toBe(404);
});
it('returns pricing payload with staleness metadata', async () => {
getCardPricing.mockResolvedValue({
prices: { current: 1.25, market: 2.5, currency: 'USD' },
pricesUpdatedAt: '2026-01-01T00:00:00.000Z',
stale: true,
outbound: {
tcgplayer: 'https://tcgplayer.example',
ebay: 'https://ebay.example',
},
});
const req = { method: 'GET', query: { id: '42' } };
const res = createRes();
await handler(req, res);
expect(res.statusCode).toBe(200);
expect(res.body.stale).toBe(true);
expect(res.body.prices.current).toBe(1.25);
expect(res.body.outbound.tcgplayer).toContain('tcgplayer');
});
});