|
| 1 | +// |
| 2 | +// RemoteDatabaseFileCorrectnessTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import Testing |
| 8 | + |
| 9 | +@testable import TablePro |
| 10 | + |
| 11 | +struct RemoteDatabaseFileCorrectnessTests { |
| 12 | + private func temporaryDirectory() throws -> URL { |
| 13 | + let url = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString, isDirectory: true) |
| 14 | + try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true) |
| 15 | + return url |
| 16 | + } |
| 17 | + |
| 18 | + // MARK: - Fingerprint |
| 19 | + |
| 20 | + @Test("A commit that moves only the write-ahead log's mtime is a change") |
| 21 | + func walModifiedIsAChange() { |
| 22 | + let base = Date(timeIntervalSince1970: 1_000_000) |
| 23 | + let recorded = RemoteFileFingerprint( |
| 24 | + mainSize: 32_768, mainModified: base, writeAheadLogSize: 234_872, writeAheadLogModified: base |
| 25 | + ) |
| 26 | + let current = RemoteFileFingerprint( |
| 27 | + mainSize: 32_768, mainModified: base, writeAheadLogSize: 234_872, writeAheadLogModified: base.addingTimeInterval(5) |
| 28 | + ) |
| 29 | + #expect(current.differs(from: recorded)) |
| 30 | + } |
| 31 | + |
| 32 | + @Test("An identical fingerprint including the log mtime is not a change") |
| 33 | + func identicalWithLogMtimeIsNotAChange() { |
| 34 | + let base = Date(timeIntervalSince1970: 1_000_000) |
| 35 | + let a = RemoteFileFingerprint(mainSize: 4_096, mainModified: base, writeAheadLogSize: 100, writeAheadLogModified: base) |
| 36 | + let b = RemoteFileFingerprint(mainSize: 4_096, mainModified: base, writeAheadLogSize: 100, writeAheadLogModified: base) |
| 37 | + #expect(!a.differs(from: b)) |
| 38 | + } |
| 39 | + |
| 40 | + // MARK: - Manifest codec |
| 41 | + |
| 42 | + @Test("A manifest written before the log mtime field decodes with nil") |
| 43 | + func manifestWithoutLogMtimeDecodesNil() throws { |
| 44 | + let json = Data(""" |
| 45 | + {"origin":"u@h:/p","username":"u","host":"h","port":22,"remotePath":"/p", |
| 46 | + "fetchedAt":"2026-01-01T00:00:00Z","remoteSize":4096,"remoteModified":"2026-01-01T00:00:00Z", |
| 47 | + "downloadedSHA256":"abc","snapshotMethod":"directCopy"} |
| 48 | + """.utf8) |
| 49 | + let decoder = JSONDecoder() |
| 50 | + decoder.dateDecodingStrategy = .iso8601 |
| 51 | + let manifest = try decoder.decode(RemoteFileManifest.self, from: json) |
| 52 | + #expect(manifest.remoteWriteAheadLogModified == nil) |
| 53 | + #expect(manifest.fingerprint.writeAheadLogModified == nil) |
| 54 | + } |
| 55 | + |
| 56 | + // MARK: - Stale sidecar clearing |
| 57 | + |
| 58 | + @Test("A snapshot fetch clears a stale write-ahead log and shared-memory index") |
| 59 | + func snapshotClearsStaleSidecars() throws { |
| 60 | + let directory = try temporaryDirectory() |
| 61 | + let fileName = "app.db" |
| 62 | + for suffix in ["", "-wal", "-shm"] { |
| 63 | + try Data("x".utf8).write(to: directory.appendingPathComponent(fileName + suffix)) |
| 64 | + } |
| 65 | + RemoteDatabaseFileTransfer.clearStaleSidecars( |
| 66 | + layout: .sqliteFamily, |
| 67 | + plan: .remoteSnapshot(executable: "sqlite3"), |
| 68 | + destinationDirectory: directory, |
| 69 | + fileName: fileName |
| 70 | + ) |
| 71 | + #expect(FileManager.default.fileExists(atPath: directory.appendingPathComponent(fileName).path)) |
| 72 | + #expect(!FileManager.default.fileExists(atPath: directory.appendingPathComponent(fileName + "-wal").path)) |
| 73 | + #expect(!FileManager.default.fileExists(atPath: directory.appendingPathComponent(fileName + "-shm").path)) |
| 74 | + } |
| 75 | + |
| 76 | + @Test("A direct copy keeps the log it fetched and clears the shared-memory index") |
| 77 | + func directCopyKeepsFetchedLog() throws { |
| 78 | + let directory = try temporaryDirectory() |
| 79 | + let fileName = "app.db" |
| 80 | + for suffix in ["", "-wal", "-shm"] { |
| 81 | + try Data("x".utf8).write(to: directory.appendingPathComponent(fileName + suffix)) |
| 82 | + } |
| 83 | + RemoteDatabaseFileTransfer.clearStaleSidecars( |
| 84 | + layout: .sqliteFamily, |
| 85 | + plan: .directCopy(sidecars: ["-wal"]), |
| 86 | + destinationDirectory: directory, |
| 87 | + fileName: fileName |
| 88 | + ) |
| 89 | + #expect(FileManager.default.fileExists(atPath: directory.appendingPathComponent(fileName + "-wal").path)) |
| 90 | + #expect(!FileManager.default.fileExists(atPath: directory.appendingPathComponent(fileName + "-shm").path)) |
| 91 | + } |
| 92 | + |
| 93 | + // MARK: - Killed remote command |
| 94 | + |
| 95 | + @Test("A signal-killed remote command does not report success") |
| 96 | + func signalKilledCommandIsNotSuccess() { |
| 97 | + let killed = RemoteCommandResult(exitStatus: 0, standardOutput: "", standardError: "", exitSignal: "KILL") |
| 98 | + #expect(!killed.succeeded) |
| 99 | + let clean = RemoteCommandResult(exitStatus: 0, standardOutput: "ok", standardError: "", exitSignal: nil) |
| 100 | + #expect(clean.succeeded) |
| 101 | + } |
| 102 | + |
| 103 | + // MARK: - Prune |
| 104 | + |
| 105 | + @Test("An abandoned working copy is removed and a recently used one is kept") |
| 106 | + func pruneRemovesOnlyStaleCopies() throws { |
| 107 | + let root = try temporaryDirectory() |
| 108 | + let fresh = root.appendingPathComponent("fresh", isDirectory: true) |
| 109 | + let stale = root.appendingPathComponent("stale", isDirectory: true) |
| 110 | + try FileManager.default.createDirectory(at: fresh, withIntermediateDirectories: true) |
| 111 | + try FileManager.default.createDirectory(at: stale, withIntermediateDirectories: true) |
| 112 | + |
| 113 | + let now = Date(timeIntervalSince1970: 2_000_000_000) |
| 114 | + let maxAge: TimeInterval = 30 * 24 * 60 * 60 |
| 115 | + try FileManager.default.setAttributes([.modificationDate: now], ofItemAtPath: fresh.path) |
| 116 | + try FileManager.default.setAttributes( |
| 117 | + [.modificationDate: now.addingTimeInterval(-(maxAge + 86_400))], ofItemAtPath: stale.path |
| 118 | + ) |
| 119 | + |
| 120 | + RemoteDatabaseFileStore.pruneAbandoned(in: root, olderThan: maxAge, now: now) |
| 121 | + |
| 122 | + #expect(FileManager.default.fileExists(atPath: fresh.path)) |
| 123 | + #expect(!FileManager.default.fileExists(atPath: stale.path)) |
| 124 | + } |
| 125 | +} |
0 commit comments