复用一个已有推荐接口:服务端仍只把官方库检索的 top 15 税码候选与本分公司分类预筛 top 20 交给模型,一次调用返回税码/分类/规格;分类与税码都按序号回查,AI 造不出编码。前端只在字段为空且用户未手改时回填,防覆盖防循环;无 LLM 时降级为原规则引擎。
103 lines
3.4 KiB
TypeScript
103 lines
3.4 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const callWithTextMock = vi.fn();
|
|
const hasRuntimeConfigMock = vi.fn();
|
|
const resolvePromptMock = vi.fn();
|
|
const getTaxCatalogEntryByCodeMock = vi.fn();
|
|
|
|
vi.mock('../src/services/llmService', () => ({
|
|
callWithText: (...args: unknown[]) => callWithTextMock(...args),
|
|
hasRuntimeConfig: (...args: unknown[]) => hasRuntimeConfigMock(...args),
|
|
}));
|
|
|
|
vi.mock('../src/services/promptRegistryService', () => ({
|
|
resolvePrompt: (...args: unknown[]) => resolvePromptMock(...args),
|
|
}));
|
|
|
|
vi.mock('../src/services/taxCatalogService', () => ({
|
|
getTaxCatalogEntryByCode: (...args: unknown[]) => getTaxCatalogEntryByCodeMock(...args),
|
|
}));
|
|
|
|
import { suggestTaxCodeWithAi } from '../src/services/taxCodeAiService';
|
|
|
|
const taxCandidates = [
|
|
{
|
|
tax_code: '1010112000000000000',
|
|
name: '蔬菜及食用菌',
|
|
short_name: '蔬菜及食用菌',
|
|
norm: '',
|
|
short_norm: '',
|
|
score: 90,
|
|
},
|
|
];
|
|
|
|
const categories = [
|
|
{ category_code: 'CAT_VEG', category_name: '蔬菜' },
|
|
{ category_code: 'CAT_DRY', category_name: '干货' },
|
|
];
|
|
|
|
function reply(json: Record<string, unknown>) {
|
|
callWithTextMock.mockResolvedValue(JSON.stringify(json));
|
|
}
|
|
|
|
describe('税收编码 AI:一次调用同时给税码 / 分类 / 规格', () => {
|
|
beforeEach(() => {
|
|
callWithTextMock.mockReset();
|
|
hasRuntimeConfigMock.mockReset().mockResolvedValue(true);
|
|
resolvePromptMock.mockReset().mockResolvedValue({ systemPrompt: 'sys' });
|
|
getTaxCatalogEntryByCodeMock.mockReset().mockImplementation((code: string) =>
|
|
code === taxCandidates[0].tax_code ? taxCandidates[0] : null
|
|
);
|
|
});
|
|
|
|
it('税码按候选序号命中,同时返回分类与规格', async () => {
|
|
reply({
|
|
index: 0,
|
|
category_index: 1,
|
|
spec: ' 500g/袋 ',
|
|
confidence: 80,
|
|
rationale: '按名称判断',
|
|
});
|
|
|
|
const result = await suggestTaxCodeWithAi({ skuName: '土豆' }, taxCandidates, { categories });
|
|
|
|
expect(result.matched).toBe(true);
|
|
expect(result.tax_code).toBe('1010112000000000000');
|
|
expect(result.category_code).toBe('CAT_DRY');
|
|
expect(result.category_name).toBe('干货');
|
|
expect(result.suggested_spec).toBe('500g/袋');
|
|
});
|
|
|
|
it('没有税码候选时也要调用 AI,至少能给出分类建议', async () => {
|
|
reply({ index: -1, category_index: 0, spec: '', confidence: 70 });
|
|
|
|
const result = await suggestTaxCodeWithAi({ skuName: '不知名野菜' }, [], { categories });
|
|
|
|
expect(callWithTextMock).toHaveBeenCalledTimes(1);
|
|
expect(result.matched).toBe(false);
|
|
expect(result.tax_code).toBeNull();
|
|
expect(result.category_code).toBe('CAT_VEG');
|
|
expect(result.suggested_spec).toBeNull();
|
|
});
|
|
|
|
it('分类序号越界时不采用(AI 不能造分类)', async () => {
|
|
reply({ index: 0, category_index: 9, spec: '1kg/箱', confidence: 80 });
|
|
|
|
const result = await suggestTaxCodeWithAi({ skuName: '土豆' }, taxCandidates, { categories });
|
|
|
|
expect(result.category_code).toBeNull();
|
|
expect(result.category_name).toBeNull();
|
|
expect(result.suggested_spec).toBe('1kg/箱');
|
|
});
|
|
|
|
it('置信度不足 55 时整体不采用', async () => {
|
|
reply({ index: 0, category_index: 0, spec: '1kg/箱', confidence: 40 });
|
|
|
|
const result = await suggestTaxCodeWithAi({ skuName: '土豆' }, taxCandidates, { categories });
|
|
|
|
expect(result.matched).toBe(false);
|
|
expect(result.category_code).toBeNull();
|
|
expect(result.suggested_spec).toBeNull();
|
|
});
|
|
});
|