63 lines · 2.8 KB · SwiftRaw
// The faces' photos not on this phone are brought down by a seam (Android 6d83189695: no one's photo stood in Comms,// every face fell back to initials, because nothing ever fetched them). `ChatFaces` asks `media.ensure` with each// photo's key one at a time, never in Low Data Mode, never again for a key the core answered, and after a refusal// waits before asking that key again. The faces themselves only read what the phone holds.#if os(iOS)import CottonKitimport Foundationimport XCTest@testable import CottonUIfinal class ChatFacesTests: XCTestCase { /// A core that answers `media.ensure` for invented keys: fetched now, already here, or refused. private actor FaceCore: CottonCalling { private(set) var asked: [String] = [] func callRaw(_ method: String, requestJSON: String) throws -> String { struct Ask: Decodable { let key: String } guard method == CottonMethod.mediaEnsure else { return #"{"ok":false,"error":{"code":"bad_request","message":"no"}}"# } let key = try JSONDecoder().decode(Ask.self, from: Data(requestJSON.utf8)).key asked.append(key) switch key { case "avatars/new": return #"{"ok":true,"data":{"path":"/tmp/face-new.jpg","fetched":true}}"# case "avatars/here": return #"{"ok":true,"data":{"path":"/tmp/face-here.jpg","fetched":false}}"# default: return #"{"ok":false,"error":{"code":"offline","message":"offline"}}"# } } func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) {} func close() {} } @MainActor func testTheListsFacesAreBroughtDownOneByOneAndOnce() async { let core = FaceCore() let faces = ChatFaces() faces.bring(["avatars/new", "avatars/here", "", "avatars/new"], core: core, constrained: false) await faces.settled() faces.bring(["avatars/new", "avatars/here"], core: core, constrained: false) await faces.settled() let asked = await core.asked XCTAssertEqual(asked, ["avatars/new", "avatars/here"], "each photo by its own key, once") } @MainActor func testNothingIsBroughtInLowDataMode() async { let core = FaceCore() let faces = ChatFaces() faces.bring(["avatars/new"], core: core, constrained: true) await faces.settled() let asked = await core.asked XCTAssertEqual(asked, [], "Low Data Mode fetches no faces") } @MainActor func testARefusalWaitsBeforeAskingAgain() async { let core = FaceCore() let faces = ChatFaces() _ = await faces.bring("avatars/offline", core: core) _ = await faces.bring("avatars/offline", core: core) let asked = await core.asked XCTAssertEqual(asked, ["avatars/offline"], "one ask, then its wait") }}#endif