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, { parsePricesRealised, parseSalesIndex } 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 = `
`;
const PR = `Prices Realised ยท Sale 142
Sale 142 Important Coins, Medals & Banknotes
Wyoming 175 Macquarie St Sydney, NSW 2000
28-30 July 2026
All prices exclude buyer and vendor premiums.
| Lot 1 | George V - Elizabeth II, florins, 1927 Canberra, 1951 Jubilee (2), 1954 Royal Visit. Good very fine - extremely fine. (4) | $90 |
| Lot 120 | George V, sovereign, 1918 Perth (S.4001), PCGS MS64. Uncirculated. | $1,250 |
| Lot 121 | Commonwealth of Australia, ten shillings, Riddle/Sheehan (1934) (R.10). Fine. | Passed in |
`;
describe('noble-numismatics', () => {
runFixtureSuite(connector, it, expect);
it('parses the sales index (only sales with a prices-realised link are crawled)', () => {
const s = parseSalesIndex(INDEX);
expect(s.length).toBe(2);
expect(s[0]).toEqual({ number: '142', title: 'Sale 142 Important Coins, Medals & Banknotes', dateText: '28-30 July 2026', city: 'Sydney', hasPrices: true });
expect(s[1]!.hasPrices).toBe(false);
});
it('parses prices realised rows: lot id, headline, grading sentence, price or status', () => {
const p = parsePricesRealised(PR, '142')!;
expect(p.sale).toEqual({ number: '142', title: 'Sale 142 Important Coins, Medals & Banknotes', dateText: '28-30 July 2026', city: 'Wyoming 175 Macquarie St Sydney, NSW 2000' });
expect(p.premiumNote).toBe('All prices exclude buyer and vendor premiums.');
expect(p.lots.length).toBe(3);
expect(p.lots[0]).toMatchObject({ lotId: '457987', lotNumber: '1', headline: 'George V - Elizabeth II', gradingText: 'Good very fine - extremely fine.', priceText: '$90', status: null });
expect(p.lots[2]).toMatchObject({ lotId: '458101', priceText: null, status: 'Passed in' });
});
it('normalises AUD hammer sales dated by the sale, skips passed-in lots, flags multiples', async () => {
const page = parsePricesRealised(PR, '142')!;
const payload = { kind: 'prices_realised' as const, sale: page.sale, url: 'https://www.noble.com.au/auctions/sale/142/prices-realised', premiumNote: page.premiumNote, chunk: 0, totalLots: 3, lots: page.lots };
const out = await connector.normalize({ url: payload.url, externalId: 'x', kind: 'sale', engine: 'api', fetchedAt: new Date('2026-09-08T00:00:00Z'), payload });
expect(out.length).toBe(2);
const florins = out[0]!;
const sov = out[1]!;
if (florins.kind !== 'sale' || sov.kind !== 'sale') throw new Error('sale');
expect(florins).toMatchObject({ price: 90, currency: 'AUD', buyerPremiumIncluded: false, isBundle: true, lotNumber: '1', auctionHouse: 'Noble Numismatics', sourceUrl: 'https://www.noble.com.au/auctions/lot/?id=457987' });
expect(florins.saleDate.toISOString()).toBe('2026-07-28T00:00:00.000Z');
expect(florins.condition.conditionRaw).toBe('Good very fine - extremely fine');
expect(sov).toMatchObject({ price: 1250, isBundle: false });
expect(sov.grade).toMatchObject({ grader: 'pcgs', grade: 'MS64' });
expect(sov.attributes).toMatchObject({ categorySlug: 'coins', year: 1918 });
expect(sov.attributes.identifiers.noble_lot).toBe('458100');
});
it('fixtures: 2026 and 1994 sales normalise to AUD hammer prices', async () => {
for (const name of ['sale-142-prices-realised', 'sale-44-prices-realised-1994']) {
const out = await connector.normalize(loadFixture('noble-numismatics', name).raw);
expect(out.length).toBeGreaterThan(0);
for (const r of out) if (r.kind === 'sale') expect(r).toMatchObject({ currency: 'AUD', buyerPremiumIncluded: false });
}
const old = await connector.normalize(loadFixture('noble-numismatics', 'sale-44-prices-realised-1994').raw);
if (old[0]!.kind === 'sale') expect(old[0]!.saleDate.getUTCFullYear()).toBe(1994);
});
});