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, { humanizeSlug, parseCataloguePage, parseSalesSitemap } 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 NEXT = {
props: {
pageProps: {
auction: { uuid: 'a-1', sale_no: 'G000002', title: { en: 'Tour Auto 2025 • The Official Sale', fr: 'Tour Auto 2025 • La Vente Officielle' }, start_date: '2025-04-07T14:00:00.000Z', status: 'completed', type: 'live', currency: { code: 'EUR' }, premiums: [{ percent: 25, amount_over: 0 }], branch: { name: 'Aguttes', city: 'Neuilly-sur-seine', country_code: 'FR' } },
auctionLots: {
count: 39,
limit: 24,
page: 1,
results: [
{ uuid: 'l-1', lot_no: '1', status: 'sold', low: '15000.00', high: '25000.00', hammer_price: '20000.00', title: { en: 'Michael Schumacher', fr: 'Michael Schumacher' }, quantity: 1, num_of_bids: 0, primary_image: { data: { lg: { url: 'https://media.app.artisio.co/x_lg.jpeg' } } }, dynamic_fields: { en: { title: 'Michael Schumacher', artist: '', car_brand: 'Ferrari', description: 'Official BELL DOMINATOR helmet, worn during the 1999 pre-season tests
Numbered 9th.' } } },
{ uuid: 'l-2', lot_no: '2', status: 'unsold', low: '1200.00', high: '1800.00', hammer_price: null, title: { en: 'Ferrari F2008', fr: 'Ferrari F2008' }, quantity: 1, dynamic_fields: { en: { description: 'Nose cone.' } } },
{ uuid: 'l-3', lot_no: '35', status: 'sold', low: '100000.00', high: '150000.00', hammer_price: '235000.00', title: { en: 'CARTIER', fr: 'CARTIER' }, dynamic_fields: { en: { description: 'Tank wristwatch, yellow gold, circa 1975.' } } },
],
},
},
},
};
const HTML = `
xy`;
describe('aguttes', () => {
runFixtureSuite(connector, it, expect);
it('fixtures: EUR hammer-price sales with the Aguttes lot identifier', async () => {
let sales = 0;
for (const name of listFixtures('aguttes')) {
const out = await connector.normalize(loadFixture('aguttes', name).raw);
for (const r of out) {
if (!('attributes' in r)) continue;
expect(r.attributes.identifiers.aguttes_lot).toMatch(/^[^/]+\/\S+$/);
expect(r.sourceUrl.startsWith('https://www.aguttes.com/')).toBe(true);
if (r.kind === 'sale') {
sales++;
expect(r.currency).toBe('EUR');
expect(r.buyerPremiumIncluded).toBe(false);
expect(r.auctionHouse).toBe('Aguttes');
expect(r.lotNumber).toBeTruthy();
}
}
}
expect(sales).toBeGreaterThan(5);
});
it('parses the sitemap (newest catalogues first) and slugs', () => {
const xml = `https://www.aguttes.com/catalogue/11442https://www.aguttes.com/catalogue/160425https://www.aguttes.com/catalogue/livres-manuscrits-6339ab29-3d37-453b-aeeb-d33046b92b2f`;
const sales = parseSalesSitemap(xml);
expect(sales.map((s) => s.id)).toEqual(['livres-manuscrits-6339ab29-3d37-453b-aeeb-d33046b92b2f', '160425', '11442']);
expect(sales[0]!.title).toBe('livres manuscrits');
expect(humanizeSlug('livres-manuscrits-6339ab29-3d37-453b-aeeb-d33046b92b2f')).toBe('livres manuscrits');
});
it('parses a catalogue page from __NEXT_DATA__ (hammer, estimates, sold/unsold, pagination, lot URLs)', async () => {
const sale = { id: '160425', title: '160425', url: 'https://www.aguttes.com/catalogue/160425', date: null, location: null, extra: {} };
const p = parseCataloguePage(HTML, sale, 1)!;
expect(p.totalLots).toBe(39);
expect(p.hasMore).toBe(true);
expect(p.sale?.date).toBe('2025-04-07T14:00:00.000Z');
expect(p.sale?.title).toBe('Tour Auto 2025 • The Official Sale');
expect(p.lots).toHaveLength(3);
expect(p.lots[0]).toMatchObject({ lotNo: '1', price: 20000, currency: 'EUR', premiumIncluded: false, estimateLow: 15000, estimateHigh: 25000, sold: true, url: 'https://www.aguttes.com/lot/tour-auto-2025-la-vente-officielle-a-1/michael-schumacher-l-1', image: 'https://media.app.artisio.co/x_lg.jpeg' });
expect(p.lots[0]!.description).toBe('Official BELL DOMINATOR helmet, worn during the 1999 pre-season tests Numbered 9th.');
expect(p.lots[1]).toMatchObject({ lotNo: '2', price: null, sold: false });
expect(p.lots[1]!.url).toContain('#lot-2');
Object.assign(sale, p.sale, { extra: p.sale!.extra });
const out = await connector.normalize({ url: sale.url, kind: 'sale', engine: 'api', fetchedAt: new Date('2026-09-08T00:00:00Z'), payload: { kind: 'sale_results', url: sale.url, sale, page: 1, totalLots: 39, lots: p.lots } });
expect(out.map((r) => r.kind)).toEqual(['sale', 'auction_lot', 'sale']);
const helmet = out[0]!;
if (helmet.kind === 'sale') {
expect(helmet.saleDate.toISOString()).toBe('2025-04-07T14:00:00.000Z');
expect(helmet.attributes.categorySlug).toBe('automotive_memorabilia');
expect(helmet.location).toBe('Neuilly-sur-seine, France');
expect(helmet.attributes.metadata.sale_no).toBe('G000002');
}
const cartier = out[2]!;
expect(attrsOf(cartier).categorySlug).toBe('other_watches');
expect(attrsOf(cartier).brand).toBe('Cartier');
const unsold = out[1]!;
if (unsold.kind === 'auction_lot') expect(unsold.status).toBe('ended');
expect(parseCataloguePage('', sale, 1)).toBeNull();
});
});