SPB Git forge

spb/rareindex

Public
54commits 1branches 0releases
7.1 MBsize
maindefault branch
10 days agolast push
TypeScript 61.9% HTML 37.2% SQL 0.7%
3.1 KB · 49 lines typescript
Raw Blame History
1import { describe, expect, it } from 'vitest';2import { readFileSync } from 'node:fs';3import path from 'node:path';4import { fileURLToPath } from 'node:url';5import { ConnectorMetaSchema } from '@rareindex/connectors';6import { loadFixture, runFixtureSuite } from '@rareindex/connectors/testing';7import createConnector, { parseListingHtml, parseSoldText, trimItem } from './index.js';8import { parseVehicleTitle } from '../../firecrawl/_carlib/index.js';910const dir = path.dirname(fileURLToPath(import.meta.url));11const meta = ConnectorMetaSchema.parse(JSON.parse(readFileSync(path.join(dir, 'meta.json'), 'utf8')));12const connector = createConnector(meta);1314describe('bring-a-trailer', () => {15  runFixtureSuite(connector, it, expect);1617  it('only turns "Sold for" items into sales, with the winning bid, currency and end timestamp', async () => {18    const fx = loadFixture('bring-a-trailer', 'results-page');19    const items = (fx.raw.payload as { items: Array<{ sold_text?: string | null; timestamp_end?: number }> }).items;20    const out = await connector.normalize(fx.raw);21    const soldItems = items.filter((i) => /^Sold for/.test((i.sold_text ?? '').replace(/<[^>]+>/g, '')));22    expect(out.length).toBe(soldItems.length);23    for (const r of out) {24      if (r.kind !== 'sale') throw new Error('expected sale');25      expect(r.buyerPremiumIncluded).toBe(false);26      expect(r.auctionHouse).toBe('Bring a Trailer');27      expect(['automobiles', 'motorcycles']).toContain(r.attributes.categorySlug);28      expect(r.attributes.identifiers.bat_listing_id).toMatch(/^\d+$/);29      expect(r.saleDate.getTime()).toBeLessThanOrEqual(Date.now());30    }31  });3233  it('parses sold text and vehicle titles', () => {34    expect(parseSoldText('Sold for USD $23,250 <span> on 9/6/2026 </span>')).toEqual({ sold: true, date: new Date(Date.UTC(2026, 8, 6)) });35    expect(parseSoldText('Bid to USD $11,250 <span> on 9/6/2026 </span>').sold).toBe(false);36    expect(parseVehicleTitle('2004 GMC Sierra 2500HD SLT Crew Cab 4×4 Duramax')).toMatchObject({ year: 2004, make: 'GMC', model: 'Sierra 2500HD', categorySlug: 'automobiles' });37    expect(parseVehicleTitle('1975 Harley-Davidson FLH Electra Glide')).toMatchObject({ make: 'Harley-Davidson', categorySlug: 'motorcycles' });38    expect(parseVehicleTitle('1967 Land Rover Series IIA 88')).toMatchObject({ make: 'Land Rover', model: 'Series IIA' });39  });4041  it('trims items and parses a listing page', () => {42    expect(trimItem({ id: 1, title: 'x', url: 'u', watch_url: 'noise', current_bid: 5 })).toEqual({ id: 1, title: 'x', url: 'u', current_bid: 5 });43    const html = `<h1 class="post-title listing-post-title">2004 GMC Sierra 2500HD</h1><span>Sold for USD $23,250 on 9/6/26</span><li>Chassis: <a href="#">1GTHK23214F211880</a></li><li>133k Miles</li><span>Lot #261</span>`;44    const p = parseListingHtml(html, 'https://bringatrailer.com/listing/2004-gmc-sierra-2500hd-12/');45    expect(p).toMatchObject({ title: '2004 GMC Sierra 2500HD', vin: '1GTHK23214F211880', mileage: '133k Miles', lotNumber: '261' });46    expect(p.soldLine).toContain('Sold for USD $23,250');47  });48});49