cangku/backend/test/alibaba1688SimilarSizeService.test.js

51 lines
1.7 KiB
JavaScript

'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const {
analyzeAlibaba1688SimilarProductSizes,
buildSizeKey,
normalizeProducts,
} = require('../src/services/alibaba1688SimilarSizeService');
test('same physical dimensions share a stable comparison key', () => {
assert.equal(buildSizeKey(120, 60, 75), '120x75x60');
assert.equal(buildSizeKey(60, 120, 75), '120x75x60');
});
test('similar result dimensions are analyzed once and returned with a temporary cache key', async () => {
const products = [{
itemId: '10001',
skuId: '20001',
title: '实木书桌',
skuTitle: '原木色 120x60x75cm',
industryName: '住宅家具',
}];
const savedRows = [];
const result = await analyzeAlibaba1688SimilarProductSizes(7, 11, { products }, {
verifyPlanItem: async () => true,
ensureSchema: async () => {},
cleanupExpired: async () => {},
readCachedRows: async () => [],
saveRows: async (_planId, _planItemId, rows) => savedRows.push(...rows),
getConfig: async () => ({ enabled: true, apiKey: 'server-only', baseUrl: 'https://example.com' }),
requestChunk: async (_config, _context, rows) => ({
rows: rows.map((row) => ({
...row,
lengthCm: 120,
widthCm: 60,
heightCm: 75,
confidence: 'high',
evidence: 'SKU明确标注120x60x75cm',
})),
usage: { totalTokens: 1 },
}),
});
assert.equal(result.analyzedCount, 1);
assert.equal(result.rows[0].sizeKey, '120x75x60');
assert.equal(result.rows[0].sizeLabel, '120 x 60 x 75 cm');
assert.equal(savedRows.length, 1);
assert.equal(normalizeProducts(products)[0].productId, '10001');
});