来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
235 lines
8.1 KiB
TypeScript
235 lines
8.1 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import type { TenantContext } from '../src/services/tenantScope';
|
|
|
|
vi.mock('../src/db', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('../src/db')>();
|
|
return {
|
|
...actual,
|
|
pool: { query: vi.fn() },
|
|
};
|
|
});
|
|
|
|
vi.mock('../src/services/orderManageItemsService', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('../src/services/orderManageItemsService')>();
|
|
return {
|
|
...actual,
|
|
syncOrderManageItems: vi.fn(async () => ({ total_amount: 20, total_weight: 0, items: [] })),
|
|
};
|
|
});
|
|
|
|
vi.mock('../src/services/orderWorkflowService', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('../src/services/orderWorkflowService')>();
|
|
return {
|
|
...actual,
|
|
assertOrderCodeAccess: vi.fn(async () => ({ order_code: 'ORD1' })),
|
|
};
|
|
});
|
|
|
|
import { pool } from '../src/db';
|
|
import { syncOrderManageItems } from '../src/services/orderManageItemsService';
|
|
import { executeCapability, listCapabilityToolSchemas } from '../src/capabilities/registry';
|
|
import { registerBuiltinCapabilities } from '../src/capabilities/registerBuiltins';
|
|
import {
|
|
applyOrderEditHints,
|
|
pendingWriteArgsFromPreview,
|
|
resolveConfirmWriteArgs,
|
|
} from '../src/mcp/tools/orders';
|
|
import { runOrderEntryAgentConfirm } from '../src/mcp/orderEntryAgentHost';
|
|
import { resetAssistantSessionsForTests, writeAssistantSession } from '../src/services/assistantSessionStore';
|
|
|
|
registerBuiltinCapabilities();
|
|
|
|
const superAdmin: TenantContext = {
|
|
userId: 'u1',
|
|
roles: ['super_admin'],
|
|
permissions: [],
|
|
branchCode: 'BR001',
|
|
supplierCode: null,
|
|
purchaserCode: null,
|
|
isSuperAdmin: true,
|
|
isBranchAdmin: false,
|
|
isInternalStaff: true,
|
|
isSupplierAccount: false,
|
|
};
|
|
|
|
const clerk: TenantContext = {
|
|
...superAdmin,
|
|
userId: 'c1',
|
|
roles: ['staff'],
|
|
permissions: ['order_manage'],
|
|
isSuperAdmin: false,
|
|
};
|
|
|
|
describe('prepare_order_edit hints', () => {
|
|
it('does not apply quantity to every row when names collide', () => {
|
|
const rows = applyOrderEditHints(
|
|
[
|
|
{ id: 'a', item_name: '土豆', quantity: 1, sale_price: 2 },
|
|
{ id: 'b', item_name: '土豆', quantity: 3, sale_price: 4 },
|
|
],
|
|
{ sku_name: '土豆', quantity: 10 }
|
|
);
|
|
expect(rows.map((row) => row.quantity)).toEqual([1, 3]);
|
|
expect(rows.every((row) => row.highlighted)).toBe(true);
|
|
});
|
|
|
|
it('fills a unique matching row', () => {
|
|
const rows = applyOrderEditHints(
|
|
[
|
|
{ id: 'a', item_name: '土豆', quantity: 1, sale_price: 2 },
|
|
{ id: 'b', item_name: '白菜', quantity: 3, sale_price: 4 },
|
|
],
|
|
{ sku_name: '土豆', quantity: 10, sale_price: 8 }
|
|
);
|
|
expect(rows[0]).toMatchObject({ quantity: 10, sale_price: 8, highlighted: true });
|
|
expect(rows[1].quantity).toBe(3);
|
|
expect(rows[1].highlighted).toBeFalsy();
|
|
});
|
|
});
|
|
|
|
describe('confirm draft merge', () => {
|
|
it('locks order_code and accepts human items', () => {
|
|
const next = resolveConfirmWriteArgs(
|
|
'prepare_order_edit',
|
|
{ order_code: 'ORD1', items: [{ id: 'a', item_name: '土豆', quantity: 1 }] },
|
|
{ items: [{ id: 'a', item_name: '土豆', quantity: 9, sale_price: 3 }] }
|
|
);
|
|
expect(next.order_code).toBe('ORD1');
|
|
expect((next.items as Array<{ quantity: number }>)[0].quantity).toBe(9);
|
|
});
|
|
|
|
it('rejects mismatched order_code', () => {
|
|
expect(() =>
|
|
resolveConfirmWriteArgs(
|
|
'prepare_order_edit',
|
|
{ order_code: 'ORD1', items: [{ id: 'a', item_name: '土豆' }] },
|
|
{ order_code: 'ORD2', items: [{ id: 'a', item_name: '土豆' }] }
|
|
)
|
|
).toThrow(/不一致/);
|
|
});
|
|
|
|
it('does not merge items for other capabilities', () => {
|
|
const next = resolveConfirmWriteArgs('complete_preparation', { ids: ['p1'] }, {
|
|
items: [{ id: 'hack' }],
|
|
});
|
|
expect(next).toEqual({ ids: ['p1'] });
|
|
});
|
|
|
|
it('stores draft items on pending from preview', () => {
|
|
const pending = pendingWriteArgsFromPreview('prepare_order_edit', { order_code: 'ORD1' }, {
|
|
action_type: 'update',
|
|
capability: 'prepare_order_edit',
|
|
target_type: 'order',
|
|
target_id: 'ORD1',
|
|
summary: 'x',
|
|
diff: [],
|
|
draft: {
|
|
kind: 'order_items',
|
|
order_code: 'ORD1',
|
|
rows: [{ id: 'a', item_name: '土豆', quantity: 2, sale_price: 5 }],
|
|
},
|
|
});
|
|
expect(pending.order_code).toBe('ORD1');
|
|
expect((pending.items as Array<{ price?: number }>)[0].price).toBe(5);
|
|
});
|
|
});
|
|
|
|
describe('prepare_order_edit capability', () => {
|
|
beforeEach(() => {
|
|
resetAssistantSessionsForTests();
|
|
vi.mocked(pool.query).mockReset();
|
|
vi.mocked(syncOrderManageItems).mockClear();
|
|
});
|
|
|
|
it('is registered on order-management and not for order_manage-only clerk', () => {
|
|
const adminTools = listCapabilityToolSchemas({
|
|
ctx: superAdmin,
|
|
surface: 'web',
|
|
modules: ['order-management'],
|
|
}).map((row) => row.name);
|
|
expect(adminTools).toContain('prepare_order_edit');
|
|
const schema = listCapabilityToolSchemas({
|
|
ctx: superAdmin,
|
|
surface: 'web',
|
|
modules: ['order-management'],
|
|
}).find((row) => row.name === 'prepare_order_edit');
|
|
expect(JSON.stringify(schema?.inputSchema)).not.toMatch(/confirm/);
|
|
expect(schema?.description).toMatch(/不要用于新建商品档案/);
|
|
expect(schema?.description).toMatch(/派供应商/);
|
|
const clerkTools = listCapabilityToolSchemas({
|
|
ctx: clerk,
|
|
surface: 'web',
|
|
modules: ['order-management'],
|
|
}).map((row) => row.name);
|
|
expect(clerkTools).not.toContain('prepare_order_edit');
|
|
expect(clerkTools).toContain('get_order_detail');
|
|
|
|
const entryOnly: TenantContext = {
|
|
...clerk,
|
|
userId: 'oe1',
|
|
permissions: ['order_entry'],
|
|
};
|
|
const entryTools = listCapabilityToolSchemas({
|
|
ctx: entryOnly,
|
|
surface: 'web',
|
|
modules: ['order-management'],
|
|
}).map((row) => row.name);
|
|
expect(entryTools).not.toContain('get_order_detail');
|
|
expect(entryTools).not.toContain('prepare_order_edit');
|
|
});
|
|
|
|
it('returns editable draft without writing', async () => {
|
|
vi.mocked(pool.query)
|
|
.mockResolvedValueOnce({ rows: [{ order_code: 'ORD1', customer_name: '食堂' }] } as any)
|
|
.mockResolvedValueOnce({
|
|
rows: [
|
|
{ id: 'a', sku_code: 'S1', item_name: '土豆', quantity: 2, unit: '斤', sale_price: 5, category_code: 'veg' },
|
|
],
|
|
} as any);
|
|
const result = await executeCapability('prepare_order_edit', {
|
|
userId: 'u1',
|
|
ctx: superAdmin,
|
|
args: { order_code: 'ORD1' },
|
|
surface: 'web',
|
|
});
|
|
expect(result.ok).toBe(false);
|
|
if (!result.ok) {
|
|
expect(result.code).toBe('need_confirm');
|
|
expect(result.preview?.draft?.kind).toBe('order_items');
|
|
expect(result.preview?.draft?.rows?.[0]).toMatchObject({ item_name: '土豆', sale_price: 5 });
|
|
expect(JSON.stringify(result.preview)).not.toMatch(/cost_price|unit_cost/);
|
|
}
|
|
expect(syncOrderManageItems).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('saves via syncOrderManageItems on confirm with human items', async () => {
|
|
await writeAssistantSession('u1', 's1', {
|
|
lastModule: 'order-management',
|
|
lastUser: '改 ORD1',
|
|
lastReply: '请确认',
|
|
traces: [],
|
|
pendingWrite: {
|
|
capability: 'prepare_order_edit',
|
|
args: { order_code: 'ORD1', items: [{ id: 'a', item_name: '土豆', quantity: 2, price: 5 }] },
|
|
},
|
|
});
|
|
const result = await runOrderEntryAgentConfirm({
|
|
userId: 'u1',
|
|
ctx: superAdmin,
|
|
sessionId: 's1',
|
|
confirmedActionId: 'prepare_order_edit',
|
|
client: 'web',
|
|
draft: { items: [{ id: 'a', item_name: '土豆', quantity: 9, sale_price: 8 }] },
|
|
});
|
|
expect(result.provider).toBe('confirm');
|
|
expect(syncOrderManageItems).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
orderCode: 'ORD1',
|
|
items: expect.arrayContaining([expect.objectContaining({ quantity: 9, price: 8 })]),
|
|
})
|
|
);
|
|
const saved = vi.mocked(syncOrderManageItems).mock.calls[0][0];
|
|
expect(JSON.stringify(saved.items)).not.toMatch(/cost_price|unit_cost/);
|
|
});
|
|
});
|