Files
pc_builder/tests/functional/scraped_parts.spec.ts
T
Kevin L'Hereec 62ec796e05
Build & Deploy / build (push) Failing after 4s
Build & Deploy / docker (push) Skipped
fix: repair broken CSP and compare live merchant prices for new parts
CSP fix:
- The previous shield.ts config used '@viteDevUrl'/'@viteHmrUrl' CSP
  keywords that don't exist in this version of @adonisjs/shield (only
  '@viteUrl' is registered by @adonisjs/vite, and it resolves to an empty
  string here since Vite assets are proxied through AdonisJS on the same
  origin). Both placeholders were sent to the browser literally, breaking
  Google Fonts, the Vite HMR websocket, and pc-builder.io/favicon images.
- Replaced with concrete directives: 'self' covers same-origin Vite
  assets, ws://localhost:{VITE_HMR_PORT|24678} is added only in dev for
  the HMR socket, and explicit allowlists for Google Fonts
  (fonts.googleapis.com/fonts.gstatic.com), pc-builder.io product images,
  and Google's favicon service (www.google.com) used by the merchant
  comparison UI

Real purchase links + live price comparison:
- part_scraper_service.ts: ScrapedPart.sourceUrl now stores the actual
  Amazon.fr purchase link scraped from pc-builder.io's category pages
  (https://www.amazon.fr/dp/{ASIN}?tag=... or a search fallback), instead
  of the pc-builder.io product page, which isn't a place anyone can buy
  anything from
- New merchant_price_service.ts: on-demand live price lookup across
  Amazon, LDLC, Grosbill and TopAchat. Of 6 requested merchants,
  Materiel.net (search requires JS/Turbo, no results via plain GET) and
  Cdiscount/PCComponentes (empty body / 403) were dropped as not scrapable
  without a headless browser or paid API
- Matching strategy: pc-builder.io's full product names (e.g. "AMD Ryzen 3
  3200G 3.6 GHz Quad-Core Processor") return zero results on every
  merchant's search engine, so we search on a short brand+series query
  and then pick the specific result whose label contains the extracted
  model token (e.g. "3200G"). No token match means no offer returned,
  rather than risk quoting the price of a different, unrelated product
- Fixed price parsing for LDLC/Grosbill, which render prices as nested
  price/sup elements for whole euros vs cents (and LDLC additionally
  wraps that in a duplicate outer .price div) - naive text concatenation
  was misreading "64,95" as "6495"
- New GET /scraped-parts/compare endpoint (authenticated), called on
  demand per search result rather than for every row, since it makes
  several outbound requests and can take a few seconds
- NewPartSearch.vue: "Compare prices" button per result shows the
  cheapest merchant with its favicon and links to the actual offer;
  importing a part now prefers the live cheapest price/link over
  pc-builder.io's potentially stale catalog price (confirmed stale in
  practice: pc-builder.io listed a Ryzen 3 3200G at 128 euros while
  Amazon sells it for 66.89 euros)
- 3 new functional tests for the compare endpoint (auth required, 404 on
  unknown part, 422 on invalid id)
2026-07-23 14:58:33 +02:00

153 lines
5.1 KiB
TypeScript

