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'; import { parseNoteGrade } from '../_g2-grading-lib/text.js'; const connector = createConnector(localMeta(meta)); const snapshotOf = (name: string) => (loadFixture('pmg-cert', name).raw.payload as { snapshot: string }).snapshot; describe('pmg-cert', () => { runFixtureSuite(connector, it, expect); it('builds and recognises PMG cert URLs (grade segment required)', () => { expect(splitCert('8067968-054/67')).toEqual({ number: '8067968-054', grade: '67' }); expect(certUrl('8067968-054/67')).toBe('https://www.pmgnotes.com/certlookup/8067968-054/67/'); expect(certUrl('8067968-054')).toBe('https://www.pmgnotes.com/certlookup/8067968-054/'); expect(certFromUrl('https://www.pmgnotes.com/certlookup/8067968-054/67/')).toBe('8067968-054/67'); expect(certFromUrl('https://www.pmgnotes.com/certlookup/8067968-054')).toBe('8067968-054'); expect(certFromUrl('https://www.pmgnotes.com/population-report/')).toBeNull(); expect(connector.urlPatterns.some((re) => re.test(certUrl('8067968-054/67')))).toBe(true); expect(connector.certIdentifier('8067968-054/67')).toBe('8067968-054'); }); it('parses note description, Pick number, grade (+EPQ) and population', () => { const page = ccgParse(snapshotOf('bermuda-specimen-67epq')); expect(page.fields).toMatchObject({ 'PMG Cert': '8067968-054', 'Note #': 'BER31as', Region: 'BER', Grade: '67 EPQ', 'Grade Date': '2019-12-20', Comments: 'Exceptional Paper Quality' }); expect(page.fields['Note Description']).toMatch(/^Bermuda, Monetary Authority/); expect(page.atGrade).toBe(7); expect(page.higher).toBe(1); expect(page.popGradeKey).toBe('67EPQ'); expect(page.images.length).toBe(2); expect(ccgClassify(snapshotOf('bermuda-specimen-67epq'))).toBe('found'); }); it('normalises to a banknotes catalog_item + population_report', async () => { const out = await connector.normalize(loadFixture('pmg-cert', 'bermuda-specimen-67epq').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.attributes).toMatchObject({ categorySlug: 'banknotes', year: 1974, country: 'Bermuda', region: 'BER' }); expect(item.attributes.identifiers).toEqual({ pick_number: 'BER31as', pmg_cert: '8067968-054' }); expect(item.grade).toEqual({ grader: 'pmg', grade: '67', qualifier: 'EPQ', certificationNumber: '8067968-054' }); expect(item.attributes.metadata.serial_number).toMatch(/000000/); } else throw new Error('catalog_item missing'); if (pop?.kind === 'population_report') { expect(pop.byGrade).toEqual({ '67 EPQ': 7, higher: 1 }); expect(pop.total).toBe(8); } else throw new Error('population_report missing'); }); it('a wrong grade in the URL is a not-found page → zero records, no throw', async () => { const fx = loadFixture('pmg-cert', 'wrong-grade-not-found'); expect(ccgClassify((fx.raw.payload as { snapshot: string }).snapshot)).toBe('not_found'); await expect(connector.normalize(fx.raw)).resolves.toEqual([]); }); it('parses PMG grade labels', () => { expect(parseNoteGrade('67 EPQ')).toMatchObject({ grade: '67', qualifier: 'EPQ' }); expect(parseNoteGrade('58')).toMatchObject({ grade: '58', qualifier: null }); expect(parseNoteGrade('Net 30')).toMatchObject({ grade: '30', qualifier: 'Net' }); }); });