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, { parseCatalogPage, parsePastList, potterCategory } 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 LIST = `
7 BidsSold for$420
Lot 2
Passed
`;
describe('potter-auctions', () => {
runFixtureSuite(connector, it, expect);
it('parses the past list and catalog cards', () => {
const auctions = parsePastList(LIST);
expect(auctions).toEqual([{ id: '23628', slug: 'modern-magic-masters', title: 'Modern Magic Masters', url: 'https://auction.potterauctions.com/auctions/potter-potter/modern-magic-masters-23628', startText: 'Aug 15, 2026 11:00AM EDT' }]);
const p = parseCatalogPage(CATALOG, auctions[0]!, 1);
expect(p.totalLots).toBe(499);
expect(p.lots.length).toBe(2);
expect(p.lots[0]).toMatchObject({ lotNumber: '1', priceText: '$420', statusLabel: 'Sold for', bids: 7, estimateLow: 300, estimateHigh: 600, image: 'https://s1.img.bidsquare.com/item/l/3762/37629123.jpeg?t=1WC8TA' });
expect(p.lots[1]).toMatchObject({ lotNumber: '2', priceText: null, statusLabel: 'Passed' });
});
it('normalises only sold lots as hammer-price sales dated by the auction start', async () => {
const auction = parsePastList(LIST)[0]!;
const out = await connector.normalize({ url: 'x', externalId: 'a', kind: 'sale', engine: 'api', fetchedAt: new Date('2026-09-07T00:00:00Z'), payload: parseCatalogPage(CATALOG, auction, 1) });
expect(out.length).toBe(1);
const s = out[0]!;
if (s.kind !== 'sale') throw new Error('sale');
expect(s).toMatchObject({ price: 420, currency: 'USD', buyerPremiumIncluded: false, lotNumber: '1', auctionHouse: 'Potter & Potter Auctions' });
expect(s.saleDate.toISOString()).toBe('2026-08-15T00:00:00.000Z');
expect(s.attributes.categorySlug).toBe('advertising');
expect(s.attributes.identifiers.potter_lot).toBe('23628-1');
});
it('maps departments to taxonomy slugs', () => {
expect(potterCategory('Modern Magic Masters', 'Bicycle Transformation Playing Cards Deck.')).toBe('playing_cards');
expect(potterCategory('Gambling Memorabilia', 'Ivory poker chips in case')).toBe('casino_memorabilia');
expect(potterCategory('Fine Books & Manuscripts', 'HOUDINI, Harry. A Magician Among the Spirits. First edition.')).toBe('books');
expect(potterCategory('Hollywood & Pop Culture Collectibles', 'Star Wars one-sheet poster')).toBe('movie_posters');
expect(potterCategory('Modern Magic Masters', 'Okito Coin Box, brass, ca. 1950')).toBe('antiques');
expect(potterCategory('Picture This: Fine Vintage Photography', 'Portrait, gelatin silver print')).toBe('photography');
});
it('fixture sales are hammer prices in USD', async () => {
const out = await connector.normalize(loadFixture('potter-auctions', 'catalog-page').raw);
expect(out.length).toBeGreaterThan(0);
for (const r of out) if (r.kind === 'sale') expect(r).toMatchObject({ currency: 'USD', buyerPremiumIncluded: false });
});
});