import { test } from '@japa/runner'
import { DateTime } from 'luxon'
import User from '#models/user'
import ComponentType from '#models/component_type'
import ScrapedPart from '#models/scraped_part'
import {
isCategoryStale,
ensureFreshCategory,
CACHE_TTL_HOURS,
} from '#services/part_scraper_service'
async function createUser(email: string) {
return User.create({
name: 'Test User',
email,
password: 'secret@123A',
})
}
async function createType(name: string, unique = true) {
return ComponentType.firstOrCreate({ name }, { name, unique })
}
async function createScrapedPart(typeId: number, name: string, sourceRef: string) {
return ScrapedPart.create({
typeId,
name,
price: 199.99,
imageUrl: 'https://fr.pc-builder.io/images/cpu/example.jpg',
source: 'pc-builder.io',
sourceUrl: `https://fr.pc-builder.io/cpu/${sourceRef}`,
sourceRef,
scrapedAt: DateTime.now(),
})
}
test.group('Scraped parts search', () => {
test('an authenticated user can search scraped parts by name', async ({ client, assert }) => {
const user = await createUser('scraped-search-a@example.com')
const type = await createType('Processor-scraped-1')
await createScrapedPart(type.id, 'ZZZ-Test-Ryzen-Unique 5 7600X', 'amd-ryzen-5-7600x-p1311')
await createScrapedPart(type.id, 'ZZZ-Test-Intel-Unique i5-13600K', 'intel-i5-13600k-p9999')
const response = await client.get('/scraped-parts?q=ZZZ-Test-Ryzen-Unique').loginAs(user)
response.assertStatus(200)
const body = response.body()
assert.lengthOf(body, 1)
assert.equal(body[0].name, 'ZZZ-Test-Ryzen-Unique 5 7600X')
assert.equal(body[0].type.id, type.id)
})
test('search can be filtered by typeId', async ({ client, assert }) => {
const user = await createUser('scraped-search-b@example.com')
const cpuType = await createType('Processor-scraped-2')
const gpuType = await createType('Graphic Card-scraped-2')
await createScrapedPart(cpuType.id, 'ZZZ-Test-AMD Ryzen 7 7800X3D', 'amd-ryzen-7-7800x3d-p1')
await createScrapedPart(gpuType.id, 'ZZZ-Test-AMD Radeon RX 7800', 'amd-radeon-rx-7800-p2')
const response = await client
.get(`/scraped-parts?q=ZZZ-Test-AMD&typeId=${gpuType.id}`)
.loginAs(user)
response.assertStatus(200)
const body = response.body()
assert.lengthOf(body, 1)
assert.equal(body[0].name, 'ZZZ-Test-AMD Radeon RX 7800')
})
test('an unauthenticated request is redirected to login', async ({ client }) => {
const response = await client.get('/scraped-parts?q=ryzen')
response.assertRedirectsTo('/auth/login')
})
})
test.group('Scraped parts 24h cache freshness', () => {
test('a category with no cached parts is considered stale', async ({ assert }) => {
const type = await createType('Processor-freshness-1')
assert.isTrue(await isCategoryStale(type))
})
test('a category scraped within the last 24h is not stale', async ({ assert }) => {
const type = await createType('Processor-freshness-2')
await ScrapedPart.create({
typeId: type.id,
name: 'ZZZ-Test-Fresh Part',
price: 100,
imageUrl: null,
source: 'pc-builder.io',
sourceUrl: 'https://fr.pc-builder.io/cpu/fresh-part',
sourceRef: 'fresh-part-ref',
scrapedAt: DateTime.now().minus({ hours: 1 }),
})
assert.isFalse(await isCategoryStale(type))
})
test('a category scraped more than 24h ago is stale', async ({ assert }) => {
const type = await createType('Processor-freshness-3')
await ScrapedPart.create({
typeId: type.id,
name: 'ZZZ-Test-Stale Part',
price: 100,
imageUrl: null,
source: 'pc-builder.io',
sourceUrl: 'https://fr.pc-builder.io/cpu/stale-part',
sourceRef: 'stale-part-ref',
scrapedAt: DateTime.now().minus({ hours: CACHE_TTL_HOURS + 1 }),
})
assert.isTrue(await isCategoryStale(type))
})
test('ensureFreshCategory does not scrape unmapped types (no network call)', async ({
assert,
}) => {
// "Other" is a real, unmapped ComponentType (see CATEGORY_SLUGS): this
// must resolve without making any outbound HTTP request.
const type = await createType('Other-freshness-unmapped', false)
await assert.doesNotReject(() => ensureFreshCategory(type))
})
})
test.group('Scraped parts merchant price comparison', () => {
test('an unauthenticated request is redirected to login', async ({ client }) => {
const response = await client.get('/scraped-parts/compare?id=1')
response.assertRedirectsTo('/auth/login')
})
test('comparing an unknown scraped part returns 404', async ({ client }) => {
const user = await createUser('compare-unknown@example.com')
const response = await client.get('/scraped-parts/compare?id=999999999').loginAs(user)
response.assertStatus(404)
})
test('an invalid id is rejected by validation', async ({ client }) => {
const user = await createUser('compare-invalid@example.com')
const response = await client
.get('/scraped-parts/compare?id=not-a-number')
.header('Accept', 'application/json')
.loginAs(user)
response.assertStatus(422)
})
})