|
| 1 | +import AppKit |
| 2 | +import SwiftUI |
| 3 | +import XCTest |
| 4 | +@testable import OpenClaw |
| 5 | + |
| 6 | +@MainActor |
| 7 | +final class SystemAgentSettingsLayoutTests: XCTestCase { |
| 8 | + private static var retainedWindows: [NSWindow] = [] |
| 9 | + |
| 10 | + func testPaneOwnsScrollableDetailContainer() async throws { |
| 11 | + let hosting = NSHostingView(rootView: SystemAgentSettings( |
| 12 | + isActive: false, |
| 13 | + onReplyReceived: {})) |
| 14 | + hosting.frame = NSRect(x: 0, y: 0, width: 800, height: 260) |
| 15 | + let window = Self.retainWindow(hosting) |
| 16 | + |
| 17 | + try await Self.waitForLayout(hosting, stage: "system agent detail scroll") { |
| 18 | + Self.detailScrollView(in: hosting) != nil |
| 19 | + } |
| 20 | + let scroll = try XCTUnwrap(Self.detailScrollView(in: hosting)) |
| 21 | + let maximumOffset = scroll.documentView.map { |
| 22 | + max(0, $0.bounds.height - scroll.contentView.bounds.height) |
| 23 | + } ?? 0 |
| 24 | + XCTAssertGreaterThan(maximumOffset, 0) |
| 25 | + |
| 26 | + window.orderOut(nil) |
| 27 | + } |
| 28 | + |
| 29 | + func testChatUsesAvailableDetailHeight() async throws { |
| 30 | + let hosting = NSHostingView(rootView: SystemAgentSettings( |
| 31 | + isActive: false, |
| 32 | + onReplyReceived: {})) |
| 33 | + hosting.frame = NSRect(x: 0, y: 0, width: 800, height: 700) |
| 34 | + let window = Self.retainWindow(hosting) |
| 35 | + |
| 36 | + try await Self.waitForLayout(hosting, stage: "system agent chat fills detail height") { |
| 37 | + Self.chatScrollView(in: hosting)?.frame.height ?? 0 > hosting.frame.height / 2 |
| 38 | + } |
| 39 | + |
| 40 | + window.orderOut(nil) |
| 41 | + } |
| 42 | + |
| 43 | + private static func retainWindow(_ hosting: NSView) -> NSWindow { |
| 44 | + let window = NSWindow( |
| 45 | + contentRect: hosting.frame, |
| 46 | + styleMask: [.titled], |
| 47 | + backing: .buffered, |
| 48 | + defer: false) |
| 49 | + window.isReleasedWhenClosed = false |
| 50 | + window.contentView = hosting |
| 51 | + self.retainedWindows.append(window) |
| 52 | + return window |
| 53 | + } |
| 54 | + |
| 55 | + private static func detailScrollView(in view: NSView) -> NSScrollView? { |
| 56 | + self.descendants(of: NSScrollView.self, in: view).first { scrollView in |
| 57 | + guard let documentView = scrollView.documentView else { return false } |
| 58 | + let containsChatScroll = !self.descendants(of: NSScrollView.self, in: documentView).isEmpty |
| 59 | + return containsChatScroll && documentView.bounds.height > scrollView.contentView.bounds.height + 1 |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static func chatScrollView(in view: NSView) -> NSScrollView? { |
| 64 | + self.descendants(of: NSScrollView.self, in: view).first { scrollView in |
| 65 | + scrollView.frame.width > 500 && self.descendants(of: NSScrollView.self, in: scrollView).count == 1 |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static func descendants<T: NSView>(of type: T.Type, in view: NSView) -> [T] { |
| 70 | + var matches: [T] = [] |
| 71 | + if let match = view as? T { matches.append(match) } |
| 72 | + for child in view.subviews { |
| 73 | + matches.append(contentsOf: self.descendants(of: type, in: child)) |
| 74 | + } |
| 75 | + return matches |
| 76 | + } |
| 77 | + |
| 78 | + private static func waitForLayout( |
| 79 | + _ hosting: NSView, |
| 80 | + stage: String, |
| 81 | + timeout: Duration = .seconds(3), |
| 82 | + until condition: @MainActor () -> Bool) async throws |
| 83 | + { |
| 84 | + let clock = ContinuousClock() |
| 85 | + let deadline = clock.now.advanced(by: timeout) |
| 86 | + while clock.now < deadline { |
| 87 | + hosting.layoutSubtreeIfNeeded() |
| 88 | + if condition() { return } |
| 89 | + try await Task.sleep(for: .milliseconds(20)) |
| 90 | + } |
| 91 | + throw SettingsLayoutTimeout(stage: stage) |
| 92 | + } |
| 93 | + |
| 94 | + private struct SettingsLayoutTimeout: Error, CustomStringConvertible { |
| 95 | + let stage: String |
| 96 | + var description: String { |
| 97 | + "Settings layout timed out during \(self.stage)" |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments