fix(bridge): 商品链接兜底与强制检索
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { appendMissingProductLinks, collectProductLinks } = require('../src/reply-links');
|
||||
|
||||
const TOOL_RESULTS = [{
|
||||
products: [
|
||||
{ name: 'Triple Rings Ring', url: 'https://www.yehwang.com/triple-rings-ring-gold-color-17590.html', price: '$2.88', stock: 26 },
|
||||
{ name: 'Aurora Rings Ring', url: 'https://www.yehwang.com/aurora-rings-ring-silver-color-color-17549.html', price: '$3.17', stock: 63 },
|
||||
],
|
||||
category_links: [{ name: 'Rings', url: 'https://www.yehwang.com/jewelry/rings' }],
|
||||
}];
|
||||
|
||||
test('reply-links: appends real product/category links as markdown', () => {
|
||||
const r = appendMissingProductLinks({ content: 'Here are some rings.', toolResults: TOOL_RESULTS, lang: 'English' });
|
||||
assert.equal(r.appended, 3);
|
||||
assert.match(r.content, /\[Triple Rings Ring\]\(https:\/\/www\.yehwang\.com\/triple-rings-ring-gold-color-17590\.html\)/);
|
||||
assert.match(r.content, /\[Rings\]\(https:\/\/www\.yehwang\.com\/jewelry\/rings\)/);
|
||||
assert.match(r.content, /Click to view:/);
|
||||
assert.match(r.content, /26 pcs/);
|
||||
});
|
||||
|
||||
test('reply-links: skips links already written by the model', () => {
|
||||
const content = 'See https://www.yehwang.com/triple-rings-ring-gold-color-17590.html for details.';
|
||||
const r = appendMissingProductLinks({ content, toolResults: TOOL_RESULTS, lang: 'English' });
|
||||
assert.equal(r.appended, 2);
|
||||
assert.equal((r.content.match(/triple-rings-ring-gold-color-17590/g) || []).length, 1);
|
||||
});
|
||||
|
||||
test('reply-links: respects the limit', () => {
|
||||
const r = appendMissingProductLinks({ content: 'Rings:', toolResults: TOOL_RESULTS, lang: 'English', limit: 1 });
|
||||
assert.equal(r.appended, 1);
|
||||
assert.doesNotMatch(r.content, /Aurora Rings Ring/);
|
||||
});
|
||||
|
||||
test('reply-links: html mode emits clickable anchors', () => {
|
||||
const r = appendMissingProductLinks({ content: '<p>Rings:</p>', toolResults: TOOL_RESULTS, lang: 'Chinese', html: true });
|
||||
assert.equal(r.appended, 3);
|
||||
assert.match(r.content, /<a href="https:\/\/www\.yehwang\.com\/triple-rings-ring-gold-color-17590\.html"/);
|
||||
assert.match(r.content, /可直接查看:/);
|
||||
assert.doesNotMatch(r.content, /\[Triple Rings Ring\]\(/);
|
||||
});
|
||||
|
||||
test('reply-links: does not backfill when the reply already says there is no match', () => {
|
||||
const r = appendMissingProductLinks({ content: 'No exact matches found.', toolResults: TOOL_RESULTS, lang: 'English' });
|
||||
assert.equal(r.appended, 0);
|
||||
assert.equal(r.content, 'No exact matches found.');
|
||||
});
|
||||
|
||||
test('reply-links: collects only http(s) urls and dedupes', () => {
|
||||
const links = collectProductLinks([
|
||||
{ products: [{ name: 'A', url: 'https://a.example/1' }, { name: 'B', url: 'https://a.example/1' }] },
|
||||
{ category_links: [{ name: 'C', url: 'https://c.example/2' }, { name: 'bad', url: '/relative/3' }] },
|
||||
]);
|
||||
assert.deepEqual(links.map((l) => l.url), ['https://a.example/1', 'https://c.example/2']);
|
||||
});
|
||||
|
||||
test('reply-links: prefers in-stock products over out-of-stock ones', () => {
|
||||
const toolResults = [{ products: [
|
||||
{ name: 'Out', url: 'https://a.example/out', price: '$1.00', stock: 0 },
|
||||
{ name: 'In', url: 'https://a.example/in', price: '$2.00', stock: 26 },
|
||||
{ name: 'Backorder', url: 'https://a.example/bo', price: '$3.00', stock: 0 },
|
||||
] }];
|
||||
const r = appendMissingProductLinks({ content: 'Rings:', toolResults, lang: 'English', limit: 2 });
|
||||
assert.equal(r.appended, 2);
|
||||
assert.match(r.content, /\[In\]\(https:\/\/a\.example\/in\)/);
|
||||
assert.ok(r.content.indexOf('a.example/in') < r.content.indexOf('a.example/out'), 'in-stock link comes first');
|
||||
});
|
||||
Reference in New Issue
Block a user