UNPKG

focus-product-extractor2

Version:

Extract product information from chat/order data

220 lines (217 loc) 6.2 kB
"use strict"; var _index = _interopRequireDefault(require("../index.js")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } // 测试用例数据(来自test.js文件) const testCases = [{ name: "进线商品 - spuId", data: { "scene": "chat", "platform": "shopee", "shopId": "276033032", "rawData": { "messages": [{ "sender": "system", "timestamp": 1630000000, "message": { "buyer_enter_msg": { "from": 1, "order_id": "", "sku_id": "", "spu_id": "22261730607" }, "order_id": "", "sku_id": "", "spu_id": "22261730607", "sub_type": 6, "type": 1 } }] } }, expected: { type: "spu", goodsId: "22261730607" } }, { name: "商品卡片 - spuId", data: { "scene": "chat", "platform": "shopee", "shopId": "276033032", "rawData": { "messages": [{ "sender": "system", "timestamp": 1630000000, "message": { "card_msg": { "data": [{ "wares": { "list": [{ "currency": -1, "desc": "", "image_url": "", "num": 0, "price": 0, "title": "(โค้ดร้านลด 250.-) Amazfit T-Rex 3 48mm GPS นำทาง ดาวเทียม 6 ดวง กันน้ำ 10ATM ใช้ได้อุณหภูมิ -30 องศา", "ware_url": "" }], "type": 4 } }] }, "order_id": "", "sku_id": "", "spu_id": "28308051952", "sub_type": 4, "type": 1 } }] } }, expected: { type: "spu", goodsId: "28308051952" } }, { name: "订单卡片 - orderId", data: { "scene": "chat", "platform": "shopee", "shopId": "276033032", "rawData": { "messages": [{ "sender": "system", "timestamp": 1630000000, "message": { "card_msg": { "data": [{ "orders": { "currency": -1, "desc": "[ยืนยันคำสั่งซื้อ]", "list": [], "order_id": "1018285932214140", "title": "คำสั่งซื้อ #1018285932214140", "total_price": "0", "type": 5 } }] }, "order_id": "1018285932214140", "sku_id": "", "spu_id": "", "sub_type": 4, "type": 1 } }] } }, expected: { type: "order", goodsId: "1018285932214140" } }, { name: "进线订单 - orderId + spuId", data: { "scene": "chat", "platform": "shopee", "shopId": "276033032", "rawData": { "messages": [{ "sender": "system", "timestamp": 1630000000, "message": { "buyer_enter_msg": { "from": 2, "order_id": "202879018217031", "sku_id": "", "spu_id": "29951359055" }, "order_id": "202879018217031", "sku_id": "", "spu_id": "29951359055", "sub_type": 6, "type": 1 } }] } }, expected: { type: "order", goodsId: "202879018217031" } }, { name: "进线订单多个sku - 逗号分隔的spuId", data: { "scene": "chat", "platform": "shopee", "shopId": "276033032", "rawData": { "messages": [{ "sender": "system", "timestamp": 1630000000, "message": { "buyer_enter_msg": { "from": 2, "order_id": "202494874259650", "sku_id": "", "spu_id": "27658923747,27528392594" }, "order_id": "202494874259650", "sku_id": "", "spu_id": "27658923747,27528392594", "sub_type": 6, "type": 1 } }] } }, expected: { multipleResults: true, type: "order", goodsIds: ["202494874259650", "27658923747", "27528392594"] } }]; // 执行测试 console.log("开始测试真实消息数据的type字段识别...\n"); for (let i = 0; i < testCases.length; i++) { const testCase = testCases[i]; console.log(`测试 ${i + 1}: ${testCase.name}`); try { const result = await _index.default.process(testCase.data); if (result.items && result.items.length > 0) { if (testCase.expected.multipleResults) { // 测试多个结果的情况 console.log(`✅ 提取到 ${result.items.length} 个结果:`); result.items.forEach((item, index) => { console.log(` ${index + 1}. type: ${item.type}, goodsId: ${item.goodsId}`); }); // 验证类型是否正确 const allTypesCorrect = result.items.every(item => item.type === testCase.expected.type); if (allTypesCorrect) { console.log(`✅ 所有结果的type都正确: ${testCase.expected.type}`); } else { console.log(`❌ type不正确,期望: ${testCase.expected.type}`); } } else { // 测试单个结果的情况 const actualType = result.items[0].type; const actualGoodsId = result.items[0].goodsId; const expectedType = testCase.expected.type; const expectedGoodsId = testCase.expected.goodsId; if (actualType === expectedType && actualGoodsId === expectedGoodsId) { console.log(`✅ 通过 - type: ${actualType}, goodsId: ${actualGoodsId}`); } else { console.log(`❌ 失败 - 期望: type=${expectedType}, goodsId=${expectedGoodsId}`); console.log(` 实际: type=${actualType}, goodsId=${actualGoodsId}`); } } } else { console.log(`❌ 失败 - 没有提取到结果`); console.log(` 完整结果: ${JSON.stringify(result, null, 2)}`); } } catch (error) { console.log(`❌ 错误 - ${error.message}`); } console.log(""); } console.log("测试完成!");