@@ -981,11 +981,11 @@ describe('ErrsolePostgres', () => {
981
981
} ) ;
982
982
983
983
describe ( '#searchLogs' , ( ) => {
984
- // let poolQuerySpy;
984
+ let poolQuerySpy ;
985
985
986
- // beforeEach(() => {
987
- // poolQuerySpy = jest.spyOn(poolMock, 'query');
988
- // });
986
+ beforeEach ( ( ) => {
987
+ poolQuerySpy = jest . spyOn ( poolMock , 'query' ) ;
988
+ } ) ;
989
989
990
990
afterEach ( ( ) => {
991
991
jest . clearAllMocks ( ) ;
@@ -1370,6 +1370,42 @@ describe('ErrsolePostgres', () => {
1370
1370
} ) ;
1371
1371
} ) ;
1372
1372
1373
+ it ( 'should apply errsole_id filter in the search query' , async ( ) => {
1374
+ const filters = {
1375
+ errsole_id : 123 ,
1376
+ limit : 50
1377
+ } ;
1378
+
1379
+ const logs = [
1380
+ {
1381
+ id : 1 ,
1382
+ hostname : 'localhost' ,
1383
+ pid : 1234 ,
1384
+ source : 'source1' ,
1385
+ timestamp : new Date ( ) ,
1386
+ level : 'info' ,
1387
+ message : 'Log message with specific errsole_id' ,
1388
+ errsole_id : 123
1389
+ }
1390
+ ] ;
1391
+
1392
+ poolQuerySpy . mockClear ( ) ;
1393
+
1394
+ poolMock . query . mockResolvedValueOnce ( { rows : logs } ) ;
1395
+
1396
+ const result = await errsolePostgres . searchLogs ( [ ] , filters ) ;
1397
+
1398
+ expect ( poolQuerySpy ) . toHaveBeenCalledWith (
1399
+ 'SELECT id, hostname, pid, source, timestamp, level, message,errsole_id FROM errsole_logs_v2 WHERE (errsole_id = $1) ORDER BY id DESC LIMIT $1' ,
1400
+ [ 123 , 50 ]
1401
+ ) ;
1402
+
1403
+ expect ( result ) . toEqual ( {
1404
+ items : logs ,
1405
+ filters : { errsole_id : 123 , limit : 50 }
1406
+ } ) ;
1407
+ } ) ;
1408
+
1373
1409
it ( 'should handle errors in searching logs' , async ( ) => {
1374
1410
poolMock . query . mockRejectedValueOnce ( new Error ( 'Query error' ) ) ;
1375
1411
@@ -1860,10 +1896,69 @@ describe('ErrsolePostgres', () => {
1860
1896
} ) ;
1861
1897
} ) ;
1862
1898
1899
+ describe ( '#deleteExpiredNotificationItems' , ( ) => {
1900
+ let getConfigSpy ;
1901
+ let poolQuerySpy ;
1902
+ let setTimeoutSpy ;
1903
+
1904
+ beforeEach ( ( ) => {
1905
+ getConfigSpy = jest . spyOn ( errsolePostgres , 'getConfig' ) . mockResolvedValue ( { item : { key : 'logsTTL' , value : '2592000000' } } ) ;
1906
+ poolQuerySpy = jest . spyOn ( poolMock , 'query' ) ;
1907
+ setTimeoutSpy = jest . spyOn ( global , 'setTimeout' ) . mockImplementation ( ( callback ) => callback ( ) ) ;
1908
+ errsolePostgres . deleteExpiredNotificationItemsRunning = false ; // Reset the flag before each test
1909
+ } ) ;
1910
+
1911
+ afterEach ( ( ) => {
1912
+ jest . clearAllMocks ( ) ;
1913
+ } ) ;
1914
+
1915
+ it ( 'should delete expired notification items based on TTL configuration' , async ( ) => {
1916
+ poolQuerySpy
1917
+ . mockResolvedValueOnce ( { rowCount : 1000 } ) // First batch of deletions
1918
+ . mockResolvedValueOnce ( { rowCount : 0 } ) ; // Second batch indicating no more items
1919
+
1920
+ await errsolePostgres . deleteExpiredNotificationItems ( ) ;
1921
+
1922
+ expect ( getConfigSpy ) . toHaveBeenCalledWith ( 'logsTTL' ) ;
1923
+ expect ( poolQuerySpy ) . toHaveBeenCalledWith ( expect . any ( String ) , [ expect . any ( String ) ] ) ;
1924
+ expect ( setTimeoutSpy ) . toHaveBeenCalled ( ) ;
1925
+ } ) ;
1926
+
1927
+ it ( 'should handle errors during deletion process and reset running flag' , async ( ) => {
1928
+ const consoleErrorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
1929
+ poolQuerySpy . mockRejectedValueOnce ( new Error ( 'Query error' ) ) ;
1930
+
1931
+ await errsolePostgres . deleteExpiredNotificationItems ( ) ;
1932
+
1933
+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith ( new Error ( 'Query error' ) ) ;
1934
+ expect ( errsolePostgres . deleteExpiredNotificationItemsRunning ) . toBe ( false ) ;
1935
+ consoleErrorSpy . mockRestore ( ) ;
1936
+ } ) ;
1937
+
1938
+ it ( 'should use default TTL if configuration key is not found' , async ( ) => {
1939
+ getConfigSpy . mockResolvedValueOnce ( { item : null } ) ;
1940
+ poolQuerySpy
1941
+ . mockResolvedValueOnce ( { rowCount : 1000 } ) // First batch
1942
+ . mockResolvedValueOnce ( { rowCount : 0 } ) ; // No more items
1943
+
1944
+ await errsolePostgres . deleteExpiredNotificationItems ( ) ;
1945
+
1946
+ expect ( getConfigSpy ) . toHaveBeenCalledWith ( 'logsTTL' ) ;
1947
+ expect ( poolQuerySpy ) . toHaveBeenCalledWith ( expect . any ( String ) , [ expect . any ( String ) ] ) ;
1948
+ expect ( setTimeoutSpy ) . toHaveBeenCalled ( ) ;
1949
+ } ) ;
1950
+
1951
+ it ( 'should reset deleteExpiredNotificationItemsRunning flag after execution' , async ( ) => {
1952
+ poolQuerySpy . mockResolvedValueOnce ( { rowCount : 0 } ) ;
1953
+
1954
+ await errsolePostgres . deleteExpiredNotificationItems ( ) ;
1955
+
1956
+ expect ( errsolePostgres . deleteExpiredNotificationItemsRunning ) . toBe ( false ) ;
1957
+ } ) ;
1958
+ } ) ;
1959
+
1863
1960
afterAll ( ( ) => {
1864
- // Stop the cron job
1865
1961
cronJob . stop ( ) ;
1866
- // Clear the interval
1867
1962
clearInterval ( errsolePostgres . flushIntervalId ) ;
1868
1963
} ) ;
1869
1964
} ) ;
0 commit comments