SPB Git forge

spb/rareindex

Public
54commits 1branches 0releases
7.1 MBsize
maindefault branch
10 days agolast push
TypeScript 61.9% HTML 37.2% SQL 0.7%
4.5 KB · 79 lines typescript
Raw Blame History
1import { describe, expect, it } from 'vitest';2import meta from './meta.json' with { type: 'json' };3import { localMeta } from '../../api/_lib/local-meta.js';4import { loadFixture, runFixtureSuite } from '@rareindex/connectors/testing';5import createConnector, { certFromUrl, certUrl, parsePcgsPage } from './index.js';6import { parseCoinGrade } from '../_g2-grading-lib/text.js';78const connector = createConnector(localMeta(meta));9const snapshotOf = (name: string) => (loadFixture('pcgs-cert', name).raw.payload as { snapshot: string }).snapshot;1011describe('pcgs-cert', () => {12  runFixtureSuite(connector, it, expect);1314  it('builds and recognises PCGS cert URLs', () => {15    expect(certUrl('47044550')).toBe('https://www.pcgs.com/cert/47044550');16    expect(certFromUrl('https://www.pcgs.com/cert/47044550')).toBe('47044550');17    expect(certFromUrl('https://pcgs.com/cert/47044550/')).toBe('47044550');18    expect(certFromUrl('https://www.pcgs.com/cert/verify?certno=47044550')).toBe('47044550');19    expect(certFromUrl('https://www.pcgs.com/coinfacts/coin/detail/7359/58')).toBeNull();20    expect(connector.urlPatterns.some((re) => re.test(certUrl('47044550')))).toBe(true);21  });2223  it('parses the cert table: grade, PCGS#, population, mintage', () => {24    const page = parsePcgsPage(snapshotOf('peace-dollar-1922s-au58'));25    expect(page).not.toBeNull();26    expect(page!.title).toBe('1922-S $1');27    expect(page!.cert).toBe('47044550');28    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' });29    expect(page!.links['Population']).toMatch(/\/pop\/pcgsnolookup\?s=7359&g=58/);30    expect(page!.images.length).toBeGreaterThan(0);31    expect(connector.classify(snapshotOf('peace-dollar-1922s-au58'))).toBe('found');32  });3334  it('normalises to a coins catalog_item (pcgs_number) + population_report', async () => {35    const out = await connector.normalize(loadFixture('pcgs-cert', 'peace-dollar-1922s-au58').raw);36    const item = out.find((r) => r.kind === 'catalog_item');37    const pop = out.find((r) => r.kind === 'population_report');38    if (item?.kind === 'catalog_item') {39      expect(item.rawTitle).toBe('1922-S $1 PCGS AU58');40      expect(item.attributes).toMatchObject({ categorySlug: 'coins', name: '1922-S $1', year: 1922, country: 'US', productionQuantity: 17475000 });41      expect(item.attributes.identifiers).toEqual({ pcgs_number: '7359', pcgs_cert: '47044550' });42      expect(item.grade).toEqual({ grader: 'pcgs', grade: 'AU58', qualifier: null, certificationNumber: '47044550' });43      expect(item.attributes.metadata.price_guide_value_usd).toBe(92);44    } else throw new Error('catalog_item missing');45    if (pop?.kind === 'population_report') {46      expect(pop.byGrade).toEqual({ AU58: 910, higher: 10272 });47      expect(pop.total).toBe(11182);48      expect(pop.sourceUrl).toMatch(/pcgsnolookup/);49    } else throw new Error('population_report missing');50  });5152  it('parses a Mint State quarter', async () => {53    const out = await connector.normalize(loadFixture('pcgs-cert', 'quarter-1916d-ms65').raw);54    const item = out.find((r) => r.kind === 'catalog_item');55    if (item?.kind === 'catalog_item') {56      expect(item.grade.grade).toBe('MS65');57      expect(item.attributes.identifiers.pcgs_number).toBe('5674');58      expect(item.attributes.year).toBe(1916);59    }60  });6162  it('yields zero records for a not-found cert without throwing', async () => {63    const fx = loadFixture('pcgs-cert', 'not-found');64    expect(connector.classify((fx.raw.payload as { snapshot: string }).snapshot)).toBe('not_found');65    await expect(connector.normalize(fx.raw)).resolves.toEqual([]);66    expect(parsePcgsPage('<div><table><tr><td>x</td></tr></table></div>')).toBeNull();67  });6869  it('parses Sheldon grades', () => {70    expect(parseCoinGrade('AU58')).toMatchObject({ grade: 'AU58', qualifier: null });71    expect(parseCoinGrade('MS65+')).toMatchObject({ grade: 'MS65+' });72    expect(parseCoinGrade('PR69DCAM')).toMatchObject({ grade: 'PR69', qualifier: 'Dcam' });73    expect(parseCoinGrade('MS 61')).toMatchObject({ grade: 'MS61' });74    expect(parseCoinGrade('PF 70 ULTRA CAMEO')).toMatchObject({ grade: 'PF70', qualifier: 'Ultra Cameo' });75    expect(parseCoinGrade('AU DETAILS')).toMatchObject({ grade: 'AU', qualifier: 'Details' });76    expect(parseCoinGrade('UNC DETAILS - CLEANED')).toMatchObject({ grade: 'UNC', qualifier: 'Details, Cleaned' });77  });78});79