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, parsePsaPage } from './index.js'; import { parseCardGrade, psaCategorySlug } from '../_g2-grading-lib/text.js'; const connector = createConnector(localMeta(meta)); const snapshotOf = (name: string) => (loadFixture('psa-cert', name).raw.payload as { snapshot: string }).snapshot; describe('psa-cert', () => { runFixtureSuite(connector, it, expect); it('builds and recognises PSA cert URLs', () => { expect(certUrl('69211238')).toBe('https://www.psacard.com/cert/69211238'); expect(certUrl(' 69211238 ')).toBe('https://www.psacard.com/cert/69211238'); expect(certFromUrl('https://www.psacard.com/cert/69211238/psa')).toBe('69211238'); expect(certFromUrl('https://www.psacard.com/en-CA/cert/41319734/psa')).toBe('41319734'); expect(certFromUrl('https://psacard.com/cert/12345678?utm=x')).toBe('12345678'); expect(certFromUrl('https://www.psacard.com/pop/tcg-cards/1999/pokemon-game/57801')).toBeNull(); expect(connector.urlPatterns.some((re) => re.test(certUrl('69211238')))).toBe(true); expect(connector.certIdentifier('69211238')).toBe('69211238'); }); it('parses the Charizard cert page: item information, grade and population', () => { const page = parsePsaPage(snapshotOf('charizard-psa10')); expect(page).not.toBeNull(); expect(page!.title).toBe('1999 POKEMON GAME #4 CHARIZARD-HOLO'); expect(page!.fields).toMatchObject({ 'Cert Number': '69211238', 'Item Grade': 'GEM MT 10', Year: '1999', 'Brand/Title': 'POKEMON GAME', Subject: 'CHARIZARD-HOLO', 'Card Number': '4', Category: 'TCG Cards' }); expect(page!.population).toBe(488); expect(page!.popHigher).toBe(0); expect(page!.populationUrl).toMatch(/psacard\.com\/pop\//); expect(page!.estimate).toBeGreaterThan(1000); expect(page!.specId).toBe('544027'); expect(page!.images).toHaveLength(2); }); it('normalises to a pokemon catalog_item + a partial population_report', async () => { const out = await connector.normalize(loadFixture('psa-cert', 'charizard-psa10').raw); const item = out.find((r) => r.kind === 'catalog_item'); const pop = out.find((r) => r.kind === 'population_report'); expect(item && item.kind === 'catalog_item').toBeTruthy(); if (item?.kind === 'catalog_item') { expect(item.attributes.categorySlug).toBe('pokemon'); expect(item.attributes).toMatchObject({ name: 'Charizard-Holo', set: 'Pokemon Game', number: '4', year: 1999, franchise: 'Pokémon' }); expect(item.attributes.identifiers).toEqual({ psa_spec_id: '544027', psa_cert: '69211238' }); expect(item.grade).toEqual({ grader: 'psa', grade: '10', qualifier: null, certificationNumber: '69211238' }); expect(item.imageUrls.length).toBe(2); expect(item.attributes.metadata.psa_estimate_usd).toBeGreaterThan(0); expect(item.confidence).toBeLessThan(1); } expect(pop && pop.kind === 'population_report').toBeTruthy(); if (pop?.kind === 'population_report') { expect(pop.grader).toBe('psa'); expect(pop.byGrade).toEqual({ '10': 488, higher: 0 }); expect(pop.total).toBe(488); expect(pop.attributes.metadata.population_scope).toBe('grade_and_higher'); } }); it('handles a vintage baseball card with a population above the grade', async () => { const out = await connector.normalize(loadFixture('psa-cert', 'jim-palmer-psa8').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.categorySlug).toBe('baseball_cards'); expect(item.grade.grade).toBe('8'); expect(item.attributes.year).toBe(1967); } if (pop?.kind === 'population_report') { expect(pop.byGrade['8']).toBeGreaterThan(0); expect(pop.total).toBe(pop.byGrade['8']! + pop.byGrade.higher!); } }); it('yields zero records for a not-found cert (HTTP 404) without throwing', async () => { const fx = loadFixture('psa-cert', 'not-found'); expect((fx.raw.payload as { status: string }).status).toBe('not_found'); await expect(connector.normalize(fx.raw)).resolves.toEqual([]); expect(connector.classify('

Page not found

The requested cert could not be found.

')).toBe('not_found'); expect(connector.classify('
Just a moment...
')).toBe('unknown'); expect(parsePsaPage('

nothing

')).toBeNull(); }); it('parses PSA grade labels and categories', () => { expect(parseCardGrade('GEM MT 10')).toMatchObject({ grade: '10', qualifier: null }); expect(parseCardGrade('NM-MT 8 (OC)')).toMatchObject({ grade: '8', qualifier: 'OC' }); expect(parseCardGrade('EX-MT 6 MC')).toMatchObject({ grade: '6', qualifier: 'MC' }); expect(parseCardGrade('AUTHENTIC')).toMatchObject({ grade: 'authentic' }); expect(parseCardGrade('Authentic Altered')).toMatchObject({ grade: 'authentic', qualifier: 'Altered' }); expect(psaCategorySlug('TCG Cards', 'POKEMON GAME').slug).toBe('pokemon'); expect(psaCategorySlug('Baseball Cards', 'TOPPS').slug).toBe('baseball_cards'); expect(psaCategorySlug('TCG Cards', 'MAGIC THE GATHERING').slug).toBe('magic_the_gathering'); expect(psaCategorySlug('Non-Sports Cards', 'GARBAGE PAIL KIDS').slug).toBe('non_sport_cards'); expect(psaCategorySlug('Tickets', 'WORLD SERIES').confident).toBe(false); }); });