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, splitCert } from './index.js'; import { ccgClassify, ccgParse } from '../_g2-grading-lib/ccg.js'; const connector = createConnector(localMeta(meta)); const snapshotOf = (name: string) => (loadFixture('ngc-cert', name).raw.payload as { snapshot: string }).snapshot; describe('ngc-cert', () => { runFixtureSuite(connector, it, expect); it('builds and recognises NGC cert URLs (grade segment required)', () => { expect(splitCert('4627835-016/61')).toEqual({ number: '4627835-016', grade: '61' }); expect(splitCert('4627835-016 61')).toEqual({ number: '4627835-016', grade: '61' }); expect(splitCert('8444762-003:NGCDetails')).toEqual({ number: '8444762-003', grade: 'NGCDetails' }); expect(splitCert('4627835-016')).toEqual({ number: '4627835-016', grade: null }); expect(certUrl('4627835-016/61')).toBe('https://www.ngccoin.com/certlookup/4627835-016/61/'); expect(certUrl('4627835-016')).toBe('https://www.ngccoin.com/certlookup/4627835-016/'); expect(certFromUrl('https://www.ngccoin.com/certlookup/4627835-016/61/')).toBe('4627835-016/61'); expect(certFromUrl('https://www.ngccoin.com/certlookup/8444762-003/NGCDetails/')).toBe('8444762-003/NGCDetails'); expect(certFromUrl('https://www.ngccoin.uk/certlookup/4627835-016/61')).toBe('4627835-016/61'); expect(certFromUrl('https://www.ngccoin.com/population-report/')).toBeNull(); expect(connector.urlPatterns.some((re) => re.test(certUrl('4627835-016/61')))).toBe(true); expect(connector.certIdentifier('4627835-016/61')).toBe('4627835-016'); }); it('parses description, grade, label, price guide and population stats', () => { const page = ccgParse(snapshotOf('draped-bust-eagle-ms61')); expect(page.fields).toMatchObject({ 'Cert #': '4627835-016', Description: '1795 13 LEAVES $10', Grade: 'MS 61', 'Grade Date': '2018-08-29', Label: 'NGC Standard Brown' }); expect(page.atGrade).toBe(7); expect(page.popGradeKey).toBe('MS 61'); expect(page.stats.some((s) => /^\$[\d,]+/.test(s))).toBe(true); expect(page.images.length).toBeGreaterThanOrEqual(2); expect(ccgClassify(snapshotOf('draped-bust-eagle-ms61'))).toBe('found'); }); it('normalises to a coins catalog_item (bare cert identifier) + population_report', async () => { const out = await connector.normalize(loadFixture('ngc-cert', 'draped-bust-eagle-ms61').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('1795 13 LEAVES $10 NGC MS 61'); expect(item.attributes).toMatchObject({ categorySlug: 'coins', name: '1795 13 LEAVES $10', year: 1795 }); expect(item.attributes.identifiers).toEqual({ ngc_cert: '4627835-016' }); expect(item.grade).toEqual({ grader: 'ngc', grade: 'MS61', qualifier: null, certificationNumber: '4627835-016' }); expect(item.externalId).toBe('ngc:4627835-016'); expect(item.attributes.metadata.ngc_price_guide_value_usd).toBe(165000); } else throw new Error('catalog_item missing'); if (pop?.kind === 'population_report') { expect(pop.byGrade.MS61).toBe(7); expect(pop.sourceUrl).toMatch(/population-report/); } else throw new Error('population_report missing'); }); it('handles an NGC Details grade (no numeric grade)', async () => { const out = await connector.normalize(loadFixture('ngc-cert', 'capped-bust-half-details').raw); const item = out.find((r) => r.kind === 'catalog_item'); if (item?.kind === 'catalog_item') { expect(item.grade.grade).toBe('AU'); expect(item.grade.qualifier).toMatch(/^Details/); expect(item.grade.certificationNumber).toBe('8444762-003'); expect(item.attributes.year).toBe(1839); } else throw new Error('catalog_item missing'); }); it('yields zero records for a not-found cert without throwing', async () => { const fx = loadFixture('ngc-cert', 'not-found'); expect(ccgClassify((fx.raw.payload as { snapshot: string }).snapshot)).toBe('not_found'); await expect(connector.normalize(fx.raw)).resolves.toEqual([]); }); });