Files
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库: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 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

132 lines
4.0 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
DATA_AUDIT_BULK_DETAIL_LIMIT,
DATA_AUDIT_BULK_THRESHOLD,
buildDataWritePayload,
diffPatch,
pickRowKey,
} from '../src/services/dataWriteAudit';
describe('dataWriteAudit', () => {
it('diffs only changed fields and skips timestamps', () => {
expect(
diffPatch(
{ sku_name: '鸡', unit: '斤', updated_at: 'old' },
{ sku_name: '土鸡', unit: '斤', updated_at: 'new' }
)
).toEqual([{ field: 'sku_name', before: '鸡', after: '土鸡' }]);
});
it('picks sku_code from eq filter', () => {
expect(
pickRowKey(null, {
table: 'skus',
action: 'update',
filters: [{ op: 'eq', column: 'sku_code', value: 'SKU1' }],
})
).toBe('SKU1');
});
it('inserts over threshold keep first 10 details plus bulk count', () => {
const rows = Array.from({ length: DATA_AUDIT_BULK_THRESHOLD + 1 }, (_, i) => ({
sku_code: `S${i}`,
}));
const payload = buildDataWritePayload({
spec: { table: 'skus', action: 'insert', insertData: rows },
beforeRows: [],
beforeCount: 0,
resultCount: rows.length,
resultData: rows,
});
const inserted = (payload as { inserted?: Array<{ key?: string | null }> }).inserted || [];
expect(payload?.bulk).toBe(true);
expect(payload?.bulk_count).toBe(11);
// 明细只保留前 10 条样本,其余仅计数
expect(inserted).toHaveLength(DATA_AUDIT_BULK_DETAIL_LIMIT);
expect(inserted[0]?.key).toBe('S0');
});
it('updates over threshold keep first 10 details plus bulk count', () => {
// 回归:批量启用商品曾因「一行一个 operation」刷出上千条单行日志。
// 现在同一 patch 命中多行时,只记前 10 条明细 + 命中总数。
const beforeRows = Array.from({ length: DATA_AUDIT_BULK_DETAIL_LIMIT }, (_, i) => ({
code: `SKU${i}`,
name: `商品${i}`,
}));
const payload = buildDataWritePayload({
spec: {
table: 'product_catalog',
action: 'update',
updateData: { status: 1 },
filters: [{ op: 'in', column: 'code', value: beforeRows.map((r) => r.code) }],
},
beforeRows,
beforeCount: 2767,
resultCount: 2767,
resultData: [],
});
expect(payload?.bulk).toBe(true);
expect(payload?.bulk_count).toBe(2767);
expect(payload?.bulk_detail_limit).toBe(DATA_AUDIT_BULK_DETAIL_LIMIT);
expect(payload?.updated).toHaveLength(DATA_AUDIT_BULK_DETAIL_LIMIT);
expect(payload?.updated?.[0]).toEqual({ key: 'SKU0', name: '商品0' });
// 批量形态不再逐行记 changes
expect(payload?.changes).toBeUndefined();
});
it('single insert keeps name and key', () => {
const payload = buildDataWritePayload({
spec: {
table: 'skus',
action: 'insert',
insertData: { sku_code: 'S1', sku_name: '鸡' },
},
beforeRows: [],
beforeCount: 0,
resultCount: 1,
resultData: [{ sku_code: 'S1', sku_name: '鸡' }],
});
expect(payload).toEqual({
kind: 'data_write',
bulk: false,
inserted: [{ key: 'S1', name: '鸡' }],
});
});
it('copies quote version insert fields from insertData without extra keys', () => {
const payload = buildDataWritePayload({
spec: {
table: 'customer_quote_versions',
action: 'insert',
insertData: {
quote_version_code: 'QV1',
customer_code: 'C001',
sku_code: 'SKU001',
unit: '斤',
price: 12.5,
version_no: 2,
source_type: 'order_submit',
},
},
beforeRows: [],
beforeCount: 0,
resultCount: 1,
resultData: [{ sku_code: 'SKU001' }],
});
expect(payload).toEqual({
kind: 'data_write',
bulk: false,
inserted: [
{
key: 'SKU001',
name: null,
customer_code: 'C001',
price: 12.5,
unit: '斤',
source_type: 'order_submit',
},
],
});
});
});