import { describe, expect, it } from 'vitest'; import meta from './meta.json' with { type: 'json' }; import { localMeta } from '../_lib/local-meta.js'; import { listFixtures, loadFixture, runFixtureSuite } from '@rareindex/connectors/testing'; import createConnector from './index.js'; import { magicTreatments, streamGzipDataEntries } from '../_lib/wave4.js'; import { gzipSync } from 'node:zlib'; const connector = createConnector(localMeta(meta)); describe('mtgjson', () => { runFixtureSuite(connector, it, expect); it('emits scryfall-aligned variants and provider-dated observations', async () => { const names = listFixtures('mtgjson'); const fx = loadFixture('mtgjson', names[0]!); const out = await connector.normalize(fx.raw); const cats = out.filter((r) => r.kind === 'catalog_item'); const obs = out.filter((r) => r.kind === 'price_observation'); expect(cats.length).toBeGreaterThanOrEqual(1); for (const c of cats) { if (c.kind !== 'catalog_item') continue; expect(c.attributes.identifiers.mtgjson_uuid).toBeTruthy(); expect(c.attributes.identifiers.scryfall_id).toMatch(/^[0-9a-f-]{36}$/); expect(c.attributes.setCode).toMatch(/^[A-Z0-9]{2,6}$/); } for (const o of obs) { if (o.kind !== 'price_observation') continue; expect(['USD', 'EUR']).toContain(o.currency); expect(o.priceKind).toBe('market'); expect((o.attributes.metadata as { provider: string }).provider).toBeTruthy(); expect(o.observationDate.getTime()).toBeLessThanOrEqual(Date.now()); } }); it('mirrors the scryfall treatment vocabulary', () => { expect(magicTreatments({ frameEffects: ['showcase'], promoTypes: [] })).toEqual(['Showcase']); expect(magicTreatments({ frameEffects: ['extendedart'], borderColor: 'borderless' })).toEqual(['Extended Art', 'Borderless']); expect(magicTreatments({})).toEqual([]); }); it('streams top-level data entries from a gzip JSON document', async () => { const doc = { meta: { date: '2026-09-06' }, data: { a: { paper: { tcgplayer: { retail: { normal: { '2026-09-06': 1.5 } } } } }, b: { paper: {} }, c: { x: [1, 2, { y: '}' }] } } }; const gz = gzipSync(Buffer.from(JSON.stringify(doc))); const orig = globalThis.fetch; globalThis.fetch = (async () => new Response(gz, { status: 200 })) as typeof fetch; try { const seen: string[] = []; const n = await streamGzipDataEntries('https://example.test/x.json.gz', (k) => { seen.push(k); }); expect(n).toBe(3); expect(seen).toEqual(['a', 'b', 'c']); } finally { globalThis.fetch = orig; } }); });