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'); }); });