来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
84 lines
2.9 KiB
TypeScript
84 lines
2.9 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
import { readFileSync } from 'node:fs';
|
|
import path from 'path';
|
|
import { deleteUnitConversion } from '../src/services/unitDictionaryService';
|
|
|
|
describe('unit hard delete service', () => {
|
|
it('物理删除并回传被删行(预置重量键也允许)', async () => {
|
|
const query = vi.fn(async (sql: string, params: unknown[]) => {
|
|
expect(sql).toContain('DELETE FROM unit_conversions');
|
|
expect(params).toEqual(['jin']);
|
|
return {
|
|
rows: [
|
|
{
|
|
unit_code: 'jin',
|
|
unit_name: '斤',
|
|
base_unit: '斤',
|
|
factor_to_base: 1,
|
|
is_weight: true,
|
|
status: 1,
|
|
sort_order: 12,
|
|
},
|
|
],
|
|
};
|
|
});
|
|
|
|
const row = await deleteUnitConversion('jin', { query } as any);
|
|
expect(row.unit_code).toBe('jin');
|
|
expect(row.unit_name).toBe('斤');
|
|
expect(row.is_weight).toBe(true);
|
|
expect(row.factor_to_base).toBe(1);
|
|
expect(query).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('行不存在 → 404 not_found', async () => {
|
|
const query = vi.fn(async () => ({ rows: [] }));
|
|
await expect(deleteUnitConversion('nope', { query } as any)).rejects.toMatchObject({
|
|
status: 404,
|
|
code: 'not_found',
|
|
});
|
|
});
|
|
|
|
it('编码为空 → 400,且不查库', async () => {
|
|
const query = vi.fn();
|
|
await expect(deleteUnitConversion(' ', { query } as any)).rejects.toMatchObject({
|
|
status: 400,
|
|
code: 'bad_request',
|
|
});
|
|
expect(query).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('unit delete route + page contract', () => {
|
|
const route = readFileSync(
|
|
path.join(__dirname, '../src/modules/workflow/routes/unitRoutes.ts'),
|
|
'utf8'
|
|
);
|
|
const api = readFileSync(path.join(__dirname, '../../src/utils/workflowApi.ts'), 'utf8');
|
|
const page = readFileSync(
|
|
path.join(__dirname, '../../src/pages/products/UnitManagement.tsx'),
|
|
'utf8'
|
|
);
|
|
|
|
it('hard=1 走物理删除,默认仍走停用(既有调用方语义不变)', () => {
|
|
expect(route).toContain('deleteUnitConversion');
|
|
expect(route).toContain("req.query?.hard === '1'");
|
|
expect(route).toContain('deactivateUnitConversion');
|
|
expect(route).toContain('deleted: true');
|
|
// 停用分支保留 protected 语义
|
|
expect(route).toContain('protected: true');
|
|
});
|
|
|
|
it('前端物理删除 API 与停用 API 并存,页面按钮调用物理删除', () => {
|
|
expect(api).toContain('export async function purgeUnitApi');
|
|
expect(api).toContain('?hard=1');
|
|
expect(api).toContain('export async function deactivateUnitApi');
|
|
expect(page).toContain('purgeUnitApi');
|
|
expect(page).toContain("t('products.units.delete')");
|
|
expect(page).toContain("t('products.units.confirm.deleteMsg'");
|
|
expect(page).toContain('danger: true');
|
|
// 停用按钮仍在(未把停用改成删除)
|
|
expect(page).toContain('deactivateUnitApi');
|
|
});
|
|
});
|