Files
SupplierSystem/server/tests/mobileBadgeCounts.test.ts
T
Ubuntu 5fa2bbfd55
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
fix(audit-20261019): T1-T9 审计修复、存储归属与审计日志列宽
- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14)
- T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9)
- T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4)
- T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8)
- T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15)
- T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8)
- T7 前后端测试与 lint 门禁清零
- T8 应付卡片按关联订单日(口径 A)
- T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
2026-10-19 16:16:51 +08:00

176 lines
7.9 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
const { query, getSupplierPrepPriceAmendConfig } = vi.hoisted(() => ({
query: vi.fn(),
getSupplierPrepPriceAmendConfig: vi.fn(),
}));
vi.mock('../src/db', () => ({
pool: { query, connect: vi.fn() },
getRawPoolQuery: () => vi.fn(),
}));
vi.mock('../src/services/preparationPriceAmendConfigService', () => ({
getSupplierPrepPriceAmendConfig,
}));
// countPendingInspectionsAll 现在先做「重复自采待验」清理(动态 import 该服务),
// 单测只关心计数 SQL,故把清理动作 mock 成 no-op。
vi.mock('../src/services/inspectionRejectFulfillmentService', () => ({
pruneAllDuplicateSelfPurchasePendingInspections: vi.fn(async () => 0),
}));
import {
countSupplierPendingPreparations,
countSupplierPriceAmendPreparations,
} from '../src/services/supplierPreparationStatusService';
import { countPendingProcessingTasks } from '../src/services/processingTaskService';
import { countPendingPickingOrders } from '../src/services/orderManagementQueryService';
import { countPendingInspectionsAll } from '../src/services/inspectionQueryService';
import { countPendingPurchaserTasks, countPendingBranchPurchaseTasks } from '../src/services/purchaseOrderQueryService';
import { countPendingDriverDeliveryTasks } from '../src/services/deliveryTaskQueryService';
describe('mobile badge count SQL', () => {
beforeEach(() => {
query.mockReset();
getSupplierPrepPriceAmendConfig.mockReset();
getSupplierPrepPriceAmendConfig.mockResolvedValue({ window_hours: 24, enabled: true });
});
it('countSupplierPendingPreparations 对齐 /preparations active 视口径且无 LIMIT', async () => {
query.mockResolvedValueOnce({ rows: [{ c: 4 }] });
const count = await countSupplierPendingPreparations('SUP1');
expect(count).toBe(4);
const [sql, params] = query.mock.calls[0];
const text = String(sql);
expect(text).toContain('FROM supplier_preparations');
expect(text).toContain('sp.supplier_code = $1');
expect(text).toMatch(/sp\.prepare_status\s*<\s*30/);
expect(text).toContain('is_history_repair');
expect(text).toContain('_o_active.deleted_at IS NULL');
expect(text).not.toMatch(/LIMIT/i);
expect(params).toEqual(['SUP1']);
});
it('countSupplierPendingPreparations 空供应商直接返回 0 且不打库', async () => {
expect(await countSupplierPendingPreparations(' ')).toBe(0);
expect(query).not.toHaveBeenCalled();
});
it('countSupplierPriceAmendPreparations 对齐 view=price_amend 且无 LIMIT', async () => {
query.mockResolvedValueOnce({ rows: [{ c: 3 }] });
const count = await countSupplierPriceAmendPreparations('SUP1', 'B1');
expect(count).toBe(3);
expect(getSupplierPrepPriceAmendConfig).toHaveBeenCalledWith('B1');
const [sql, params] = query.mock.calls[0];
const text = String(sql);
expect(text).toContain('FROM supplier_preparations');
expect(text).toMatch(/sp\.prepare_status\s*=\s*30/);
expect(text).toContain('transferred_to_purchase');
expect(text).toContain('is_stockout');
expect(text).toContain("INTERVAL '1 hour'");
expect(text).toContain('is_history_repair');
expect(text).toContain('_o_active.deleted_at IS NULL');
expect(text).not.toMatch(/LIMIT/i);
expect(params).toEqual(['SUP1', 24]);
});
it('countSupplierPriceAmendPreparations window_hours=0 返回 0 且不 COUNT 备货表', async () => {
getSupplierPrepPriceAmendConfig.mockResolvedValueOnce({ window_hours: 0, enabled: false });
expect(await countSupplierPriceAmendPreparations('SUP1', 'B1')).toBe(0);
expect(query).not.toHaveBeenCalled();
});
it('countPendingProcessingTasks 覆盖今天+下单日轴 work/inspect 且含无订单需求单、无 LIMIT', async () => {
query.mockResolvedValueOnce({ rows: [{ c: 9 }] });
const count = await countPendingProcessingTasks();
expect(count).toBe(9);
const text = String(query.mock.calls[0][0]);
expect(text).toContain('FROM processing_tasks');
expect(text).toContain('LEFT JOIN orders o');
expect(text).toMatch(/pt\.process_status\s+IN\s*\(\s*10\s*,\s*20\s*,\s*30\s*\)/);
expect(text).toContain('pt.order_code IS NULL');
expect(text).toContain('_o_active.deleted_at IS NULL');
expect(text).toContain("AT TIME ZONE 'Asia/Shanghai'");
expect(text).toContain('pt.created_at');
expect(text).not.toContain('o.delivery_date');
expect(text).not.toMatch(/LIMIT/i);
});
it('countPendingInspectionsAll 覆盖全部待验且订单未删除、无日期、无 LIMIT', async () => {
query.mockResolvedValueOnce({ rows: [{ c: 2 }] });
const count = await countPendingInspectionsAll();
expect(count).toBe(2);
const text = String(query.mock.calls[0][0]);
expect(text).toContain('FROM inspections');
expect(text).toContain('i.inspection_time IS NULL');
expect(text).toContain('_o_active.deleted_at IS NULL');
expect(text).not.toContain('LEFT JOIN orders');
expect(text).not.toContain('delivery_date');
expect(text).not.toMatch(/LIMIT/i);
expect(text).not.toContain('source_type');
});
it('countPendingPurchaserTasks 覆盖今天+下单日待采、本人或自由池、无 LIMIT', async () => {
query.mockResolvedValueOnce({ rows: [{ c: 4 }] });
const count = await countPendingPurchaserTasks('B1', 'P1');
expect(count).toBe(4);
const [sql, params] = query.mock.calls[0];
const text = String(sql);
expect(text).toContain('FROM purchase_orders');
expect(text).toContain('LEFT JOIN orders o');
expect(text).toMatch(/po\.purchase_status\s+IN\s*\(\s*10\s*,\s*32\s*\)/);
expect(text).toContain('_o_active.deleted_at IS NULL');
expect(text).toContain('o.created_at');
expect(text).toContain("AT TIME ZONE 'Asia/Shanghai'");
expect(text).not.toMatch(/LIMIT/i);
expect(params).toEqual(['B1', 'P1']);
});
it('countPendingBranchPurchaseTasks 覆盖今天+下单日待采、分公司全部、无 LIMIT', async () => {
query.mockResolvedValueOnce({ rows: [{ c: 7 }] });
const count = await countPendingBranchPurchaseTasks('B1');
expect(count).toBe(7);
const [sql, params] = query.mock.calls[0];
const text = String(sql);
expect(text).toContain('FROM purchase_orders');
expect(text).toContain('LEFT JOIN orders o');
expect(text).toMatch(/po\.purchase_status\s+IN\s*\(\s*10\s*,\s*32\s*\)/);
expect(text).toContain('_o_active.deleted_at IS NULL');
expect(text).toContain('o.created_at');
expect(text).not.toContain('purchaser_code = $2');
expect(text).not.toMatch(/LIMIT/i);
expect(params).toEqual(['B1']);
});
it('countPendingPickingOrders 数今天配送日待处理 status<50、排除垃圾桶且无 LIMIT', async () => {
query.mockResolvedValueOnce({ rows: [{ c: 6 }] });
const count = await countPendingPickingOrders();
expect(count).toBe(6);
const text = String(query.mock.calls[0][0]);
expect(text).toContain('FROM orders o');
expect(text).toMatch(/o\.order_status\s*<\s*50/);
expect(text).toContain('o.deleted_at IS NULL');
expect(text).toContain('o.delivery_date::date');
expect(text).toContain("AT TIME ZONE 'Asia/Shanghai'");
expect(text).not.toMatch(/LIMIT/i);
});
it('countPendingDriverDeliveryTasks 数今天配送日未完成且无 LIMIT', async () => {
query.mockResolvedValueOnce({ rows: [{ c: 3 }] });
const count = await countPendingDriverDeliveryTasks({ personCode: 'D1', branchCode: 'B1' });
expect(count).toBe(3);
const [sql, params] = query.mock.calls[0];
const text = String(sql);
expect(text).toContain('FROM delivery_tasks dt');
expect(text).toContain('INNER JOIN orders o');
expect(text).toMatch(/dt\.task_status[\s\S]*<\s*30/);
expect(text).toMatch(/o\.order_status[\s\S]*>=\s*50/);
expect(text).toContain('o.delivery_date');
expect(text).toContain("AT TIME ZONE 'Asia/Shanghai'");
expect(text).not.toMatch(/LIMIT/i);
expect(params[0]).toBe('D1');
expect(params[1]).toBe('B1');
});
});