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 '../../api/_lib/local-meta.js';
import createConnector, { cherrystoneCategory, parseCategoryLinks, parseCategoryPage, parseHome } from './index.js';
const dir = path.dirname(fileURLToPath(import.meta.url));
const connector = createConnector(localMeta(JSON.parse(readFileSync(path.join(dir, 'meta.json'), 'utf8'))));
const HOME_MD = `[Click here](https://auctions.cherrystoneauctions.com/catalog.aspx?auctionid=34) for Prices Realized from U.S. and Worldwide Sale - July 28-29, 2026
[Click here](https://auctions.cherrystoneauctions.com/catalog.aspx?auctionid=23) for Prices Realized from The Confederate Stamps and Covers Sale - June 9, 2026`;
const HOME_HTML = `Click here for Prices Realized from U.S. and Worldwide Sale - July 28-29, 2026
`;
const CATALOG = `All1847 Issuedupother`;
const PAGE = `
Final Price: $750
Estimate: $450 - $500
Estimate: $1,000 - $1,200
`;
describe('cherrystone', () => {
runFixtureSuite(connector, it, expect);
it('parses home (markdown and HTML), category links and lot cards', () => {
expect(parseHome(HOME_MD)).toEqual([
{ id: '34', title: 'U.S. and Worldwide Sale', dateText: 'July 28-29, 2026' },
{ id: '23', title: 'The Confederate Stamps and Covers Sale', dateText: 'June 9, 2026' },
]);
expect(parseHome(HOME_HTML)[0]).toMatchObject({ id: '34' });
expect(parseCategoryLinks(CATALOG, '34')).toEqual([{ name: '1847 Issue', url: 'https://auctions.cherrystoneauctions.com/Category/1847_Issue-4150.html?auctionid=34' }]);
const p = parseCategoryPage(PAGE, { id: '34', title: 'U.S. and Worldwide Sale', dateText: 'July 28-29, 2026' }, '1847 Issue', 'u');
expect(p.lots.length).toBe(2);
expect(p.lots[0]).toMatchObject({ lotNumber: '10', finalPriceText: '$750', estimateText: '$450 - $500', image: 'https://auctions.cherrystoneauctions.com/ItemImages/000034/34097A_sm.jpeg' });
expect(p.lots[1]!.finalPriceText).toBeNull();
});
it('normalises sold lots only, dated by the sale date, premium unknown', async () => {
const payload = parseCategoryPage(PAGE, { id: '34', title: 'U.S. and Worldwide Sale', dateText: 'July 28-29, 2026' }, '1847 Issue', 'u');
const out = await connector.normalize({ url: 'u', externalId: 'a', kind: 'sale', engine: 'firecrawl', fetchedAt: new Date('2026-09-07T00:00:00Z'), payload });
expect(out.length).toBe(1);
const s = out[0]!;
if (s.kind !== 'sale') throw new Error('sale');
expect(s).toMatchObject({ price: 750, currency: 'USD', buyerPremiumIncluded: null, lotNumber: '10', auctionHouse: 'Cherrystone Auctions' });
expect(s.saleDate.toISOString()).toBe('2026-07-28T00:00:00.000Z');
expect(s.attributes).toMatchObject({ categorySlug: 'stamps', set: '1847 Issue', year: 1847 });
expect(s.attributes.identifiers.cherrystone_lot).toBe('34097');
});
it('maps banknotes and coins when titled so', () => {
expect(cherrystoneCategory('United States 1847 Issue 5c red brown')).toBe('stamps');
expect(cherrystoneCategory('Confederate States $100 note, 1864, fine')).toBe('banknotes');
expect(cherrystoneCategory('1921 Morgan silver dollar coin, uncirculated')).toBe('coins');
});
it('fixture lots normalise to USD sales', async () => {
const out = await connector.normalize(loadFixture('cherrystone', 'category-page').raw);
expect(out.length).toBeGreaterThan(0);
for (const r of out) if (r.kind === 'sale') expect(r.currency).toBe('USD');
});
});