import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import test from 'node:test'; import { extractOzonCharacteristics, extractOzonDescription, extractOzonGalleryImageUrls, } from '../lib/ozon-parser.ts'; import { dedupeImageUrl, normaliseImageUrl } from '../lib/product-media.ts'; test('Ozon gallery fixture only returns product-gallery images', async () => { const fixture = await readFile( new URL('../../../packages/reference/ozon1.html', import.meta.url), 'utf8', ); const state = fixture.match( /id="state-webGallery-[^"]+"\s+data-state='([^']+)'/, )?.[1]; assert.ok(state, 'Ozon gallery widget state must exist in the fixture'); const urls = extractOzonGalleryImageUrls( JSON.parse(state), 'https://www.ozon.ru/product/ryukzak-5387005639/', ); assert.equal(urls.length, 11); assert.ok(urls.every((url) => /\/s3\/multimedia-/.test(url))); assert.ok( urls.every( (url) => !/logo_ozon|payments-cdn|recommend|review/i.test(url), ), ); }); test('Ozon short characteristics fixture returns its product parameters', async () => { const fixture = await readFile( new URL('../../../packages/reference/ozon1.html', import.meta.url), 'utf8', ); const state = fixture.match( /id="state-webShortCharacteristics-[^"]+"\s+data-state='([^']+)'/, )?.[1]; assert.ok(state, 'Ozon short characteristics widget state must exist'); const attributes = extractOzonCharacteristics([ { ...JSON.parse(state), __atlasWidgetName: 'webShortCharacteristics-fixture', }, ]); assert.equal(attributes.length, 5); assert.deepEqual( attributes.slice(0, 3).map(({ name, value }) => ({ name, value })), [ { name: '类型', value: '背包' }, { name: '原产国', value: '中国' }, { name: '材料', value: 'Cordura 1000D' }, ], ); }); test('Ozon rich annotation converts to description text without images', () => { const description = extractOzonDescription([ { __atlasWidgetName: 'webDescription-fixture', richAnnotationJson: JSON.stringify([ { type: 'text', content: '耐用的背包' }, { type: 'image', src: 'https://ir.ozone.ru/s3/example.jpg' }, { type: 'text', text: '适合旅行' }, ]), }, ]); assert.equal(description?.value, '耐用的背包\n适合旅行'); }); test('Ozon thumbnail variants normalise to one original URL', () => { const original = 'https://ir.ozone.ru/s3/multimedia-1-9/13597160877.jpg'; const thumbnail = 'https://ir.ozone.ru/s3/multimedia-1-9/wc100/13597160877.jpg?width=100&quality=80'; assert.equal(normaliseImageUrl(thumbnail, original, 'ozon'), original); assert.equal( dedupeImageUrl(thumbnail, 'ozon'), dedupeImageUrl(original, 'ozon'), ); }); test('Alibaba transformed image URLs normalise before de-duplication', () => { const original = 'https://cbu01.alicdn.com/img/ibank/O1CN01.jpg'; const transformed = `${original}_400x400.jpg?width=400`; assert.equal(normaliseImageUrl(transformed, original, '1688'), original); });