|
| 1 | +// |
| 2 | +// ConnectionStorageSyncDeleteTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import TableProPluginKit |
| 8 | +import Testing |
| 9 | + |
| 10 | +@testable import TablePro |
| 11 | + |
| 12 | +@Suite("ConnectionStorage sync delete ordering") |
| 13 | +@MainActor |
| 14 | +struct ConnectionStorageSyncDeleteTests { |
| 15 | + private let storage: ConnectionStorage |
| 16 | + private let metadata: SyncMetadataStorage |
| 17 | + private let storageDirectory: URL |
| 18 | + |
| 19 | + init() { |
| 20 | + let unique = UUID().uuidString |
| 21 | + let fileURL = FileManager.default.temporaryDirectory |
| 22 | + .appendingPathComponent("tablepro-tests") |
| 23 | + .appendingPathComponent("connections_\(unique).json") |
| 24 | + storageDirectory = fileURL.deletingLastPathComponent() |
| 25 | + try? FileManager.default.createDirectory(at: storageDirectory, withIntermediateDirectories: true) |
| 26 | + |
| 27 | + let defaults = UserDefaults(suiteName: "com.TablePro.tests.ConnectionStorage.\(unique)")! |
| 28 | + let syncDefaults = UserDefaults(suiteName: "com.TablePro.tests.Sync.\(unique)")! |
| 29 | + metadata = SyncMetadataStorage(userDefaults: syncDefaults) |
| 30 | + storage = ConnectionStorage( |
| 31 | + fileURL: fileURL, |
| 32 | + userDefaults: defaults, |
| 33 | + syncTracker: SyncChangeTracker(metadataStorage: metadata) |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + @Test("Deleting a connection records a sync tombstone after it is persisted") |
| 38 | + func deleteRecordsTombstoneAfterPersisting() { |
| 39 | + let connection = TestFixtures.makeConnection() |
| 40 | + storage.addConnection(connection) |
| 41 | + #expect(metadata.tombstones(for: .connection).isEmpty) |
| 42 | + |
| 43 | + storage.deleteConnection(connection) |
| 44 | + |
| 45 | + #expect(metadata.tombstones(for: .connection).contains { $0.id == connection.id.uuidString }) |
| 46 | + } |
| 47 | + |
| 48 | + @Test("A delete that fails to persist records no tombstone (persist before notify)") |
| 49 | + func failedPersistenceRecordsNoTombstone() { |
| 50 | + let connection = TestFixtures.makeConnection() |
| 51 | + storage.addConnection(connection) |
| 52 | + try? FileManager.default.removeItem(at: storageDirectory) |
| 53 | + |
| 54 | + storage.deleteConnection(connection) |
| 55 | + |
| 56 | + #expect(metadata.tombstones(for: .connection).isEmpty) |
| 57 | + } |
| 58 | +} |
0 commit comments