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, { nkCategory, parseProduct } 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);
const PAGE = `
Condition: SW (MINT/New)
`;
describe('noble-knight', () => {
runFixtureSuite(connector, it, expect);
it('parses product JSON-LD and info lines', () => {
const p = parseProduct(PAGE, 'https://www.nobleknight.com/P/2147354979/Catan')!;
expect(p).toMatchObject({ nkId: '2147354979', name: 'Catan (5th Edition)', mpn: 'CN3071', brand: 'Catan Studio', price: 44.95, currency: 'USD', itemCondition: 'NewCondition', availability: 'InStock', publisher: 'Catan Studio', productLine: 'Catan', category: 'Board Games' });
expect(nkCategory(p)).toBe('board_games');
expect(nkCategory({ ...p, category: 'Miniatures', productLine: 'Warhammer 40,000', publisher: 'Games Workshop' })).toBe('warhammer');
expect(nkCategory({ ...p, category: 'Role Playing Games', productLine: 'D&D' })).toBeNull();
});
it('normalises to a catalog item + listing with a boxed_toys condition', async () => {
const p = parseProduct(PAGE, 'https://www.nobleknight.com/P/2147354979/Catan')!;
const out = await connector.normalize({ url: p.url, externalId: p.nkId, kind: 'listing', engine: 'api', fetchedAt: new Date('2026-09-07T00:00:00Z'), payload: { kind: 'product_page', product: p } });
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: 44.95, currency: 'USD', availability: 'available' });
expect(l.condition.condition).toBe('mint_in_box');
expect(l.attributes.identifiers).toMatchObject({ nobleknight_id: '2147354979', mpn: 'CN3071' });
});
it('fixtures normalise', async () => {
const out = await connector.normalize(loadFixture('noble-knight', 'board-game').raw);
expect(out.length).toBe(2);
});
});