来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
101 lines
3.1 KiB
TypeScript
101 lines
3.1 KiB
TypeScript
import { describe, expect, it, beforeEach } from 'vitest';
|
|
import {
|
|
applySetFields,
|
|
isForbiddenFieldName,
|
|
matchDraftItem,
|
|
registerCopilotPage,
|
|
runPageAction,
|
|
waitForCopilotPath,
|
|
type CopilotPageAdapter,
|
|
} from './copilotPageBus';
|
|
|
|
const items = [
|
|
{ id: 'a', skuName: '土豆', originalName: '土豆' },
|
|
{ id: 'b', skuName: '白菜', originalName: '小白菜' },
|
|
{ id: 'c', skuName: '土豆', originalName: '黄土豆' },
|
|
];
|
|
|
|
describe('matchDraftItem', () => {
|
|
it('matches unique name', () => {
|
|
expect(matchDraftItem(items, { sku_name: '白菜' }).item?.id).toBe('b');
|
|
});
|
|
|
|
it('refuses ambiguous name', () => {
|
|
expect(matchDraftItem(items, { sku_name: '土豆' }).error).toMatch(/多行/);
|
|
});
|
|
|
|
it('matches by id', () => {
|
|
expect(matchDraftItem(items, { item_id: 'c' }).item?.skuName).toBe('土豆');
|
|
});
|
|
});
|
|
|
|
describe('page contract bus', () => {
|
|
beforeEach(() => {
|
|
registerCopilotPage({
|
|
pageId: 'customer-order',
|
|
path: '/customer-order',
|
|
fields: [
|
|
{ name: 'remark', type: 'string' },
|
|
{ name: 'items[].quantity', type: 'number' },
|
|
],
|
|
actions: [{ id: 'request_commit', description: '确认提交', confirm: true }],
|
|
getSnapshot: () => ({
|
|
pageId: 'customer-order',
|
|
path: '/customer-order',
|
|
fields: [],
|
|
actions: [],
|
|
values: {},
|
|
}),
|
|
setFields: () => ({ ok: true }),
|
|
runAction: async (id) =>
|
|
id === 'request_commit' ? { ok: true } : { ok: false, error: 'no' },
|
|
} satisfies CopilotPageAdapter);
|
|
});
|
|
|
|
it('rejects unregistered page after unregister', async () => {
|
|
const unreg = registerCopilotPage({
|
|
pageId: 'x',
|
|
path: '/x',
|
|
fields: [],
|
|
actions: [{ id: 'request_commit', description: '' }],
|
|
getSnapshot: () => ({
|
|
pageId: 'x',
|
|
path: '/x',
|
|
fields: [],
|
|
actions: [],
|
|
values: {},
|
|
}),
|
|
setFields: () => ({ ok: true }),
|
|
runAction: async () => ({ ok: true }),
|
|
});
|
|
unreg();
|
|
const result = await runPageAction('request_commit');
|
|
expect(result.ok).toBe(false);
|
|
expect(result.error).toMatch(/请先打开/);
|
|
});
|
|
|
|
it('rejects price and unit_cost patches', async () => {
|
|
expect(isForbiddenFieldName('items[].price')).toBe(true);
|
|
expect(isForbiddenFieldName('unit_cost')).toBe(true);
|
|
const result = await applySetFields([{ name: 'price', value: 1 }]);
|
|
expect(result.ok).toBe(false);
|
|
expect(result.error).toMatch(/价格或成本/);
|
|
expect(isForbiddenFieldName('items[].sale_price')).toBe(true);
|
|
expect(isForbiddenFieldName('items[].cost_price')).toBe(true);
|
|
});
|
|
|
|
it('allows quantity field declared on page', async () => {
|
|
const result = await applySetFields([{ name: 'items[].quantity', value: 20, sku_name: '白菜' }]);
|
|
expect(result.ok).toBe(true);
|
|
});
|
|
|
|
it('request_commit is an action not a write', async () => {
|
|
const result = await runPageAction('request_commit');
|
|
expect(result.ok).toBe(true);
|
|
});
|
|
|
|
it('waitForCopilotPath resolves when the page adapter is registered', async () => {
|
|
await expect(waitForCopilotPath('/customer-order', 200)).resolves.toBe(true);
|
|
});
|
|
});
|