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, { fallbackDate, parseResultsIndex, parseResultsPage } 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 INDEX = `Spring 2026 August 2026 Spring 2026`;
const PAGE = `
SPRING 2026 LIVE AUCTION RESULTS Sale date: Thursday, May 21 2026 SESSIONS: Post-War & Contemporary Art () Old Master, Impressionist & Modern Art () The following Lots were sold at the prices stated. Prices include Buyer's Premium.
`;
describe('heffel', () => {
runFixtureSuite(connector, it, expect);
it('parses the results index (deduplicated) and a results page', () => {
const sales = parseResultsIndex(INDEX);
expect(sales.map((s) => s.label)).toEqual(['Spring 2026', 'August 2026']);
const p = parseResultsPage(PAGE, sales[0]!);
expect(p.saleDateText).toBe('May 21 2026');
expect(p.premiumIncluded).toBe(true);
expect(p.sessions[0]).toContain('Post-War');
expect(p.lots.length).toBe(2);
expect(p.lots[1]).toMatchObject({ lotNumber: '002', title: 'Snow Light in My House', artist: 'Pratt, Mary Frances', priceText: '$205,250', image: 'https://www.heffel.com/Images/Lots/150/PCRE-04581-0021-01.jpg' });
});
it('normalises CAD sales with premium included and the published sale date', async () => {
const sale = parseResultsIndex(INDEX)[0]!;
const out = await connector.normalize({ url: 'x', externalId: 'a', kind: 'sale', engine: 'api', fetchedAt: new Date('2026-09-07T00:00:00Z'), payload: parseResultsPage(PAGE, sale) });
expect(out.length).toBe(2);
const s = out[0]!;
if (s.kind !== 'sale') throw new Error('sale');
expect(s).toMatchObject({ price: 73250, currency: 'CAD', buyerPremiumIncluded: true, lotNumber: '001', auctionHouse: 'Heffel', confidence: 0.9 });
expect(s.saleDate.toISOString()).toBe('2026-05-21T00:00:00.000Z');
expect(s.attributes.brand).toBe('Molinari, Guido');
expect(['art', 'contemporary_art']).toContain(s.attributes.categorySlug);
});
it('falls back to the month of a monthly online sale', () => {
expect(fallbackDate('August 2026')?.toISOString()).toBe('2026-08-01T00:00:00.000Z');
expect(fallbackDate('Spring 2026')).toBeNull();
});
it('fixture lots are CAD with premium', async () => {
const out = await connector.normalize(loadFixture('heffel', 'results-page').raw);
expect(out.length).toBeGreaterThan(0);
for (const r of out) if (r.kind === 'sale') expect(r).toMatchObject({ currency: 'CAD', buyerPremiumIncluded: true });
});
});