#if os(iOS)importCottonKitimport XCTest@testable importCottonUI/// Where the music player finds a song's bytes (`MusicFiles`).finalclassMusicFilesTests: XCTestCase {/// RULE: a song not on this phone is fetched and opened by the core (`media.ensure`), never read from the object's signed address: a person's song is kept sealed there (R6) and the decoder cannot read it.func testASongNotHereIsFetchedAndOpenedByTheCore() asyncthrows {let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString, isDirectory: true)tryFileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)defer { try? FileManager.default.removeItem(at: folder) }let opened = folder.appendingPathComponent("0f0f")let core = SongsCore(opened: opened)let key = "db/music/0f0f.wav"let url = tryawaitMusicFiles.url(for: key, core: core).get() XCTAssertEqual(url.pathExtension, "wav", "the decoder is handed the song's own extension") XCTAssertEqual(tryData(contentsOf: url), Data("the opened song".utf8)) XCTAssertEqual(core.asked, [CottonMethod.readMediaPath, CottonMethod.mediaEnsure])// Here now: read where it is, nothing fetched again. _ = tryawaitMusicFiles.url(for: key, core: core).get() XCTAssertEqual(core.asked.filter { $0 == CottonMethod.mediaEnsure }.count, 1) XCTAssertFalse(core.asked.contains(CottonMethod.mediaStream), "the signed address is never the player's") }/// RULE: a fetch that reached nothing is offline; any other refusal (a seal that does not open, a file not there) is unreadable.func testWhatCouldNotBeFetched() async {let nowhere = URL(fileURLWithPath: "/nowhere/at/all")let offline = awaitMusicFiles.url(for: "db/music/aa.mp3", core: SongsCore(opened: nowhere, refusal: "unreachable_server")) XCTAssertEqual(offline.failure, .offline)let sealed = awaitMusicFiles.url(for: "db/music/aa.mp3", core: SongsCore(opened: nowhere, refusal: "seal_key_missing")) XCTAssertEqual(sealed.failure, .unreadable) }}privateextensionResult {var failure: Failure? { ifcase .failure(let why) = self { why } else { nil } }}/// The core's `read.mediaPath` and `media.ensure` for one song: not here/// until ensured, which lands the opened file.privatefinalclassSongsCore: CottonCalling, @unchecked Sendable {privatelet opened: URLprivatelet refusal: String?private(set) var asked: [String] = []init(opened: URL, refusal: String? = nil) {self.opened = openedself.refusal = refusal }func callRaw(_ method: String, requestJSON: String) asyncthrows -> String { asked.append(method)switch method {caseCottonMethod.mediaEnsure:iflet refusal { return #"{"ok":false,"error":{"code":"\#(refusal)","message":"not fetched"}}"# }tryData("the opened song".utf8).write(to: opened)return #"{"ok":true,"data":{"path":"\#(opened.path)","fetched":true}}"#caseCottonMethod.readMediaPath whereFileManager.default.fileExists(atPath: opened.path):return #"{"ok":true,"data":{"path":"\#(opened.path)"}}"#default:return #"{"ok":true,"data":{}}"# } }func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) {}func close() {}}#endif