import { describe, expect, it } from 'vitest'; import metaJson from './meta.json' with { type: 'json' }; import { loadFixture, runFixtureSuite } from '@rareindex/connectors/testing'; import { localMeta } from '../_lib/local-meta.js'; import createConnector, { makeDeref, pageUrl, parseBrowseHtml } from './index.js'; const connector = createConnector(localMeta(metaJson)); describe('1stdibs', () => { runFixtureSuite(connector, it, expect); it('parses Item records out of the serverVars_data Relay store', () => { const html = String((loadFixture('1stdibs', 'seating-chairs-p1').raw.payload as { snapshot?: string }).snapshot ?? ''); const p = parseBrowseHtml(html); expect(p).not.toBeNull(); expect(p!.items.length).toBeGreaterThanOrEqual(2); expect(p!.totalResults).toBeGreaterThan(1000); expect(p!.maxPages).toBe(50); const it0 = p!.items[0]!; expect(it0.serviceId).toMatch(/^f_\d+$/); expect(it0.url).toMatch(/^https:\/\/www\.1stdibs\.com\/furniture\/.+\/id-f_\d+\/$/); expect(it0.prices.USD).toBeGreaterThan(0); expect(it0.prices.EUR).toBeGreaterThan(0); expect(it0.seller.serviceId).toMatch(/^f_\d+$/); expect(it0.images[0]).toMatch(/1stdibscdn\.com/); expect(it0.categoryCode).toMatch(/^F_/); expect(pageUrl('/furniture/seating/', 2)).toBe('https://www.1stdibs.com/furniture/seating/?page=2'); }); it('dereferences __ref / __refs chains', () => { const store = { a: { __typename: 'A', b: { __ref: 'b' }, list: { __refs: ['c', 'c'] } }, b: { __typename: 'B', v: 1 }, c: { __typename: 'C', v: 2 } }; const deref = makeDeref(store as never); expect(deref(store.a)).toEqual({ __typename: 'A', b: { __typename: 'B', v: 1 }, list: [{ __typename: 'C', v: 2 }, { __typename: 'C', v: 2 }] }); expect(parseBrowseHtml('no store')).toBeNull(); }); it('normalises items into USD dealer listings with designer, seller and identifiers', async () => { const fx = loadFixture('1stdibs', 'seating-chairs-p1'); const out = await connector.normalize(fx.raw); expect(out.length).toBeGreaterThan(0); for (const r of out) { if (r.kind !== 'listing') throw new Error('expected listing'); expect(r.currency).toBe('USD'); expect(r.price).toBeGreaterThan(0); expect(r.attributes.identifiers.firstdibs_item_id).toMatch(/^f_\d+$/); expect(['design_furniture', 'antiques']).toContain(r.attributes.categorySlug); expect(r.seller).toBeTruthy(); } const jw = loadFixture('1stdibs', 'wrist-watches-p1'); const jwOut = await connector.normalize(jw.raw); expect(jwOut.length).toBeGreaterThan(0); for (const r of jwOut) expect(['rolex', 'omega', 'patek_philippe', 'audemars_piguet', 'other_watches']).toContain((r as { attributes: { categorySlug: string } }).attributes.categorySlug); }); });