#if os(macOS) import AppKit import Foundation import Testing @testable import EquipoKit @testable import EquipoMac @MainActor private final class WindowMoveProbe: NSView { var moves = 0 override func viewDidMoveToWindow() { super.viewDidMoveToWindow(); moves += 1 } } @MainActor @Suite(.serialized) struct MacTranscriptTests { func fixture() throws -> (EquipoSession, [FeedItem]) { let origin = URL(string: "https://transcript.example.test")! let session = EquipoSession(account: .new(origin: origin, label: "Test"), accounts: DeviceAccounts(store: MemorySecretStore())) let snapshot: Snapshot = try Fixture.decode("thin/snapshot") let template = try #require(snapshot.messages?.first) let items: [FeedItem] = (0..<50).map { i in var message = template message.id = "message-\(i)"; message.turnId = "turn-\(i)" message.text = "**Reply \(i)**\n\n" + String(repeating: "This is a realistic reply with a [task](/tasks/tk1). ", count: 5) return .coordinator(message) } return (session, items) } @Test func fiftyRepliesUpdateOnlyTheChangedRowWithinBudget() throws { let (session, initial) = try fixture() var items = initial let view = LiveTranscript(session: session, openTask: { _ in }) view.frame = NSRect(x: 0, y: 0, width: 900, height: 800) let window = NSWindow(contentRect: view.frame, styleMask: [.borderless], backing: .buffered, defer: false) window.contentView = view defer { window.contentView = nil } view.update(items: items) view.layoutSubtreeIfNeeded() let document = try #require(view.subviews.compactMap { $0 as? NSScrollView }.first?.documentView) let rows = document.subviews #expect(rows.count == 50) let probes = rows.map { row in let probe = WindowMoveProbe() row.addSubview(probe) return probe } let initialMoves = probes.map(\.moves) func hosts(_ row: NSView) -> [NSView] { row.subviews.filter { String(reflecting: type(of: $0)).contains("NSHostingView") } } let initialHosts = try rows.map { try #require(hosts($0).first) } let initialRevisions = view.renderedRevisions var times: [Double] = [] for revision in 1...10 { guard case .coordinator(var message) = items[49] else { return } message.text += " More words \(revision)."; message.revision += 1 items[49] = .coordinator(message) let start = ContinuousClock.now view.update(items: items) view.layoutSubtreeIfNeeded() let elapsed = start.duration(to: .now).components times.append(Double(elapsed.seconds) * 1000 + Double(elapsed.attoseconds) / 1e15) #expect(view.renderedRevisions == initialRevisions + revision, "only the changed row receives content") #expect(document.subviews.count == 50) #expect(probes.map(\.moves) == initialMoves, "updates must not detach and reattach existing rows") #expect(zip(rows, initialHosts).allSatisfy { hosts($0).count == 1 && hosts($0).first === $1 }, "each reply keeps its single host") #expect(zip(document.subviews, rows).allSatisfy { $0 === $1 }, "feed deltas must preserve row identity, including the changed reply") } let median = times.sorted()[times.count / 2] print("Mac transcript: 50 replies, update median \(median) ms, max \(times.max() ?? 0) ms") // 50 ms allows debug builds and shared CI machines; it still rejects // the observed 1,156 ms full rebuild by more than an order of magnitude. #expect(median < 50) view.update(items: Array(items.dropFirst())) #expect(document.subviews.count == 49) #expect(rows[0].superview == nil) #expect(zip(document.subviews, rows.dropFirst()).allSatisfy { $0 === $1 }) view.update(items: Array(items.reversed())) #expect(document.subviews.count == 50) #expect(zip(document.subviews.dropLast(), rows.dropFirst().reversed()).allSatisfy { $0 === $1 }) #expect(document.subviews.last !== rows[0], "a removed row gets a fresh lifetime when reinserted") let ordered = document.subviews #expect(zip(ordered, ordered.dropFirst()).allSatisfy { $0.frame.maxY <= $1.frame.minY }) view.frame.size.width = 1100 view.layoutSubtreeIfNeeded() #expect(zip(document.subviews, ordered).allSatisfy { $0 === $1 }, "width changes resize existing rows") #expect(document.subviews.allSatisfy { $0.frame.width == 768 }) } @Test func actualTranscriptGrowsThroughFastLargeDeltas() async throws { let (session, history) = try fixture() let snapshot: Snapshot = try Fixture.decode("thin/snapshot") var turn = try #require(snapshot.turns?.first) turn.id = "live"; turn.state = "running" let text = String(repeating: "A long reply with **emphasis** and words. ", count: 1000) let view = LiveTranscript(session: session, openTask: { _ in }) view.frame = NSRect(x: 0, y: 0, width: 900, height: 800) let window = NSWindow(contentRect: view.frame, styleMask: [.borderless], backing: .buffered, defer: false) window.contentView = view defer { window.contentView = nil; session.markdown.removeAll() } func feed(_ count: Int) -> [FeedItem] { Array(history.prefix(49)) + [.draft(turn, text: String(text.prefix(count)))] } view.update(items: feed(16000)) view.layoutSubtreeIfNeeded() let document = try #require(view.subviews.compactMap { $0 as? NSScrollView }.first?.documentView) let rows = document.subviews let reply = try #require(rows.last) var heights: [CGFloat] = [] for step in 1...67 { view.update(items: feed(17000 + 8000 * step / 67)) try await Task.sleep(for: .milliseconds(5)) heights.append(reply.frame.height) #expect(zip(document.subviews, rows).allSatisfy { $0 === $1 }) } let renderer = session.markdown.renderer(for: session.origin.absoluteString + "/reply-" + turn.id) #expect(renderer.publication != nil) print("Mac live transcript: \(Set(heights.dropLast()).count) growing heights during 67 deltas") #expect(Set(heights.dropLast()).count >= 5) var message = try #require(snapshot.messages?.first) message.id = "completed"; message.turnId = turn.id; message.text = String(text.prefix(25000)) view.update(items: Array(history.prefix(49)) + [.coordinator(message)]) #expect(document.subviews.last === reply, "completion updates the same row") view.update(items: Array(history.prefix(49))) #expect(reply.superview == nil) #expect(renderer.publication == nil, "removing the message ends its parser lifetime") } } #endif