import { describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { loadFixture, runFixtureSuite } from '@rareindex/connectors/testing';
import { localMeta } from '../_lib/local-meta.js';
import { bidsquareDate, parseBidsquareCatalog, parseBidsquareEvents, sportsCategory } from '../_memorabilia-lib/index.js';
import createConnector from './index.js';
const dir = path.dirname(fileURLToPath(import.meta.url));
const meta = localMeta(JSON.parse(readFileSync(path.join(dir, 'meta.json'), 'utf8')));
const connector = createConnector(meta);
const EVENTS = `
`;
const CATALOG = `
`;
describe('scp-auctions', () => {
runFixtureSuite(connector, it, expect);
it('parses the event list and catalog cards', () => {
expect(parseBidsquareEvents(EVENTS)).toEqual([{ id: '23246', name: '2026 Summer Premier', url: 'https://catalogs.scpauctions.com/auctions/scp-auctions-inc/2026-summer-premier-23246', status: 'past', startDate: null, endDate: null }]);
const cat = parseBidsquareCatalog(CATALOG, 1)!;
expect(cat.event).toMatchObject({ id: '23246', status: 'past', endDate: '2026-07-26 22:30:00 EDT' });
expect(cat.totalPages).toBe(12);
expect(cat.items.length).toBe(2);
expect(cat.items[0]).toMatchObject({ itemId: '9310176', lotNumber: '47', priceLabel: 'Sold for', price: 14400, bids: 32 });
expect(cat.items[1]!.price).toBeNull();
expect(bidsquareDate('2026-07-26 22:30:00 EDT')?.toISOString()).toBe('2026-07-27T02:30:00.000Z');
});
it('normalises sold lots only, dated by the event end, with sport-aware categories', async () => {
const cat = parseBidsquareCatalog(CATALOG, 1)!;
const out = await connector.normalize({ url: 'x', externalId: 'e', kind: 'sale', engine: 'api', fetchedAt: new Date('2026-09-07T00:00:00Z'), payload: { kind: 'catalog_page', house: 'SCP Auctions', event: cat.event, page: 1, totalPages: 12, items: cat.items } });
expect(out.length).toBe(1);
const s = out[0]!;
if (s.kind !== 'sale') throw new Error('expected sale');
expect(s).toMatchObject({ price: 14400, currency: 'USD', lotNumber: '47', auctionHouse: 'SCP Auctions', buyerPremiumIncluded: null });
expect(s.saleDate.toISOString()).toBe('2026-07-27T02:30:00.000Z');
expect(s.attributes.categorySlug).toBe('baseball_cards');
expect(s.grade).toMatchObject({ grader: 'sgc', grade: '1.5' });
expect(sportsCategory('1986 Fleer Michael Jordan Rookie PSA 8')).toBe('basketball_cards');
expect(sportsCategory('1927 Babe Ruth Game Used Bat')).toBe('sports_memorabilia');
});
it('fixture sales carry the event end date and USD prices', async () => {
const out = await connector.normalize(loadFixture('scp-auctions', 'catalog-page').raw);
expect(out.length).toBeGreaterThan(0);
for (const r of out) if (r.kind === 'sale') expect(r.currency).toBe('USD');
});
});