88 lines
4.5 KiB
JavaScript
88 lines
4.5 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { amount, publicLink, shopifyProducts, wooProducts, schemaProducts } = require('../server/products.cjs');
|
|
const context = { url: 'https://fixture-store.example/', name: 'Fixture Store', id: 'fixture-store', category: 'general' };
|
|
|
|
test('price parsing preserves free and decimal prices without inventing missing values', () => {
|
|
assert.equal(amount(0), 0);
|
|
assert.equal(amount('১,২৩৪.৫০'), 1234.5);
|
|
assert.equal(amount(1e-7), 1e-7);
|
|
for (const value of [undefined, null, '', 'Call for price', true, -2, Infinity, '1,2', '12USD']) assert.equal(amount(value), null);
|
|
});
|
|
|
|
test('Shopify preserves each variant and its SKU, actual compare price and currency', () => {
|
|
const rows = shopifyProducts({ products: [{ id: 7, title: 'Serum', handle: 'serum', variants: [
|
|
{ id: 71, title: '5ml', sku: 'SERUM-5', price: '220.00', compare_at_price: '270.00' },
|
|
{ id: 72, title: '30ml', sku: 'SERUM-30', price: '0.00', compare_at_price: null }
|
|
] }] }, context, 'BDT');
|
|
assert.equal(rows.length, 2);
|
|
assert.equal(rows[0].rawPrice, 220);
|
|
assert.equal(rows[0].rawRegularPrice, 270);
|
|
assert.equal(rows[0].sku, 'SERUM-5');
|
|
assert.equal(rows[1].rawPrice, 0);
|
|
assert.equal(rows[1].regularPrice, '');
|
|
assert.notEqual(rows[0].id, rows[1].id);
|
|
});
|
|
|
|
test('missing Shopify prices remain unknown, and unspecified currency is not guessed as BDT', () => {
|
|
const row = shopifyProducts({ products: [{ title: 'Unpriced item', variants: [{}] }] }, context)[0];
|
|
assert.equal(row.rawPrice, null);
|
|
assert.equal(row.currency, null);
|
|
assert.equal(row.price, 'দাম পাওয়া যায়নি');
|
|
assert.equal(row.rawRegularPrice, null);
|
|
});
|
|
|
|
test('Woo Store API minor units preserve source prices and reject an unknown scale', () => {
|
|
const input = { id: 8, name: 'Item', prices: { price: '12345', regular_price: '15000', currency_minor_unit: 2, currency_code: 'USD' } };
|
|
const row = wooProducts([input], context)[0];
|
|
assert.equal(row.rawPrice, 123.45);
|
|
assert.equal(row.rawRegularPrice, 150);
|
|
assert.equal(row.currency, 'USD');
|
|
delete input.prices.currency_minor_unit;
|
|
assert.equal(wooProducts([input], context)[0].rawPrice, null);
|
|
});
|
|
|
|
test('schema products keep missing price unknown and do not turn aggregate lowPrice into an exact variant price', () => {
|
|
const rows = schemaProducts({ '@graph': [
|
|
{ '@type': 'Product', name: 'Unknown', sku: 'U' },
|
|
{ '@type': 'Product', name: 'From price', sku: 'F', offers: { '@type': 'AggregateOffer', lowPrice: 100, highPrice: 900, priceCurrency: 'BDT' } },
|
|
{ '@type': 'Product', name: 'Free sample', sku: 'Z', offers: { price: 0, priceCurrency: 'BDT' } }
|
|
] }, context);
|
|
assert.deepEqual(rows.map(r => r.rawPrice), [null, null, 0]);
|
|
assert.ok(rows.every(r => r.rawRegularPrice === null));
|
|
});
|
|
|
|
test('unsafe image/link schemes are excluded and stable variant identifiers repeat', () => {
|
|
for (const value of ['javascript:alert(1)', 'data:text/html,unsafe', 'https://user:pass@example.com/']) assert.equal(publicLink(value, context.url), '');
|
|
assert.equal(publicLink('/image.jpg', context.url), 'https://fixture-store.example/image.jpg');
|
|
const payload = { products: [{ id: 5, title: 'Product', variants: [{ id: 6, price: '1.25' }] }] };
|
|
assert.equal(shopifyProducts(payload, context)[0].id, shopifyProducts(payload, context)[0].id);
|
|
});
|
|
|
|
test('products without platform IDs do not overwrite each other', () => {
|
|
const rows = shopifyProducts({ products: [
|
|
{ title: 'First', variants: [{ price: '10' }] },
|
|
{ title: 'Second', variants: [{ price: '20' }] }
|
|
] }, context);
|
|
assert.notEqual(rows[0].id, rows[1].id);
|
|
});
|
|
|
|
test('a schema strikethrough amount alone is not represented as the selling price', () => {
|
|
const row = schemaProducts({ '@type': 'Product', name: 'Item', offers: {
|
|
priceSpecification: { priceType: 'https://schema.org/StrikethroughPrice', price: 270, priceCurrency: 'BDT' }
|
|
} }, context)[0];
|
|
assert.equal(row.rawPrice, null);
|
|
assert.equal(row.rawRegularPrice, 270);
|
|
});
|
|
|
|
test('Woo variable parent range is not treated as an exact selected variant price', () => {
|
|
const row = wooProducts([{ id: 9, type: 'variable', name: 'Shirt', prices: {
|
|
price: '10000', regular_price: '15000', currency_minor_unit: 2, currency_code: 'BDT',
|
|
price_range: { min_amount: '10000', max_amount: '20000' }
|
|
} }], context)[0];
|
|
assert.equal(row.rawPrice, null);
|
|
assert.equal(row.rawRegularPrice, null);
|
|
assert.equal(row.requiresVariantSelection, true);
|
|
assert.deepEqual(row.priceRange, { minimum: 100, maximum: 200 });
|
|
});
|