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, { parseLotPage, slabIds } 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 PAGE = `
Berlin Auction 400 · Lot 663
1 Feb 2024 Finished
SCHWEDEN KÖNIGREICH Oskar II., 1872-1907.1 Krone 1884, Stockholm.
| Nominal/Year | 1 Krone 1884, |
| Mint | Stockholm. |
| Condition | Fast Stempelglanz / In US-Plastikholder der PCGS mit der Bewertung MS 64 PL (812624.64/38930174). |
| Quotes | SM (2022) 69 |
`;
const UNSOLD = PAGE.replace(/[\s\S]*?<\/div><\/div><\/div>/, '
');
describe('kuenker', () => {
runFixtureSuite(connector, it, expect);
it('parses a finished lot page', () => {
const p = parseLotPage(PAGE, 'https://www.kuenker.de/schweden-koenigreich-oskar-ii.-1872-1907.-1-krone-1884-stockholm./956421')!;
expect(p).toMatchObject({ productId: '956421', title: 'SCHWEDEN KÖNIGREICH Oskar II., 1872-1907. 1 Krone 1884, Stockholm.', auctionName: 'Berlin Auction 400', lotNumber: '663', dateText: '1 Feb 2024', statusText: 'Finished', estimateText: '€750', hammerText: '€1,100' });
expect(p.properties).toMatchObject({ 'Nominal/Year': '1 Krone 1884,', Mint: 'Stockholm.', Quotes: 'SM (2022) 69' });
expect(p.images).toEqual(['https://kuenker.s3.eu-central-1.amazonaws.com/images/auction/00129/00663q00.jpg']);
expect(p.description).toContain('1 Krone 1884');
expect(parseLotPage(UNSOLD, 'https://www.kuenker.de/x/1')!.hammerText).toBeNull();
expect(slabIds('Fast Stempelglanz / In US-Plastikholder der PCGS mit der Bewertung MS 64 PL (812624.64/38930174).')).toEqual({ pcgsNumber: '812624', cert: '38930174' });
expect(slabIds('Vorzüglich')).toEqual({ pcgsNumber: null, cert: null });
});
it('normalises a hammer sale in EUR with PCGS slab data; unsold pages yield nothing', async () => {
const url = 'https://www.kuenker.de/schweden-koenigreich-oskar-ii.-1872-1907.-1-krone-1884-stockholm./956421';
const out = await connector.normalize({ url, externalId: '956421', kind: 'sale', engine: 'api', fetchedAt: new Date('2026-09-08T00:00:00Z'), payload: parseLotPage(PAGE, url) });
expect(out.length).toBe(1);
const s = out[0]!;
if (s.kind !== 'sale') throw new Error('sale');
expect(s).toMatchObject({ price: 1100, currency: 'EUR', buyerPremiumIncluded: false, lotNumber: '663', auctionHouse: 'Fritz Rudolf Künker', location: 'DE', isBundle: false });
expect(s.saleDate.toISOString()).toBe('2024-02-01T00:00:00.000Z');
expect(s.grade).toEqual({ grader: 'pcgs', grade: 'MS64PL', qualifier: null, certificationNumber: '38930174' });
expect(s.attributes).toMatchObject({ categorySlug: 'coins', year: 1884, country: 'SE', variant: 'Stockholm' });
expect(s.attributes.identifiers).toEqual({ kuenker_product_id: '956421', pcgs_number: '812624', pcgs_cert: '38930174' });
expect(s.attributes.metadata).toMatchObject({ estimate: 750, auction: 'Berlin Auction 400', mint: 'Stockholm.' });
expect(s.condition.conditionRaw).toBe('Fast Stempelglanz');
expect(await connector.normalize({ url, externalId: '1', kind: 'sale', engine: 'api', fetchedAt: new Date(), payload: parseLotPage(UNSOLD, url) })).toEqual([]);
});
it('fixtures: three live lot pages normalise to EUR hammer sales', async () => {
for (const name of ['lot-taler-juelich-berg-147194', 'lot-5-dukaten-esslingen-956135', 'lot-china-100-yuan-922356']) {
const out = await connector.normalize(loadFixture('kuenker', name).raw);
expect(out.length).toBe(1);
if (out[0]!.kind === 'sale') expect(out[0]).toMatchObject({ currency: 'EUR', buyerPremiumIncluded: false });
}
const china = await connector.normalize(loadFixture('kuenker', 'lot-china-100-yuan-922356').raw);
if (china[0]!.kind === 'sale') expect(china[0]!.attributes).toMatchObject({ country: 'CN', year: 1998, material: 'gold' });
});
});