// FEEDBACK-4 B4 (owner on iPhone, Comms 26, 23 Nov 2026): a link sent shows no preview, where WhatsApp shows its // title and picture. Now the sender's own phone fetches the card, never the server, with a setting to turn it off // (chat CONTRACT.md "Link previews"). Over Chat Dev's invented chats (-chat-warm-seed YES, the core's own answers // from write_chat_warm_fixture's `linkPreview`), whose invented pages and picture the seed serves on the phone (a UI // test never reaches the internet): a link written in Asha's chat shows its card above the writing box before Send; // closed, the message goes without one and the link offers none again; sent, the bubble carries the card with its // picture; with the setting off nothing is offered or fetched; an address on his own network is never asked, nor a // link that moves on to one. Each at the default size and the largest text. Shots link-preview-*. import XCTest final class ChatLinkPreviewTests: XCTestCase { private let asha = "11111111-1111-4111-8111-0000000000a1" private static let largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"] // The invented link and page (write_chat_warm_fixture's link_preview_warm). private let words = "The loop we walked https://walks.example/hill-loop" private let moved = "And the other one https://walks.example/moved" private let inside = "The printer page http://192.168.1.20/status" private let title = "The hill loop, six kilometres" private let site = "Hill Walks" override func setUp() { continueAfterFailure = false XCUIDevice.shared.orientation = .portrait } private func keep(_ name: String) { let shot = XCTAttachment(screenshot: XCUIScreen.main.screenshot()) shot.name = "link-preview-\(name)" shot.lifetime = .keepAlways add(shot) } private func size(_ large: Bool) -> String { large ? "largest" : "default" } @MainActor private func opened(_ extra: [String], large: Bool) -> XCUIApplication { let app = XCUIApplication() app.launchArguments += ["-chat-warm-seed", "YES", "-chat-warm-open", asha, "-chat-warm-link-log", "YES"] + extra + (large ? Self.largestText : []) app.launch() XCTAssertTrue(app.tables["chat-thread"].waitForExistence(timeout: 30), "the chat opened") // The card of a device asking to be let in can come down a few seconds in and move things: wait for it. _ = ShellCard.refuse(app).waitForExistence(timeout: 6) return app } @MainActor private func any(_ id: String, in app: XCUIApplication) -> XCUIElement { app.descendants(matching: .any)[id].firstMatch } @MainActor private func write(_ text: String, in app: XCUIApplication) { let field = app.textViews["chat-composer-text"] XCTAssertTrue(field.waitForExistence(timeout: 10)) field.tap() field.typeText(text) } /// What the seed's server was asked this launch, by address. @MainActor private func asked(_ app: XCUIApplication) -> String { let log = any("seed-link-asked", in: app) guard log.waitForExistence(timeout: 5) else { return "" } return (log.value as? String) ?? "" } /// Whether the invented picture's hill (its green, made small and a JPEG) stands inside `frame` on the screen. @MainActor private func hill(in frame: CGRect) -> Bool { let shot = XCUIScreen.main.screenshot().image guard let cg = shot.cgImage, frame.width > 4, frame.height > 4 else { return false } let scale = CGFloat(cg.width) / shot.size.width let rect = CGRect(x: frame.minX * scale, y: frame.minY * scale, width: frame.width * scale, height: frame.height * scale) .integral.intersection(CGRect(x: 0, y: 0, width: cg.width, height: cg.height)) guard let part = cg.cropping(to: rect) else { return false } let (w, h) = (part.width, part.height) var pixels = [UInt8](repeating: 0, count: w * h * 4) guard let context = CGContext(data: &pixels, width: w, height: h, bitsPerComponent: 8, bytesPerRow: w * 4, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return false } context.draw(part, in: CGRect(x: 0, y: 0, width: w, height: h)) var green = 0 for i in stride(from: 0, to: pixels.count, by: 4) { let (r, g, b) = (Int(pixels[i]), Int(pixels[i + 1]), Int(pixels[i + 2])) if g > 95, g < 165, g > r + 30, g > b + 20 { green += 1 } } return green > 60 } /// The card standing above the writing box, with its site, title and picture. @MainActor private func card(in app: XCUIApplication) -> XCUIElement { let card = any("chat-preview", in: app) XCTAssertTrue(card.waitForExistence(timeout: 15), "the card stands before Send: " + String(app.debugDescription.prefix(2500))) let named = card.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@", title)).firstMatch XCTAssertTrue(named.waitForExistence(timeout: 5), "its title") XCTAssertTrue(card.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@", site)).firstMatch.exists, "its site") XCTAssertTrue(any("chat-preview-close", in: app).exists, "its close mark") let box = any("chat-composer", in: app) XCTAssertLessThanOrEqual(card.frame.maxY, box.frame.minY + 1, "above the writing box") let there = XCTNSPredicateExpectation(predicate: NSPredicate { _, _ in self.hill(in: card.frame) }, object: nil) XCTAssertEqual(XCTWaiter().wait(for: [there], timeout: 8), .completed, "its picture") return card } @MainActor private func sent(in app: XCUIApplication) -> XCUIElement { app.buttons["chat-composer-send"].tap() let bubble = any("chat-bubble-seed-sent-1", in: app) XCTAssertTrue(bubble.waitForExistence(timeout: 10), "the message landed") return bubble } // MARK: a link written shows its card before Send @MainActor private func shows(_ large: Bool) { let app = opened([], large: large) write(words, in: app) _ = card(in: app) XCTAssertTrue(asked(app).contains("https://walks.example/hill-loop"), "this phone fetched the page") keep("card-\(size(large))") } // RULE (owner, FEEDBACK-4 B4): a link written shows its card (picture, site, title) above the writing box before Send. @MainActor func testALinkWrittenShowsItsCardBeforeSend() { shows(false) } @MainActor func testALinkWrittenShowsItsCardBeforeSendAtTheLargestText() { shows(true) } // MARK: closed, none goes @MainActor private func closes(_ large: Bool) { let app = opened([], large: large) write(words, in: app) _ = card(in: app) any("chat-preview-close", in: app).tap() XCTAssertTrue(any("chat-preview", in: app).waitForNonExistence(timeout: 5), "closed") // Written on and back to the same words: that link offers no card again in this message. let field = app.textViews["chat-composer-text"] field.typeText(" ") field.typeText(XCUIKeyboardKey.delete.rawValue) _ = app.buttons["__none__"].waitForExistence(timeout: 3) XCTAssertFalse(any("chat-preview", in: app).exists, "the link closed offers none again") keep("closed-\(size(large))") let bubble = sent(in: app) XCTAssertFalse(bubble.label.contains(title), "no card went: \(bubble.label)") XCTAssertFalse(hill(in: bubble.frame), "and no picture") } // RULE: closing the card sends the message without one, and that link offers none again in this message. @MainActor func testClosingTheCardSendsNone() { closes(false) } @MainActor func testClosingTheCardSendsNoneAtTheLargestText() { closes(true) } // MARK: the sent bubble carries the card and its picture @MainActor private func carries(_ large: Bool) { let app = opened([], large: large) write(words, in: app) _ = card(in: app) let bubble = sent(in: app) XCTAssertFalse(any("chat-preview", in: app).exists, "the card went with the send") XCTAssertTrue(bubble.label.contains(title) && bubble.label.contains(site), "the bubble's card: \(bubble.label)") // The card stands over the words: at the largest text the bubble is taller than the room left by the keyboard, // so the chat is drawn down a little at a time until its picture is in view. let table = app.tables["chat-thread"] var seen = false for _ in 0..<8 { Thread.sleep(forTimeInterval: 1) if hill(in: bubble.frame) { seen = true; break } let from = table.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.3)) from.press(forDuration: 0.05, thenDragTo: from.withOffset(CGVector(dx: 0, dy: 140))) } XCTAssertTrue(seen, "with the picture this phone made") Thread.sleep(forTimeInterval: 1) keep("sent-\(size(large))") } // RULE: the sent bubble shows the card with its picture (`link.thumb`, the JPEG the sender's phone made). @MainActor func testTheSentBubbleShowsTheCardWithItsPicture() { carries(false) } @MainActor func testTheSentBubbleShowsTheCardWithItsPictureAtTheLargestText() { carries(true) } // MARK: the setting @MainActor private func off(_ large: Bool) { // You, Privacy: "Link previews", on until turned off, with its line. let you = XCUIApplication() you.launchArguments += ["-chat-warm-seed", "YES", "-chat-warm-place", "you", "-chat-warm-you", "privacy"] + (large ? Self.largestText : []) you.launch() let toggle = you.switches["you-link-previews"] XCTAssertTrue(toggle.waitForExistence(timeout: 30), "Privacy offers Link previews: " + String(you.debugDescription.prefix(2500))) for _ in 0..<6 where !toggle.isHittable { you.swipeUp() } XCTAssertTrue(toggle.label.contains("Link previews"), toggle.label) XCTAssertTrue(toggle.label.contains("The server never sees it."), "its line: \(toggle.label)") let on = XCTNSPredicateExpectation(predicate: NSPredicate(format: "value == '1'"), object: toggle) XCTAssertEqual(XCTWaiter().wait(for: [on], timeout: 10), .completed, "on until turned off") keep("setting-on-\(size(large))") // The switch's knob, at the row's trailing end. toggle.coordinate(withNormalizedOffset: CGVector(dx: 0.94, dy: 0.5)).tap() let turned = XCTNSPredicateExpectation(predicate: NSPredicate(format: "value == '0'"), object: toggle) XCTAssertEqual(XCTWaiter().wait(for: [turned], timeout: 10), .completed, "turned off, and read back off") Thread.sleep(forTimeInterval: 1.5) XCTAssertEqual(toggle.value as? String, "0", "kept off") keep("setting-off-\(size(large))") // Off: a link written offers no card and nothing is fetched. let app = opened(["-chat-warm-link-previews", "NO"], large: large) write(words, in: app) _ = app.buttons["__none__"].waitForExistence(timeout: 4) XCTAssertFalse(any("chat-preview", in: app).exists, "no card") XCTAssertEqual(asked(app), "", "nothing fetched") keep("off-\(size(large))") let bubble = sent(in: app) XCTAssertFalse(bubble.label.contains(title), "and none sent: \(bubble.label)") } // RULE: with Link previews off (You, Privacy), no card is offered and this phone fetches nothing. @MainActor func testWithTheSettingOffNoCardAndNoFetch() { off(false) } @MainActor func testWithTheSettingOffNoCardAndNoFetchAtTheLargestText() { off(true) } // MARK: his own network @MainActor private func never(_ large: Bool) { // A link to an address on his own network offers no card, and is never asked. var app = opened([], large: large) write(inside, in: app) _ = app.buttons["__none__"].waitForExistence(timeout: 4) XCTAssertFalse(any("chat-preview", in: app).exists, "no card for his own network") XCTAssertFalse(asked(app).contains("192.168."), "never asked: \(asked(app))") keep("inside-\(size(large))") // A public link that moves on to his own network: the first page is asked, the move is never followed. app = opened([], large: large) write(moved, in: app) let first = XCTNSPredicateExpectation(predicate: NSPredicate { _, _ in self.asked(app).contains("https://walks.example/moved") }, object: nil) XCTAssertEqual(XCTWaiter().wait(for: [first], timeout: 10), .completed, "the public page was asked") _ = app.buttons["__none__"].waitForExistence(timeout: 3) XCTAssertFalse(asked(app).contains("10.0.0.8"), "the move to his own network never followed: \(asked(app))") XCTAssertFalse(any("chat-preview", in: app).exists, "and no card from it") keep("moved-\(size(large))") } // RULE: an address on this phone's own network is never fetched, nor a link that moves on to one. @MainActor func testAPrivateAddressIsNeverFetched() { never(false) } @MainActor func testAPrivateAddressIsNeverFetchedAtTheLargestText() { never(true) } }