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, { buildIdFromHtml, currencyFromSymbol, parseAuctionJson, parsePastAuctions } 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 ENTRY = { uuid: 'e33169b2', saleNumber: 'S26016', title: 'Important Multiples of Great Britain | S26016', department: 'Stamps', slug: '/auctions/gb-blocks-and-multiples', amsDate: '2026-07-30T13:30:00.000Z' }; const LIST = { pageProps: { auctionsListData: [{ title: 'Pokémon Summer Celebrations | E26010', saleNumber: 'E26010', date: '31 July 2026', amsDate: '2026-07-31T15:00:00.000Z', arrowLink: '/auctions/pokemon-summer-sale-e26010', auctionId: 'b46b7467', department: 'Trading Cards' }, { title: ENTRY.title, saleNumber: 'S26016', date: '30 July 2026 14.30 BST', amsDate: ENTRY.amsDate, arrowLink: ENTRY.slug, auctionId: ENTRY.uuid, department: 'Stamps' }] } }; const AUCTION = { pageProps: { amsData: { status: 'completed', premiums: [{ percent: 23, amount_over: 0 }] }, auctionPageData: { attributes: { dateTime: '30 July 2026 14.30 BST' } }, categories: [{ uuid: 'cat1', name: 'GB Stamps' }], lots: [ { uuid: 'lot1', auction: { currency: { symbol: '£' }, status: 'completed' }, lot_no: '1', title: { en: 'SG 2 1840 1d black Plate 7 block mint' }, dynamic_fields: { en: { country: 'Great Britain' } }, images: [{ data: { original: { url: 'https://media.app.artisio.co/x/0001_original.jpg' } } }], low: '18000.00', high: '20000.00', start_price: '16000.00', hammer_price: '26000.00', financeItem: { hammer_price: '26000.00', premium: '5980.00', premium_tiers: [{ percent: 23, amount_over: 0 }] }, category_uuid: 'cat1', status: 'sold', session_date: '2026-07-30T13:30:00.000Z', country: 'Great Britain' }, { uuid: 'lot4', auction: { currency: { symbol: '£' }, status: 'completed' }, lot_no: '4', title: { en: 'SG 7 1841 1d red-brown strip of 11 mint' }, images: [], low: '1000.00', high: '1200.00', start_price: '900.00', hammer_price: null, financeItem: null, category_uuid: 'cat1', status: 'unsold', session_date: '2026-07-30T13:30:00.000Z', country: 'Great Britain' }, ], }, }; describe('sgbaldwins', () => { runFixtureSuite(connector, it, expect); it('reads the buildId, filters the past-auction list and parses lots with hammer + premium', () => { expect(buildIdFromHtml('')).toBe('QR6Ic5gmqCZAHXbcQV1E7'); const list = parsePastAuctions(LIST); expect(list.length).toBe(2); expect(list[1]).toEqual(ENTRY); const p = parseAuctionJson(AUCTION, ENTRY)!; expect(p.auction).toMatchObject({ status: 'completed', currencySymbol: '£', dateTimeText: '30 July 2026 14.30 BST', premiums: [{ percent: 23, amount_over: 0 }] }); expect(p.lots[0]).toMatchObject({ lotNo: '1', hammerPrice: '26000.00', premium: '5980.00', status: 'sold', categoryName: 'GB Stamps', country: 'Great Britain', images: ['https://media.app.artisio.co/x/0001_original.jpg'] }); expect(p.lots[1]!.hammerPrice).toBeNull(); expect(currencyFromSymbol('£')).toBe('GBP'); expect(currencyFromSymbol('$')).toBe('USD'); }); it('normalises sold lots as GBP hammer sales with premium in metadata; unsold skipped', async () => { const p = parseAuctionJson(AUCTION, ENTRY)!; const payload = { kind: 'auction_lots' as const, auction: p.auction, url: 'https://sgbaldwins.com/auctions/gb-blocks-and-multiples', chunk: 0, totalLots: 2, lots: p.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(1); const s = out[0]!; if (s.kind !== 'sale') throw new Error('sale'); expect(s).toMatchObject({ price: 26000, currency: 'GBP', buyerPremiumIncluded: false, lotNumber: '1', auctionHouse: "Stanley Gibbons Baldwin's", location: 'GB' }); expect(s.saleDate.toISOString()).toBe('2026-07-30T00:00:00.000Z'); expect(s.attributes).toMatchObject({ categorySlug: 'stamps', year: 1840, country: 'GB' }); expect(s.attributes.identifiers).toEqual({ sgbaldwins_lot: 'lot1', sg_number: '2' }); expect(s.attributes.metadata).toMatchObject({ estimate_low: 18000, estimate_high: 20000, buyer_premium_amount: 5980, buyer_premium_percent: 23, total_with_premium: 31980 }); }); it('fixtures: stamps and coins auctions normalise to GBP hammer sales', async () => { for (const name of ['stamps-gb-blocks-s26016', 'coins-auction-131-c26005']) { const out = await connector.normalize(loadFixture('sgbaldwins', name).raw); expect(out.length).toBeGreaterThan(0); for (const r of out) if (r.kind === 'sale') expect(r).toMatchObject({ currency: 'GBP', buyerPremiumIncluded: false }); } const coins = await connector.normalize(loadFixture('sgbaldwins', 'coins-auction-131-c26005').raw); expect(coins.every((r) => r.kind === 'sale' && r.attributes.categorySlug !== 'stamps')).toBe(true); }); });