import { describe, expect, it } from 'vitest'; import meta from './meta.json' with { type: 'json' }; import { localMeta } from '../../api/_lib/local-meta.js'; import { loadFixture, runFixtureSuite } from '@rareindex/connectors/testing'; import createConnector, { certFromUrl, certUrl, parsePcgsPage } from './index.js'; import { parseCoinGrade } from '../_g2-grading-lib/text.js'; const connector = createConnector(localMeta(meta)); const snapshotOf = (name: string) => (loadFixture('pcgs-cert', name).raw.payload as { snapshot: string }).snapshot; describe('pcgs-cert', () => { runFixtureSuite(connector, it, expect); it('builds and recognises PCGS cert URLs', () => { expect(certUrl('47044550')).toBe('https://www.pcgs.com/cert/47044550'); expect(certFromUrl('https://www.pcgs.com/cert/47044550')).toBe('47044550'); expect(certFromUrl('https://pcgs.com/cert/47044550/')).toBe('47044550'); expect(certFromUrl('https://www.pcgs.com/cert/verify?certno=47044550')).toBe('47044550'); expect(certFromUrl('https://www.pcgs.com/coinfacts/coin/detail/7359/58')).toBeNull(); expect(connector.urlPatterns.some((re) => re.test(certUrl('47044550')))).toBe(true); }); it('parses the cert table: grade, PCGS#, population, mintage', () => { const page = parsePcgsPage(snapshotOf('peace-dollar-1922s-au58')); expect(page).not.toBeNull(); expect(page!.title).toBe('1922-S $1'); expect(page!.cert).toBe('47044550'); expect(page!.rows).toMatchObject({ Grade: 'AU58', 'PCGS #': '7359', 'Date, Mintmark': '1922-S', Denomination: '$1', Population: '910', 'Pop Higher': '10,272', Region: 'The United States of America', Mintage: '17,475,000' }); expect(page!.links['Population']).toMatch(/\/pop\/pcgsnolookup\?s=7359&g=58/); expect(page!.images.length).toBeGreaterThan(0); expect(connector.classify(snapshotOf('peace-dollar-1922s-au58'))).toBe('found'); }); it('normalises to a coins catalog_item (pcgs_number) + population_report', async () => { const out = await connector.normalize(loadFixture('pcgs-cert', 'peace-dollar-1922s-au58').raw); const item = out.find((r) => r.kind === 'catalog_item'); const pop = out.find((r) => r.kind === 'population_report'); if (item?.kind === 'catalog_item') { expect(item.rawTitle).toBe('1922-S $1 PCGS AU58'); expect(item.attributes).toMatchObject({ categorySlug: 'coins', name: '1922-S $1', year: 1922, country: 'US', productionQuantity: 17475000 }); expect(item.attributes.identifiers).toEqual({ pcgs_number: '7359', pcgs_cert: '47044550' }); expect(item.grade).toEqual({ grader: 'pcgs', grade: 'AU58', qualifier: null, certificationNumber: '47044550' }); expect(item.attributes.metadata.price_guide_value_usd).toBe(92); } else throw new Error('catalog_item missing'); if (pop?.kind === 'population_report') { expect(pop.byGrade).toEqual({ AU58: 910, higher: 10272 }); expect(pop.total).toBe(11182); expect(pop.sourceUrl).toMatch(/pcgsnolookup/); } else throw new Error('population_report missing'); }); it('parses a Mint State quarter', async () => { const out = await connector.normalize(loadFixture('pcgs-cert', 'quarter-1916d-ms65').raw); const item = out.find((r) => r.kind === 'catalog_item'); if (item?.kind === 'catalog_item') { expect(item.grade.grade).toBe('MS65'); expect(item.attributes.identifiers.pcgs_number).toBe('5674'); expect(item.attributes.year).toBe(1916); } }); it('yields zero records for a not-found cert without throwing', async () => { const fx = loadFixture('pcgs-cert', 'not-found'); expect(connector.classify((fx.raw.payload as { snapshot: string }).snapshot)).toBe('not_found'); await expect(connector.normalize(fx.raw)).resolves.toEqual([]); expect(parsePcgsPage('
x
')).toBeNull(); }); it('parses Sheldon grades', () => { expect(parseCoinGrade('AU58')).toMatchObject({ grade: 'AU58', qualifier: null }); expect(parseCoinGrade('MS65+')).toMatchObject({ grade: 'MS65+' }); expect(parseCoinGrade('PR69DCAM')).toMatchObject({ grade: 'PR69', qualifier: 'Dcam' }); expect(parseCoinGrade('MS 61')).toMatchObject({ grade: 'MS61' }); expect(parseCoinGrade('PF 70 ULTRA CAMEO')).toMatchObject({ grade: 'PF70', qualifier: 'Ultra Cameo' }); expect(parseCoinGrade('AU DETAILS')).toMatchObject({ grade: 'AU', qualifier: 'Details' }); expect(parseCoinGrade('UNC DETAILS - CLEANED')).toMatchObject({ grade: 'UNC', qualifier: 'Details, Cleaned' }); }); });