import { describe, expect, it } from 'vitest'; import { readFileSync } from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { loadFixture, runFixtureSuite } from '@rareindex/connectors/testing'; import { localMeta } from '../_lib/local-meta.js'; import createConnector, { splitTitle, trimProduct } from './index.js'; const dir = path.dirname(fileURLToPath(import.meta.url)); const meta = localMeta(JSON.parse(readFileSync(path.join(dir, 'meta.json'), 'utf8'))); const connector = createConnector(meta); describe('trainz', () => { runFixtureSuite(connector, it, expect); it('splits Trainz condition suffixes and trims products', () => { expect(splitTitle('Lionel 6-18005 O Gauge 700E Hudson Steam Locomotive LN/Box')).toEqual({ name: 'Lionel 6-18005 O Gauge 700E Hudson Steam Locomotive', conditionCode: 'LN/Box' }); expect(splitTitle('S. Soho & Co. HO BRASS Passenger Coach Car- Unpainted EX/Box').conditionCode).toBe('EX/Box'); expect(splitTitle('Marklin 3000 HO Steam Locomotive').conditionCode).toBeNull(); const p = trimProduct({ id: 1, title: 'X LN/Box', handle: 'x', vendor: 'Lionel', product_type: 'Trains', tags: ['condition:Like New', 'foo', 'scale:O Gauge'], variants: [{ id: 2, sku: 'A', price: '10.00', available: true }], images: [] }); expect(p?.tags).toEqual(['condition:Like New', 'scale:O Gauge']); }); it('emits a catalog item and a dealer listing per product with a normalised condition', async () => { const out = await connector.normalize({ url: 'https://www.trainz.com/collections/lionel-o-postwar-trains/products.json?limit=250&page=1', externalId: 'lionel-o-postwar-trains:1', kind: 'listing', engine: 'api', fetchedAt: new Date('2026-09-07T00:00:00Z'), payload: { kind: 'collection_page', collection: 'lionel-o-postwar-trains', page: 1, products: [{ id: 99, title: 'Lionel 2343 O Gauge Santa Fe F3 AA Diesel Locomotive Set EX/Box', handle: 'lionel-2343', vendor: 'Lionel', product_type: 'Trains', tags: ['condition:Excellent', 'era:Postwar', 'scale:O Gauge'], created_at: '2026-09-01T00:00:00-04:00', updated_at: null, variants: [{ id: 1, sku: 'P123', price: '499.99', compare_at_price: null, available: true }], images: [{ src: 'https://cdn.shopify.com/x.jpg' }] }] }, }); expect(out.map((r) => r.kind)).toEqual(['catalog_item', 'listing']); const l = out[1]!; if (l.kind !== 'listing') throw new Error('listing'); expect(l).toMatchObject({ price: 499.99, currency: 'USD', availability: 'available', seller: 'Trainz' }); expect(l.attributes).toMatchObject({ categorySlug: 'model_trains', brand: 'Lionel', series: 'O Gauge', name: 'Lionel 2343 O Gauge Santa Fe F3 AA Diesel Locomotive Set' }); expect(l.condition).toMatchObject({ condition: 'boxed', conditionRaw: 'Excellent' }); expect(l.attributes.identifiers.trainz_sku).toBe('P123'); }); it('fixture products all carry a vendor and a USD price', async () => { const out = await connector.normalize(loadFixture('trainz', 'collection-page').raw); expect(out.filter((r) => r.kind === 'listing').length).toBeGreaterThan(0); }); });