Cotton code
152 lines · 6.2 KB · SwiftRaw
// A new KEY drops the picture; a re-ask over the SAME key keeps it on screen.//// THE DEFECT THIS EXISTS FOR. Every picture in the Work lane began its `.task`// with `state = .resolving`, and `.task(id:)` re-runs on a pop-back with the// very same id — so returning to a project or a person's page// blanked pictures that were already decoded and drawn, and the person watched// the screen they had just left flash empty and fill again.//// The rule is `PhotosPicture`'s, which had it right; `MediaImageSlot` is that// rule as a value, so the six Work-lane callers cannot each forget it.import CoreGraphicsimport Foundation#if canImport(UIKit)import UIKit#endifimport XCTest@testable import CottonUIfinal class MediaImageSlotTests: XCTestCase {    /// A 1×1 image — this is about identity and state, not about pixels.    ///    /// Drawn through CoreGraphics rather than `UIGraphicsImageRenderer`, which    /// is UIKit's alone: this target builds for the Mac too, where `UIImage` is    /// CottonUI's own typealias for `NSImage` (`Platform/AppKitAnswers.swift`),    /// and a test of a pure state machine should run on both.    private func picture() -> UIImage {        let context = CGContext(            data: nil,            width: 1,            height: 1,            bitsPerComponent: 8,            bytesPerRow: 4,            space: CGColorSpaceCreateDeviceRGB(),            bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue        )!        context.setFillColor(red: 0, green: 0, blue: 0, alpha: 1)        context.fill(CGRect(x: 0, y: 0, width: 1, height: 1))        let made = context.makeImage()!        #if canImport(UIKit)            return UIImage(cgImage: made)        #else            return UIImage(cgImage: made, size: CGSize(width: 1, height: 1))        #endif    }    func testAFirstAskResolves() {        var slot = MediaImageSlot()        let askA = slot.begin("a")        XCTAssertNil(slot.image)        slot.finish(.ready(picture()), for: "a", ask: askA)        XCTAssertNotNil(slot.image)    }    func testAReAskOverTheSameKeyKeepsThePictureOnScreen() {        var slot = MediaImageSlot()        let askA = slot.begin("a")        slot.finish(.ready(picture()), for: "a", ask: askA)        let drawn = slot.image        // The pop-back: the same key, asked again.        slot.begin("a")        XCTAssertTrue(            slot.image === drawn,            "a re-ask over the same key dropped the picture — this is the blink"        )    }    func testANewKeyDropsThePictureAtOnce() {        var slot = MediaImageSlot()        let askA = slot.begin("a")        slot.finish(.ready(picture()), for: "a", ask: askA)        slot.begin("b")        XCTAssertNil(slot.image, "a different key is a different picture — keeping it would be a lie")    }    func testALateAnswerForAKeyTheViewHasMovedOnFromIsDropped() {        // A fast scroll re-uses the view, and two asks overlap. Assigning the        // state directly, the SLOWER one used to win and paint the wrong        // picture into the row.        var slot = MediaImageSlot()        let askA = slot.begin("a")        slot.begin("b")        slot.finish(.ready(picture()), for: "a", ask: askA)        XCTAssertNil(slot.image)        XCTAssertFalse(slot.isAbsent, "the answer for `a` should not have settled `b` either")    }    func testAnAbsenceIsAStateTheCallerCanDraw() {        var slot = MediaImageSlot()        let askA = slot.begin("a")        slot.finish(.absent, for: "a", ask: askA)        XCTAssertTrue(slot.isAbsent)        XCTAssertNil(slot.image)    }    func testAnEmptyKeyIsStillAKeyAndStillSettles() {        // The callers pass `nil` or `""` for "this row has no picture" and        // finish `.absent` against it; that must not be mistaken for a late        // answer to some other key.        var slot = MediaImageSlot()        let askNil = slot.begin(nil)        slot.finish(.absent, for: nil, ask: askNil)        XCTAssertTrue(slot.isAbsent)    }    /// B7: a person keeps their name card until their own print has decoded.    /// A key alone, an absent file, and a previous person's ready image are    /// all insufficient, even before SwiftUI starts the next keyed task.    func testAPrintIsAvailableOnlyForItsCurrentKeyAndDecodedBytes() {        var slot = MediaImageSlot()        XCTAssertNil(slot.image(for: "a"))        let askA = slot.begin("a")        XCTAssertNil(slot.image(for: "a"))        slot.finish(.absent, for: "a", ask: askA)        XCTAssertNil(slot.image(for: "a"))        let made = picture()        slot.finish(.ready(made), for: "a", ask: askA)        XCTAssertTrue(slot.image(for: "a") === made)        XCTAssertNil(slot.image(for: "b"), "a changed record must not wear the previous face while its task starts")        XCTAssertNil(slot.image(for: nil))        XCTAssertNil(slot.image(for: ""))        slot.begin("b")        slot.finish(.ready(made), for: "a", ask: askA)        XCTAssertNil(slot.image(for: "b"), "a late decode must not replace the new person's card")    }    func testAPrintArrivalReplacesTheCardAndASameKeyReaskKeepsThePrint() {        var slot = MediaImageSlot()        var askA = slot.begin("a")        slot.finish(.absent, for: "a", ask: askA)        XCTAssertNil(slot.image(for: "a"))        let made = picture()        askA = slot.begin("a")        slot.finish(.ready(made), for: "a", ask: askA)        slot.begin("a")        XCTAssertTrue(slot.image(for: "a") === made, "returning from a page must not flash the name card over a kept print")    }    /// GUARD: an older ask over the SAME key never settles the slot — the bell rings while a tile's first read is in flight, that read answers absent (it looked before the bytes landed), and only the newer read's picture is kept (sharing2 walk, 16 Oct 2026: a shared database's tiles stayed grey until tapped).    func testAnOlderAskOverTheSameKeyNeverSettlesIt() {        var slot = MediaImageSlot()        let first = slot.begin("a")        let bell = slot.begin("a")        let made = picture()        slot.finish(.ready(made), for: "a", ask: bell)        slot.finish(.absent, for: "a", ask: first)        XCTAssertTrue(slot.image(for: "a") === made, "the first read's late absent turned the tile grey")    }}