40 lines
1.9 KiB
JavaScript
40 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { translateProductQuery, extractProductKeyword } = require('../src/product-query');
|
|
|
|
test('translateProductQuery: Chinese category alias -> English title', () => {
|
|
const entries = [
|
|
{ title: 'Rings', category: 'jewelry/rings', url: 'https://www.yehwang.com/jewelry/rings', aliases: ['戒指', 'ringen'] },
|
|
{ title: 'Earrings', category: 'jewelry/earrings', url: 'https://www.yehwang.com/jewelry/earrings', aliases: ['耳环'] },
|
|
];
|
|
const out = translateProductQuery('简约不锈钢戒指', entries);
|
|
assert.equal(out.translated, true);
|
|
assert.equal(out.query, 'Rings');
|
|
assert.equal(out.category.url, 'https://www.yehwang.com/jewelry/rings');
|
|
});
|
|
|
|
test('translateProductQuery: English query unchanged', () => {
|
|
const out = translateProductQuery('hoop earrings', []);
|
|
assert.equal(out.translated, false);
|
|
assert.equal(out.query, 'hoop earrings');
|
|
});
|
|
|
|
test('extractProductKeyword: drops quantities/currency/transaction words', () => {
|
|
assert.equal(extractProductKeyword('We need 500 stainless steel rings wholesale under 10 dollars'), 'stainless steel rings');
|
|
assert.equal(extractProductKeyword('500 rings'), 'rings');
|
|
assert.equal(extractProductKeyword('do you have gold hoop earrings?'), 'gold hoop earrings');
|
|
assert.equal(extractProductKeyword('do you accept returns on stainless steel rings'), 'stainless steel rings');
|
|
});
|
|
|
|
test('extractProductKeyword: caps at 4 tokens, keeps CJK', () => {
|
|
assert.equal(extractProductKeyword('neo stainless steel rings 500 pcs'), 'neo stainless steel rings');
|
|
assert.equal(extractProductKeyword('简约不锈钢戒指'), '简约不锈钢戒指');
|
|
});
|
|
|
|
test('extractProductKeyword: pure quantity/transaction returns empty', () => {
|
|
assert.equal(extractProductKeyword('500 pcs wholesale'), '');
|
|
assert.equal(extractProductKeyword(''), '');
|
|
});
|