import Foundation import SwiftUI import Testing @testable import EquipoKit #if os(macOS) import AppKit /// Paul, 2026-11-30: four options on a question card sat side by side and /// the long first one spilled out of its button. No button may be narrower /// than its label, and tapping one must not re-stack the row. @MainActor @Suite struct ChoiceRowTests { static let options = ["It's on Amadan, I'll give you the repos", "It's hosted somewhere else", "It's a new project, start one", "I meant a different name"] @Test func rowOnlyWhenEveryLabelFitsItsShare() { #expect(ChoiceRow.fitsInRow([100, 100], width: 210, spacing: 10)) #expect(!ChoiceRow.fitsInRow([150, 50], width: 210, spacing: 10), "the total fits, the long one does not") #expect(!ChoiceRow.fitsInRow([100.4, 100], width: 210, spacing: 10), "no slack beyond float error") #expect(ChoiceRow.fitsInRow([900], width: 300, spacing: 10), "one button never stacks") } @Test(arguments: [(390.0, false), (720.0, false), (1100.0, false), (1300.0, true)]) func noButtonIsNarrowerThanItsLabel(width: Double, row: Bool) throws { let ideals = Self.options.map { NSHostingView(rootView: RosterButton(title: $0, expanded: true)).fittingSize.width } let (frames, host) = layOut(Self.options, width: width) for (index, ideal) in ideals.enumerated() { let frame = try #require(frames[index]) #expect(frame.width + 0.01 >= ideal, "\(Self.options[index]) at \(Int(width)): \(frame.width) < \(ideal)") } let ys = Set(frames.values.map { $0.minY.rounded() }), widths = Set(frames.values.map { $0.width.rounded() }) #expect(row ? ys.count == 1 && widths.count == 1 : ys.count == Self.options.count, "row \(row) at \(Int(width)): \(frames)") try save(host, name: "choices-\(Int(width))") } @Test func tappingAnOptionKeepsTheRow() throws { let options = ["Yes", "No", "Not yet"] let (idle, _) = layOut(options, width: 300) let (busy, host) = layOut(options, width: 300, busyIndex: 0) #expect(Set(idle.values.map { $0.minY.rounded() }).count == 1, "three short options sit in a row") #expect(busy == idle, "Sending… must not widen the button") try save(host, name: "choices-busy-300") } /// The cards themselves use ChoiceRow: at 1100 the long options stack, /// so the card grows by three more buttons than a two-option card. @Test func questionCardStacksLongOptions() throws { let session = EquipoSession(account: .new(origin: URL(string: "https://team.example.test")!, label: "Test"), accounts: DeviceAccounts(store: MemorySecretStore())) var card: ThinCard = try Fixture.decode("project-card") card.rememberName = nil; card.placementByName = nil func height(_ options: [String]) -> CGFloat { card.options = options let host = NSHostingView(rootView: LiveQuestionCard(card: card, taskTitle: nil, session: session) .padding(16).frame(width: 1100).environment(\.colorScheme, .light).background(.white)) host.frame.size = host.fittingSize host.layoutSubtreeIfNeeded() return host.frame.height } let button = NSHostingView(rootView: RosterButton(title: "Yes", expanded: true)).fittingSize.height #expect(height(Self.options) - height(["Yes", "No"]) >= 3 * button) } private func layOut(_ options: [String], width: Double, busyIndex: Int? = nil) -> ([Int: CGRect], NSHostingView) { let frames = Box() let row = ChoiceRow { ForEach(Array(options.enumerated()), id: \.offset) { index, title in RosterButton(title: title, primary: index == 0, expanded: true, busy: index == busyIndex ? "Sending…" : nil, busyInPlace: true) .onGeometryChange(for: CGRect.self, of: { $0.frame(in: .global) }) { frames.values[index] = $0 } } } .padding(16).frame(width: width).background(.white).environment(\.colorScheme, .light) let host = NSHostingView(rootView: row) host.frame.size = CGSize(width: width, height: host.fittingSize.height) host.layoutSubtreeIfNeeded() return (frames.values, host) } final class Box { var values: [Int: CGRect] = [:] } private func save(_ host: NSView, name: String) throws { guard let directory = ProcessInfo.processInfo.environment["EQUIPO_CHOICE_SCREENSHOTS"] else { return } let bitmap = try #require(host.bitmapImageRepForCachingDisplay(in: host.bounds)) host.cacheDisplay(in: host.bounds, to: bitmap) let folder = URL(fileURLWithPath: directory) try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) try #require(bitmap.representation(using: .png, properties: [:])).write(to: folder.appendingPathComponent(name + ".png")) } } #endif