Files
SupplierSystem/server/tests/mainSystemPrepLedgerPriority.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

118 lines
4.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { readFileSync } from 'node:fs';
import { beforeEach, describe, expect, it, vi } from 'vitest';
/**
* B2(2026-10-19 T2 终局复攻):行级台账处置**按优先级合并** —— 硬删不被 `reallocated` 降级。
*
* 旧实现 `markPrepDisposition` 是裸 UPDATE:item_removed→removed 后,迟到的 reallocated 事件
* 会把 disposition 改回 reallocated,`needsAppendToOrder` 于是允许追加 ⇒ **已删行被重建**。
* 修复:reallocated 遇到硬删(removed/cancelled/returned/completed/suspended)保持原值;
* removed 仍可覆盖 reallocated(升级)。
*/
const H = vi.hoisted(() => ({ poolQuery: vi.fn() }));
vi.mock('../src/db', () => ({
pool: { query: (...args: unknown[]) => H.poolQuery(...args), connect: vi.fn() },
query: (...args: unknown[]) => H.poolQuery(...args),
getRawPoolQuery: () => H.poolQuery,
}));
import { markPrepDisposition } from '../src/services/mainSystemPrepLedger';
beforeEach(() => {
H.poolQuery.mockReset();
});
describe('B2:markPrepDisposition 优先级(源码/SQL 契约)', () => {
it('UPDATE 含优先级 CASE:reallocated 不降级硬删;removed 可升级', async () => {
H.poolQuery.mockResolvedValue({ rows: [], rowCount: 1 });
await markPrepDisposition({ branchCode: 'BR002', prepId: '11111111-1111-4111-8111-111111111111', disposition: 'reallocated', reason: '改派' });
const sql = String(H.poolQuery.mock.calls[0][0]);
expect(sql).toContain("WHEN $3::text = 'reallocated'");
expect(sql).toContain("IN ('removed', 'cancelled', 'returned', 'completed', 'suspended')");
expect(sql).toContain('ELSE $3');
});
it('服务里不再出现裸 UPDATE disposition = $3', () => {
const source = readFileSync(new URL('../src/services/mainSystemPrepLedger.ts', import.meta.url), 'utf8');
expect(source).not.toMatch(/SET disposition = \$3, disposition_reason = \$4/);
});
});
type PgClient = {
connect: () => Promise<void>;
end: () => Promise<void>;
query: (sql: string, params?: unknown[]) => Promise<{ rows: Array<Record<string, unknown>> }>;
};
async function loadPgClient(): Promise<PgClient | null> {
const { loadEnv } = await import('../src/env');
loadEnv();
if (!process.env.DATABASE_URL) return null;
const pg = (await import('pg')).default as unknown as {
Client: new (cfg: { connectionString: string }) => PgClient;
};
const client = new pg.Client({ connectionString: process.env.DATABASE_URL });
try {
await client.connect();
} catch {
return null;
}
return client;
}
describe('B2:真实状态(事务内回滚,无库时跳过)', () => {
it('removed → 迟到 reallocated 仍是 removed;reallocated → removed 正常升级', async (ctx) => {
const client = await loadPgClient();
if (!client) {
ctx.skip();
return;
}
const PREP = 'dddddddd-0001-4000-8000-000000000001';
try {
await client.query('BEGIN');
await client.query(
`INSERT INTO public.main_system_order_import_items
(branch_code, main_order_code, prep_id, disposition, disposition_reason)
VALUES ('__T2B2__','M_X',$1::uuid,'removed','主系统删除商品行')`,
[PREP]
);
const mark = (disposition: 'reallocated' | 'removed', reason: string) =>
markPrepDisposition({
branchCode: '__T2B2__',
prepId: PREP,
disposition,
reason,
client: { query: (sql: string, params?: unknown[]) => client.query(sql, params) },
});
await mark('reallocated', '迟到的改派事件');
let row = (
await client.query(
`SELECT disposition, disposition_reason FROM public.main_system_order_import_items
WHERE branch_code='__T2B2__' AND prep_id=$1::uuid`,
[PREP]
)
).rows[0];
expect(row.disposition, '硬删不得被 reallocated 降级').toBe('removed');
expect(row.disposition_reason).toBe('主系统删除商品行');
// 反向:先 reallocated,再 removed ⇒ 升级
await client.query(
`UPDATE public.main_system_order_import_items SET disposition='reallocated' WHERE branch_code='__T2B2__'`
);
await mark('removed', '主系统删除商品行');
row = (
await client.query(
`SELECT disposition FROM public.main_system_order_import_items WHERE branch_code='__T2B2__'`
)
).rows[0];
expect(row.disposition).toBe('removed');
} finally {
await client.query('ROLLBACK').catch(() => undefined);
await client.end();
}
});
});