| 1 | import Foundation |
| 2 | import SwiftUI |
| 3 | import Testing |
| 4 | @testable import EquipoKit |
| 5 | #if os(macOS) |
| 6 | import AppKit |
| 7 | |
| 8 | /// Paul, 2026-11-30: four options on a question card sat side by side and |
| 9 | /// the long first one spilled out of its button. No button may be narrower |
| 10 | /// than its label, and tapping one must not re-stack the row. |
| 11 | @MainActor @Suite struct ChoiceRowTests { |
| 12 | static let options = ["It's on Amadan, I'll give you the repos", "It's hosted somewhere else", |
| 13 | "It's a new project, start one", "I meant a different name"] |
| 14 | |
| 15 | @Test func rowOnlyWhenEveryLabelFitsItsShare() { |
| 16 | #expect(ChoiceRow.fitsInRow([100, 100], width: 210, spacing: 10)) |
| 17 | #expect(!ChoiceRow.fitsInRow([150, 50], width: 210, spacing: 10), "the total fits, the long one does not") |
| 18 | #expect(!ChoiceRow.fitsInRow([100.4, 100], width: 210, spacing: 10), "no slack beyond float error") |
| 19 | #expect(ChoiceRow.fitsInRow([900], width: 300, spacing: 10), "one button never stacks") |
| 20 | } |
| 21 | |
| 22 | @Test(arguments: [(390.0, false), (720.0, false), (1100.0, false), (1300.0, true)]) |
| 23 | func noButtonIsNarrowerThanItsLabel(width: Double, row: Bool) throws { |
| 24 | let ideals = Self.options.map { NSHostingView(rootView: RosterButton(title: $0, expanded: true)).fittingSize.width } |
| 25 | let (frames, host) = layOut(Self.options, width: width) |
| 26 | for (index, ideal) in ideals.enumerated() { |
| 27 | let frame = try #require(frames[index]) |
| 28 | #expect(frame.width + 0.01 >= ideal, "\(Self.options[index]) at \(Int(width)): \(frame.width) < \(ideal)") |
| 29 | } |
| 30 | let ys = Set(frames.values.map { $0.minY.rounded() }), widths = Set(frames.values.map { $0.width.rounded() }) |
| 31 | #expect(row ? ys.count == 1 && widths.count == 1 : ys.count == Self.options.count, "row \(row) at \(Int(width)): \(frames)") |
| 32 | try save(host, name: "choices-\(Int(width))") |
| 33 | } |
| 34 | |
| 35 | @Test func tappingAnOptionKeepsTheRow() throws { |
| 36 | let options = ["Yes", "No", "Not yet"] |
| 37 | let (idle, _) = layOut(options, width: 300) |
| 38 | let (busy, host) = layOut(options, width: 300, busyIndex: 0) |
| 39 | #expect(Set(idle.values.map { $0.minY.rounded() }).count == 1, "three short options sit in a row") |
| 40 | #expect(busy == idle, "Sending… must not widen the button") |
| 41 | try save(host, name: "choices-busy-300") |
| 42 | } |
| 43 | |
| 44 | /// The cards themselves use ChoiceRow: at 1100 the long options stack, |
| 45 | /// so the card grows by three more buttons than a two-option card. |
| 46 | @Test func questionCardStacksLongOptions() throws { |
| 47 | let session = EquipoSession(account: .new(origin: URL(string: "https://team.example.test")!, label: "Test"), |
| 48 | accounts: DeviceAccounts(store: MemorySecretStore())) |
| 49 | var card: ThinCard = try Fixture.decode("project-card") |
| 50 | card.rememberName = nil; card.placementByName = nil |
| 51 | func height(_ options: [String]) -> CGFloat { |
| 52 | card.options = options |
| 53 | let host = NSHostingView(rootView: LiveQuestionCard(card: card, taskTitle: nil, session: session) |
| 54 | .padding(16).frame(width: 1100).environment(\.colorScheme, .light).background(.white)) |
| 55 | host.frame.size = host.fittingSize |
| 56 | host.layoutSubtreeIfNeeded() |
| 57 | return host.frame.height |
| 58 | } |
| 59 | let button = NSHostingView(rootView: RosterButton(title: "Yes", expanded: true)).fittingSize.height |
| 60 | #expect(height(Self.options) - height(["Yes", "No"]) >= 3 * button) |
| 61 | } |
| 62 | |
| 63 | private func layOut(_ options: [String], width: Double, busyIndex: Int? = nil) -> ([Int: CGRect], NSHostingView<some View>) { |
| 64 | let frames = Box() |
| 65 | let row = ChoiceRow { |
| 66 | ForEach(Array(options.enumerated()), id: \.offset) { index, title in |
| 67 | RosterButton(title: title, primary: index == 0, expanded: true, |
| 68 | busy: index == busyIndex ? "Sending…" : nil, busyInPlace: true) |
| 69 | .onGeometryChange(for: CGRect.self, of: { $0.frame(in: .global) }) { frames.values[index] = $0 } |
| 70 | } |
| 71 | } |
| 72 | .padding(16).frame(width: width).background(.white).environment(\.colorScheme, .light) |
| 73 | let host = NSHostingView(rootView: row) |
| 74 | host.frame.size = CGSize(width: width, height: host.fittingSize.height) |
| 75 | host.layoutSubtreeIfNeeded() |
| 76 | return (frames.values, host) |
| 77 | } |
| 78 | |
| 79 | final class Box { var values: [Int: CGRect] = [:] } |
| 80 | |
| 81 | private func save(_ host: NSView, name: String) throws { |
| 82 | guard let directory = ProcessInfo.processInfo.environment["EQUIPO_CHOICE_SCREENSHOTS"] else { return } |
| 83 | let bitmap = try #require(host.bitmapImageRepForCachingDisplay(in: host.bounds)) |
| 84 | host.cacheDisplay(in: host.bounds, to: bitmap) |
| 85 | let folder = URL(fileURLWithPath: directory) |
| 86 | try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) |
| 87 | try #require(bitmap.representation(using: .png, properties: [:])).write(to: folder.appendingPathComponent(name + ".png")) |
| 88 | } |
| 89 | } |
| 90 | #endif |
| 91 | |