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 } from './index.js';6import { ccgClassify, ccgParse } from '../_g2-grading-lib/ccg.js';7import { comicsCategorySlug, parseDate } from '../_g2-grading-lib/text.js';89const connector = createConnector(localMeta(meta));10const snapshotOf = (name: string) => (loadFixture('cgc-cert', name).raw.payload as { snapshot: string }).snapshot;1112describe('cgc-cert', () => {13 runFixtureSuite(connector, it, expect);1415 it('builds and recognises CGC Comics cert URLs', () => {16 expect(certUrl('1134755001')).toBe('https://www.cgccomics.com/certlookup/1134755001/');17 expect(certFromUrl('https://www.cgccomics.com/certlookup/1134755001/')).toBe('1134755001');18 expect(certFromUrl('https://www.cgccomics.com/certlookup/1134755001')).toBe('1134755001');19 expect(certFromUrl('https://www.cgccomics.com/1134755001/')).toBe('1134755001');20 expect(certFromUrl('https://www.cgccomics.com/population-report/')).toBeNull();21 expect(certFromUrl('https://www.cgccards.com/certlookup/4340250003/')).toBeNull();22 expect(connector.urlPatterns.some((re) => re.test(certUrl('1134755001')))).toBe(true);23 });2425 it('parses the CCG dl/dt/dd fields and population stats', () => {26 const page = ccgParse(snapshotOf('action-comics-1'));27 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' });28 expect(page.lines['Grader Notes']!.length).toBeGreaterThan(3);29 expect(page.atGrade).toBe(2);30 expect(page.higher).toBe(0);31 expect(page.popGradeKey).toBe('9.0');32 expect(page.populationUrl).toMatch(/population-report\/comics/);33 expect(ccgClassify(snapshotOf('action-comics-1'))).toBe('found');34 });3536 it('normalises Action Comics #1 to a dc_comics catalog_item + population_report', async () => {37 const out = await connector.normalize(loadFixture('cgc-cert', 'action-comics-1').raw);38 const item = out.find((r) => r.kind === 'catalog_item');39 const pop = out.find((r) => r.kind === 'population_report');40 if (item?.kind === 'catalog_item') {41 expect(item.rawTitle).toBe('Action Comics #1 (DC Comics, 6/38) CGC 9.0');42 expect(item.attributes).toMatchObject({ categorySlug: 'dc_comics', series: 'Action Comics', name: 'Action Comics #1', number: '1', year: 1938, brand: 'DC Comics' });43 expect(item.attributes.identifiers).toEqual({ cgc_cert: '1134755001' });44 expect(item.grade).toEqual({ grader: 'cgc', grade: '9.0', qualifier: null, certificationNumber: '1134755001' });45 expect(item.description).toMatch(/1st appearance of Superman/);46 expect(item.attributes.metadata.page_quality).toBe('WHITE');47 }48 if (pop?.kind === 'population_report') {49 expect(pop.byGrade).toEqual({ '9.0': 2, higher: 0 });50 expect(pop.total).toBe(2);51 expect(pop.sourceUrl).toMatch(/population-report/);52 } else throw new Error('population_report missing');53 });5455 it('parses the second comic (higher-grade population > 0)', async () => {56 const out = await connector.normalize(loadFixture('cgc-cert', 'cert-3727616007').raw);57 const pop = out.find((r) => r.kind === 'population_report');58 const item = out.find((r) => r.kind === 'catalog_item');59 if (item?.kind === 'catalog_item') expect(item.grade.grade).toBe('8.5');60 if (pop?.kind === 'population_report') {61 expect(pop.byGrade.higher).toBeGreaterThan(0);62 expect(pop.total).toBe(pop.byGrade['8.5']! + pop.byGrade.higher!);63 }64 });6566 it('yields zero records for a not-found cert without throwing', async () => {67 const fx = loadFixture('cgc-cert', 'not-found');68 expect(ccgClassify((fx.raw.payload as { snapshot: string }).snapshot)).toBe('not_found');69 await expect(connector.normalize(fx.raw)).resolves.toEqual([]);70 expect(ccgClassify('<html><body>Just a moment...</body></html>')).toBe('unknown');71 });7273 it('maps publishers and cover dates', () => {74 expect(comicsCategorySlug('DC Comics')).toBe('dc_comics');75 expect(comicsCategorySlug('Marvel Comics')).toBe('marvel_comics');76 expect(comicsCategorySlug('Image Comics')).toBe('comics');77 expect(parseDate('6/38')?.toISOString()).toBe('1938-06-01T00:00:00.000Z');78 expect(parseDate('2018-08-15')?.toISOString()).toBe('2018-08-15T00:00:00.000Z');79 });80});81