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, { lotsUrl, parseArchive, parseLotsPage, parseOverview } 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 ARCHIVE = `
Auction 321-332
June 3,2024 - June 8,2024
Auction 291-297
November 28,2022 - December 3,2022
`;
const OVERVIEW = `Auction 291-297
Catalogue 291: Europe & Overseas
Catalogue 294: Ceylon
`;
const LOTS = `
Recess by Perkins Bacon 1856/57: Proof for the initial issue, 6 d. black imperforate, a sheet marginal block of four (SG 1).
Starting bid : 200.00 CHF
Hammer price : 260.00 CHF
Lot# : 6015Ceylon
1857 4 d. dull rose, unused without gum, a fine example. Cert. RPSL (1973).
Starting bid : 200.00 CHF
Hammer price : not sold
`;
describe('corinphila', () => {
runFixtureSuite(connector, it, expect);
it('parses the archive list and the auction overview (auctionData JSON + catalogue parts)', () => {
expect(parseArchive(ARCHIVE)).toEqual([
{ id: '28', name: 'Auction 321-332', dateText: 'June 3,2024 - June 8,2024' },
{ id: '24', name: 'Auction 291-297', dateText: 'November 28,2022 - December 3,2022' },
]);
const ov = parseOverview(OVERVIEW, '24')!;
expect(ov.auction).toEqual({ id: '24', name: 'Auction 291-297', currency: 'CHF', startDate: '2022-11-28 14:34:07', endDate: '2022-12-03 14:34:07', status: 'closed' });
expect(ov.parts).toEqual([{ catalogPart: '199', title: 'Catalogue 291: Europe & Overseas', lotCount: 1636 }, { catalogPart: '194', title: 'Catalogue 294: Ceylon', lotCount: 744 }]);
expect(lotsUrl('24', '194', 2)).toBe('https://corinphila.ch/en/_auctions/&action=showLots&auctionID=24&catalogPart=194&show_all_lots=1&page=2');
});
it('parses lot blocks with hammer / not sold and normalises CHF hammer sales', async () => {
const ov = parseOverview(OVERVIEW, '24')!;
const p = parseLotsPage(LOTS, ov.auction, ov.parts[1]!, lotsUrl('24', '194', 1));
expect(p.totalPages).toBe(8);
expect(p.lots.length).toBe(2);
expect(p.lots[0]).toMatchObject({ lotNo: '6001', country: 'Ceylon', startText: '200.00 CHF', hammerText: '260.00 CHF', conditionCodes: ['8', '9'], images: ['https://d2xqn5t7wr4wg1.cloudfront.net/modules/auctions/24/pics/small/a.jpg'] });
expect(p.lots[1]!.hammerText).toBe('not sold');
const out = await connector.normalize({ url: p.url, externalId: 'x', 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('sale');
expect(s).toMatchObject({ price: 260, currency: 'CHF', buyerPremiumIncluded: false, lotNumber: '6001', auctionHouse: 'Corinphila Auctions', location: 'CH', sourceUrl: 'https://corinphila.ch/en/_auctions/&action=showLot&auctionID=24&lotno=6001' });
expect(s.saleDate.toISOString()).toBe('2022-11-28T00:00:00.000Z');
expect(s.attributes).toMatchObject({ categorySlug: 'stamps', country: 'LK', set: 'Catalogue 294: Ceylon' });
expect(s.attributes.identifiers.corinphila_lot).toBe('24/6001');
expect(s.attributes.metadata).toMatchObject({ starting_bid: 200, michel_or_sg: 'SG 1' });
});
it('fixture normalises to CHF hammer sales and skips not-sold lots', async () => {
const fx = loadFixture('corinphila', 'lots-page-auction-24-part-194');
const out = await connector.normalize(fx.raw);
expect(out.length).toBe(8);
for (const r of out) if (r.kind === 'sale') expect(r).toMatchObject({ currency: 'CHF', buyerPremiumIncluded: false });
});
});