import { describe, expect, it } from 'vitest'; import type { TenantContext } from '../src/services/tenantScope'; import { executeCapability } from '../src/capabilities/registry'; import { registerBuiltinCapabilities } from '../src/capabilities/registerBuiltins'; import { compactToolResultForModel, SYSTEM_PROMPT } from '../src/mcp/orderEntryAgentHost'; import { executeOrderEntryTool } from '../src/mcp/orderEntryTools'; import { PAGE_GUIDES } from '../src/mcp/pageGuides'; registerBuiltinCapabilities(); const ctx: TenantContext = { userId: 'u1', roles: ['super_admin'], permissions: [], branchCode: 'BR001', supplierCode: null, purchaserCode: null, isSuperAdmin: true, isBranchAdmin: false, isInternalStaff: true, isSupplierAccount: false, }; describe('page guides', () => { it('list_pages attaches brief only for visible guided pages', async () => { const pages = [ { path: '/inventory', label: '仓库管理' }, { path: '/system/config', label: '系统配置' }, { path: '/orders', label: '订单管理' }, ]; const result = await executeCapability('list_pages', { userId: 'u1', ctx, args: {}, surface: 'web', visiblePages: pages, }); expect(result.ok).toBe(true); if (!result.ok) return; const data = result.data as { pages: Array<{ path: string; brief?: string }> }; const inventory = data.pages.find((row) => row.path === '/inventory'); const config = data.pages.find((row) => row.path === '/system/config'); const orders = data.pages.find((row) => row.path === '/orders'); expect(inventory?.brief).toMatch(/多采库存抵扣/); expect(inventory?.brief).toMatch(/不是系统配置/); expect(config?.brief).toMatch(/没有库存抵扣/); expect(config?.brief).toMatch(/允许成本价为 0/); expect(orders?.brief).toMatch(/垃圾桶/); expect(data.pages).toHaveLength(3); }); it('omits inventory brief when warehouse page is not visible', async () => { const result = await executeCapability('list_pages', { userId: 'u1', ctx, args: {}, surface: 'web', visiblePages: [{ path: '/orders', label: '订单管理' }], }); expect(result.ok).toBe(true); if (!result.ok) return; const data = result.data as { pages: Array<{ path: string }> }; expect(data.pages.some((row) => row.path === '/inventory')).toBe(false); expect(data.pages.some((row) => row.path === '/inventory' && Boolean((row as { brief?: string }).brief))).toBe( false ); }); it('describe_page with path returns guide without snapshot', async () => { const result = await executeCapability('describe_page', { userId: 'u1', ctx, args: { path: '/inventory' }, surface: 'web', visiblePages: [{ path: '/inventory', label: '仓库管理' }], }); expect(result.ok).toBe(true); if (!result.ok) return; const data = result.data as { how_to: string; features: string[]; fields: unknown[] }; expect(data.how_to).toMatch(/多采库存抵扣/); expect(data.features.some((row) => /页头/.test(row))).toBe(true); expect(data.fields).toEqual([]); }); it('describe_page rejects path not in visible pages', async () => { await expect( executeOrderEntryTool('u1', ctx, 'describe_page', { path: '/inventory' }, { visiblePages: [{ path: '/orders', label: '订单' }] }) ).rejects.toMatchObject({ code: 'forbidden' }); }); it('does not truncate list_pages briefs in model view', () => { const pages = Object.values(PAGE_GUIDES).map((row) => ({ path: row.path, brief: row.brief, label: row.path, })); const encoded = JSON.stringify(pages); expect(encoded.length).toBeGreaterThan(2000); const view = compactToolResultForModel('list_pages', { ok: true, data: { count: pages.length, pages }, }); const packed = JSON.stringify(view); expect(packed.endsWith('…')).toBe(false); expect(packed).toContain('多采库存抵扣'); expect((view as { pages: unknown[] }).pages).toHaveLength(pages.length); }); it('describe_page for system config lists remaining keys and denies inventory toggle', async () => { const result = await executeCapability('describe_page', { userId: 'u1', ctx, args: { path: '/system/config' }, surface: 'web', visiblePages: [{ path: '/system/config', label: '系统配置' }], }); expect(result.ok).toBe(true); if (!result.ok) return; const data = result.data as { how_to: string; features: string[] }; expect(data.how_to).toMatch(/仓库管理页头/); expect(data.how_to).toMatch(/智能分配/); expect(data.how_to).toMatch(/允许成本价为 0/); expect(data.how_to).toMatch(/超管登录本页不显示/); expect(data.features.some((row) => /无库存抵扣/.test(row))).toBe(true); expect(data.features.some((row) => /允许成本价为 0/.test(row))).toBe(true); }); it('covers every side-bar menu path', () => { const menuPaths = [ '/dashboard', '/customer-order', '/orders', '/orders/bids', '/customers', '/customers/quote-versions', '/customers/alias-library', '/products/categories', '/products/skus', '/products/units', '/suppliers', '/inventory', '/trace', '/purchase', '/purchase/preparations', '/purchase/price-normalize', '/purchase/quality-reports', '/inspection', '/inspection/review', '/inspection/processing', '/delivery', '/finance', '/finance/costs', '/finance/customer-statements', '/finance/supplier-statements', '/ops', '/picking', '/market-monitor', '/system/config', '/system/ai-models', '/system/ai-prompts', '/system/weigh-ota', '/system/branches', '/system/users', '/system/roles', '/system/logs', '/system/notifications', '/system/menu-layout', '/system/print-templates', ]; const missing = menuPaths.filter((path) => !PAGE_GUIDES[path]); expect(missing).toEqual([]); }); it('hides system config guide when that menu is not visible', async () => { await expect( executeOrderEntryTool( 'u1', ctx, 'describe_page', { path: '/system/config' }, { visiblePages: [{ path: '/inventory', label: '仓库管理' }] } ) ).rejects.toMatchObject({ code: 'forbidden' }); }); it('finance overview guide names tabs and three order date fields', () => { const guide = PAGE_GUIDES['/finance']; const blob = `${guide.brief}\n${guide.how_to}\n${guide.features.join('\n')}`; expect(blob).toMatch(/经营总览/); expect(blob).toMatch(/商品毛利/); expect(blob).toMatch(/配送日/); expect(blob).toMatch(/下单日/); expect(blob).toMatch(/入库日/); expect(blob).not.toMatch(/默认下单日/); }); it('customer statements guide names unpaid filter and human-only writes', () => { const guide = PAGE_GUIDES['/finance/customer-statements']; const blob = `${guide.brief}\n${guide.how_to}\n${guide.features.join('\n')}`; expect(blob).toMatch(/近15天/); expect(blob).toMatch(/配送日/); expect(blob).toMatch(/下单日/); expect(blob).toMatch(/入库日/); expect(blob).toMatch(/未结清/); expect(blob).toMatch(/补齐/); expect(blob).not.toMatch(/供应商结算/); }); it('cost management guide lists six tabs and human-only root cause', () => { const guide = PAGE_GUIDES['/finance/costs']; const blob = `${guide.brief}\n${guide.how_to}\n${guide.features.join('\n')}`; expect(blob).toMatch(/总览/); expect(blob).toMatch(/商品成本/); expect(blob).toMatch(/明细/); expect(blob).toMatch(/预警/); expect(blob).toMatch(/固定成本/); expect(blob).toMatch(/开始分析须人点/); expect(blob).toMatch(/配送日/); expect(blob).toMatch(/下单日/); expect(blob).toMatch(/入库日/); expect(blob).toMatch(/允许成本价为 0/); expect(blob).toMatch(/不是「允许成本价为 0」开关页/); }); it('SYSTEM tells model to use brief then describe_page, not dump catalog', () => { expect(SYSTEM_PROMPT).toMatch(/list_pages 看每页 brief/); expect(SYSTEM_PROMPT).toMatch(/禁止凭印象说「没有这个开关」/); expect(SYSTEM_PROMPT).toMatch(/允许成本价为 0/); expect(SYSTEM_PROMPT).toMatch(/describe_page/); expect(SYSTEM_PROMPT).not.toContain('/system/config'); expect(SYSTEM_PROMPT).not.toContain('explain_product_trace'); }); const MASTER_DATA_GUIDE_TERMS: Record = { '/customers': [/月预算/, /导入/], '/products/skus': [/商品直送/, /直送客户/, /供货供应商/, /过手|管库存/, /没有成本价/], '/products/categories': [/需要报告|检验报告/, /生鲜/, /拍照/], '/customers/quote-versions': [/生效报价|报价版本/, /商品库/, /售价/], '/suppliers': [/供应商不入仓/, /不是商品直送/], '/orders/bids': [/开标/, /历史/], '/inventory': [/多采库存抵扣/, /Tab/], '/market-monitor': [/灯/, /Tab/], '/trace': [/入库日/, /公开/], '/purchase/quality-reports': [/供应商/, /分类管理/], '/products/units': [/字典/, /商品管理/], '/customers/alias-library': [/别名/, /商品库/], }; it('covers master-data sidebar pages with distinctive terms', () => { const missing = Object.keys(MASTER_DATA_GUIDE_TERMS).filter((path) => !PAGE_GUIDES[path]); expect(missing).toEqual([]); for (const [path, terms] of Object.entries(MASTER_DATA_GUIDE_TERMS)) { const guide = PAGE_GUIDES[path]; const blob = `${guide.brief}\n${guide.how_to}\n${guide.features.join('\n')}`; for (const term of terms) { expect(blob, `${path} should match ${term}`).toMatch(term); } } }); it('keeps 商品直送 on sku page and 供应商不入仓 on suppliers', () => { const sku = PAGE_GUIDES['/products/skus']; const supplier = PAGE_GUIDES['/suppliers']; const skuText = `${sku.brief}\n${sku.how_to}\n${sku.features.join('\n')}`; const supplierText = `${supplier.brief}\n${supplier.how_to}\n${supplier.features.join('\n')}`; expect(skuText).toMatch(/商品直送/); expect(skuText).toMatch(/不代开直送/); expect(skuText).not.toMatch(/供应商不入仓/); expect(supplierText).toMatch(/供应商不入仓/); expect(supplierText).toMatch(/不是商品直送/); }); });