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 { morphyCategory } from '../_memorabilia-lib/index.js';
import createConnector, { parseCatalog, parsePastList } 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 LIST = `
`;
const CATALOG = ` / 19
# Bids: 6
Min Bid: $150.00
Final Price: $369.00
Estimate: $300 - $500
# Bids: 0
Min Bid: $100.00
Estimate: $200 - $500
`;
describe('morphy', () => {
runFixtureSuite(connector, it, expect);
it('parses the past-auction list and a catalog page', () => {
const list = parsePastList(LIST);
expect(list).toEqual([
{ id: '716', title: 'Online Only Perfume Bottles', dept: 'Fine & Decorative Arts', dateText: 'August 11, 2026', pageUrl: 'https://morphyauctions.com/auctions/past-auctions/online-only-perfume-bottles/' },
{ id: '700', title: 'Online Only Firearms & Militaria', dept: 'Firearms', dateText: 'August 04, 2026', pageUrl: '#' },
]);
const cat = parseCatalog(CATALOG, list[0]!, 1);
expect(cat.totalPages).toBe(19);
expect(cat.lots.length).toBe(2);
expect(cat.lots[0]).toMatchObject({ lotNumber: '1001', finalPrice: 369, minBid: 150, bids: 6, estimateText: '$300 - $500', url: 'https://auctions.morphyauctions.com/CAMEO_GLASS_FLORAL_PERFUME_WITH_ATOMIZER-LOT662424.aspx' });
expect(cat.lots[1]!.finalPrice).toBeNull();
});
it('skips firearms and maps departments to slugs', () => {
expect(morphyCategory('Firearms', 'Online Only Firearms & Militaria', 'WINCHESTER MODEL 1873 RIFLE')).toBeNull();
expect(morphyCategory('Fine & Decorative Arts', 'Online Only Perfume Bottles', 'CAMEO GLASS FLORAL PERFUME WITH ATOMIZER')).toBe('perfume');
expect(morphyCategory('Advertising & General Store, Automobilia & Petroliana', 'Automobilia, Petroliana, & Soda Advertising', 'TEXACO PORCELAIN SIGN')).toBe('advertising');
expect(morphyCategory('Coin-Op & Advertising', 'Coin-Op & Antique Advertising', 'MILLS 5 CENT SLOT MACHINE')).toBe('casino_memorabilia');
expect(morphyCategory('Toys, Dolls & Figural Cast Iron', 'Toys & Trains', 'LIONEL STANDARD GAUGE 400E LOCOMOTIVE')).toBe('model_trains');
});
it('normalises sold lots with the auction date and USD final price', async () => {
const list = parsePastList(LIST);
const out = await connector.normalize({ url: 'x', externalId: 'a', kind: 'sale', engine: 'api', fetchedAt: new Date('2026-09-07T00:00:00Z'), payload: parseCatalog(CATALOG, list[0]!, 1) });
expect(out.length).toBe(1);
const s = out[0]!;
if (s.kind !== 'sale') throw new Error('sale');
expect(s).toMatchObject({ price: 369, currency: 'USD', auctionHouse: 'Morphy Auctions', lotNumber: '1001', buyerPremiumIncluded: null });
expect(s.saleDate.toISOString()).toBe('2026-08-11T00:00:00.000Z');
expect(s.attributes.categorySlug).toBe('perfume');
expect(s.attributes.identifiers.morphy_lot).toBe('662424');
});
it('fixture normalises', async () => {
expect((await connector.normalize(loadFixture('morphy', 'catalog-page').raw)).length).toBeGreaterThan(0);
});
});