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

108 lines
4.9 KiB
TypeScript
Raw Permalink 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 path from 'node:path';
import { afterEach, describe, expect, it, vi } from 'vitest';
const query = vi.fn();
vi.mock('../src/db', () => ({
pool: { query },
}));
describe('operationLogRetention', () => {
afterEach(() => {
delete process.env.OPERATION_LOG_RETENTION_DAYS;
query.mockReset();
});
it('未配环境变量按自然月;0 关闭;正整数按天', async () => {
const { resolveOperationLogRetention } = await import(
'../src/services/operationLogRetentionService'
);
expect(resolveOperationLogRetention()).toEqual({ enabled: true, mode: 'calendar_month' });
process.env.OPERATION_LOG_RETENTION_DAYS = '31';
expect(resolveOperationLogRetention()).toEqual({ enabled: true, mode: 'days', days: 31 });
process.env.OPERATION_LOG_RETENTION_DAYS = '0';
expect(resolveOperationLogRetention()).toEqual({ enabled: false });
});
it('关闭时不发 DELETE', async () => {
process.env.OPERATION_LOG_RETENTION_DAYS = '0';
const { purgeExpiredOperationLogsForCurrentTenant } = await import(
'../src/services/operationLogRetentionService'
);
const result = await purgeExpiredOperationLogsForCurrentTenant();
expect(result).toEqual({ deleted: 0, more: false, skipped: true });
expect(query).not.toHaveBeenCalled();
});
it('按天截止分批,满 20 批标记 more', async () => {
process.env.OPERATION_LOG_RETENTION_DAYS = '31';
const {
OPERATION_LOG_PURGE_BATCH_SIZE,
OPERATION_LOG_PURGE_MAX_BATCHES,
purgeExpiredOperationLogsForCurrentTenant,
} = await import('../src/services/operationLogRetentionService');
query.mockResolvedValue({ rowCount: OPERATION_LOG_PURGE_BATCH_SIZE });
const result = await purgeExpiredOperationLogsForCurrentTenant();
expect(query).toHaveBeenCalledTimes(OPERATION_LOG_PURGE_MAX_BATCHES);
expect(result.deleted).toBe(OPERATION_LOG_PURGE_BATCH_SIZE * OPERATION_LOG_PURGE_MAX_BATCHES);
expect(result.more).toBe(true);
const sql = String(query.mock.calls[0][0]);
expect(sql).toContain('DELETE FROM operation_logs');
expect(sql).toContain('INTERVAL \'1 day\'');
expect(sql).not.toContain('inventory_logs');
});
it('默认自然月截止不碰 inventory_logs', async () => {
const { purgeExpiredOperationLogsForCurrentTenant } = await import(
'../src/services/operationLogRetentionService'
);
query.mockResolvedValue({ rowCount: 3 });
const result = await purgeExpiredOperationLogsForCurrentTenant();
expect(result).toEqual({ deleted: 3, more: false, skipped: false });
const sql = String(query.mock.calls[0][0]);
expect(sql).toContain("INTERVAL '1 month'");
expect(sql).not.toContain('inventory_logs');
});
it('末批不足 batch size 则 more=false', async () => {
process.env.OPERATION_LOG_RETENTION_DAYS = '31';
const { purgeExpiredOperationLogsForCurrentTenant } = await import(
'../src/services/operationLogRetentionService'
);
query
.mockResolvedValueOnce({ rowCount: 5000 })
.mockResolvedValueOnce({ rowCount: 12 });
const result = await purgeExpiredOperationLogsForCurrentTenant();
expect(query).toHaveBeenCalledTimes(2);
expect(result).toEqual({ deleted: 5012, more: false, skipped: false });
});
});
describe('scheduledJobs 操作日志清理挂载', () => {
it('北京 4 点、日 key、照片之后、不挂冷启动', () => {
const jobs = readFileSync(path.join(__dirname, '../src/services/scheduledJobs.ts'), 'utf8');
expect(jobs).toContain('runDailyOperationLogPurge');
expect(jobs).toContain('lastOperationLogPurgeKey');
expect(jobs).toContain('purgeExpiredOperationLogsForCurrentTenant');
const tick = jobs.slice(jobs.indexOf('async function runScheduledJobsTick'));
// 照片这一环已由「30 天删除」改为「归档打包」(2026-09-23),顺序仍是「照片 → 操作日志」
expect(tick).toContain('await runDailyInspectionPhotoArchive()');
expect(tick).toContain('await runDailyOperationLogPurge()');
expect(tick.indexOf('runDailyInspectionPhotoArchive')).toBeLessThan(
tick.indexOf('runDailyOperationLogPurge')
);
const start = jobs.slice(jobs.indexOf('export function startScheduledJobs'));
const lockBlock = start.slice(
start.indexOf('void withDistributedLock(SCHEDULER_LOCK_KEY'),
start.indexOf('timer = setInterval')
);
// 冷启动块:只跑 tick,**不再**在启动时做照片处理(2026-09-23 起照片由「删除」改为
// 「归档打包」,归档包可达数百 MB,冷启动跑会拖慢启动;见 design 20260923-inspection-photo-archive §8.2)
expect(lockBlock).toContain('runScheduledJobsTick(false)');
expect(lockBlock).not.toContain('purgeExpiredInspectionPhotos');
expect(lockBlock).not.toContain('runInspectionPhotoArchive');
expect(lockBlock).not.toContain('purgeExpiredOperationLogs');
expect(lockBlock).not.toContain('runDailyOperationLogPurge');
});
});