|
1 | 1 | import Foundation
|
2 |
| -@testable import Swiftly |
3 |
| -@testable import SwiftlyCore |
4 | 2 | import SystemPackage
|
5 | 3 | import Testing
|
6 | 4 |
|
7 |
| -@Suite("JsonFileProgressReporter Tests") |
8 |
| -struct JsonFileProgressReporterTests { |
9 |
| - @Test("Test update method writes progress to file") |
10 |
| - func testUpdateWritesProgressToFile() throws { |
| 5 | +@testable import Swiftly |
| 6 | +@testable import SwiftlyCore |
| 7 | + |
| 8 | +@Suite struct JsonFileProgressReporterTests { |
| 9 | + @Test("Test update method writes progress to file as valid JSONNL") |
| 10 | + func testUpdateWritesProgressToFile() async throws { |
11 | 11 | let tempFile = fs.mktemp(ext: ".json")
|
12 |
| - let reporter = JsonFileProgressReporter(filePath: tempFile) |
| 12 | + try await fs.create(.mode(Int(0o644)), file: tempFile) |
| 13 | + defer { try? FileManager.default.removeItem(atPath: tempFile.string) } |
| 14 | + let reporter = try JsonFileProgressReporter(SwiftlyTests.ctx, filePath: tempFile) |
13 | 15 |
|
14 | 16 | reporter.update(step: 1, total: 10, text: "Processing item 1")
|
15 |
| - |
16 |
| - let fileContent = try String(contentsOfFile: tempFile.string) |
17 |
| - |
18 |
| - #expect(fileContent.contains("Processing item 1")) |
19 |
| - #expect(fileContent.contains("\"percent\":10")) |
20 |
| - #expect(fileContent.contains("\"step\"")) |
21 |
| - #expect(fileContent.contains("\"timestamp\"")) |
22 |
| - |
23 |
| - try FileManager.default.removeItem(atPath: tempFile.string) |
| 17 | + try reporter.close() |
| 18 | + |
| 19 | + let decoder = JSONDecoder() |
| 20 | + |
| 21 | + let info = try String(contentsOfFile: tempFile.string).split(separator: "\n") |
| 22 | + .filter { |
| 23 | + !$0.isEmpty |
| 24 | + }.map { |
| 25 | + try decoder.decode( |
| 26 | + ProgressInfo.self, |
| 27 | + from: Data($0.trimmingCharacters(in: .whitespacesAndNewlines).utf8) |
| 28 | + ) |
| 29 | + } |
| 30 | + |
| 31 | + #expect(info.count == 1) |
| 32 | + |
| 33 | + if case let .step(timestamp, percent, text) = info.first { |
| 34 | + #expect(text == "Processing item 1") |
| 35 | + #expect(percent == 10) |
| 36 | + #expect(timestamp.timeIntervalSince1970 > 0) |
| 37 | + } else { |
| 38 | + Issue.record("Expected step info but got \(info[0])") |
| 39 | + return |
| 40 | + } |
24 | 41 | }
|
25 | 42 |
|
26 | 43 | @Test("Test complete method writes completion status")
|
27 |
| - func testCompleteWritesCompletionStatus() throws { |
| 44 | + func testCompleteWritesCompletionStatus() async throws { |
28 | 45 | let tempFile = fs.mktemp(ext: ".json")
|
29 |
| - let reporter = JsonFileProgressReporter(filePath: tempFile) |
30 |
| - |
31 |
| - reporter.complete(success: true) |
32 |
| - |
33 |
| - let fileContent = try String(contentsOfFile: tempFile.string) |
34 |
| - |
35 |
| - #expect(fileContent.contains("\"success\":true")) |
36 |
| - #expect(fileContent.contains("\"complete\"")) |
| 46 | + try await fs.create(.mode(Int(0o644)), file: tempFile) |
| 47 | + defer { try? FileManager.default.removeItem(atPath: tempFile.string) } |
37 | 48 |
|
38 |
| - try FileManager.default.removeItem(atPath: tempFile.string) |
39 |
| - } |
| 49 | + let reporter = try JsonFileProgressReporter(SwiftlyTests.ctx, filePath: tempFile) |
40 | 50 |
|
41 |
| - @Test("Test complete method writes failure status") |
42 |
| - func testCompleteWritesFailureStatus() throws { |
43 |
| - let tempFile = fs.mktemp(ext: ".json") |
44 |
| - let reporter = JsonFileProgressReporter(filePath: tempFile) |
| 51 | + let status = Bool.random() |
| 52 | + reporter.complete(success: status) |
| 53 | + try reporter.close() |
45 | 54 |
|
46 |
| - reporter.complete(success: false) |
| 55 | + let decoder = JSONDecoder() |
47 | 56 |
|
48 |
| - let fileContent = try String(contentsOfFile: tempFile.string) |
| 57 | + let info = try String(contentsOfFile: tempFile.string).split(separator: "\n") |
| 58 | + .filter { |
| 59 | + !$0.isEmpty |
| 60 | + }.map { |
| 61 | + try decoder.decode(ProgressInfo.self, from: Data($0.utf8)) |
| 62 | + } |
49 | 63 |
|
50 |
| - #expect(fileContent.contains("\"success\":false")) |
51 |
| - #expect(fileContent.contains("\"complete\"")) |
| 64 | + #expect(info.count == 1) |
52 | 65 |
|
53 |
| - try FileManager.default.removeItem(atPath: tempFile.string) |
| 66 | + if case let .complete(success) = info.first { |
| 67 | + #expect(success == status) |
| 68 | + } else { |
| 69 | + Issue.record("Expected completion info but got \(info)") |
| 70 | + return |
| 71 | + } |
54 | 72 | }
|
55 | 73 |
|
56 | 74 | @Test("Test percentage calculation")
|
57 |
| - func testPercentageCalculation() throws { |
| 75 | + func testPercentageCalculation() async throws { |
58 | 76 | let tempFile = fs.mktemp(ext: ".json")
|
59 |
| - let reporter = JsonFileProgressReporter(filePath: tempFile) |
| 77 | + try await fs.create(.mode(Int(0o644)), file: tempFile) |
| 78 | + defer { try? FileManager.default.removeItem(atPath: tempFile.string) } |
| 79 | + let reporter = try JsonFileProgressReporter(SwiftlyTests.ctx, filePath: tempFile) |
60 | 80 |
|
61 | 81 | reporter.update(step: 25, total: 100, text: "Quarter way")
|
62 |
| - |
63 |
| - let fileContent = try String(contentsOfFile: tempFile.string) |
64 |
| - |
65 |
| - #expect(fileContent.contains("\"percent\":25")) |
| 82 | + try reporter.close() |
| 83 | + |
| 84 | + let decoder = JSONDecoder() |
| 85 | + let info = try String(contentsOfFile: tempFile.string).split(separator: "\n") |
| 86 | + .filter { |
| 87 | + !$0.isEmpty |
| 88 | + }.map { |
| 89 | + try decoder.decode(ProgressInfo.self, from: Data($0.utf8)) |
| 90 | + } |
| 91 | + #expect(info.count == 1) |
| 92 | + if case let .step(_, percent, text) = info.first { |
| 93 | + #expect(percent == 25) |
| 94 | + #expect(text == "Quarter way") |
| 95 | + } else { |
| 96 | + Issue.record("Expected step info but got \(info)") |
| 97 | + return |
| 98 | + } |
66 | 99 |
|
67 | 100 | try FileManager.default.removeItem(atPath: tempFile.string)
|
68 | 101 | }
|
69 | 102 |
|
70 |
| - @Test("Test clear method removes file") |
71 |
| - func testClearRemovesFile() throws { |
| 103 | + @Test("Test clear method truncates the file") |
| 104 | + func testClearTruncatesFile() async throws { |
72 | 105 | let tempFile = fs.mktemp(ext: ".json")
|
73 |
| - let reporter = JsonFileProgressReporter(filePath: tempFile) |
| 106 | + try await fs.create(.mode(Int(0o644)), file: tempFile) |
| 107 | + defer { try? FileManager.default.removeItem(atPath: tempFile.string) } |
| 108 | + let reporter = try JsonFileProgressReporter(SwiftlyTests.ctx, filePath: tempFile) |
| 109 | + defer { try? reporter.close() } |
74 | 110 |
|
75 | 111 | reporter.update(step: 1, total: 2, text: "Test")
|
76 | 112 |
|
77 |
| - #expect(FileManager.default.fileExists(atPath: tempFile.string)) |
| 113 | + #expect(try String(contentsOf: tempFile).lengthOfBytes(using: String.Encoding.utf8) > 0) |
78 | 114 |
|
79 | 115 | reporter.clear()
|
80 | 116 |
|
81 |
| - #expect(!FileManager.default.fileExists(atPath: tempFile.string)) |
| 117 | + #expect(try String(contentsOf: tempFile).lengthOfBytes(using: String.Encoding.utf8) == 0) |
82 | 118 | }
|
83 | 119 |
|
84 | 120 | @Test("Test multiple progress updates create multiple lines")
|
85 |
| - func testMultipleUpdatesCreateMultipleLines() throws { |
| 121 | + func testMultipleUpdatesCreateMultipleLines() async throws { |
86 | 122 | let tempFile = fs.mktemp(ext: ".json")
|
87 |
| - let reporter = JsonFileProgressReporter(filePath: tempFile) |
| 123 | + try await fs.create(.mode(Int(0o644)), file: tempFile) |
| 124 | + defer { try? FileManager.default.removeItem(atPath: tempFile.string) } |
88 | 125 |
|
89 |
| - reporter.update(step: 1, total: 3, text: "Step 1") |
90 |
| - reporter.update(step: 2, total: 3, text: "Step 2") |
91 |
| - reporter.complete(success: true) |
| 126 | + let reporter = try JsonFileProgressReporter(SwiftlyTests.ctx, filePath: tempFile) |
92 | 127 |
|
93 |
| - let fileContent = try String(contentsOfFile: tempFile.string) |
94 |
| - let lines = fileContent.components(separatedBy: .newlines).filter { !$0.isEmpty } |
| 128 | + reporter.update(step: 5, total: 100, text: "Processing item 5") |
| 129 | + reporter.update(step: 10, total: 100, text: "Processing item 10") |
| 130 | + reporter.update(step: 50, total: 100, text: "Processing item 50") |
| 131 | + reporter.update(step: 100, total: 100, text: "Processing item 100") |
95 | 132 |
|
96 |
| - #expect(lines.count == 3) |
97 |
| - #expect(lines[0].contains("Step 1")) |
98 |
| - #expect(lines[1].contains("Step 2")) |
99 |
| - #expect(lines[2].contains("\"success\":true")) |
100 |
| - |
101 |
| - try FileManager.default.removeItem(atPath: tempFile.string) |
102 |
| - } |
103 |
| - |
104 |
| - @Test("Test zero step edge case") |
105 |
| - func testZeroStepEdgeCase() throws { |
106 |
| - let tempFile = fs.mktemp(ext: ".json") |
107 |
| - let reporter = JsonFileProgressReporter(filePath: tempFile) |
108 |
| - |
109 |
| - reporter.update(step: 0, total: 10, text: "Starting") |
110 |
| - |
111 |
| - let fileContent = try String(contentsOfFile: tempFile.string) |
112 |
| - |
113 |
| - #expect(fileContent.contains("\"percent\":0")) |
114 |
| - #expect(fileContent.contains("Starting")) |
115 |
| - |
116 |
| - try FileManager.default.removeItem(atPath: tempFile.string) |
117 |
| - } |
118 |
| - |
119 |
| - @Test("Test full completion edge case") |
120 |
| - func testFullCompletionEdgeCase() throws { |
121 |
| - let tempFile = fs.mktemp(ext: ".json") |
122 |
| - let reporter = JsonFileProgressReporter(filePath: tempFile) |
123 |
| - |
124 |
| - reporter.update(step: 100, total: 100, text: "Done") |
125 |
| - |
126 |
| - let fileContent = try String(contentsOfFile: tempFile.string) |
127 |
| - |
128 |
| - #expect(fileContent.contains("\"percent\":100")) |
129 |
| - #expect(fileContent.contains("Done")) |
130 |
| - |
131 |
| - try FileManager.default.removeItem(atPath: tempFile.string) |
| 133 | + reporter.complete(success: true) |
| 134 | + try? reporter.close() |
| 135 | + |
| 136 | + let decoder = JSONDecoder() |
| 137 | + let info = try String(contentsOfFile: tempFile.string).split(separator: "\n") |
| 138 | + .filter { |
| 139 | + !$0.isEmpty |
| 140 | + }.map { |
| 141 | + try decoder.decode(ProgressInfo.self, from: Data($0.utf8)) |
| 142 | + } |
| 143 | + |
| 144 | + #expect(info.count == 5) |
| 145 | + |
| 146 | + for (idx, pct) in [5, 10, 50, 100].enumerated() { |
| 147 | + if case let .step(_, percent, text) = info[idx] { |
| 148 | + #expect(text == "Processing item \(pct)") |
| 149 | + #expect(percent == pct) |
| 150 | + } else { |
| 151 | + Issue.record("Expected step info but got \(info[idx])") |
| 152 | + return |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if case let .complete(success) = info[4] { |
| 157 | + #expect(success == true) |
| 158 | + } else { |
| 159 | + Issue.record("Expected completion info but got \(info[4])") |
| 160 | + return |
| 161 | + } |
132 | 162 | }
|
133 | 163 | }
|
0 commit comments