// 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_DEVimportFoundationimportSwiftUIimport UIKitenumChatWarmLinks {/// `write_chat_warm_fixture`'s `linkPreview`.structFixture: 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: ChatActAnswervar settings: SettingsstructOffer: Decodable {var words: Stringvar composer: ChatComposeAnswer }structSettings: Decodable {var on: JSONValuevar off: JSONValue } }staticlet fixture: Fixture? = ChatWarmSeed.load()?.linkPreview/// This phone's setting in the seed: the core's own is never written by a test.privatestaticlet lock = NSLock() nonisolated(unsafe) privatestaticvar turnedOff: Bool? nonisolated(unsafe) privatestaticvar heard: [String] = []staticvar on: Bool { lock.withLock { turnedOff.map { !$0 } } ?? (UserDefaults.standard.object(forKey: "chat-warm-link-previews").map { "\($0)" != "NO" && "\($0)" != "0" } ?? true) }staticfunc 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.staticfunc addresses(_ host: String) -> [String]? {guardlet fixture else { returnnil }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.staticfunc composer(_ thread: String, _ text: String) -> ChatComposeAnswer? {guardlet fixture, fixture.thread == thread else { returnnil }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).staticfunc card(_ thread: String, _ url: String, _ page: String) -> ChatLinkCardAnswer? {guard on, let fixture, fixture.thread == thread, fixture.pages.values.contains(page) else { returnnil }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.staticfunc link(_ ask: ChatActAsk) -> ChatLinkCard? {guard on, let card = ask.preview, let text = ask.text, text.contains(card.url), card.title != nilelse { returnnil }return card }// MARK: what the seed's phone servesstructServed {var status: Intvar type: Stringvar moved: String?var body: Data }staticfunc asked(_ url: String) { lock.withLock { heard.append(url) } }/// Every address asked of the seed's server this launch.staticvar askedSoFar: [String] { lock.withLock { heard } }staticfunc served(_ url: String) -> Served {guardlet fixture else { returnServed(status: 404, type: "text/plain", body: Data()) }iflet to = fixture.moves[url] { returnServed(status: 302, type: "text/html", moved: to, body: Data()) }iflet page = fixture.pages[url] { returnServed(status: 200, type: "text/html", body: Data(page.utf8)) }if url.hasSuffix(".jpg"), let picture { returnServed(status: 200, type: "image/jpeg", body: picture) }returnServed(status: 404, type: "text/plain", body: Data()) }/// `-chat-warm-link-log YES`: the writing box carries what the seed's server was asked (`ChatWarmLinkLog`).staticvar logged: Bool { UserDefaults.standard.bool(forKey: "chat-warm-link-log") }/// An invented picture, larger than a card's, so the phone makes it small.privatestaticlet 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.structChatWarmLinkLog: View {var body: someView {ifChatWarmLinks.logged {TimelineView(.periodic(from: .now, by: 0.5)) { _ inColor.clear .frame(width: 1, height: 1) .accessibilityElement() .accessibilityValue(Text(verbatim: ChatWarmLinks.askedSoFar.joined(separator: " "))) .accessibilityIdentifier("seed-link-asked") } .frame(width: 1, height: 1) } }}#endif