#if os(iOS) import SwiftUI import UIKit import XCTest @testable import CottonUI /// What the fan keeps of each database over a switch (lane stalepic, 15 Oct /// 2026), now that nothing covers one: a switch reads the new database's /// Home before it turns (295 A). @MainActor final class StalePagesTests: XCTestCase { /// RULE: Only the database shown keeps its page pictures decoded; every other database keeps its pictures as compressed bytes alone, over the switch (fan3: decoded pictures beyond reach are what grew memory). func testOnlyTheShownDatabaseKeepsDecodedPictures() async { let stale = StalePages() let factory = stale.pictures(of: "factory") { PagePictures(quality: 0.8, reach: 4, scale: 2) } await factory.keep(Self.page(), for: "home", families: nil) factory.refocus(keys: ["home"], settled: 0) await factory.decodingDone() XCTAssertNotNil(factory.decoded["home"], "the database shown keeps its picture decoded") _ = stale.pictures(of: "personal") { PagePictures(quality: 0.8, reach: 4, scale: 2) } XCTAssertTrue(factory.decoded.isEmpty, "another database shown keeps no decoded picture") XCTAssertTrue(stale.pictures(of: "factory") { PagePictures(quality: 0.8, reach: 4, scale: 2) } === factory, "its pictures are kept over the switch") XCTAssertFalse(factory.needsDrawing("home"), "and its page is not drawn again") } /// A slow card stays covered until its real reading is ready. A timer /// cannot declare an unread page drawn. func testTheLivePageWaitsForItsReadingInsteadOfATimer() throws { let fan = SourceSweep.withoutComments( try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Surfaces/Board/FanSurface.swift", ownedBy: "transitions")) XCTAssertFalse(fan.contains("live-by-ceiling")) XCTAssertTrue(fan.contains("guard reading.ready")) XCTAssertTrue(fan.contains("liveDrawn = pageKey")) let page = SourceSweep.withoutComments( try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Surfaces/Board/PageFoot.swift", ownedBy: "transitions")) XCTAssertTrue(page.contains("reading.ready")) XCTAssertFalse(page.contains("Task.sleep")) } /// GUARD: a switch draws the chosen database's page at rest — no picture of a page left scrolled stands under it. /// RULE: a switch forgets where the chosen database's pages were left, so each draws at its top; a picture drawn while its page stood below its top is of no page the switch will show, and goes (walker291: in the first frame after a switch to Tinmen, Home's page stood ~70 pt down with its head under the island). func testASwitchNeverShowsAPageLeftScrolled() async { let stale = StalePages() let tinmen = stale.pictures(of: "tinmen") { PagePictures(quality: 0.8, reach: 4, scale: 2) } await tinmen.keep(Self.page(), for: "home", families: nil, scrolled: true) await tinmen.keep(Self.page(), for: "notes", families: nil) _ = stale.pictures(of: "personal") { PagePictures(quality: 0.8, reach: 4, scale: 2) } _ = stale.pictures(of: "tinmen") { PagePictures(quality: 0.8, reach: 4, scale: 2) } XCTAssertTrue(tinmen.needsDrawing("home"), "Home was left scrolled: it is drawn again at rest") XCTAssertFalse(tinmen.needsDrawing("notes"), "a page drawn at its top keeps its picture") } /// RULE: The fan's live page is drawn again when the tiles change, not only when its own tile does — Home's page draws the tiles, and after a switch they arrive with the database's answer while the fan already stands on Home; a live page equal on its tile alone kept the unanswered Home, with no tile on it, after the answer came (lane stalepic, 15 Oct 2026). func testTheLivePageIsDrawnAgainWhenTheTilesChange() { let page: (Int) -> FramedPage = { _ in FramedPage(page: AnyView(EmptyView())) } let unanswered = FanLivePage(key: "home", index: 0, tiles: ["home"], page: page) let answered = FanLivePage(key: "home", index: 0, tiles: ["home", "notes", "mood"], page: page) XCTAssertNotEqual(unanswered, answered, "the tiles arrived: Home's page is drawn again") XCTAssertEqual(answered, FanLivePage(key: "home", index: 0, tiles: ["home", "notes", "mood"], page: page)) } private static func page() -> UIImage { UIGraphicsImageRenderer(size: CGSize(width: 40, height: 60), format: PagePictures.format(scale: 2)).image { context in UIColor(red: 0.95, green: 0.92, blue: 0.85, alpha: 1).setFill() context.fill(CGRect(x: 0, y: 0, width: 40, height: 60)) } } } #endif