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 createConnector, { closedDate, kelleherCategory, parseCategoryPage, parseFirmPage, parseGroupLinks } 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 FIRM = `
Daniel F. Kelleher Auctions, LLC - Sale 5337 US, British and Worldwide Stamps and Postal History - August 30, 2026
Daniel F. Kelleher Auctions, LLC US, British and Worldwide Stamps and Postal History - December 28, 2025 Daniel F. Kelleher Auctions, LLC US & Worldwide Coins & Banknotes - December 22, 2025 `;
const GROUPS = `USdup`;
const CATS = `1847`;
const PAGE = ` Sale No: 5303 Lot No: 9001 Symbol: Z Cat No: 1 | 1847, 5¢ red brown (Scott 1), red cancel; creased along 2 crossed letter folds, Fine. Scott $425. (Image) Suggested Bid $62 | Opening US$ 74.00 Sold...US$ 74.00
Closed..Dec-28-2025, 21:00:00 EST Sold For 74 |
Sale No: 5303 Lot No: 9002 Cat No: 2 | 1847, 10¢ black (Scott 2), used, fine. | Opening US$ 300.00 Unsold
Closed..Dec-28-2025, 21:00:00 EST |
`;
describe('kelleher', () => {
runFixtureSuite(connector, it, expect);
it('discovers only cCatalog sales after the PricesRealized anchor', () => {
const sales = parseFirmPage(FIRM);
expect(sales).toEqual([{ sale: '5303', title: 'US, British and Worldwide Stamps and Postal History', dateText: 'December 28, 2025', url: 'https://StampAuctionNetwork.com/cCatalog.cfm?SrchFirm=V&SrchSale=5303' }]);
expect(parseGroupLinks(GROUPS, 2)).toEqual(['https://StampAuctionNetwork.com/cCatalog2.cfm?MAJGROUP=United%20States&SrchFirm=V&SrchSale=5303']);
expect(parseGroupLinks(CATS, 3).length).toBe(1);
});
it('parses lot tables and normalises sold lots as hammer prices dated by Closed', async () => {
const sale = parseFirmPage(FIRM)[0]!;
const url = 'https://StampAuctionNetwork.com/cCatalog3.cfm?MAJGROUP=United%20States&CATDESCR=1847%20Issue&SrchFirm=V&SrchSale=5303';
const p = parseCategoryPage(PAGE, sale, url);
expect(p.majorGroup).toBe('United States');
expect(p.category).toBe('1847 Issue');
expect(p.lots.length).toBe(2);
expect(p.lots[0]).toMatchObject({ lotNumber: '9001', catNo: '1', headline: '1847, 5ยข red brown (Scott 1),', soldText: 'US$ 74.00', closedText: 'Dec-28-2025', soldFor: '74', image: 'https://StampAuctionNetwork.com/Photos/V/5303/9001.jpg' });
expect(p.lots[1]!.soldFor).toBeNull();
const out = await connector.normalize({ url, externalId: 'a', kind: 'sale', engine: 'api', fetchedAt: new Date('2026-09-07T00:00:00Z'), payload: p });
expect(out.length).toBe(1);
const s = out[0]!;
if (s.kind !== 'sale') throw new Error('sale');
expect(s).toMatchObject({ price: 74, currency: 'USD', buyerPremiumIncluded: false, lotNumber: '9001', auctionHouse: 'Daniel F. Kelleher Auctions' });
expect(s.saleDate.toISOString()).toBe('2025-12-28T00:00:00.000Z');
expect(s.attributes).toMatchObject({ categorySlug: 'stamps', set: '1847 Issue', country: 'United States', year: 1847 });
expect(s.attributes.identifiers.san_lot).toBe('V-5303-9001');
});
it('helpers', () => {
expect(closedDate('Dec-28-2025')?.toISOString()).toBe('2025-12-28T00:00:00.000Z');
expect(kelleherCategory('United States', '1847 5c red brown')).toBe('stamps');
expect(kelleherCategory('Coins', '1921 Morgan dollar')).toBe('coins');
expect(kelleherCategory('Paper Money', '$5 legal tender note')).toBe('banknotes');
});
it('fixture lots normalise to USD hammer sales', async () => {
const out = await connector.normalize(loadFixture('kelleher', 'category-page').raw);
expect(out.length).toBeGreaterThan(0);
for (const r of out) if (r.kind === 'sale') expect(r).toMatchObject({ currency: 'USD', buyerPremiumIncluded: false });
});
});