|
| 1 | +/** |
| 2 | + * Memory retention scheduler tests — mocks the MemoryService and the logger, |
| 3 | + * uses fake timers to assert the boot pass vs. daily-tick cadence. |
| 4 | + */ |
| 5 | + |
| 6 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 7 | + |
| 8 | +const { mockService } = vi.hoisted(() => { |
| 9 | + const mockService = { |
| 10 | + decayMemories: vi.fn(async () => 0), |
| 11 | + cleanupMemories: vi.fn(async () => 0), |
| 12 | + }; |
| 13 | + return { mockService }; |
| 14 | +}); |
| 15 | + |
| 16 | +vi.mock('../memory-service.js', () => ({ |
| 17 | + getMemoryService: () => mockService, |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock('../log.js', () => ({ |
| 21 | + getLog: () => ({ info: vi.fn(), warn: vi.fn(), debug: vi.fn(), error: vi.fn() }), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock('@ownpilot/core', () => ({ |
| 25 | + getErrorMessage: (e: unknown) => (e instanceof Error ? e.message : String(e)), |
| 26 | +})); |
| 27 | + |
| 28 | +const { runMemoryRetentionCleanup, startMemoryRetention, stopMemoryRetention } = |
| 29 | + await import('./retention.js'); |
| 30 | + |
| 31 | +const DAY_MS = 24 * 60 * 60 * 1000; |
| 32 | + |
| 33 | +beforeEach(() => { |
| 34 | + vi.clearAllMocks(); |
| 35 | +}); |
| 36 | + |
| 37 | +afterEach(() => { |
| 38 | + stopMemoryRetention(); |
| 39 | + vi.useRealTimers(); |
| 40 | +}); |
| 41 | + |
| 42 | +describe('runMemoryRetentionCleanup', () => { |
| 43 | + it('runs decay then cleanup by default', async () => { |
| 44 | + await runMemoryRetentionCleanup('default'); |
| 45 | + expect(mockService.decayMemories).toHaveBeenCalledWith('default'); |
| 46 | + expect(mockService.cleanupMemories).toHaveBeenCalledWith('default'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('skips decay when { decay: false }', async () => { |
| 50 | + await runMemoryRetentionCleanup('default', { decay: false }); |
| 51 | + expect(mockService.decayMemories).not.toHaveBeenCalled(); |
| 52 | + expect(mockService.cleanupMemories).toHaveBeenCalledTimes(1); |
| 53 | + }); |
| 54 | + |
| 55 | + it('still runs cleanup when decay throws', async () => { |
| 56 | + mockService.decayMemories.mockRejectedValueOnce(new Error('boom')); |
| 57 | + await runMemoryRetentionCleanup('default'); |
| 58 | + expect(mockService.cleanupMemories).toHaveBeenCalledTimes(1); |
| 59 | + }); |
| 60 | + |
| 61 | + it('never throws when cleanup throws', async () => { |
| 62 | + mockService.cleanupMemories.mockRejectedValueOnce(new Error('boom')); |
| 63 | + await expect(runMemoryRetentionCleanup('default')).resolves.toBeUndefined(); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('startMemoryRetention', () => { |
| 68 | + it('runs a cleanup-only boot pass immediately (no decay)', async () => { |
| 69 | + vi.useFakeTimers(); |
| 70 | + startMemoryRetention('default'); |
| 71 | + // Flush the microtask from the fire-and-forget boot pass. |
| 72 | + await vi.advanceTimersByTimeAsync(0); |
| 73 | + expect(mockService.cleanupMemories).toHaveBeenCalledTimes(1); |
| 74 | + expect(mockService.decayMemories).not.toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('runs a full decay + cleanup pass on the daily tick', async () => { |
| 78 | + vi.useFakeTimers(); |
| 79 | + startMemoryRetention('default'); |
| 80 | + await vi.advanceTimersByTimeAsync(0); // boot pass |
| 81 | + expect(mockService.decayMemories).not.toHaveBeenCalled(); |
| 82 | + |
| 83 | + await vi.advanceTimersByTimeAsync(DAY_MS); |
| 84 | + expect(mockService.decayMemories).toHaveBeenCalledTimes(1); |
| 85 | + // boot cleanup + daily cleanup |
| 86 | + expect(mockService.cleanupMemories).toHaveBeenCalledTimes(2); |
| 87 | + }); |
| 88 | + |
| 89 | + it('is idempotent — a second start does not schedule a second timer', async () => { |
| 90 | + vi.useFakeTimers(); |
| 91 | + startMemoryRetention('default'); |
| 92 | + startMemoryRetention('default'); |
| 93 | + await vi.advanceTimersByTimeAsync(0); |
| 94 | + // Only one boot pass despite two start calls. |
| 95 | + expect(mockService.cleanupMemories).toHaveBeenCalledTimes(1); |
| 96 | + |
| 97 | + await vi.advanceTimersByTimeAsync(DAY_MS); |
| 98 | + // Only one daily decay despite two start calls. |
| 99 | + expect(mockService.decayMemories).toHaveBeenCalledTimes(1); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('stopMemoryRetention', () => { |
| 104 | + it('stops the timer so no further passes run', async () => { |
| 105 | + vi.useFakeTimers(); |
| 106 | + startMemoryRetention('default'); |
| 107 | + await vi.advanceTimersByTimeAsync(0); |
| 108 | + stopMemoryRetention(); |
| 109 | + |
| 110 | + await vi.advanceTimersByTimeAsync(DAY_MS * 3); |
| 111 | + expect(mockService.decayMemories).not.toHaveBeenCalled(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('is idempotent', () => { |
| 115 | + expect(() => { |
| 116 | + stopMemoryRetention(); |
| 117 | + stopMemoryRetention(); |
| 118 | + }).not.toThrow(); |
| 119 | + }); |
| 120 | +}); |
0 commit comments