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 } from './index.js'; import { ccgClassify, ccgParse } from '../_g2-grading-lib/ccg.js'; import { cardCategoryFromText, parseCardGrade } from '../_g2-grading-lib/text.js'; const connector = createConnector(localMeta(meta)); const snapshotOf = (name: string) => (loadFixture('cgc-cards-cert', name).raw.payload as { snapshot: string }).snapshot; describe('cgc-cards-cert', () => { runFixtureSuite(connector, it, expect); it('builds and recognises CGC Cards cert URLs', () => { expect(certUrl('4340250003')).toBe('https://www.cgccards.com/certlookup/4340250003/'); expect(certFromUrl('https://www.cgccards.com/certlookup/4340250003/')).toBe('4340250003'); expect(certFromUrl('https://www.cgccards.com/CERTLOOKUP/4340250003/0_0/')).toBe('4340250003'); expect(certFromUrl('https://www.cgccomics.com/certlookup/1134755001/')).toBeNull(); expect(connector.urlPatterns.some((re) => re.test(certUrl('4340250003')))).toBe(true); }); it('parses the card fields, grade and the "Coming Soon" population placeholder', () => { const page = ccgParse(snapshotOf('lances-charizard')); expect(page.fields).toMatchObject({ 'Cert #': '4340250003', 'Card Name': "Lance's Charizard", Game: 'Pokémon', Year: '2001', Language: 'Japanese', 'Card Number': '097/141', Grade: 'MINT+ 9.5' }); expect(page.atGrade).toBeNull(); expect(page.higher).toBe(0); expect(page.images.length).toBe(2); expect(ccgClassify(snapshotOf('lances-charizard'))).toBe('found'); }); it('normalises to a pokemon catalog_item without a population report', async () => { const out = await connector.normalize(loadFixture('cgc-cards-cert', 'lances-charizard').raw); expect(out.map((r) => r.kind)).toEqual(['catalog_item']); const item = out[0]!; if (item.kind === 'catalog_item') { expect(item.attributes).toMatchObject({ categorySlug: 'pokemon', name: "Lance's Charizard", number: '097', year: 2001, language: 'Japanese', franchise: 'Pokémon' }); expect(item.attributes.identifiers).toEqual({ cgc_cert: '4340250003' }); expect(item.grade).toEqual({ grader: 'cgc', grade: '9.5', qualifier: null, certificationNumber: '4340250003' }); expect(item.attributes.metadata.total_in_set).toBe('141'); expect(item.attributes.metadata.population_note).toMatch(/Coming Soon/); expect(item.imageUrls.length).toBe(2); } }); it('keeps odd test-slab text verbatim (edge case) and still yields a valid record', async () => { const out = await connector.normalize(loadFixture('cgc-cards-cert', 'test-slab-1').raw); expect(out.length).toBe(1); if (out[0]!.kind === 'catalog_item') expect(out[0]!.grade.grade).toBe('10'); }); it('yields zero records for a not-found cert without throwing', async () => { const fx = loadFixture('cgc-cards-cert', 'not-found'); expect(ccgClassify((fx.raw.payload as { snapshot: string }).snapshot)).toBe('not_found'); await expect(connector.normalize(fx.raw)).resolves.toEqual([]); }); it('parses CGC Cards grade labels and categories', () => { expect(parseCardGrade('MINT+ 9.5')).toMatchObject({ grade: '9.5' }); expect(parseCardGrade('PRISTINE 10')).toMatchObject({ grade: '10' }); expect(parseCardGrade('GEM MINT 10')).toMatchObject({ grade: '10' }); expect(cardCategoryFromText('Pokémon Pokémon VS Lance\'s Charizard')).toEqual({ slug: 'pokemon', confident: true }); expect(cardCategoryFromText('Basketball 2019 Panini Prizm Zion Williamson', 'sports_cards')).toEqual({ slug: 'basketball_cards', confident: true }); expect(cardCategoryFromText('2021 Bowman Chrome Prospects')).toEqual({ slug: 'sports_cards', confident: false }); expect(cardCategoryFromText('Unknown Game Card')).toEqual({ slug: 'trading_cards', confident: false }); }); });