Cotton code
93 lines · 6.0 KB · SwiftRaw
import CottonKitimport CottonValuesimport XCTest@testable import CottonUIfinal class StoriesPresentationTests: XCTestCase {    // RULE: A Story in a person section uses its picture caption and posted time, never the portrait-name fallback; its declared media filter still hides dropped bytes.    func testAnInlineStoryUsesTheSamePicturePrintAsItsGrid() throws {        let loaded = FixtureDefinitions.load(from: try TestResources.bundle())        let definition = try XCTUnwrap(loaded.definitions.first { $0.id == "story" })        let picture = StoriesPresentation.pictureDefinition(definition)        let dropped = TableRecord(id: "invented", values: ["caption": .text("The garden after rain"), "media_key": .text("invented/garden.png"), "media_dropped_at": .text("2026-10-07T12:00:00Z")])        let shown = StoriesPresentation.displayed(dropped, definition: picture)        XCTAssertTrue(shown["media_key"].isNull)        XCTAssertEqual(shown["caption"], dropped["caption"])        XCTAssertEqual(shown.id, dropped.id)    }    private func story(_ id: String, person: String? = nil, sender: String = "leah.garden", posted: String,        media: String? = nil, dropped: String? = nil, kept: Bool = false) -> TableRecord {        TableRecord(id: id, values: [            "person_id": person.map(RecordValue.text) ?? .null,            "sender": .text(sender), "sender_label": .text("Leah Alder"),            "source": .text("instagram"), "account": .text("mira.garden"),            "posted_at": .text(posted), "media_key": media.map(RecordValue.text) ?? .null,            "media_dropped_at": dropped.map(RecordValue.text) ?? .null, "kept": .bool(kept),        ])    }    // RULE: Stories from one linked person stay together, newest first; a newer story from someone else does not split their strip.    func testLinkedPeopleHaveOneStripInNewestOrder() {        let older = story("leah-old", person: "leah", posted: "2026-10-07T10:00:00Z")        let other = story("noor", person: "noor", posted: "2026-10-07T11:00:00Z")        let newer = story("leah-new", person: "leah", posted: "2026-10-07T12:00:00Z")        let groups = StoriesPresentation.groups([older, other, newer])        XCTAssertEqual(groups.map(\.id), [.person("leah"), .person("noor")])        XCTAssertEqual(groups[0].records.map(\.id), ["leah-new", "leah-old"])        XCTAssertEqual(groups[0].filter, ["person_id": .text("leah")])    }    // RULE: A sender's display name never links them to People or combines two unknown senders with the same name.    func testUnknownSendersKeepTheirOwnIdentityAndStripFilter() {        let first = story("first", sender: "leah.garden", posted: "2026-10-07T12:00:00Z")        let second = story("second", sender: "leah.kitchen", posted: "2026-10-07T11:00:00Z")        let groups = StoriesPresentation.groups([first, second])        XCTAssertEqual(groups.count, 2)        XCTAssertEqual(groups[0].filter, ["source": .text("instagram"), "account": .text("mira.garden"), "sender": .text("leah.garden")])        XCTAssertNotEqual(groups[0].id, groups[1].id)    }    // RULE: If the reader omits the account, two unknown rows must not be joined by a handle that could belong to separate source accounts.    func testMissingAccountKeepsUnknownRowsSeparate() {        var first = story("first", posted: "2026-10-07T12:00:00Z")        var second = story("second", posted: "2026-10-07T11:00:00Z")        first.values["account"] = .null        second.values["account"] = .null        let groups = StoriesPresentation.groups([first, second])        XCTAssertEqual(groups.map(\.id), [.unassigned("first"), .unassigned("second")])        XCTAssertEqual(groups[0].filter, ["id": .text("first")])    }    // RULE: A person's opening is a strip; a general filter such as Kept still shows the people grid.    func testOnlyOneIdentityOpensAsAStrip() {        XCTAssertTrue(StoriesPresentation.isStrip(["person_id": .text("leah")]))        XCTAssertTrue(StoriesPresentation.isStrip(["source": .text("instagram"), "account": .text("mira.garden"), "sender": .text("leah.garden")]))        XCTAssertFalse(StoriesPresentation.isStrip(["kept": .bool(true)]))        XCTAssertFalse(StoriesPresentation.isStrip(["person_id": .list([.text("leah"), .text("noor")])]))        XCTAssertFalse(StoriesPresentation.isStrip(["sender_label": .text("Leah Alder")]))    }    // RULE: A dropped story's picture stays hidden even if its retained key still names cached bytes, or Keep is pressed afterwards.    func testDroppedMediaIsNeverDrawnFromItsRetainedKey() {        let live = story("live", posted: "2026-10-07T12:00:00Z", media: "file/messages/statuses/garden.jpg")        let dropped = story("dropped", posted: "2026-10-07T12:00:00Z", media: "file/messages/statuses/garden.jpg", dropped: "2026-10-08T12:00:00Z", kept: true)        XCTAssertEqual(StoriesPresentation.mediaKey(of: live), "file/messages/statuses/garden.jpg")        XCTAssertNil(StoriesPresentation.mediaKey(of: dropped))        XCTAssertEqual(dropped["media_key"], live["media_key"], "hiding a picture must not rewrite its retained story row")    }    // RULE: A dropped story's page hides both the picture and the playable file, while keeping its caption, identity and Keep state.    func testDroppedPageHidesEveryMediaSlotWithoutChangingTheRow() {        var row = story("garden-film", posted: "2026-10-07T12:00:00Z", media: "file/messages/statuses/garden.jpg", dropped: "2026-10-08T12:00:00Z", kept: true)        row.values["film"] = .text("file/messages/statuses/garden.mp4")        row.values["caption"] = .text("Leah's garden after the rain")        let shown = StoriesPresentation.displayed(row, mediaColumns: ["media_key", "film"])        XCTAssertTrue(shown["media_key"].isNull)        XCTAssertTrue(shown["film"].isNull)        XCTAssertEqual(shown["caption"], row["caption"])        XCTAssertEqual(shown["kept"], .bool(true))        XCTAssertEqual(shown.id, row.id)        XCTAssertEqual(row["film"], .text("file/messages/statuses/garden.mp4"))    }}