1313using Nexus . External . Apis . FileSystem ;
1414using Nexus . External . Apis . ProcessManagement ;
1515
16+ using NLog ;
17+ using NLog . Config ;
18+ using NLog . Targets ;
19+
1620using Xunit ;
1721
1822namespace Nexus . Engine . Tests . Internal ;
@@ -594,12 +598,18 @@ public void CleanupOldCdbLogs_DeletesFilesOlderThanRetentionDays()
594598
595599 var oldFilePath = Path . Combine ( sessionsDirectory , "cdb_old.log" ) ;
596600 var recentFilePath = Path . Combine ( sessionsDirectory , "cdb_recent.log" ) ;
601+ var oldCompressedFilePath = Path . Combine ( sessionsDirectory , "cdb_old.log.gz" ) ;
602+ var recentCompressedFilePath = Path . Combine ( sessionsDirectory , "cdb_recent.log.gz" ) ;
597603
598604 File . WriteAllText ( oldFilePath , "old" ) ;
599605 File . WriteAllText ( recentFilePath , "recent" ) ;
606+ File . WriteAllText ( oldCompressedFilePath , "old_compressed" ) ;
607+ File . WriteAllText ( recentCompressedFilePath , "recent_compressed" ) ;
600608
601609 File . SetCreationTime ( oldFilePath , DateTime . Now . AddDays ( - 10 ) ) ;
610+ File . SetCreationTime ( oldCompressedFilePath , DateTime . Now . AddDays ( - 10 ) ) ;
602611 File . SetCreationTime ( recentFilePath , DateTime . Now ) ;
612+ File . SetCreationTime ( recentCompressedFilePath , DateTime . Now ) ;
603613
604614 try
605615 {
@@ -615,8 +625,11 @@ public void CleanupOldCdbLogs_DeletesFilesOlderThanRetentionDays()
615625
616626 // Assert
617627 _ = File . Exists ( oldFilePath ) . Should ( ) . BeFalse ( ) ;
628+ _ = File . Exists ( oldCompressedFilePath ) . Should ( ) . BeFalse ( ) ;
618629 _ = File . Exists ( recentFilePath ) . Should ( ) . BeTrue ( ) ;
630+ _ = File . Exists ( recentCompressedFilePath ) . Should ( ) . BeTrue ( ) ;
619631 m_MockFileSystem . Verify ( fs => fs . DeleteFile ( oldFilePath ) , Times . Once ) ;
632+ m_MockFileSystem . Verify ( fs => fs . DeleteFile ( oldCompressedFilePath ) , Times . Once ) ;
620633 }
621634 finally
622635 {
@@ -1373,4 +1386,68 @@ public async Task InitializeAsync_WhenInitializationFails_DisposesSession()
13731386 // Assert - Session should be disposed after initialization failure
13741387 _ = session . IsInitialized . Should ( ) . BeFalse ( ) ;
13751388 }
1389+
1390+ /// <summary>
1391+ /// Verifies that disposing the session compresses the CDB log file and deletes the original log.
1392+ /// </summary>
1393+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
1394+ [ Fact ]
1395+ public async Task DisposeAsync_CompressesCdbLog_AndDeletesOriginalLog ( )
1396+ {
1397+ // Arrange
1398+ var originalConfig = LogManager . Configuration ;
1399+ var tempRoot = Path . Combine ( Path . GetTempPath ( ) , "MCPNexusCdbCompressionTests" , Guid . NewGuid ( ) . ToString ( "N" ) ) ;
1400+ var logsDirectory = Path . Combine ( tempRoot , "Logs" ) ;
1401+ _ = Directory . CreateDirectory ( logsDirectory ) ;
1402+
1403+ try
1404+ {
1405+ var config = new LoggingConfiguration ( ) ;
1406+ var mainLogPath = Path . Combine ( logsDirectory , "main.log" ) ;
1407+ var fileTarget = new FileTarget ( "mainFile" )
1408+ {
1409+ FileName = mainLogPath ,
1410+ } ;
1411+ config . AddTarget ( fileTarget ) ;
1412+ config . AddRuleForAllLevels ( fileTarget ) ;
1413+ LogManager . Configuration = config ;
1414+
1415+ var accessor = new CdbSessionTestAccessor ( m_Settings . Object , m_MockFileSystem . Object , m_MockProcessManager . Object ) ;
1416+
1417+ var sessionId = "session-compress" ;
1418+ var sessionIdProperty = typeof ( CdbSession ) . GetProperty ( "SessionId" , System . Reflection . BindingFlags . Instance | System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Public ) ;
1419+ sessionIdProperty ! . SetValue ( accessor , sessionId ) ;
1420+
1421+ var getLogPathMethod = typeof ( CdbSession ) . GetMethod ( "GetCdbSessionBasedLogPath" , System . Reflection . BindingFlags . Instance | System . Reflection . BindingFlags . NonPublic ) ;
1422+ var logPath = ( string ) getLogPathMethod ! . Invoke ( accessor , new object [ ] { sessionId } ) ! ;
1423+
1424+ var logDirectory = Path . GetDirectoryName ( logPath ) ;
1425+ if ( ! string . IsNullOrEmpty ( logDirectory ) )
1426+ {
1427+ _ = Directory . CreateDirectory ( logDirectory ) ;
1428+ }
1429+
1430+ const string logContent = "cdb log content" ;
1431+ File . WriteAllText ( logPath , logContent ) ;
1432+
1433+ // Act
1434+ await accessor . DisposeAsync ( ) ;
1435+
1436+ // Assert
1437+ var compressedPath = $ "{ logPath } .gz";
1438+ _ = File . Exists ( logPath ) . Should ( ) . BeFalse ( ) ;
1439+ _ = File . Exists ( compressedPath ) . Should ( ) . BeTrue ( ) ;
1440+ var compressedFileInfo = new FileInfo ( compressedPath ) ;
1441+ _ = compressedFileInfo . Length . Should ( ) . BeGreaterThan ( 0 ) ;
1442+ }
1443+ finally
1444+ {
1445+ LogManager . Configuration = originalConfig ;
1446+
1447+ if ( Directory . Exists ( tempRoot ) )
1448+ {
1449+ Directory . Delete ( tempRoot , true ) ;
1450+ }
1451+ }
1452+ }
13761453}
0 commit comments