Cotton code
123 lines · 5.7 KB · SwiftRaw
// Link previews in Chat Dev's seed (FEEDBACK-4 B4, chat CONTRACT.md "Link previews"): the core's own answers over// Asha's chat, written by cotton-ffi `write_chat_warm_fixture` (Fixtures/chat-warm.json's `linkPreview`): the writing// box offering an invented link's card, the cards the core read from the invented pages, a message sent with its card// and picture, and the setting on and off. The invented pages, a link that moves on to his own network and an// invented picture are served on this phone (`ChatWarmLinkServer`, only on ChatLinkFetch's own session), so a UI// test never reaches the internet. `-chat-warm-link-previews NO` starts with the setting off.#if os(iOS) && COTTON_DEVimport Foundationimport SwiftUIimport UIKitenum ChatWarmLinks {    /// `write_chat_warm_fixture`'s `linkPreview`.    struct Fixture: Decodable {        var thread: String        /// The writing box for these words, as the core answered it with the setting on, and off.        var offers: [Offer]        var offOffers: [Offer]        /// The invented pages, by address; a move, by address, to where it goes.        var pages: [String: String]        var moves: [String: String]        /// `chat.linkCard` for each invented page.        var cards: [String: ChatLinkCardAnswer]        /// `chat.act send` with the card and its picture.        var sent: ChatActAnswer        var settings: Settings        struct Offer: Decodable {            var words: String            var composer: ChatComposeAnswer        }        struct Settings: Decodable {            var on: JSONValue            var off: JSONValue        }    }    static let fixture: Fixture? = ChatWarmSeed.load()?.linkPreview    /// This phone's setting in the seed: the core's own is never written by a test.    private static let lock = NSLock()    nonisolated(unsafe) private static var turnedOff: Bool?    nonisolated(unsafe) private static var heard: [String] = []    static var on: Bool {        lock.withLock { turnedOff.map { !$0 } } ?? (UserDefaults.standard.object(forKey: "chat-warm-link-previews").map { "\($0)" != "NO" && "\($0)" != "0" } ?? true)    }    static func set(_ on: Bool) { lock.withLock { turnedOff = !on } }    /// The address the seed's invented pages stand at: a documentation address (TEST-NET-3), public by every rule.    static func addresses(_ host: String) -> [String]? {        guard let fixture else { return nil }        let hosts = Set((Array(fixture.pages.keys) + Array(fixture.moves.keys)).compactMap { URL(string: $0)?.host()?.lowercased() })        return hosts.contains(host.lowercased()) ? ["203.0.113.7"] : nil    }    /// The writing box for `text` as the core answers it under the setting now, where the fixture holds it.    static func composer(_ thread: String, _ text: String) -> ChatComposeAnswer? {        guard let fixture, fixture.thread == thread else { return nil }        return (on ? fixture.offers : fixture.offOffers).first { $0.words == text }?.composer    }    /// `chat.linkCard` as the core read the page; nil where it reads none (the setting off, or an unknown page).    static func card(_ thread: String, _ url: String, _ page: String) -> ChatLinkCardAnswer? {        guard on, let fixture, fixture.thread == thread, fixture.pages.values.contains(page) else { return nil }        return fixture.cards[url]    }    /// A message sent with its card, as the core keeps it (`meta.preview`): only with the setting on and its link in    /// the words, and its picture the one this phone made.    static func link(_ ask: ChatActAsk) -> ChatLinkCard? {        guard on, let card = ask.preview, let text = ask.text, text.contains(card.url), card.title != nil else { return nil }        return card    }    // MARK: what the seed's phone serves    struct Served {        var status: Int        var type: String        var moved: String?        var body: Data    }    static func asked(_ url: String) { lock.withLock { heard.append(url) } }    /// Every address asked of the seed's server this launch.    static var askedSoFar: [String] { lock.withLock { heard } }    static func served(_ url: String) -> Served {        guard let fixture else { return Served(status: 404, type: "text/plain", body: Data()) }        if let to = fixture.moves[url] { return Served(status: 302, type: "text/html", moved: to, body: Data()) }        if let page = fixture.pages[url] { return Served(status: 200, type: "text/html", body: Data(page.utf8)) }        if url.hasSuffix(".jpg"), let picture { return Served(status: 200, type: "image/jpeg", body: picture) }        return Served(status: 404, type: "text/plain", body: Data())    }    /// `-chat-warm-link-log YES`: the writing box carries what the seed's server was asked (`ChatWarmLinkLog`).    static var logged: Bool { UserDefaults.standard.bool(forKey: "chat-warm-link-log") }    /// An invented picture, larger than a card's, so the phone makes it small.    private static let picture: Data? = ChatWarmSeed.inventedHill()}/// What the seed's server was asked this launch, as an element a UI test reads (`seed-link-asked`'s value): a page/// never fetched is seen not to be. Only with `-chat-warm-link-log YES`, so no other walk meets it.struct ChatWarmLinkLog: View {    var body: some View {        if ChatWarmLinks.logged {            TimelineView(.periodic(from: .now, by: 0.5)) { _ in                Color.clear                    .frame(width: 1, height: 1)                    .accessibilityElement()                    .accessibilityValue(Text(verbatim: ChatWarmLinks.askedSoFar.joined(separator: " ")))                    .accessibilityIdentifier("seed-link-asked")            }            .frame(width: 1, height: 1)        }    }}#endif