|
| 1 | +// |
| 2 | +// MockCloudKitServiceTests.swift |
| 3 | +// BushelCloud |
| 4 | +// |
| 5 | +// Created by Leo Dion. |
| 6 | +// Copyright © 2025 BrightDigit. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person |
| 9 | +// obtaining a copy of this software and associated documentation |
| 10 | +// files (the "Software"), to deal in the Software without |
| 11 | +// restriction, including without limitation the rights to use, |
| 12 | +// copy, modify, merge, publish, distribute, sublicense, and/or |
| 13 | +// sell copies of the Software, and to permit persons to whom the |
| 14 | +// Software is furnished to do so, subject to the following |
| 15 | +// conditions: |
| 16 | +// |
| 17 | +// The above copyright notice and this permission notice shall be |
| 18 | +// included in all copies or substantial portions of the Software. |
| 19 | +// |
| 20 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 22 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 28 | +// |
| 29 | + |
| 30 | +import Foundation |
| 31 | +import MistKit |
| 32 | +import Testing |
| 33 | +@testable import BushelCloudKit |
| 34 | + |
| 35 | +// MARK: - Mock CloudKit Service Tests |
| 36 | + |
| 37 | +@Suite("Mock CloudKit Service Tests") |
| 38 | +struct MockCloudKitServiceTests { |
| 39 | + @Test("Query returns empty array initially") |
| 40 | + func testQueryEmptyInitially() async throws { |
| 41 | + let service = MockCloudKitService() |
| 42 | + |
| 43 | + let results = try await service.queryRecords(recordType: "RestoreImage") |
| 44 | + |
| 45 | + #expect(results.isEmpty) |
| 46 | + } |
| 47 | + |
| 48 | + @Test("Create operation stores record") |
| 49 | + func testCreateOperationStoresRecord() async throws { |
| 50 | + let service = MockCloudKitService() |
| 51 | + let record = TestFixtures.sonoma14_2_1 |
| 52 | + |
| 53 | + let operation = RecordOperation( |
| 54 | + operationType: .create, |
| 55 | + recordType: "RestoreImage", |
| 56 | + recordName: "RestoreImage-\(record.buildNumber)", |
| 57 | + fields: record.toCloudKitFields() |
| 58 | + ) |
| 59 | + |
| 60 | + try await service.executeBatchOperations([operation], recordType: "RestoreImage") |
| 61 | + |
| 62 | + let storedRecords = await service.getStoredRecords(ofType: "RestoreImage") |
| 63 | + #expect(storedRecords.count == 1) |
| 64 | + #expect(storedRecords[0].recordName == "RestoreImage-23C71") |
| 65 | + } |
| 66 | + |
| 67 | + @Test("ForceReplace operation replaces existing record") |
| 68 | + func testForceReplaceOperation() async throws { |
| 69 | + let service = MockCloudKitService() |
| 70 | + let recordName = "RestoreImage-23C71" |
| 71 | + |
| 72 | + // Create initial record |
| 73 | + let initialRecord = TestFixtures.sonoma14_2_1 |
| 74 | + let createOp = RecordOperation( |
| 75 | + operationType: .create, |
| 76 | + recordType: "RestoreImage", |
| 77 | + recordName: recordName, |
| 78 | + fields: initialRecord.toCloudKitFields() |
| 79 | + ) |
| 80 | + try await service.executeBatchOperations([createOp], recordType: "RestoreImage") |
| 81 | + |
| 82 | + // Replace with updated record |
| 83 | + let updatedRecord = RestoreImageRecord( |
| 84 | + version: "14.2.1 Updated", |
| 85 | + buildNumber: "23C71", |
| 86 | + releaseDate: initialRecord.releaseDate, |
| 87 | + downloadURL: initialRecord.downloadURL, |
| 88 | + fileSize: 99999, |
| 89 | + sha256Hash: initialRecord.sha256Hash, |
| 90 | + sha1Hash: initialRecord.sha1Hash, |
| 91 | + isSigned: false, |
| 92 | + isPrerelease: false, |
| 93 | + source: "updated-source", |
| 94 | + notes: "Updated record", |
| 95 | + sourceUpdatedAt: nil |
| 96 | + ) |
| 97 | + |
| 98 | + let replaceOp = RecordOperation( |
| 99 | + operationType: .forceReplace, |
| 100 | + recordType: "RestoreImage", |
| 101 | + recordName: recordName, |
| 102 | + fields: updatedRecord.toCloudKitFields() |
| 103 | + ) |
| 104 | + try await service.executeBatchOperations([replaceOp], recordType: "RestoreImage") |
| 105 | + |
| 106 | + // Verify only one record exists with updated data |
| 107 | + let storedRecords = await service.getStoredRecords(ofType: "RestoreImage") |
| 108 | + #expect(storedRecords.count == 1) |
| 109 | + |
| 110 | + let storedFields = storedRecords[0].fields |
| 111 | + if case .int64(let fileSize) = storedFields["fileSize"] { |
| 112 | + #expect(fileSize == 99999) |
| 113 | + } else { |
| 114 | + Issue.record("fileSize field not found or wrong type") |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Test("Delete operation removes record") |
| 119 | + func testDeleteOperation() async throws { |
| 120 | + let service = MockCloudKitService() |
| 121 | + let recordName = "RestoreImage-23C71" |
| 122 | + |
| 123 | + // Create record |
| 124 | + let record = TestFixtures.sonoma14_2_1 |
| 125 | + let createOp = RecordOperation( |
| 126 | + operationType: .create, |
| 127 | + recordType: "RestoreImage", |
| 128 | + recordName: recordName, |
| 129 | + fields: record.toCloudKitFields() |
| 130 | + ) |
| 131 | + try await service.executeBatchOperations([createOp], recordType: "RestoreImage") |
| 132 | + |
| 133 | + // Delete record |
| 134 | + let deleteOp = RecordOperation( |
| 135 | + operationType: .delete, |
| 136 | + recordType: "RestoreImage", |
| 137 | + recordName: recordName |
| 138 | + ) |
| 139 | + try await service.executeBatchOperations([deleteOp], recordType: "RestoreImage") |
| 140 | + |
| 141 | + // Verify record is gone |
| 142 | + let storedRecords = await service.getStoredRecords(ofType: "RestoreImage") |
| 143 | + #expect(storedRecords.isEmpty) |
| 144 | + } |
| 145 | + |
| 146 | + @Test("Batch operations process multiple records") |
| 147 | + func testBatchOperations() async throws { |
| 148 | + let service = MockCloudKitService() |
| 149 | + |
| 150 | + let operations = [ |
| 151 | + RecordOperation( |
| 152 | + operationType: .create, |
| 153 | + recordType: "RestoreImage", |
| 154 | + recordName: "RestoreImage-23C71", |
| 155 | + fields: TestFixtures.sonoma14_2_1.toCloudKitFields() |
| 156 | + ), |
| 157 | + RecordOperation( |
| 158 | + operationType: .create, |
| 159 | + recordType: "RestoreImage", |
| 160 | + recordName: "RestoreImage-24A5264n", |
| 161 | + fields: TestFixtures.sequoia15_0_beta.toCloudKitFields() |
| 162 | + ), |
| 163 | + RecordOperation( |
| 164 | + operationType: .create, |
| 165 | + recordType: "XcodeVersion", |
| 166 | + recordName: "XcodeVersion-15C65", |
| 167 | + fields: TestFixtures.xcode15_1.toCloudKitFields() |
| 168 | + ), |
| 169 | + ] |
| 170 | + |
| 171 | + try await service.executeBatchOperations( |
| 172 | + Array(operations[0...1]), |
| 173 | + recordType: "RestoreImage" |
| 174 | + ) |
| 175 | + try await service.executeBatchOperations([operations[2]], recordType: "XcodeVersion") |
| 176 | + |
| 177 | + let restoreImages = await service.getStoredRecords(ofType: "RestoreImage") |
| 178 | + let xcodeVersions = await service.getStoredRecords(ofType: "XcodeVersion") |
| 179 | + |
| 180 | + #expect(restoreImages.count == 2) |
| 181 | + #expect(xcodeVersions.count == 1) |
| 182 | + } |
| 183 | + |
| 184 | + @Test("Query error throws expected error") |
| 185 | + func testQueryError() async { |
| 186 | + let service = MockCloudKitService() |
| 187 | + await service.setShouldFailQuery(true) |
| 188 | + await service.setQueryError(MockCloudKitError.networkError) |
| 189 | + |
| 190 | + do { |
| 191 | + _ = try await service.queryRecords(recordType: "RestoreImage") |
| 192 | + Issue.record("Expected error to be thrown") |
| 193 | + } catch is MockCloudKitError { |
| 194 | + // Success - error was thrown as expected |
| 195 | + } catch { |
| 196 | + Issue.record("Unexpected error type: \(error)") |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + @Test("Modify error throws expected error") |
| 201 | + func testModifyError() async { |
| 202 | + let service = MockCloudKitService() |
| 203 | + await service.setShouldFailModify(true) |
| 204 | + await service.setModifyError(MockCloudKitError.authenticationFailed) |
| 205 | + |
| 206 | + let operation = RecordOperation( |
| 207 | + operationType: .create, |
| 208 | + recordType: "RestoreImage", |
| 209 | + recordName: "test", |
| 210 | + fields: TestFixtures.sonoma14_2_1.toCloudKitFields() |
| 211 | + ) |
| 212 | + |
| 213 | + do { |
| 214 | + try await service.executeBatchOperations([operation], recordType: "RestoreImage") |
| 215 | + Issue.record("Expected error to be thrown") |
| 216 | + } catch is MockCloudKitError { |
| 217 | + // Success - error was thrown as expected |
| 218 | + } catch { |
| 219 | + Issue.record("Unexpected error type: \(error)") |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + @Test("Operation history tracks all operations") |
| 224 | + func testOperationHistory() async throws { |
| 225 | + let service = MockCloudKitService() |
| 226 | + |
| 227 | + let batch1 = [ |
| 228 | + RecordOperation( |
| 229 | + operationType: .create, |
| 230 | + recordType: "RestoreImage", |
| 231 | + recordName: "test1", |
| 232 | + fields: TestFixtures.sonoma14_2_1.toCloudKitFields() |
| 233 | + ) |
| 234 | + ] |
| 235 | + |
| 236 | + let batch2 = [ |
| 237 | + RecordOperation( |
| 238 | + operationType: .create, |
| 239 | + recordType: "XcodeVersion", |
| 240 | + recordName: "test2", |
| 241 | + fields: TestFixtures.xcode15_1.toCloudKitFields() |
| 242 | + ) |
| 243 | + ] |
| 244 | + |
| 245 | + try await service.executeBatchOperations(batch1, recordType: "RestoreImage") |
| 246 | + try await service.executeBatchOperations(batch2, recordType: "XcodeVersion") |
| 247 | + |
| 248 | + let history = await service.getOperationHistory() |
| 249 | + #expect(history.count == 2) |
| 250 | + #expect(history[0].count == 1) |
| 251 | + #expect(history[1].count == 1) |
| 252 | + } |
| 253 | + |
| 254 | + @Test("Clear storage removes all records") |
| 255 | + func testClearStorage() async throws { |
| 256 | + let service = MockCloudKitService() |
| 257 | + |
| 258 | + // Add some records |
| 259 | + let operation = RecordOperation( |
| 260 | + operationType: .create, |
| 261 | + recordType: "RestoreImage", |
| 262 | + recordName: "test", |
| 263 | + fields: TestFixtures.sonoma14_2_1.toCloudKitFields() |
| 264 | + ) |
| 265 | + try await service.executeBatchOperations([operation], recordType: "RestoreImage") |
| 266 | + |
| 267 | + // Clear storage |
| 268 | + await service.clearStorage() |
| 269 | + |
| 270 | + // Verify everything is cleared |
| 271 | + let storedRecords = await service.getStoredRecords(ofType: "RestoreImage") |
| 272 | + let history = await service.getOperationHistory() |
| 273 | + |
| 274 | + #expect(storedRecords.isEmpty) |
| 275 | + #expect(history.isEmpty) |
| 276 | + } |
| 277 | +} |
| 278 | + |
| 279 | +// MARK: - Helper Extensions for Actor |
| 280 | + |
| 281 | +extension MockCloudKitService { |
| 282 | + func setShouldFailQuery(_ value: Bool) { |
| 283 | + self.shouldFailQuery = value |
| 284 | + } |
| 285 | + |
| 286 | + func setShouldFailModify(_ value: Bool) { |
| 287 | + self.shouldFailModify = value |
| 288 | + } |
| 289 | + |
| 290 | + func setQueryError(_ error: (any Error)?) { |
| 291 | + self.queryError = error |
| 292 | + } |
| 293 | + |
| 294 | + func setModifyError(_ error: (any Error)?) { |
| 295 | + self.modifyError = error |
| 296 | + } |
| 297 | +} |
0 commit comments