import CottonKit import CottonValues import Foundation import XCTest @testable import CottonUI @MainActor final class WhatsAppMediaTests: XCTestCase { func testWhatsAppMediaStoreKeySurvivesTheRecordBoundary() throws { let value = RecordValue.list([.object([ "key": .text("synthetic/whatsapp/picture.jpg"), "contentType": .text("image/jpeg"), "kind": .text("image") ])]) let json = try JSONDecoder().decode(JSONValue.self, from: JSONEncoder().encode(value)) let entry = MessageMedia(try XCTUnwrap(json.array?.first)) XCTAssertEqual(entry.key, "synthetic/whatsapp/picture.jpg") XCTAssertTrue(entry.isImage) XCTAssertFalse(entry.isAudio) } // A stored WhatsApp picture is drawn from this device's media path; a // thumbnail never starts a download for every message on the screen. func testStoredPictureResolvesLocallyAndMissingBytesStayMissing() async throws { let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) let path = root.appendingPathComponent("picture.png") let png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" try XCTUnwrap(Data(base64Encoded: png)).write(to: path) let reply = try XCTUnwrap(String(data: JSONSerialization.data(withJSONObject: [ "present": true, "path": path.path, "tier": "thumb" ]), encoding: .utf8)) let core = ScriptedCore([CottonMethod.readMediaPath: .ok(reply)]) var slot = MediaImageSlot() let key = "synthetic/whatsapp/\(UUID().uuidString).png" let ask = slot.begin(key) slot.finish(await MediaImages.load(from: core, key: key, edge: 176), for: key, ask: ask) XCTAssertNotNil(slot.image) await core.answer(CottonMethod.readMediaPath, .ok(#"{"present":false}"#)) let missingKey = key + "missing" let missingAsk = slot.begin(missingKey) slot.finish(await MediaImages.load(from: core, key: missingKey, edge: 176), for: missingKey, ask: missingAsk) XCTAssertNil(slot.image) XCTAssertTrue(slot.isAbsent) let methods = await core.methodsCalled() XCTAssertEqual(methods, [CottonMethod.readMediaPath, CottonMethod.readMediaPath]) } }