| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | import SwiftUI |
| 4 | import Testing |
| 5 | @testable import EquipoKit |
| 6 | @testable import EquipoMac |
| 7 | |
| 8 | /// Paul, 2026-11-30: in the Mac conversation, a question with four options |
| 9 | /// drew them in one row and the long first title spilled out of its button. |
| 10 | @MainActor @Suite(.serialized) struct MacChoiceTests { |
| 11 | static let options = ["It's on Amadan, I'll give you the repos", "It's hosted somewhere else", |
| 12 | "It's a new project, start one", "I meant a different name"] |
| 13 | |
| 14 | private func optionButtons(_ options: [String], width: CGFloat) throws -> [ActionButton] { |
| 15 | let s = try ProjectsReviewTests().session(ScriptedTransport()) |
| 16 | var card: ThinCard = try Fixture.decode("project-card") |
| 17 | card.placementByName = nil; card.rememberName = nil; card.options = options |
| 18 | let view = LiveTranscript(session: s, openTask: { _ in }) |
| 19 | view.frame = NSRect(x: 0, y: 0, width: width, height: 800) |
| 20 | let window = NSWindow(contentRect: view.frame, styleMask: [.borderless], backing: .buffered, defer: false) |
| 21 | window.contentView = view |
| 22 | defer { window.contentView = nil } |
| 23 | view.update(items: [.question(card, RosterByline(who: .coordinator, verb: "asks", project: nil, time: nil))]); view.layout() |
| 24 | func all(_ v: NSView) -> [ActionButton] { ((v as? ActionButton).map { [$0] } ?? []) + v.subviews.flatMap(all) } |
| 25 | let found = options.compactMap { title in all(view).first { $0.title == title } } |
| 26 | try #require(found.count == options.count) |
| 27 | if let directory = ProcessInfo.processInfo.environment["EQUIPO_CHOICE_SCREENSHOTS"], |
| 28 | let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) { |
| 29 | view.cacheDisplay(in: view.bounds, to: bitmap) |
| 30 | let name = "mac-\(options.count)-\(Int(width)).png" |
| 31 | try bitmap.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: directory).appendingPathComponent(name)) |
| 32 | } |
| 33 | return found |
| 34 | } |
| 35 | |
| 36 | @Test(arguments: [700.0, 1000.0]) |
| 37 | func longOptionsStackAndNoTitleSpills(width: Double) throws { |
| 38 | let buttons = try optionButtons(Self.options, width: width) |
| 39 | for b in buttons { |
| 40 | #expect(b.frame.width >= ActionButton.idealWidth(b.title) || b.frame.height > 32, |
| 41 | "\(b.title) at \(Int(width)): \(b.frame.width) < \(ActionButton.idealWidth(b.title)) on one line") |
| 42 | } |
| 43 | #expect(Set(buttons.map(\.frame.minY)).count == buttons.count, "one per line") |
| 44 | #expect(Set(buttons.map(\.frame.width)).count == 1) |
| 45 | } |
| 46 | |
| 47 | @Test func shortOptionsStayInARow() throws { |
| 48 | let buttons = try optionButtons(["Yes", "No", "Not yet"], width: 700) |
| 49 | #expect(Set(buttons.map(\.frame.minY)).count == 1) |
| 50 | #expect(buttons.allSatisfy { $0.frame.width >= ActionButton.idealWidth($0.title) }) |
| 51 | } |
| 52 | |
| 53 | @Test func sendingKeepsTheRow() { |
| 54 | func place(busy: Bool) -> [NSRect] { |
| 55 | let host = FlippedView() |
| 56 | let buttons = ["Yes", "No", "Not yet"].enumerated().map { index, title in |
| 57 | button(title, index == 0 ? .primary : .choice, busy: busy && index == 0 ? "Sending…" : nil) |
| 58 | } |
| 59 | _ = placeChoices(buttons, in: host, x: 0, y: 0, width: 260) |
| 60 | return buttons.map(\.frame) |
| 61 | } |
| 62 | let idle = place(busy: false) |
| 63 | #expect(Set(idle.map(\.minY)).count == 1) |
| 64 | #expect(place(busy: true) == idle) |
| 65 | } |
| 66 | |
| 67 | @Test func aTitleLongerThanTheCardWraps() { |
| 68 | let title = "It's on Amadan, I'll give you the repos, all of them, under the oficina organisation" |
| 69 | let b = button(title, .primary) |
| 70 | let bottom = placeChoices([b, button("No")], in: FlippedView(), x: 0, y: 0, width: 240) |
| 71 | #expect(b.frame.width == 240) |
| 72 | #expect(b.frame.height > 32, "taller, so the wrapped title fits") |
| 73 | #expect(bottom > b.frame.maxY) |
| 74 | } |
| 75 | } |
| 76 | #endif |
| 77 | |