TypeScript 61.9%
HTML 37.2%
SQL 0.7%
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, parsePsaPage } from './index.js';6import { parseCardGrade, psaCategorySlug } from '../_g2-grading-lib/text.js';78const connector = createConnector(localMeta(meta));9const snapshotOf = (name: string) => (loadFixture('psa-cert', name).raw.payload as { snapshot: string }).snapshot;1011describe('psa-cert', () => {12 runFixtureSuite(connector, it, expect);1314 it('builds and recognises PSA cert URLs', () => {15 expect(certUrl('69211238')).toBe('https://www.psacard.com/cert/69211238');16 expect(certUrl(' 69211238 ')).toBe('https://www.psacard.com/cert/69211238');17 expect(certFromUrl('https://www.psacard.com/cert/69211238/psa')).toBe('69211238');18 expect(certFromUrl('https://www.psacard.com/en-CA/cert/41319734/psa')).toBe('41319734');19 expect(certFromUrl('https://psacard.com/cert/12345678?utm=x')).toBe('12345678');20 expect(certFromUrl('https://www.psacard.com/pop/tcg-cards/1999/pokemon-game/57801')).toBeNull();21 expect(connector.urlPatterns.some((re) => re.test(certUrl('69211238')))).toBe(true);22 expect(connector.certIdentifier('69211238')).toBe('69211238');23 });2425 it('parses the Charizard cert page: item information, grade and population', () => {26 const page = parsePsaPage(snapshotOf('charizard-psa10'));27 expect(page).not.toBeNull();28 expect(page!.title).toBe('1999 POKEMON GAME #4 CHARIZARD-HOLO');29 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' });30 expect(page!.population).toBe(488);31 expect(page!.popHigher).toBe(0);32 expect(page!.populationUrl).toMatch(/psacard\.com\/pop\//);33 expect(page!.estimate).toBeGreaterThan(1000);34 expect(page!.specId).toBe('544027');35 expect(page!.images).toHaveLength(2);36 });3738 it('normalises to a pokemon catalog_item + a partial population_report', async () => {39 const out = await connector.normalize(loadFixture('psa-cert', 'charizard-psa10').raw);40 const item = out.find((r) => r.kind === 'catalog_item');41 const pop = out.find((r) => r.kind === 'population_report');42 expect(item && item.kind === 'catalog_item').toBeTruthy();43 if (item?.kind === 'catalog_item') {44 expect(item.attributes.categorySlug).toBe('pokemon');45 expect(item.attributes).toMatchObject({ name: 'Charizard-Holo', set: 'Pokemon Game', number: '4', year: 1999, franchise: 'Pokémon' });46 expect(item.attributes.identifiers).toEqual({ psa_spec_id: '544027', psa_cert: '69211238' });47 expect(item.grade).toEqual({ grader: 'psa', grade: '10', qualifier: null, certificationNumber: '69211238' });48 expect(item.imageUrls.length).toBe(2);49 expect(item.attributes.metadata.psa_estimate_usd).toBeGreaterThan(0);50 expect(item.confidence).toBeLessThan(1);51 }52 expect(pop && pop.kind === 'population_report').toBeTruthy();53 if (pop?.kind === 'population_report') {54 expect(pop.grader).toBe('psa');55 expect(pop.byGrade).toEqual({ '10': 488, higher: 0 });56 expect(pop.total).toBe(488);57 expect(pop.attributes.metadata.population_scope).toBe('grade_and_higher');58 }59 });6061 it('handles a vintage baseball card with a population above the grade', async () => {62 const out = await connector.normalize(loadFixture('psa-cert', 'jim-palmer-psa8').raw);63 const item = out.find((r) => r.kind === 'catalog_item');64 const pop = out.find((r) => r.kind === 'population_report');65 if (item?.kind === 'catalog_item') {66 expect(item.attributes.categorySlug).toBe('baseball_cards');67 expect(item.grade.grade).toBe('8');68 expect(item.attributes.year).toBe(1967);69 }70 if (pop?.kind === 'population_report') {71 expect(pop.byGrade['8']).toBeGreaterThan(0);72 expect(pop.total).toBe(pop.byGrade['8']! + pop.byGrade.higher!);73 }74 });7576 it('yields zero records for a not-found cert (HTTP 404) without throwing', async () => {77 const fx = loadFixture('psa-cert', 'not-found');78 expect((fx.raw.payload as { status: string }).status).toBe('not_found');79 await expect(connector.normalize(fx.raw)).resolves.toEqual([]);80 expect(connector.classify('<main><h1>Page not found</h1><p>The requested cert could not be found.</p></main>')).toBe('not_found');81 expect(connector.classify('<html><body><div>Just a moment...</div></body></html>')).toBe('unknown');82 expect(parsePsaPage('<main><p>nothing</p></main>')).toBeNull();83 });8485 it('parses PSA grade labels and categories', () => {86 expect(parseCardGrade('GEM MT 10')).toMatchObject({ grade: '10', qualifier: null });87 expect(parseCardGrade('NM-MT 8 (OC)')).toMatchObject({ grade: '8', qualifier: 'OC' });88 expect(parseCardGrade('EX-MT 6 MC')).toMatchObject({ grade: '6', qualifier: 'MC' });89 expect(parseCardGrade('AUTHENTIC')).toMatchObject({ grade: 'authentic' });90 expect(parseCardGrade('Authentic Altered')).toMatchObject({ grade: 'authentic', qualifier: 'Altered' });91 expect(psaCategorySlug('TCG Cards', 'POKEMON GAME').slug).toBe('pokemon');92 expect(psaCategorySlug('Baseball Cards', 'TOPPS').slug).toBe('baseball_cards');93 expect(psaCategorySlug('TCG Cards', 'MAGIC THE GATHERING').slug).toBe('magic_the_gathering');94 expect(psaCategorySlug('Non-Sports Cards', 'GARBAGE PAIL KIDS').slug).toBe('non_sport_cards');95 expect(psaCategorySlug('Tickets', 'WORLD SERIES').confident).toBe(false);96 });97});98