test: Use fake timers for history compaction tests (#35573)

This commit is contained in:
Matsu
2026-08-05 08:52:26 +00:00
committed by GitHub
parent 679a5ad9c7
commit e75e7f41e7
@@ -64,6 +64,16 @@ describe('WorkflowHistoryCompactionService', () => {
});
describe('startCompacting', () => {
beforeEach(() => {
// Set the system to a time that isn't 3 AM to avoid hitting the "trim once a day" window
const mockDate = new Date(2026, 10, 10, 1, 0, 0);
vi.setSystemTime(mockDate);
});
afterEach(() => {
vi.useRealTimers();
});
it('should start compacting if service is enabled and DB is migrated', () => {
const compactingService = new WorkflowHistoryCompactionService(
config,
@@ -194,4 +204,64 @@ describe('WorkflowHistoryCompactionService', () => {
// should still call recent history compaction
expect(optimizeHistoriesSpy).toHaveBeenCalled();
});
it('should trim if triggered at 3 AM with trimOnStartUp as false', () => {
const mockDate = new Date(2026, 10, 10, 3, 0, 0);
vi.setSystemTime(mockDate);
const compactingService = new WorkflowHistoryCompactionService(
{ ...config, trimOnStartUp: false },
globalConfig,
mockLogger(),
mock<InstanceSettings>({ isLeader: true, instanceType: 'main', isMultiMain: true }),
dbConnection,
mock(),
mock<EventService>(),
);
vi
// @ts-expect-error Private method
.spyOn(compactingService, 'optimizeHistories')
.mockImplementation((() => {}) as never);
const trimLongRunningHistoriesSpy = vi
// @ts-expect-error Private method
.spyOn(compactingService, 'trimLongRunningHistories')
.mockImplementation((() => {}) as never);
compactingService.startCompacting();
expect(trimLongRunningHistoriesSpy).toHaveBeenCalled();
vi.useRealTimers();
});
it('should not trim if triggered outside of 3 AM with trimOnStartUp as false', () => {
const mockDate = new Date(2026, 10, 10, 5, 0, 0);
vi.setSystemTime(mockDate);
const compactingService = new WorkflowHistoryCompactionService(
{ ...config, trimOnStartUp: false },
globalConfig,
mockLogger(),
mock<InstanceSettings>({ isLeader: true, instanceType: 'main', isMultiMain: true }),
dbConnection,
mock(),
mock<EventService>(),
);
vi
// @ts-expect-error Private method
.spyOn(compactingService, 'optimizeHistories')
.mockImplementation((() => {}) as never);
const trimLongRunningHistoriesSpy = vi
// @ts-expect-error Private method
.spyOn(compactingService, 'trimLongRunningHistories')
.mockImplementation((() => {}) as never);
compactingService.startCompacting();
expect(trimLongRunningHistoriesSpy).not.toHaveBeenCalled();
vi.useRealTimers();
});
});