Files
SupplierSystem/server/tests/priceInvertRiskQuery.test.ts
T
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

129 lines
4.2 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { TenantContext } from '../src/services/tenantScope';
import { listPurchaseOrdersForManagement } from '../src/services/purchaseOrderQueryService';
import {
listPriceInvertRisks,
PRICE_INVERT_RISK_LIMIT,
} from '../src/services/priceInvertRiskQueryService';
const { query } = vi.hoisted(() => ({
query: vi.fn(async () => ({ rows: [] })),
}));
vi.mock('../src/db', () => ({
pool: { query, connect: vi.fn() },
getRawPoolQuery: () => vi.fn(),
}));
vi.mock('../src/services/selfPurchaseStockInboundService', () => ({
healOrphanIndependentInboundsAfterPass: vi.fn(async () => undefined),
}));
vi.mock('../src/services/inspectionPhotoService', () => ({
attachCategoryPhotoInspectionRequirements: vi.fn(async (rows: unknown[]) => rows),
}));
vi.mock('../src/services/skuDirectDelivery', async (importOriginal) => {
const actual = await importOriginal<typeof import('../src/services/skuDirectDelivery')>();
return {
...actual,
listSkuDirectDeliveryCustomers: vi.fn(async () => ({})),
};
});
const purchaser: TenantContext = {
userId: '11111111-1111-1111-1111-111111111111',
roles: ['purchaser'],
permissions: ['purchase_manage'],
branchCode: 'BR001',
supplierCode: null,
purchaserCode: 'P001',
isSuperAdmin: false,
isBranchAdmin: false,
isInternalStaff: false,
isSupplierAccount: false,
};
const admin: TenantContext = {
...purchaser,
userId: '22222222-2222-2222-2222-222222222222',
roles: ['admin'],
purchaserCode: null,
isInternalStaff: true,
isBranchAdmin: true,
};
describe('listPriceInvertRisks', () => {
beforeEach(() => {
query.mockClear();
query.mockImplementation(async () => ({ rows: [] }));
});
it('admin SQL unions prep, inverts cost > sale, caps without OFFSET', async () => {
await listPriceInvertRisks(admin, 'BR001', {
dateFrom: '2026-09-19',
dateTo: '2026-09-24',
dateField: 'delivery_date',
});
expect(query).toHaveBeenCalledTimes(1);
const sql = String(query.mock.calls[0][0] || '');
expect(sql).toContain('UNION ALL');
expect(sql).toContain("'self_purchase'");
expect(sql).toContain("'supplier_prep'");
expect(sql).toContain('NULLIF(po.actual_price');
expect(sql).toContain('NULLIF(oi.locked_price');
expect(sql).toMatch(/\) > \(/);
expect(sql).toContain('FROM supplier_preparations sp');
expect(sql).toContain('order_item_fulfillments');
expect(sql).toContain('AS order_code');
expect(sql).toContain(`LIMIT ${PRICE_INVERT_RISK_LIMIT}`);
expect(sql).not.toMatch(/OFFSET\s+\$/);
expect(sql).not.toMatch(/\bOFFSET\s+\d+/);
});
it('purchaser SQL skips prep UNION', async () => {
await listPriceInvertRisks(purchaser, 'BR001', {
dateFrom: '2026-09-19',
dateTo: '2026-09-24',
dateField: 'delivery_date',
});
const sql = String(query.mock.calls[0][0] || '');
expect(sql).not.toContain('UNION ALL');
expect(sql).not.toContain('FROM supplier_preparations sp');
expect(sql).toContain('po.purchaser_code');
expect(sql).toContain(`LIMIT ${PRICE_INVERT_RISK_LIMIT}`);
});
it('empty dates use today-2..+3 window', async () => {
await listPriceInvertRisks(admin, 'BR001', {});
const sql = String(query.mock.calls[0][0] || '');
expect(sql).toContain("::date - 2)");
expect(sql).toContain("::date + 3)");
});
});
describe('listPurchaseOrdersForManagement does not embed invert SQL', () => {
beforeEach(() => {
query.mockClear();
query.mockImplementation(async () => ({ rows: [] }));
});
it('management list UNION has OFFSET and no invert kinds', async () => {
await listPurchaseOrdersForManagement(admin, 'BR001', {
page: 2,
pageSize: 20,
statusGroup: 'all',
dateRangePreset: 'custom',
dateFrom: '2026-09-19',
dateTo: '2026-09-24',
dateField: 'delivery_date',
});
const sqls = query.mock.calls.map((call) => String(call[0] || ''));
const joined = sqls.join('\n');
expect(joined).not.toContain("'self_purchase'");
expect(joined).not.toContain("'supplier_prep'");
expect(joined).not.toContain('invert_rows');
expect(sqls.some((sql) => /OFFSET\s+\$/.test(sql) || /\bOFFSET\s+\d+/.test(sql))).toBe(true);
});
});