oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1#if os(macOS)
2import AppKit
3import Foundation
4import Testing
5@testable import EquipoKit
6@testable import EquipoMac
7
8@MainActor private final class WindowMoveProbe: NSView {
9 var moves = 0
10 override func viewDidMoveToWindow() { super.viewDidMoveToWindow(); moves += 1 }
11}
12
13@MainActor @Suite(.serialized) struct MacTranscriptTests {
14 func fixture() throws -> (EquipoSession, [FeedItem]) {
15 let origin = URL(string: "https://transcript.example.test")!
16 let session = EquipoSession(account: .new(origin: origin, label: "Test"), accounts: DeviceAccounts(store: MemorySecretStore()))
17 let snapshot: Snapshot = try Fixture.decode("thin/snapshot")
18 let template = try #require(snapshot.messages?.first)
19 let items: [FeedItem] = (0..<50).map { i in
20 var message = template
21 message.id = "message-\(i)"; message.turnId = "turn-\(i)"
22 message.text = "**Reply \(i)**\n\n" + String(repeating: "This is a realistic reply with a [task](/tasks/tk1). ", count: 5)
23 return .coordinator(message)
24 }
25 return (session, items)
26 }
27 @Test func fiftyRepliesUpdateOnlyTheChangedRowWithinBudget() throws {
28 let (session, initial) = try fixture()
29 var items = initial
30 let view = LiveTranscript(session: session, openTask: { _ in })
31 view.frame = NSRect(x: 0, y: 0, width: 900, height: 800)
32 let window = NSWindow(contentRect: view.frame, styleMask: [.borderless], backing: .buffered, defer: false)
33 window.contentView = view
34 defer { window.contentView = nil }
35 view.update(items: items)
36 view.layoutSubtreeIfNeeded()
37 let document = try #require(view.subviews.compactMap { $0 as? NSScrollView }.first?.documentView)
38 let rows = document.subviews
39 #expect(rows.count == 50)
40 let probes = rows.map { row in
41 let probe = WindowMoveProbe()
42 row.addSubview(probe)
43 return probe
44 }
45 let initialMoves = probes.map(\.moves)
46 func hosts(_ row: NSView) -> [NSView] { row.subviews.filter { String(reflecting: type(of: $0)).contains("NSHostingView") } }
47 let initialHosts = try rows.map { try #require(hosts($0).first) }
48 let initialRevisions = view.renderedRevisions
49 var times: [Double] = []
50 for revision in 1...10 {
51 guard case .coordinator(var message) = items[49] else { return }
52 message.text += " More words \(revision)."; message.revision += 1
53 items[49] = .coordinator(message)
54 let start = ContinuousClock.now
55 view.update(items: items)
56 view.layoutSubtreeIfNeeded()
57 let elapsed = start.duration(to: .now).components
58 times.append(Double(elapsed.seconds) * 1000 + Double(elapsed.attoseconds) / 1e15)
59 #expect(view.renderedRevisions == initialRevisions + revision, "only the changed row receives content")
60 #expect(document.subviews.count == 50)
61 #expect(probes.map(\.moves) == initialMoves, "updates must not detach and reattach existing rows")
62 #expect(zip(rows, initialHosts).allSatisfy { hosts($0).count == 1 && hosts($0).first === $1 }, "each reply keeps its single host")
63 #expect(zip(document.subviews, rows).allSatisfy { $0 === $1 }, "feed deltas must preserve row identity, including the changed reply")
64 }
65 let median = times.sorted()[times.count / 2]
66 print("Mac transcript: 50 replies, update median \(median) ms, max \(times.max() ?? 0) ms")
67 // 50 ms allows debug builds and shared CI machines; it still rejects
68 // the observed 1,156 ms full rebuild by more than an order of magnitude.
69 #expect(median < 50)
70 view.update(items: Array(items.dropFirst()))
71 #expect(document.subviews.count == 49)
72 #expect(rows[0].superview == nil)
73 #expect(zip(document.subviews, rows.dropFirst()).allSatisfy { $0 === $1 })
74 view.update(items: Array(items.reversed()))
75 #expect(document.subviews.count == 50)
76 #expect(zip(document.subviews.dropLast(), rows.dropFirst().reversed()).allSatisfy { $0 === $1 })
77 #expect(document.subviews.last !== rows[0], "a removed row gets a fresh lifetime when reinserted")
78 let ordered = document.subviews
79 #expect(zip(ordered, ordered.dropFirst()).allSatisfy { $0.frame.maxY <= $1.frame.minY })
80 view.frame.size.width = 1100
81 view.layoutSubtreeIfNeeded()
82 #expect(zip(document.subviews, ordered).allSatisfy { $0 === $1 }, "width changes resize existing rows")
83 #expect(document.subviews.allSatisfy { $0.frame.width == 768 })
84 }
85
86 @Test func actualTranscriptGrowsThroughFastLargeDeltas() async throws {
87 let (session, history) = try fixture()
88 let snapshot: Snapshot = try Fixture.decode("thin/snapshot")
89 var turn = try #require(snapshot.turns?.first)
90 turn.id = "live"; turn.state = "running"
91 let text = String(repeating: "A long reply with **emphasis** and words. ", count: 1000)
92 let view = LiveTranscript(session: session, openTask: { _ in })
93 view.frame = NSRect(x: 0, y: 0, width: 900, height: 800)
94 let window = NSWindow(contentRect: view.frame, styleMask: [.borderless], backing: .buffered, defer: false)
95 window.contentView = view
96 defer { window.contentView = nil; session.markdown.removeAll() }
97 func feed(_ count: Int) -> [FeedItem] { Array(history.prefix(49)) + [.draft(turn, text: String(text.prefix(count)))] }
98 view.update(items: feed(16000))
99 view.layoutSubtreeIfNeeded()
100 let document = try #require(view.subviews.compactMap { $0 as? NSScrollView }.first?.documentView)
101 let rows = document.subviews
102 let reply = try #require(rows.last)
103 var heights: [CGFloat] = []
104 for step in 1...67 {
105 view.update(items: feed(17000 + 8000 * step / 67))
106 try await Task.sleep(for: .milliseconds(5))
107 heights.append(reply.frame.height)
108 #expect(zip(document.subviews, rows).allSatisfy { $0 === $1 })
109 }
110 let renderer = session.markdown.renderer(for: session.origin.absoluteString + "/reply-" + turn.id)
111 #expect(renderer.publication != nil)
112 print("Mac live transcript: \(Set(heights.dropLast()).count) growing heights during 67 deltas")
113 #expect(Set(heights.dropLast()).count >= 5)
114 var message = try #require(snapshot.messages?.first)
115 message.id = "completed"; message.turnId = turn.id; message.text = String(text.prefix(25000))
116 view.update(items: Array(history.prefix(49)) + [.coordinator(message)])
117 #expect(document.subviews.last === reply, "completion updates the same row")
118 view.update(items: Array(history.prefix(49)))
119 #expect(reply.superview == nil)
120 #expect(renderer.publication == nil, "removing the message ends its parser lifetime")
121 }
122}
123#endif
124