Cotton code
180 lines · 9.1 KB · SwiftRaw
// KEEP slice 1 and KEEP 1e: where each photo of a kept album is, on this phone (AlbumsStore's kept_album and// kept_item), and Keep's rules that need no phone (KeepRule). Nothing is ever deleted: a photo taken out of the// album, or out of the library, keeps its copy and is counted. Never decided by absence (review M1); kept only on the// object's own answer, per file (review H1).#if os(iOS)import Foundationimport XCTest@testable import CottonUIfinal class AlbumsKeepStoreTests: XCTestCase {    private var url: URL!    private var store: AlbumsStore!    override func setUp() {        url = FileManager.default.temporaryDirectory.appendingPathComponent("albums-keep-\(UUID().uuidString).sqlite")        store = AlbumsStore(url: url)    }    override func tearDown() {        store = nil        for suffix in ["", "-wal", "-shm"] { try? FileManager.default.removeItem(atPath: url.path + suffix) }    }    private func members(_ list: [String], keeping: Bool = true) {        store.setKeptMembers("A", list, keeping: keeping, whole: true)    }    private func uploaded(_ keys: String...) -> [String: KeepRule.File] {        Dictionary(uniqueKeysWithValues: keys.map { ($0, KeepRule.File(key: $0, phase: "uploaded")) })    }    // The keep loop, as the store sees it: waiting, handed over, kept only once the object holds every original.    func testAPhotoIsKeptOnlyWhenTheObjectHoldsEveryOriginalOfIt() {        store.keep("A", title: "Family", cellular: false)        members(["p1", "p2", "p3"])        XCTAssertEqual(store.toKeep(10).map(\.asset).sorted(), ["p1", "p2", "p3"])        store.keptSending("p1", keys: ["k1"], records: ["photo:aa"], bytes: 1_000)        store.keptSending("p2", keys: ["k2", "k2m"], records: ["photo:bb", "video:bm"], bytes: 2_000)        var c = store.keptCounts("A")        XCTAssertEqual([c.waiting, c.sending, c.kept], [1, 2, 0])        XCTAssertEqual(c.bytes, 3_000)        // k2's film is still going up: p2 is not kept, however empty the rest of the queue is.        var files = uploaded("k1", "k2")        files["k2m"] = KeepRule.File(key: "k2m", phase: "uploading", sent: 400, total: 1_000)        let verdict = KeepRule.verdict(store.keptSendingKeys(), files)        XCTAssertEqual(verdict.confirmed, ["p1"])        XCTAssertEqual([verdict.files], [1])        XCTAssertEqual(verdict.bytes, 600)        store.keptConfirmed(verdict.confirmed)        c = store.keptCounts("A")        XCTAssertEqual([c.sending, c.kept], [1, 1])        XCTAssertEqual(store.toKeep(10).map(\.asset), ["p3"])        XCTAssertEqual(store.keptSendingKeys().map(\.asset), ["p2"])    }    // The kept check with a refused file (review H1): could not be kept, with its reason, never tried again on its own.    func testARefusedOriginalIsNotKeptAndSaysWhy() {        store.keep("A", title: "Family", cellular: false)        members(["p1", "p2"])        store.keptSending("p1", keys: ["k1"], records: ["photo:aa"], bytes: 1_000)        store.keptSending("p2", keys: ["k2"], records: ["video:bb"], bytes: 9_000)        var files = uploaded("k1")        files["k2"] = KeepRule.File(key: "k2", phase: "failed", code: "too_big", line: "Too large for Cotton")        let verdict = KeepRule.verdict(store.keptSendingKeys(), files)        XCTAssertEqual(verdict.confirmed, ["p1"])        XCTAssertEqual(verdict.refused, [KeepRule.Refusal(asset: "p2", code: "too_big", why: "Too large for Cotton")])        store.keptConfirmed(verdict.confirmed)        for r in verdict.refused { store.keptRefused(r.asset, code: r.code, why: r.why) }        let c = store.keptCounts("A")        XCTAssertEqual([c.kept, c.refused, c.sending, c.retrying], [1, 1, 0, 0])        XCTAssertEqual(store.keptRefusals("A").map(\.code), ["too_big"])        XCTAssertEqual(store.toKeep(10).count, 0, "a refused photo is not tried again on its own")    }    // A retrying file is neither kept nor refused; one the queue no longer knows is kept again.    func testARetryingFileWaitsAndALostOneIsKeptAgain() {        store.keep("A", title: "Family", cellular: false)        members(["p1", "p2"])        store.keptSending("p1", keys: ["k1"], records: ["photo:aa"], bytes: 1_000)        store.keptSending("p2", keys: ["k2"], records: ["photo:bb"], bytes: 1_000)        let files = ["k1": KeepRule.File(key: "k1", phase: "retrying", total: 1_000)]        let verdict = KeepRule.verdict(store.keptSendingKeys(), files)        XCTAssertEqual(verdict.confirmed, [])        XCTAssertEqual(verdict.refused, [])        XCTAssertEqual(verdict.lost, ["p2"])        store.keptAgain(verdict.lost)        XCTAssertEqual(store.toKeep(10).map(\.asset), ["p2"])    }    // Never decided by absence (review M1): limited access, an album not found, an empty fetch change no row.    func testAReadThatIsNotWholeChangesNothing() {        store.keep("A", title: "Family", cellular: false)        members(["p1", "p2"])        store.keptSending("p1", keys: ["k1"], records: ["photo:aa"], bytes: 1_000)        store.keptConfirmed(["p1"])        let before = store.keptCounts("A")        store.setKeptMembers("A", [], keeping: true, whole: true)        store.setKeptMembers("A", ["p9"], keeping: true, whole: false)        XCTAssertEqual(store.keptCounts("A"), before)        XCTAssertEqual([before.kept, before.waiting, before.left, before.gone], [1, 1, 0, 0])    }    // Left or gone only on the library's word, and only for a photo in Cotton; an unsent one is just unqueued.    func testOnlyTheLibrarysWordMarksAPhotoLeftOrGone() {        store.keep("A", title: "Family", cellular: false)        members(["p1", "p2", "p3", "p4"])        store.keptSending("p1", keys: ["k1"], records: ["photo:aa"], bytes: 1_000)        store.keptConfirmed(["p1"])        store.keptSending("p2", keys: ["k2"], records: ["photo:bb"], bytes: 500)        // A whole read without p1, p2, p3: p3 never reached Cotton and is no longer queued; p1 and p2 stay as they are.        members(["p4"])        var c = store.keptCounts("A")        XCTAssertEqual([c.kept, c.sending, c.waiting, c.left, c.total], [1, 1, 1, 0, 3])        // The library reports p1 and p2 taken out: p1 left (its copy stays), p2 left once the object confirms it.        store.keptRemoved("A", ["p1", "p2"])        c = store.keptCounts("A")        XCTAssertEqual([c.kept, c.left, c.sending], [0, 1, 1])        store.keptConfirmed(["p2"])        c = store.keptCounts("A")        XCTAssertEqual([c.left, c.sending], [2, 0])        XCTAssertEqual(c.bytes, 1_500, "the copies are still counted")        // p1 deleted from the library: gone, its copy stays.        store.keptDeleted(["p1"])        c = store.keptCounts("A")        XCTAssertEqual([c.left, c.gone], [1, 1])        // p2 comes back into the album: checked against Cotton again, never assumed kept.        members(["p2", "p4"])        XCTAssertEqual(store.keptSendingKeys().map(\.asset), ["p2"])        // p3 comes back: it never reached Cotton, so it waits again.        members(["p2", "p3", "p4"])        XCTAssertTrue(store.toKeep(10).map(\.asset).contains("p3"))    }    // Stop keeping: nothing new taken in, every copy stays; the band's "in Cotton" counts only the kept.    func testStopKeepingKeepsEveryCopyAndSendsNothingNew() {        store.keep("A", title: "Family", cellular: false)        members(["p1", "p2"])        store.keptSending("p1", keys: ["k1"], records: ["photo:aa"], bytes: 1_000)        store.stopKeeping("A")        XCTAssertFalse(store.keeping("A"))        XCTAssertEqual(store.toKeep(10).count, 0)        members(["p1", "p2", "p3"], keeping: false)        let c = store.keptCounts("A")        XCTAssertEqual([c.total, c.sending, c.kept], [2, 1, 0], "a photo added after Stop is not taken in")    }    // "Albums tries again" only while it does.    func testAFailedPhotoIsTriedAgainAFewTimesThenNoLongerCountedAsRetrying() {        store.keep("A", title: "Family", cellular: false)        members(["p1"])        store.keptFailed("A", "p1", why: "test")        XCTAssertEqual(store.keptCounts("A").retrying, 1)        for _ in 0..<4 { store.keptFailed("A", "p1", why: "test") }        XCTAssertEqual(store.toKeep(10).count, 0)        let c = store.keptCounts("A")        XCTAssertEqual([c.failed, c.retrying], [1, 0])    }    // The network rule (review M4) and expiry (review M3): a read goes on only on a network Keep may use, while    // nothing stopped it and the album is still kept.    func testTheNetworkRuleAndTheStop() {        XCTAssertEqual(KeepRule.network(cellular: false), "wifi")        XCTAssertEqual(KeepRule.network(cellular: true), "any")        XCTAssertTrue(KeepRule.mayGo(stopped: false, cellular: false, onWiFi: true, keeping: true))        XCTAssertFalse(KeepRule.mayGo(stopped: false, cellular: false, onWiFi: false, keeping: true), "Wi-Fi went: cancel")        XCTAssertTrue(KeepRule.mayGo(stopped: false, cellular: true, onWiFi: false, keeping: true))        XCTAssertFalse(KeepRule.mayGo(stopped: false, cellular: true, onWiFi: true, keeping: false), "Stop keeping: cancel")        let stop = KeepStop()        stop.set(true)        XCTAssertFalse(KeepRule.mayGo(stopped: stop.value, cellular: true, onWiFi: true, keeping: true), "expiry: cancel")    }}#endif