UNPKG

n8n-nodes-databricks

Version:

Databricks node for n8n

138 lines 7.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const jest_mock_extended_1 = require("jest-mock-extended"); const StoreCleanupService_1 = require("../StoreCleanupService"); describe('StoreCleanupService', () => { let vectorStores; let storeMetadata; let onCleanupMock; const addTestStore = (key, sizeBytes, createdHoursAgo, accessedHoursAgo) => { const mockStore = (0, jest_mock_extended_1.mock)(); vectorStores.set(key, mockStore); const now = Date.now(); storeMetadata.set(key, { size: sizeBytes, createdAt: new Date(now - createdHoursAgo * 3600000), lastAccessed: new Date(now - accessedHoursAgo * 3600000), }); }; beforeEach(() => { vectorStores = new Map(); storeMetadata = new Map(); onCleanupMock = jest.fn(); }); describe('TTL-based cleanup', () => { it('should identify inactive stores correctly', () => { const service = new StoreCleanupService_1.StoreCleanupService(100 * 1024 * 1024, 24 * 3600 * 1000, vectorStores, storeMetadata, onCleanupMock); const recentMetadata = { size: 1024, createdAt: new Date(Date.now() - 48 * 3600 * 1000), lastAccessed: new Date(Date.now() - 12 * 3600 * 1000), }; const inactiveMetadata = { size: 1024, createdAt: new Date(Date.now() - 48 * 3600 * 1000), lastAccessed: new Date(Date.now() - 36 * 3600 * 1000), }; expect(service.isStoreInactive(recentMetadata)).toBe(false); expect(service.isStoreInactive(inactiveMetadata)).toBe(true); }); it('should never identify stores as inactive when TTL is disabled', () => { const service = new StoreCleanupService_1.StoreCleanupService(100 * 1024 * 1024, -1, vectorStores, storeMetadata, onCleanupMock); const veryOldMetadata = { size: 1024, createdAt: new Date(Date.now() - 365 * 24 * 3600 * 1000), lastAccessed: new Date(Date.now() - 365 * 24 * 3600 * 1000), }; expect(service.isStoreInactive(veryOldMetadata)).toBe(false); }); it('should clean up inactive stores', () => { const service = new StoreCleanupService_1.StoreCleanupService(100 * 1024 * 1024, 24 * 3600 * 1000, vectorStores, storeMetadata, onCleanupMock); addTestStore('active1', 1024 * 1024, 48, 12); addTestStore('active2', 2048 * 1024, 72, 20); addTestStore('inactive1', 3072 * 1024, 100, 30); addTestStore('inactive2', 4096 * 1024, 120, 48); service.cleanupInactiveStores(); expect(vectorStores.has('active1')).toBe(true); expect(vectorStores.has('active2')).toBe(true); expect(vectorStores.has('inactive1')).toBe(false); expect(vectorStores.has('inactive2')).toBe(false); expect(storeMetadata.has('active1')).toBe(true); expect(storeMetadata.has('active2')).toBe(true); expect(storeMetadata.has('inactive1')).toBe(false); expect(storeMetadata.has('inactive2')).toBe(false); expect(onCleanupMock).toHaveBeenCalledWith(expect.arrayContaining(['inactive1', 'inactive2']), 7168 * 1024, 'ttl'); }); it('should not run TTL cleanup when disabled', () => { const service = new StoreCleanupService_1.StoreCleanupService(100 * 1024 * 1024, -1, vectorStores, storeMetadata, onCleanupMock); addTestStore('store1', 1024 * 1024, 48, 30); addTestStore('store2', 2048 * 1024, 72, 48); service.cleanupInactiveStores(); expect(vectorStores.size).toBe(2); expect(storeMetadata.size).toBe(2); expect(onCleanupMock).not.toHaveBeenCalled(); }); }); describe('Memory-based cleanup', () => { it('should clean up oldest stores to make room for new data', () => { const maxMemoryBytes = 10 * 1024 * 1024; const service = new StoreCleanupService_1.StoreCleanupService(maxMemoryBytes, 24 * 3600 * 1000, vectorStores, storeMetadata, onCleanupMock); addTestStore('newest', 2 * 1024 * 1024, 1, 1); addTestStore('newer', 3 * 1024 * 1024, 2, 1); addTestStore('older', 3 * 1024 * 1024, 3, 1); addTestStore('oldest', 2 * 1024 * 1024, 4, 1); service.cleanupOldestStores(5 * 1024 * 1024); expect(vectorStores.has('newest')).toBe(true); expect(vectorStores.has('newer')).toBe(true); expect(vectorStores.has('older')).toBe(false); expect(vectorStores.has('oldest')).toBe(false); expect(onCleanupMock).toHaveBeenCalledWith(expect.arrayContaining(['older', 'oldest']), 5 * 1024 * 1024, 'memory'); }); it('should run TTL cleanup before memory cleanup', () => { const maxMemoryBytes = 10 * 1024 * 1024; const service = new StoreCleanupService_1.StoreCleanupService(maxMemoryBytes, 24 * 3600 * 1000, vectorStores, storeMetadata, onCleanupMock); addTestStore('active-newest', 2 * 1024 * 1024, 1, 1); addTestStore('active-older', 3 * 1024 * 1024, 3, 12); addTestStore('inactive', 3 * 1024 * 1024, 3, 30); addTestStore('active-oldest', 2 * 1024 * 1024, 4, 20); service.cleanupOldestStores(5 * 1024 * 1024); expect(vectorStores.has('active-newest')).toBe(true); expect(vectorStores.has('active-older')).toBe(true); expect(vectorStores.has('inactive')).toBe(false); expect(vectorStores.has('active-oldest')).toBe(false); expect(onCleanupMock).toHaveBeenCalledTimes(2); expect(onCleanupMock).toHaveBeenNthCalledWith(1, ['inactive'], 3 * 1024 * 1024, 'ttl'); expect(onCleanupMock).toHaveBeenNthCalledWith(2, ['active-oldest'], 2 * 1024 * 1024, 'memory'); }); it('should not perform memory cleanup when limit is disabled', () => { const service = new StoreCleanupService_1.StoreCleanupService(-1, 24 * 3600 * 1000, vectorStores, storeMetadata, onCleanupMock); addTestStore('store1', 5 * 1024 * 1024, 1, 1); addTestStore('store2', 10 * 1024 * 1024, 2, 1); service.cleanupOldestStores(100 * 1024 * 1024); expect(vectorStores.size).toBe(2); expect(storeMetadata.size).toBe(2); expect(onCleanupMock).not.toHaveBeenCalled(); }); it('should handle empty stores during cleanup', () => { const service = new StoreCleanupService_1.StoreCleanupService(10 * 1024 * 1024, 24 * 3600 * 1000, vectorStores, storeMetadata, onCleanupMock); service.cleanupOldestStores(5 * 1024 * 1024); service.cleanupInactiveStores(); expect(onCleanupMock).not.toHaveBeenCalled(); }); it('should update the cache when stores are removed', () => { const service = new StoreCleanupService_1.StoreCleanupService(10 * 1024 * 1024, 24 * 3600 * 1000, vectorStores, storeMetadata, onCleanupMock); addTestStore('newest', 2 * 1024 * 1024, 1, 1); addTestStore('middle', 3 * 1024 * 1024, 3, 1); addTestStore('oldest', 4 * 1024 * 1024, 5, 1); service.cleanupOldestStores(4 * 1024 * 1024); expect(vectorStores.has('oldest')).toBe(false); expect(vectorStores.has('middle')).toBe(true); expect(vectorStores.has('newest')).toBe(true); const cacheKeys = service['oldestStoreKeys']; expect(cacheKeys.includes('oldest')).toBe(false); expect(cacheKeys.includes('middle')).toBe(true); expect(cacheKeys.includes('newest')).toBe(true); }); }); }); //# sourceMappingURL=StoreCleanupService.test.js.map