|
| 1 | +// |
| 2 | +// NetworkLogCleanerTests.swift |
| 3 | +// ScytherTests |
| 4 | +// |
| 5 | +// Created by Brandon Stillitano on 01/01/2026. |
| 6 | +// |
| 7 | + |
| 8 | +#if !os(macOS) |
| 9 | +@testable import Scyther |
| 10 | +import XCTest |
| 11 | + |
| 12 | +@MainActor |
| 13 | +final class NetworkLogCleanerTests: XCTestCase { |
| 14 | + |
| 15 | + private var testDirectory: URL! |
| 16 | + private var fileManager: FileManager! |
| 17 | + |
| 18 | + override func setUp() async throws { |
| 19 | + try await super.setUp() |
| 20 | + fileManager = FileManager.default |
| 21 | + |
| 22 | + // Create a temporary test directory |
| 23 | + testDirectory = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 24 | + try fileManager.createDirectory(at: testDirectory, withIntermediateDirectories: true) |
| 25 | + } |
| 26 | + |
| 27 | + override func tearDown() async throws { |
| 28 | + // Clean up test directory |
| 29 | + if let testDirectory = testDirectory { |
| 30 | + try? fileManager.removeItem(at: testDirectory) |
| 31 | + } |
| 32 | + try await super.tearDown() |
| 33 | + } |
| 34 | + |
| 35 | + // MARK: - Singleton Tests |
| 36 | + |
| 37 | + func testSingletonInstance() { |
| 38 | + let instance1 = NetworkLogCleaner.shared |
| 39 | + let instance2 = NetworkLogCleaner.shared |
| 40 | + XCTAssertTrue(instance1 === instance2) |
| 41 | + } |
| 42 | + |
| 43 | + // MARK: - Retention Period Tests |
| 44 | + |
| 45 | + func testRetentionDaysIsSevenDays() { |
| 46 | + XCTAssertEqual(NetworkLogCleaner.retentionDays, 7) |
| 47 | + } |
| 48 | + |
| 49 | + // MARK: - File Pattern Matching Tests |
| 50 | + |
| 51 | + func testCleanupDoesNotDeleteNonLogFiles() { |
| 52 | + // Create a non-log file |
| 53 | + let nonLogFile = testDirectory.appendingPathComponent("user_data.json") |
| 54 | + fileManager.createFile(atPath: nonLogFile.path, contents: Data()) |
| 55 | + |
| 56 | + // Verify file exists |
| 57 | + XCTAssertTrue(fileManager.fileExists(atPath: nonLogFile.path)) |
| 58 | + |
| 59 | + // cleanupOldLogs should not affect non-log files (testing the pattern matching logic) |
| 60 | + // Since we can't inject the directory, we verify the file pattern matching works correctly |
| 61 | + let fileName = "user_data.json" |
| 62 | + XCTAssertFalse(isNetworkLogFile(fileName)) |
| 63 | + } |
| 64 | + |
| 65 | + func testRequestBodyFilePatternRecognized() { |
| 66 | + XCTAssertTrue(isNetworkLogFile("logger_request_body_10:30:45.123_ABC123")) |
| 67 | + XCTAssertTrue(isNetworkLogFile("logger_request_body_unknown_XYZ789")) |
| 68 | + } |
| 69 | + |
| 70 | + func testResponseBodyFilePatternRecognized() { |
| 71 | + XCTAssertTrue(isNetworkLogFile("logger_response_body_10:30:45.123_ABC123")) |
| 72 | + XCTAssertTrue(isNetworkLogFile("logger_response_body_unknown_XYZ789")) |
| 73 | + } |
| 74 | + |
| 75 | + func testSessionLogFilePatternRecognized() { |
| 76 | + XCTAssertTrue(isNetworkLogFile("SessionLog.log")) |
| 77 | + } |
| 78 | + |
| 79 | + func testNonLogFilePatternsNotRecognized() { |
| 80 | + XCTAssertFalse(isNetworkLogFile("user_preferences.json")) |
| 81 | + XCTAssertFalse(isNetworkLogFile("database.sqlite")) |
| 82 | + XCTAssertFalse(isNetworkLogFile("console.log")) |
| 83 | + XCTAssertFalse(isNetworkLogFile("logger_crash_body_123")) |
| 84 | + XCTAssertFalse(isNetworkLogFile("SessionLog.txt")) |
| 85 | + } |
| 86 | + |
| 87 | + // MARK: - Helper to test file pattern matching |
| 88 | + |
| 89 | + /// Mirrors the logic in NetworkLogCleaner.isNetworkLogFile |
| 90 | + private func isNetworkLogFile(_ fileName: String) -> Bool { |
| 91 | + return fileName.hasPrefix("logger_request_body_") || |
| 92 | + fileName.hasPrefix("logger_response_body_") || |
| 93 | + fileName == "SessionLog.log" |
| 94 | + } |
| 95 | + |
| 96 | + // MARK: - Integration Tests |
| 97 | + |
| 98 | + func testCleanupOldLogsDoesNotCrashOnEmptyDirectory() { |
| 99 | + // Should handle empty directory gracefully without crashing |
| 100 | + NetworkLogCleaner.shared.cleanupOldLogs() |
| 101 | + // If we get here without crashing, the test passes |
| 102 | + } |
| 103 | + |
| 104 | + func testDeleteAllLogFilesDoesNotCrashOnEmptyDirectory() { |
| 105 | + // Should handle empty directory gracefully without crashing |
| 106 | + NetworkLogCleaner.shared.deleteAllLogFiles() |
| 107 | + // If we get here without crashing, the test passes |
| 108 | + } |
| 109 | + |
| 110 | + func testCleanupPreservesFilePattern() { |
| 111 | + // Test that the file pattern matching logic correctly identifies log files |
| 112 | + let testCases: [(fileName: String, isLogFile: Bool)] = [ |
| 113 | + ("logger_request_body_12:34:56.789_UUID123", true), |
| 114 | + ("logger_response_body_12:34:56.789_UUID456", true), |
| 115 | + ("SessionLog.log", true), |
| 116 | + ("other_file.txt", false), |
| 117 | + ("logger_request_body", false), // Missing timestamp and UUID |
| 118 | + ("request_body_12:34:56.789_UUID", false), // Missing logger_ prefix |
| 119 | + ("SessionLog", false), // Missing .log extension |
| 120 | + ] |
| 121 | + |
| 122 | + for testCase in testCases { |
| 123 | + XCTAssertEqual( |
| 124 | + isNetworkLogFile(testCase.fileName), |
| 125 | + testCase.isLogFile, |
| 126 | + "Pattern matching failed for: \(testCase.fileName)" |
| 127 | + ) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | +#endif |
0 commit comments