importFoundationimport XCTest@testable importCottonKit/// The phone's data leaves the app group: every entry the core wrote moves to/// the app's own folder, and what the extensions share with the app stays.finalclassPhoneDataDirectoryTests: XCTestCase {privatefunc folder(_ name: String) throws -> URL {let url = URL(fileURLWithPath: NSTemporaryDirectory()) .appendingPathComponent("cotton-data-dir-\(UUID().uuidString)") .appendingPathComponent(name)tryFileManager.default.createDirectory(at: url, withIntermediateDirectories: true) addTeardownBlock { try? FileManager.default.removeItem(at: url.deletingLastPathComponent()) }return url }privatefunc put(_ path: String, in root: URL) throws {let url = root.appendingPathComponent(path)tryFileManager.default.createDirectory( at: url.deletingLastPathComponent(), withIntermediateDirectories: true)tryData(path.utf8).write(to: url) }privatefunc exists(_ path: String, in root: URL) -> Bool {FileManager.default.fileExists(atPath: root.appendingPathComponent(path).path) }// RULE: The phone's database lives in the app's own folder, never the app group's, because iOS kills a suspended app that holds a lock on a file in a shared container (0xDEAD10CC).func testTheCoresEntriesMoveAndTheExtensionsSharesStay() throws {let group = try folder("group")let own = try folder("own")for path in ["client/connection.json", "client/ws-1/replica.db", "client/media/aa/one.jpg","server.log", "cache/thumbs/x.jpg","Library/Preferences/group.plist", "share-inbox/a.pdf", "recordings/act/a.m4a",".com.apple.mobile_container_manager.metadata.plist", ] {try put(path, in: group) }let moved = PhoneDataDirectory.adopt(from: group, into: own) XCTAssertEqual(moved, ["cache", "client", "server.log"])for path in ["client/connection.json", "client/ws-1/replica.db", "client/media/aa/one.jpg", "server.log"] { XCTAssertTrue(exists(path, in: own), path) XCTAssertFalse(exists(path, in: group), path) }for path in ["Library/Preferences/group.plist", "share-inbox/a.pdf", "recordings/act/a.m4a",".com.apple.mobile_container_manager.metadata.plist", ] { XCTAssertTrue(exists(path, in: group), path) XCTAssertFalse(exists(path, in: own), path) } }// RULE: Moving the phone's data out of the app group resumes on the next open and never replaces a folder the app already uses.func testAnOpenKilledHalfWayFinishesNextTimeAndNeverOverwrites() throws {let group = try folder("group")let own = try folder("own")try put("client/ws-1/replica.db", in: own)try put("client/connection.json", in: group)try put("media/x", in: group) XCTAssertEqual(PhoneDataDirectory.adopt(from: group, into: own), ["media"]) XCTAssertTrue(exists("client/connection.json", in: group), "a folder already in use is not replaced") XCTAssertEqual(PhoneDataDirectory.adopt(from: group, into: own), []) }}