import { describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { runFixtureSuite } from '@rareindex/connectors/testing';
import { localMeta } from '../_lib/local-meta.js';
import { monthYearDate } from '../_g7-auctions-na-lib/index.js';
import createConnector, { cleanSweepCategory, isBundle, monthKey, parseLotPage, parseMonthPage, parsePastIndex } 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);
// Real fragments captured live from www.cleansweepauctions.com on 2026-09-08.
const INDEX = `
Past Auctions`;
const MONTH_PAGE = `April 2025
`;
const LOT_PAGE = `
`;
describe('clean-sweep', () => {
runFixtureSuite(connector, it, expect);
it('parses the past-auction index newest first', () => {
expect(parsePastIndex(INDEX)).toEqual(['december-2025', 'may-2025', 'april-2025', 'april-2008']);
expect(monthKey('april-2025')).toBe(202504);
expect(monthKey('not-a-month')).toBeNull();
});
it('parses a month page (lots + page count)', () => {
const mp = parseMonthPage(MONTH_PAGE, 'april-2025', 1);
expect(mp.monthLabel).toBe('April 2025');
expect(mp.totalPages).toBe(12);
expect(mp.lots.length).toBe(2);
expect(mp.lots[0]).toEqual({ slug: '1971-topps-556-jim-mcclothlin-8', url: 'https://www.cleansweepauctions.com/past-auctions/april-2025/1971-topps-556-jim-mcclothlin-8', title: '1971 Topps 556 Jim McClothlin 8', image: 'https://marketplace.cleansweepauctions.com/images/main/mcclothlin713341.jpg' });
});
it('parses the lot page winning bid and month date', () => {
const p = parseLotPage(LOT_PAGE, 'https://www.cleansweepauctions.com/past-auctions/april-2025/1971-topps-556-jim-mcclothlin-8', 'april-2025', '1971-topps-556-jim-mcclothlin-8', 'https://www.cleansweepauctions.com/past-auctions/april-2025')!;
expect(p).toMatchObject({ title: '1971 Topps 556 Jim McClothlin 8', winningBidText: '$70.00', price: 70, monthLabel: 'April 2025' });
expect(p.image).toBe('https://marketplace.cleansweepauctions.com/images/main/mcclothlin713341.jpg');
expect(monthYearDate('april-2025')?.toISOString()).toBe('2025-04-01T00:00:00.000Z');
});
it('normalises to a hammer sale with month precision and no inferred grader', async () => {
const p = parseLotPage(LOT_PAGE, 'https://www.cleansweepauctions.com/past-auctions/april-2025/1971-topps-556-jim-mcclothlin-8', 'april-2025', '1971-topps-556-jim-mcclothlin-8', 'https://www.cleansweepauctions.com/past-auctions/april-2025')!;
const out = await connector.normalize({ url: p.url, externalId: 'april-2025/1971-topps-556-jim-mcclothlin-8', kind: 'sale', engine: 'api', fetchedAt: new Date('2026-09-08T00:00:00Z'), payload: p });
expect(out.length).toBe(1);
const s = out[0]!;
if (s.kind !== 'sale') throw new Error('expected sale');
expect(s).toMatchObject({ price: 70, currency: 'USD', buyerPremiumIncluded: false, auctionHouse: 'Clean Sweep Auctions', confidence: 0.7, isBundle: false });
expect(s.saleDate.toISOString()).toBe('2025-04-01T00:00:00.000Z');
expect(s.attributes.categorySlug).toBe('baseball_cards');
expect(s.attributes.metadata).toMatchObject({ sale_date_precision: 'month', buyers_premium_pct: 22, hammer_price: 70 });
expect(s.attributes.identifiers.clean_sweep_lot_slug).toBe('april-2025/1971-topps-556-jim-mcclothlin-8');
expect(s.grade.grader).toBeNull();
expect(s.attributes.year).toBe(1971);
});
it('keeps graders when the title names one and flags bundles', async () => {
const graded = { ...parseLotPage(LOT_PAGE, 'https://www.cleansweepauctions.com/x', 'april-2025', 'x', 'y')!, title: '1947 Exhibit 294 Dodgers Team 1956 PSA 3.5', price: 120 };
const [s] = await connector.normalize({ url: 'https://www.cleansweepauctions.com/x', externalId: 'x', kind: 'sale', engine: 'api', fetchedAt: new Date('2026-09-08T00:00:00Z'), payload: graded });
if (!s || s.kind !== 'sale') throw new Error('expected sale');
expect(s.grade).toMatchObject({ grader: 'psa', grade: '3.5' });
expect(s.confidence).toBe(0.75);
expect(isBundle('1970s Reds Signed Dinner Programs w/Bench, Rose (3 pcs) 9')).toBe(true);
expect(isBundle('1971 Topps 556 Jim McClothlin 8')).toBe(false);
expect(cleanSweepCategory('2018 Topps Update 285 Ohtani RC Ex')).toBe('baseball_cards');
expect(cleanSweepCategory('1986 Fleer 57 Michael Jordan RC PSA 7')).toBe('basketball_cards');
expect(cleanSweepCategory('1970 NLCS At Cincinnati Program NM')).toBe('sports_memorabilia');
expect(cleanSweepCategory('Big Red Machine Single Signed Ball Lot (27 different) 8')).toBe('sports_memorabilia');
const unsold = await connector.normalize({ url: 'https://www.cleansweepauctions.com/x', externalId: 'x', kind: 'sale', engine: 'api', fetchedAt: new Date(), payload: { ...graded, price: null, winningBidText: null } });
expect(unsold).toEqual([]);
});
});