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 { comicsCategorySlug, parseDate } from '../_g2-grading-lib/text.js'; const connector = createConnector(localMeta(meta)); const snapshotOf = (name: string) => (loadFixture('cgc-cert', name).raw.payload as { snapshot: string }).snapshot; describe('cgc-cert', () => { runFixtureSuite(connector, it, expect); it('builds and recognises CGC Comics cert URLs', () => { expect(certUrl('1134755001')).toBe('https://www.cgccomics.com/certlookup/1134755001/'); expect(certFromUrl('https://www.cgccomics.com/certlookup/1134755001/')).toBe('1134755001'); expect(certFromUrl('https://www.cgccomics.com/certlookup/1134755001')).toBe('1134755001'); expect(certFromUrl('https://www.cgccomics.com/1134755001/')).toBe('1134755001'); expect(certFromUrl('https://www.cgccomics.com/population-report/')).toBeNull(); expect(certFromUrl('https://www.cgccards.com/certlookup/4340250003/')).toBeNull(); expect(connector.urlPatterns.some((re) => re.test(certUrl('1134755001')))).toBe(true); }); it('parses the CCG dl/dt/dd fields and population stats', () => { const page = ccgParse(snapshotOf('action-comics-1')); expect(page.fields).toMatchObject({ 'CGC Cert #': '1134755001', Title: 'Action Comics', Issue: '1', 'Issue Date': '6/38', 'Issue Year': '1938', Publisher: 'DC Comics', Grade: '9.0', 'Page Quality': 'WHITE', 'Grade Date': '2018-08-15', 'Label Category': 'Universal' }); expect(page.lines['Grader Notes']!.length).toBeGreaterThan(3); expect(page.atGrade).toBe(2); expect(page.higher).toBe(0); expect(page.popGradeKey).toBe('9.0'); expect(page.populationUrl).toMatch(/population-report\/comics/); expect(ccgClassify(snapshotOf('action-comics-1'))).toBe('found'); }); it('normalises Action Comics #1 to a dc_comics catalog_item + population_report', async () => { const out = await connector.normalize(loadFixture('cgc-cert', 'action-comics-1').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('Action Comics #1 (DC Comics, 6/38) CGC 9.0'); expect(item.attributes).toMatchObject({ categorySlug: 'dc_comics', series: 'Action Comics', name: 'Action Comics #1', number: '1', year: 1938, brand: 'DC Comics' }); expect(item.attributes.identifiers).toEqual({ cgc_cert: '1134755001' }); expect(item.grade).toEqual({ grader: 'cgc', grade: '9.0', qualifier: null, certificationNumber: '1134755001' }); expect(item.description).toMatch(/1st appearance of Superman/); expect(item.attributes.metadata.page_quality).toBe('WHITE'); } if (pop?.kind === 'population_report') { expect(pop.byGrade).toEqual({ '9.0': 2, higher: 0 }); expect(pop.total).toBe(2); expect(pop.sourceUrl).toMatch(/population-report/); } else throw new Error('population_report missing'); }); it('parses the second comic (higher-grade population > 0)', async () => { const out = await connector.normalize(loadFixture('cgc-cert', 'cert-3727616007').raw); const pop = out.find((r) => r.kind === 'population_report'); const item = out.find((r) => r.kind === 'catalog_item'); if (item?.kind === 'catalog_item') expect(item.grade.grade).toBe('8.5'); if (pop?.kind === 'population_report') { expect(pop.byGrade.higher).toBeGreaterThan(0); expect(pop.total).toBe(pop.byGrade['8.5']! + pop.byGrade.higher!); } }); it('yields zero records for a not-found cert without throwing', async () => { const fx = loadFixture('cgc-cert', 'not-found'); expect(ccgClassify((fx.raw.payload as { snapshot: string }).snapshot)).toBe('not_found'); await expect(connector.normalize(fx.raw)).resolves.toEqual([]); expect(ccgClassify('Just a moment...')).toBe('unknown'); }); it('maps publishers and cover dates', () => { expect(comicsCategorySlug('DC Comics')).toBe('dc_comics'); expect(comicsCategorySlug('Marvel Comics')).toBe('marvel_comics'); expect(comicsCategorySlug('Image Comics')).toBe('comics'); expect(parseDate('6/38')?.toISOString()).toBe('1938-06-01T00:00:00.000Z'); expect(parseDate('2018-08-15')?.toISOString()).toBe('2018-08-15T00:00:00.000Z'); }); });