import { describe, expect, it } from 'vitest';
import type { NormalizedRecord } from '@rareindex/shared';
import { ConnectorMetaSchema } from '@rareindex/connectors';
import { listFixtures, loadFixture, runFixtureSuite } from '@rareindex/connectors/testing';
import metaJson from './meta.json' with { type: 'json' };
import createConnector, { parseAuDate, parseResultsIndex, parseResultsPage } from './index.js';
const meta = ConnectorMetaSchema.parse(metaJson);
const connector = createConnector(meta);
const attrsOf = (r: NormalizedRecord) => {
if (!('attributes' in r)) throw new Error(`record kind ${r.kind} has no attributes`);
return r.attributes;
};
const INDEX = `
Modern Design
AUCTION
Monday 7 Sep 2026, 10:00am
Timed Online Auction (Melbourne)
Fine Jewels Sydney
BrowseMonday 17 Aug 2026, 6:30pm
Live Auction (Sydney)
`;
const PAGE = `Fine Jewels Sydney
Sale: LJ8818
17 August 2026, 6:30pm 36-40 Queen Street, Woollahra
Lot 201
ROBERT CLERC EGYPTIAN COLLECTION LAPIS LAZULI, EMERALD, QUARTZ AND DIAMOND NECKLACE, CIRCA 1993 Property of a Private New South Wa...
Estimate:
$4,600 - 5,500
Sold for $8,500
Lot 202
ROLEX DATEJUST WRISTWATCH REF 16233
Estimate:
$8,000 - 12,000
`;
describe('leonard-joel', () => {
runFixtureSuite(connector, it, expect);
it('fixtures: AUD "Sold for" results', async () => {
let sales = 0;
for (const name of listFixtures('leonard-joel')) {
for (const r of await connector.normalize(loadFixture('leonard-joel', name).raw)) {
if (!('attributes' in r)) continue;
expect(r.attributes.identifiers.leonard_joel_lot).toMatch(/^[A-Z]{1,3}\d+\/\S+$/);
expect(r.sourceUrl).toMatch(/^https:\/\/auctions\.leonardjoel\.com\.au\//);
if (r.kind === 'sale') {
sales++;
expect(r.currency).toBe('AUD');
expect(r.buyerPremiumIncluded).toBeNull();
expect(r.auctionHouse).toBe('Leonard Joel');
}
}
}
expect(sales).toBeGreaterThan(10);
});
it('parses the results index and a results page (AUD, estimates, pagination)', async () => {
const sales = parseResultsIndex(INDEX);
expect(sales.map((s) => s.id)).toEqual(['IT127', 'LJ8818']);
expect(sales[0]).toMatchObject({ title: 'Modern Design', date: '2026-09-07T00:00:00.000Z', location: 'Melbourne' });
expect(parseAuDate('17 August 2026, 6:30pm')).toBe('2026-08-17T00:00:00.000Z');
const p = parseResultsPage(PAGE, sales[1]!, 1)!;
expect(p.hasMore).toBe(false); // only 2 lots (< page size) even though a page-2 link exists
expect(p.sale).toMatchObject({ title: 'Fine Jewels Sydney', date: '2026-08-17T00:00:00.000Z', location: '36-40 Queen Street, Woollahra' });
expect(p.lots[0]).toMatchObject({ lotNo: '201', price: 8500, currency: 'AUD', premiumIncluded: null, estimateLow: 4600, estimateHigh: 5500, sold: true, image: 'https://auctions.leonardjoel.com.au/Thumb//777/40936777tn.jpg' });
expect(p.lots[0]!.url).toBe('https://auctions.leonardjoel.com.au/asp/fullCatalogue.asp?salelot=LJ8818+++201+&refno=40936777&saletype=');
expect(p.lots[1]).toMatchObject({ lotNo: '202', price: null, sold: false, estimateLow: 8000, estimateHigh: 12000 });
const out = await connector.normalize({ url: sales[1]!.url, kind: 'sale', engine: 'api', fetchedAt: new Date(), payload: { kind: 'sale_results', url: sales[1]!.url, sale: { ...sales[1]!, ...p.sale, extra: sales[1]!.extra }, page: 1, totalLots: null, lots: p.lots } });
expect(out.map((r) => r.kind)).toEqual(['sale', 'auction_lot']);
expect(attrsOf(out[0]!).categorySlug).toBe('jewelry');
expect(attrsOf(out[1]!).categorySlug).toBe('rolex');
expect(parseResultsPage('x', sales[1]!, 1)).toBeNull();
});
});