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, { dlrcGrade, LotSchema } 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 LOT = { auctionId: 24, lotId: 9580, lotNumber: 7001, totalBids: 2, lotCurrentBid: 265500, lotClosesAt: '2023-09-18T00:00:00Z', soldInAuction: true, id: 752042, itemType: 'US_COIN', name: '1793 1/2C NGC/CAC Poor 01', shortDescription: 'First Year Half Cent', description: 'A rare coin in exciting, lowball condition!', seriesName: 'Liberty Cap Half Cent', groupName: 'Half-Cents and Cents', categoryName: 'U.S. Coins', certificationNumber: '6460687001', gradingService: 'NGC', grade: 1, fullGrade: 'PO1BN', isPlus: false, isCac: true, isEPQ: false, price: null, salePrice: 265500, status: 'SOLD', catalogEntry: { id: 3915, pcgsNumber: 1000, title: '1793 1/2C, BN', denomination: '1/2C', coinDate: 1793, mintMark: 'P', designation: 'BN', majorVariety: '', dieVariety: '', strikeType: 'MS', mintage: 35334, numistaTypeId: null, kmNumber: null, categoryName: 'Liberty Cap Half Cent' }, images: [{ thumbnailUrl: 'https://d2lj9qe62mju4a.cloudfront.net/inventory/3/752042/a.jpg?width=256&height=256', url: 'https://d2lj9qe62mju4a.cloudfront.net/inventory/3/752042/a.jpg' }], }; const UNSOLD = { ...LOT, id: 1, lotId: 2, salePrice: null, lotCurrentBid: 1000, soldInAuction: false, status: 'AVAILABLE' }; describe('dlrc', () => { runFixtureSuite(connector, it, expect); it('maps grading service / full grade / CAC / details into the grade block', () => { expect(dlrcGrade(LotSchema.parse(LOT))).toEqual({ grader: 'ngc', grade: 'PO1BN', qualifier: 'CAC', certificationNumber: '6460687001' }); expect(dlrcGrade({ gradingService: 'ANACS', fullGrade: 'VG8BN', grade: 8, isPlus: false, isCac: false, certificationNumber: null, name: '1804 1/2C ANACS VG Details (Plain 4, No Stems, Damaged)' })).toMatchObject({ grader: 'anacs', grade: 'VG8BN', qualifier: 'VG Details (Plain 4, No Stems, Damaged)' }); expect(dlrcGrade({ gradingService: 'PCGS', fullGrade: 'MS65', grade: 65, isPlus: true, isCac: false, certificationNumber: '12345678', name: '1881-S $1 PCGS MS65+' }).grade).toBe('MS65+'); }); it('normalises sold lots: cents → dollars, no buyer premium, cert + PCGS number identifiers, unsold skipped', async () => { const payload = { kind: 'past_auction_page' as const, url: 'https://api.collectiblesshowcase.com/consumer/inventory/past/auction?auctions=24&pageNumber=1&pageSize=100', auctionId: 24, auctionTitle: 'Test', pageNumber: 1, totalPages: 1, totalItems: 2, lots: [LOT, UNSOLD] }; 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: 2655, currency: 'USD', buyerPremiumIncluded: true, lotNumber: '7001', auctionHouse: 'David Lawrence Rare Coins', sourceUrl: 'https://www.davidlawrence.com/inventory/752042', isBundle: false }); expect(s.saleDate.toISOString()).toBe('2023-09-18T00:00:00.000Z'); expect(s.grade).toEqual({ grader: 'ngc', grade: 'PO1BN', qualifier: 'CAC', certificationNumber: '6460687001' }); expect(s.attributes).toMatchObject({ categorySlug: 'coins', year: 1793, country: 'US', series: 'Liberty Cap Half Cent', variant: 'P mint' }); expect(s.attributes.identifiers).toEqual({ dlrc_inventory_id: '752042', pcgs_number: '1000', ngc_cert: '6460687001' }); expect(s.attributes.metadata).toMatchObject({ mintage: 35334, denomination: '1/2C', designation: 'BN' }); expect(s.imageUrls).toEqual(['https://d2lj9qe62mju4a.cloudfront.net/inventory/3/752042/a.jpg']); }); it('fixture pages normalise to USD sales with grades', async () => { const out = await connector.normalize(loadFixture('dlrc', 'auction-337-page-1').raw); expect(out.length).toBeGreaterThan(3); for (const r of out) if (r.kind === 'sale') expect(r).toMatchObject({ currency: 'USD', buyerPremiumIncluded: true }); const legacy = await connector.normalize(loadFixture('dlrc', 'archive-page-legacy-lots').raw); expect(legacy.some((r) => r.kind === 'sale' && r.saleDate.getUTCFullYear() < 2020)).toBe(true); }); });