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) { 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(); }); });