无分类不再硬编码映射蔬菜及食用菌(原来会给出 62% 的蔬菜推荐);官方库检索改为商品名/去规格名/分类名分项取最高分合并,避免拼接稀释命中;前端加请求代际守卫,过期响应不写回。税率 0% 属分类生鲜标记口径,未改。
115 lines
3.6 KiB
TypeScript
115 lines
3.6 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, suggestTaxCodeByRules } 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();
|
||
});
|
||
|
||
it('「无分类」不再被当成蔬菜(可乐不能推成蔬菜及食用菌)', () => {
|
||
const result = suggestTaxCodeByRules({ skuName: '可乐', categoryName: '(无分类)' });
|
||
expect(result.matched).toBe(false);
|
||
expect(result.tax_code).toBeNull();
|
||
});
|
||
|
||
it('商品名与分类分开检索:可乐 + 汽水 能找到「碳酸型饮料(汽水)」', async () => {
|
||
const result = await resolveTaxCodeConfirmation({
|
||
skuName: '可乐',
|
||
categoryName: '汽水',
|
||
useAi: false,
|
||
});
|
||
|
||
expect(result.matched).toBe(true);
|
||
expect(result.tax_code).toBe('1030307010000000000');
|
||
expect(result.tax_name).toContain('碳酸型饮料');
|
||
});
|
||
});
|