复用一个已有推荐接口:服务端仍只把官方库检索的 top 15 税码候选与本分公司分类预筛 top 20 交给模型,一次调用返回税码/分类/规格;分类与税码都按序号回查,AI 造不出编码。前端只在字段为空且用户未手改时回填,防覆盖防循环;无 LLM 时降级为原规则引擎。
97 lines
2.9 KiB
TypeScript
97 lines
2.9 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||
|
||
const suggestAiMock = vi.fn();
|
||
const fetchCategoryRowsMock = vi.fn();
|
||
const lookupOfficialMock = vi.fn();
|
||
|
||
vi.mock('../src/services/taxCodeAiService', () => ({
|
||
suggestTaxCodeWithAi: (...args: unknown[]) => suggestAiMock(...args),
|
||
}));
|
||
|
||
vi.mock('../src/services/categoryCatalogService', () => ({
|
||
fetchCategoryRows: (...args: unknown[]) => fetchCategoryRowsMock(...args),
|
||
}));
|
||
|
||
vi.mock('../src/services/providers/taxCodeOfficialAdapter', () => ({
|
||
lookupOfficialTaxCode: (...args: unknown[]) => lookupOfficialMock(...args),
|
||
}));
|
||
|
||
import { resolveTaxCodeConfirmation } from '../src/services/taxCodeLookupService';
|
||
|
||
const categoryRows = [
|
||
{
|
||
category_code: 'CAT_VEG',
|
||
category_name: '蔬菜',
|
||
parent_code: null,
|
||
level: 1,
|
||
sort_order: 1,
|
||
loss_threshold: null,
|
||
is_fresh: true,
|
||
status: 1,
|
||
},
|
||
{
|
||
category_code: 'CAT_DISABLED',
|
||
category_name: '停用分类',
|
||
parent_code: null,
|
||
level: 1,
|
||
sort_order: 2,
|
||
loss_threshold: null,
|
||
is_fresh: false,
|
||
status: 0,
|
||
},
|
||
];
|
||
|
||
beforeEach(() => {
|
||
suggestAiMock.mockReset().mockResolvedValue({
|
||
matched: false,
|
||
tax_code: null,
|
||
tax_name: null,
|
||
short_name: null,
|
||
confidence: 80,
|
||
rationale: 'test',
|
||
category_code: 'CAT_VEG',
|
||
category_name: '蔬菜',
|
||
suggested_spec: '500g/袋',
|
||
});
|
||
fetchCategoryRowsMock.mockReset().mockResolvedValue(categoryRows);
|
||
lookupOfficialMock.mockReset().mockResolvedValue({ matched: false });
|
||
});
|
||
|
||
/**
|
||
* 商品名一输入就要有「分类 + 规格」建议 ⇒ 即使规则引擎已精确命中税码,
|
||
* 只要请求了 need_category / need_spec,也必须真的调一次 AI;
|
||
* 不请求时保持原成本路径(规则命中就不调 AI)。
|
||
*/
|
||
describe('resolveTaxCodeConfirmation:分类/规格建议开关', () => {
|
||
it('请求分类/规格时,规则精确命中也会调 AI 并透传建议', async () => {
|
||
const result = await resolveTaxCodeConfirmation({
|
||
skuName: '蔬菜及食用菌',
|
||
branchCode: 'BR001',
|
||
needCategory: true,
|
||
needSpec: true,
|
||
});
|
||
|
||
expect(result.source).toBe('rule_exact');
|
||
expect(suggestAiMock).toHaveBeenCalledTimes(1);
|
||
const aiOptions = suggestAiMock.mock.calls[0][2] as {
|
||
categories: Array<{ category_code: string }>;
|
||
};
|
||
// 停用分类不参与推荐
|
||
expect(aiOptions.categories).toEqual([{ category_code: 'CAT_VEG', category_name: '蔬菜' }]);
|
||
expect(result.category_code).toBe('CAT_VEG');
|
||
expect(result.suggested_spec).toBe('500g/袋');
|
||
});
|
||
|
||
it('不请求时不额外调 AI,也不回填分类/规格', async () => {
|
||
const result = await resolveTaxCodeConfirmation({
|
||
skuName: '蔬菜及食用菌',
|
||
branchCode: 'BR001',
|
||
});
|
||
|
||
expect(result.source).toBe('rule_exact');
|
||
expect(suggestAiMock).not.toHaveBeenCalled();
|
||
expect(result.category_code).toBeUndefined();
|
||
expect(result.suggested_spec).toBeUndefined();
|
||
});
|
||
});
|