#if os(iOS) import Foundation import UIKit import XCTest @testable import DocsKit /// iPhone text-view cost per publication (realtime probes). The iOS /// NativeDocumentTextView replaces `attributedText` wholesale for every /// published projection; this prices that against reconciling the same /// change into the view's own text storage, plus the per-keystroke getters. @MainActor final class RealtimeAuditIOSTests: XCTestCase { func testTextViewPublicationCost() async throws { guard let dir = ProcessInfo.processInfo.environment["DOCS_RT_AUDIT_DOCS"] else { throw XCTSkip("Set DOCS_RT_AUDIT_DOCS") } let names = (ProcessInfo.processInfo.environment["DOCS_RT_AUDIT_ONLY"] ?? "p1,p10,p20,p50").split(separator: ",").map(String.init) for name in names { let bytes = try Data(contentsOf: URL(fileURLWithPath: "\(dir)/\(name).bin")) let replica = CRDTReplica(guid: "ios-" + name) try await replica.open(update: bytes) let projection = try await replica.projection() var content = NativeTextProjection.render(projection) let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 402, height: 874)) // The production view: DocsTextView (TextKit 1, ensureLayout in layoutSubviews). let text = DocsTextView(frame: window.bounds) text.paged = ProcessInfo.processInfo.environment["DOCS_RT_AUDIT_PAGED"] != "0" window.addSubview(text); window.makeKeyAndVisible() var t = ContinuousClock.now text.attributedText = content; text.layoutIfNeeded() RealtimeAuditTests.record(name, "ios: initial attributedText + layout", [RealtimeAuditTests.ms(t)], note: "utf16=\(content.length)") let middle = RealtimeAuditTests.middle(content) text.selectedRange = NSRange(location: middle, length: 0) text.scrollRangeToVisible(text.selectedRange) var replace: [Double] = [], reconcile: [Double] = [], getAttributed: [Double] = [], getText: [Double] = [] for i in 0..<8 { let next = NSMutableAttributedString(attributedString: content) next.replaceCharacters(in: NSRange(location: middle + i, length: 0), with: NSAttributedString(string: "x", attributes: content.attributes(at: middle + i, effectiveRange: nil))) // Production path (NativeDocumentTextView.swift, iOS applyRender). t = .now text.attributedText = next text.selectedRange = NSRange(location: middle + i + 1, length: 0) text.layoutIfNeeded() replace.append(RealtimeAuditTests.ms(t)) // Alternative: the Mac's reconciliation into the live storage. let again = NSMutableAttributedString(attributedString: next) again.replaceCharacters(in: NSRange(location: middle + i + 1, length: 0), with: NSAttributedString(string: "y", attributes: next.attributes(at: middle + i, effectiveRange: nil))) t = .now DocumentRenderReconciliation.apply(again, to: text.textStorage) text.layoutIfNeeded() reconcile.append(RealtimeAuditTests.ms(t)) content = again // Per keystroke: shouldChangeTextIn reads attributedText (a copy); // textViewDidChangeSelection reads text twice. t = .now; _ = text.attributedText; getAttributed.append(RealtimeAuditTests.ms(t)) t = .now; _ = text.text.utf16.count; _ = text.text.hasSuffix("\n"); getText.append(RealtimeAuditTests.ms(t)) } RealtimeAuditTests.record(name, "ios: attributedText = (production) + layout", replace) RealtimeAuditTests.record(name, "ios: reconcile into textStorage + layout", reconcile) RealtimeAuditTests.record(name, "ios: textView.attributedText getter", getAttributed) RealtimeAuditTests.record(name, "ios: textView.text x2 (selection change)", getText) window.isHidden = true await replica.close() } } } #endif