Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

186 KB
Raw Blame

// Appended to SwarmTitlebar.swift by check_swarm_titlebar.sh. Same-file
// extensions can inspect private controls without exposing them in the app API.
private struct TitlebarCheckFailure: Error {
  let message: String
}

private var titlebarCheckCount = 0
private func checkTitlebar(_ condition: @autoclosure () -> Bool, _ message: String) throws {
  guard condition() else { throw TitlebarCheckFailure(message: message) }
  titlebarCheckCount += 1
}

private final class TitlebarMouseUpProbe: NSResponder {
  var mouseUps = 0
  override func mouseUp(with event: NSEvent) { mouseUps += 1 }
}

private extension NSView {
  func renderedBitmap() -> NSBitmapImageRep {
    precondition(bounds.width >= 1 && bounds.height >= 1,
      "Cannot render empty \(type(of: self)) at \(frame), after \(titlebarCheckCount) checks")
    let bitmap = NSBitmapImageRep(bitmapDataPlanes: nil,
      pixelsWide: Int(bounds.width), pixelsHigh: Int(bounds.height),
      bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
      colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)!
    let count = bitmap.bytesPerRow * bitmap.pixelsHigh
    bitmap.bitmapData!.initialize(repeating: 0, count: count)
    NSGraphicsContext.saveGraphicsState()
    NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: bitmap)
    draw(bounds)
    NSGraphicsContext.restoreGraphicsState()
    return bitmap
  }
  func renderedPixels() -> Data {
    let bitmap = renderedBitmap()
    return Data(bytes: bitmap.bitmapData!, count: bitmap.bytesPerRow * bitmap.pixelsHigh)
  }

  func renderedTree(background: NSColor) -> NSBitmapImageRep {
    let bitmap = NSBitmapImageRep(bitmapDataPlanes: nil,
      pixelsWide: Int(bounds.width * 2), pixelsHigh: Int(bounds.height * 2),
      bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
      colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)!
    NSGraphicsContext.saveGraphicsState()
    defer { NSGraphicsContext.restoreGraphicsState() }
    NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: bitmap)
    let scale = NSAffineTransform(); scale.scale(by: 2); scale.concat()
    background.setFill(); bounds.fill()
    func paint(_ view: NSView, parentFlipped: Bool) {
      guard !view.isHidden else { return }
      NSGraphicsContext.saveGraphicsState()
      defer { NSGraphicsContext.restoreGraphicsState() }
      // Match AppKit's coordinate system for each child. Calling draw directly
      // in the bitmap's unflipped context concealed flipped-image bugs.
      if view.isFlipped != parentFlipped {
        let flip = NSAffineTransform()
        flip.translateX(by: 0, yBy: view.bounds.height)
        flip.scaleX(by: 1, yBy: -1)
        flip.concat()
      }
      NSGraphicsContext.current = NSGraphicsContext(
        cgContext: NSGraphicsContext.current!.cgContext, flipped: view.isFlipped)
      view.bounds.clip()
      view.draw(view.bounds)
      for child in view.subviews {
        NSGraphicsContext.saveGraphicsState()
        let move = NSAffineTransform()
        move.translateX(by: child.frame.minX - view.bounds.minX, yBy: child.frame.minY - view.bounds.minY)
        move.concat()
        paint(child, parentFlipped: view.isFlipped)
        NSGraphicsContext.restoreGraphicsState()
      }
    }
    paint(self, parentFlipped: false)
    return bitmap
  }
}

private func checkIconTooltipTracking() throws {
  let button = SwarmIconButton(frame: NSRect(x: 0, y: 0, width: 40, height: 28))
  button.toolTip = "Open Harness · ⌘P"
  let tooltipAreas = button.trackingAreas
  try checkTitlebar(!tooltipAreas.isEmpty, "AppKit installs tooltip tracking for an icon button")
  for _ in 0..<3 {
    button.updateTrackingAreas()
    try checkTitlebar(tooltipAreas.allSatisfy { area in button.trackingAreas.contains { $0 === area } },
      "Updating icon hover preserves AppKit's tooltip tracking")
    try checkTitlebar(button.trackingAreas.count == tooltipAreas.count + 1,
      "Repeated layout keeps exactly one custom hover area")
  }
}

private extension SwarmTabStrip {
  func checkActivityMarks() throws {
    func payload(_ mark: String, _ label: String, working: Bool = false) -> [String: Any] {
      ["mark": mark, "label": label, "working": working, "color": Int64(0xff64d2ff)]
    }
    let states: [(String, String)] = [("⠋", "Working"), ("?", "Needs your input"),
      ("✗", "Failed"), ("✓", "Finished · unread"), ("", "Idle"),
      ("◌", "Starting"), ("■", "Stopped"), ("⊘", "Offline")]
    update(["enabled": true, "activeId": "activity", "tabs": [
      ["id": "activity", "name": "desktop", "label": "1:desktop", "activity": payload("⠋", "Working", working: true)]
    ]])
    let tab = tabs[0]
    try checkTitlebar(activityTimer == nil, "A hidden native strip runs no animation timer")
    try tab.checkActivityDrawing(states: states)
    try tab.checkShortNamesFit()
    update(["enabled": true, "reduceMotion": true, "activeId": "activity", "tabs": [
      ["id": "activity", "name": "desktop", "label": "1:desktop", "activity": payload("⠋", "Working", working: true)]
    ]])
    try checkTitlebar(activityTimer == nil && tab.activityFrame == 0, "Reduce Motion parks the native spinner")
    update(["enabled": true, "tabs": []])
    try checkTitlebar(activityTimer == nil, "Removing the last working tab leaves no timer")
  }
}

private extension SwarmTabButton {
  func checkLabelMeasurementChanges() throws {
    // Reuse one populated tab through each single-property change. Compare it
    // with a newly constructed tab so an unrelated invalidation cannot hide
    // a stale width, stale status slot, old font, or old foreground color.
    func compareWithFresh(_ reason: String) throws {
      let fresh = SwarmTabButton(id: "fresh-label-check")
      fresh.name = name
      fresh.displayLabel = displayLabel
      fresh.shortcutHint = shortcutHint
      fresh.showsShortcutHint = showsShortcutHint
      fresh.foreground = foreground
      fresh.canShowAgentMessages = canShowAgentMessages
      fresh.labelFont = labelFont
      fresh.selected = selected
      fresh.attention = attention
      fresh.activity = activity
      fresh.activityFrame = activityFrame
      fresh.actionsEnabled = actionsEnabled
      fresh.frame = frame
      layoutSubtreeIfNeeded()
      fresh.layoutSubtreeIfNeeded()
      try checkTitlebar(naturalTitleWidth == max(label.size().width, emphasizedLabel.size().width),
        "\(reason): measured title matches AppKit's actual text")
      try checkTitlebar(titleRect == fresh.titleRect && activityRect == fresh.activityRect &&
        preferredWidth == fresh.preferredWidth && minimumWidth == fresh.minimumWidth,
        "\(reason): retained and fresh tab geometry agree")
      try checkTitlebar(toolTip == fresh.toolTip && renderedPixels() == fresh.renderedPixels(),
        "\(reason): retained and fresh tab pixels and tooltip agree")
    }
    frame = NSRect(x: 0, y: 0, width: 230, height: 40)
    displayLabel = "desktop"
    activity = HarnessNativeActivity(["mark": "⠋", "label": "Working", "working": true,
      "color": Int64(0xff64d2ff)])
    try compareWithFresh("Initial populated tab")
    for hint: String? in ["⌘1", "⌃⌥⌘R", nil] {
      shortcutHint = hint
      showsShortcutHint = true
      try compareWithFresh("Shortcut text and visibility")
    }
    showsShortcutHint = false
    for title in ["a-very-long-project-name-that-truncates", "", "设备 日本語", "🧑🏽‍💻 café مرحبا", "desktop"] {
      displayLabel = title
      try compareWithFresh("Rename displayed title")
    }
    for font in [NSFont.systemFont(ofSize: 20), NSFont.monospacedSystemFont(ofSize: 36, weight: .regular),
                 NSFont.systemFont(ofSize: 13)] {
      labelFont = font
      try compareWithFresh("Change font")
    }
    for log in [true, false] {
      canShowAgentMessages = log
      try compareWithFresh("Agent Messages log \(log)")
    }
    for color in [NSColor.systemRed, NSColor.black, NSColor.white] {
      foreground = color
      try compareWithFresh("Change foreground")
    }
    for active in [true, false] {
      selected = active
      try compareWithFresh("Change selected weight")
    }
    for tick in 0..<10 {
      activityFrame = tick
      try compareWithFresh("Animate frame \(tick)")
    }
    for enabled in [false, true] {
      actionsEnabled = enabled
      try compareWithFresh("Enter or leave a modal")
    }
    activity = nil
    try compareWithFresh("Remove activity")
    for needsInput in [true, false] {
      attention = needsInput
      try compareWithFresh("Change legacy attention")
    }
    for width in [CGFloat(28), 56, 125, 400] {
      frame.size.width = width
      needsLayout = true
      try compareWithFresh("Resize tab to \(width)")
    }
    name = "Renamed full title"
    try compareWithFresh("Rename full tooltip")
  }

  func checkActivityDrawing(states: [(String, String)]) throws {
    func payload(_ mark: String, _ label: String) -> [String: Any] {
      ["mark": mark, "label": label, "color": Int64(0xff64d2ff)]
    }
    let width = preferredWidth
    let displayedWidth = frame.width
    let rect = activityRect
    try checkTitlebar(label.string == "desktop" && statusSpace >= activityRect.width + 6,
      "Activity measures its gap and cell separately from the tab name")
    try checkTitlebar(rect.minX >= titleRect.maxX + 6,
      "The native activity follows the complete tab name")
    var pictures = Set<Data>()
    for frame in 0..<10 {
      // Sample inside each interval: Date's reference-epoch conversion can put
      // an exact decimal boundary a fraction of a microsecond before it.
      try checkTitlebar(harnessActivityFrame(at: Date(timeIntervalSince1970: Double(frame) / 10 + 0.05)) == frame,
        "The native spinner uses hn's exact 100ms frame \(frame)")
      activityFrame = frame
      pictures.insert(renderedPixels())
      try checkTitlebar(preferredWidth == width && activityRect == rect,
        "A spinner frame never moves native tab text")
    }
    try checkTitlebar(pictures.count == 10, "All ten Braille frames render distinctly")
    for (mark, label) in states {
      activity = HarnessNativeActivity(payload(mark, label))
      layoutSubtreeIfNeeded()
      try checkTitlebar(preferredWidth == width && (mark.isEmpty || activityRect == rect),
        "\(label) preserves tab width and visible marks share their inline cell")
      try checkTitlebar(toolTip == label && selectButton.accessibilityHelp() == label,
        "\(label) has readable hover and accessibility descriptions")
      for narrow in [CGFloat(28), CGFloat(56), width, displayedWidth] {
        frame.size.width = narrow
        let bitmap = renderedBitmap()
        if let capture = ProcessInfo.processInfo.environment["HARNESS_ACTIVITY_CAPTURE_DIR"] {
          try bitmap.representation(using: .png, properties: [:])!.write(to:
            URL(fileURLWithPath: capture).appendingPathComponent("native-\(label)-\(Int(narrow)).png"))
        }
        let colored = (0..<bitmap.pixelsWide).contains { x in
          (0..<bitmap.pixelsHigh).contains { y in
            guard let pixel = bitmap.colorAt(x: x, y: y)?.usingColorSpace(.sRGB),
                  pixel.alphaComponent > 0.2 else { return false }
            // Antialiased dots need not contain a fully opaque ink pixel.
            // Only the mark has cyan chroma; the title and background are gray.
            return pixel.blueComponent - pixel.redComponent > 0.15 &&
              pixel.greenComponent - pixel.redComponent > 0.10 &&
              pixel.blueComponent - pixel.greenComponent > 0.04
          }
        }
        try checkTitlebar(colored == !mark.isEmpty,
          "\(label) \(mark.isEmpty ? "stays unmarked" : "remains visible") at tab width \(narrow)")
      }
      frame.size.width = displayedWidth
    }
  }

  func checkShortNamesFit() throws {
    for name in ["desktop", "device", "swarm", "daemons", "web", "tui", "mobile"] {
      displayLabel = name
      activity = HarnessNativeActivity(["mark": "", "label": "Idle", "color": Int64(0xff999999)])
      frame.size.width = preferredWidth
      let available = titleRect.width
      try checkTitlebar(available >= max(label.size().width, emphasizedLabel.size().width),
        "\(name) fits at its preferred width in both weights without a false ellipsis")
    }
  }
}

private extension SwarmContextButton {
  func checkPullRequestIcons() throws {
    let previous = Self.statusIcons
    defer { Self.statusIcons = previous }
    Self.statusIcons = SwarmHistoryIcons(assetURL: { asset in
      guard let root = ProcessInfo.processInfo.environment["HARNESS_TITLEBAR_ASSETS"] else { return nil }
      return URL(fileURLWithPath: root).appendingPathComponent(String(asset.dropFirst("assets/".count)))
    })
    font = NSFont.monospacedSystemFont(ofSize: 13, weight: .regular)
    let states: [(String, String, Int64)] = [("Open", "git-pull-request", 0xff3fb950),
      ("Merged", "git-merge", 0xffbc8cff), ("Closed", "git-pull-request-closed", 0xfff85149),
      ("Draft", "git-pull-request-draft", 0xff9198a1)]
    var shapes = Set<Data>()
    var width: CGFloat?
    for (state, name, color) in states {
      var payload: [String: Any] = ["text": "#436", "label": "#436 \(state)",
        "detail": "#436 \(state) — Open on GitHub", "interactive": true,
        "iconAsset": "assets/octicons/\(name).svg", "iconColor": color,
        "segments": [["text": "#436", "foreground": Int64(0xffdddddd)]]]
      update(payload, enabled: true)
      frame = NSRect(x: 0, y: 0, width: preferredWidth, height: 28)
      let bitmap = renderedBitmap()
      let expected = statusColor(color, fallback: .clear).usingColorSpace(.sRGB)!
      let colored = (0..<13).contains { x in
        (0..<28).contains { y in
          guard let pixel = bitmap.colorAt(x: x, y: y)?.usingColorSpace(.sRGB), pixel.alphaComponent > 0.4 else { return false }
          return abs(pixel.redComponent - expected.redComponent) < 0.03 &&
            abs(pixel.greenComponent - expected.greenComponent) < 0.03 &&
            abs(pixel.blueComponent - expected.blueComponent) < 0.03
        }
      }
      try checkTitlebar(colored && accessibilityValue() as? String == "#436 \(state)" && toolTip?.contains(state) == true,
        "\(state) draws its colored Octicon and retains the full accessible/hover status")
      width = width ?? preferredWidth
      try checkTitlebar(preferredWidth == width, "Changing PR state never moves the title bar")
      if let capture = ProcessInfo.processInfo.environment["HARNESS_ACTIVITY_CAPTURE_DIR"] {
        try bitmap.representation(using: .png, properties: [:])!.write(to:
          URL(fileURLWithPath: capture).appendingPathComponent("native-pr-\(state).png"))
      }
      payload["iconColor"] = Int64(0xffdddddd)
      update(payload, enabled: true)
      shapes.insert(renderedPixels())
      // A ribbon uses the PR state as its fill; the same Octicon and number
      // must use contrasting ink inside it, not the standalone icon's tint.
      payload["segmented"] = true
      payload["roundedEnd"] = true
      payload["segments"] = [["text": "#436", "foreground": Int64(0xff111111),
        "background": color]]
      update(payload, enabled: true)
      frame.size.width = preferredWidth
      let ribbon = renderedBitmap()
      let cell = ("m" as NSString).size(withAttributes: [.font: textFont]).width
      let iconInk = (Int(cell)..<Int(cell) + 13).contains { x in
        (0..<28).contains { y in
          guard let pixel = ribbon.colorAt(x: x, y: y)?.usingColorSpace(.sRGB) else { return false }
          // The thin 13px strokes are antialiased at this fixture's 1x scale.
          return pixel.alphaComponent > 0.9 && pixel.redComponent < expected.redComponent * 0.7 &&
            pixel.greenComponent < expected.greenComponent * 0.7 &&
            pixel.blueComponent < expected.blueComponent * 0.7
        }
      }
      let fill = ribbon.colorAt(x: 1, y: 14)!.usingColorSpace(.sRGB)!
      try checkTitlebar(iconInk && abs(fill.redComponent - expected.redComponent) < 0.03 &&
        abs(fill.greenComponent - expected.greenComponent) < 0.03 &&
        abs(fill.blueComponent - expected.blueComponent) < 0.03,
        "\(state) draws contrasting icon ink inside the final colored ribbon block")
      try checkTitlebar(abs(preferredWidth - width! - cell * 3) < 1,
        "The PR ribbon reserves only the shared segment padding and end cap")
    }
    try checkTitlebar(shapes.count == 4, "All four PR states remain distinct with Color off")
  }

  func checkThemeSymbolsAndCaps() throws {
    font = NSFont.monospacedSystemFont(ofSize: 13, weight: .regular)
    textAlignment = .left
    nextBackground = nil
    let pointer = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    for ribbon in [false, true] {
      var segment: [String: Any] = ["text": "feature/日本語", "foreground": Int64(0xff11111b)]
      if ribbon { segment["background"] = Int64(0xfff9e2af) }
      var payload: [String: Any] = ["text": "feature/日本語", "interactive": true,
        "segmented": ribbon, "segments": [segment]]
      update(payload, enabled: true)
      let withoutSymbol = preferredWidth
      segment["branchSymbol"] = true
      payload["segments"] = [segment]
      update(payload, enabled: true)
      try checkTitlebar(abs(preferredWidth - withoutSymbol - cellWidth * 2) < 1,
        "Branch symbol reserves two cells in plain and segmented themes")
      frame = NSRect(x: 0, y: 0, width: preferredWidth, height: 28)
      let resting = renderedPixels()
      let width = preferredWidth
      mouseEntered(with: pointer)
      try checkTitlebar(renderedPixels() != resting && width == preferredWidth,
        "Branch symbol text becomes bold without moving the icon or click target")
      mouseExited(with: pointer)
      try checkTitlebar(accessibilityValue() as? String == "feature/日本語",
        "Decorative branch symbols leave the full accessible branch name intact")
      for narrow in [CGFloat(12), CGFloat(40), CGFloat(80)] {
        frame.size.width = narrow
        try checkTitlebar(!renderedPixels().isEmpty, "Icon-bearing status safely truncates at width \(narrow)")
      }
      guard ribbon else { continue }
      payload["roundedStart"] = true
      payload["roundedEnd"] = true
      payload["roundedSeparators"] = true
      update(payload, enabled: true)
      frame.size.width = preferredWidth
      let rounded = renderedBitmap()
      try checkTitlebar((rounded.colorAt(x: 0, y: 7)?.alphaComponent ?? 1) < 0.1 &&
        (rounded.colorAt(x: 4, y: 14)?.alphaComponent ?? 0) > 0.99,
        "Rounded palettes have a clear capsule corner and an opaque center")
      nextBackground = NSColor(srgbRed: 0.7, green: 0.5, blue: 0.8, alpha: 1)
      let joined = renderedBitmap()
      try checkTitlebar((joined.colorAt(x: 0, y: 7)?.alphaComponent ?? 1) < 0.1 &&
        (joined.colorAt(x: joined.pixelsWide - 1, y: 7)?.alphaComponent ?? 0) > 0.99,
        "A joined PR fills the tail without filling the leading rounded corner")
      nextBackground = nil
    }
  }
}

private extension SwarmTabButton {
  // `label` is private to the tab; a same-file extension reads what it draws.
  var drawnFont: NSFont? { label.attribute(.font, at: 0, effectiveRange: nil) as? NSFont }
  var drawsShortcut: Bool { displaysShortcut }

  func checkHoverStyleAndTooltips() throws {
    frame = NSRect(x: 0, y: 0, width: 160, height: 40)
    contentCenterY = 20
    name = "office"
    displayLabel = "office"
    layoutSubtreeIfNeeded()
    try checkTitlebar(toolTip == nil, "A fully visible tab name has no repeating tooltip")
    displayLabel = "code"
    layoutSubtreeIfNeeded()
    try checkTitlebar(toolTip == "office", "A different underlying tab name remains available")
    name = "code"
    layoutSubtreeIfNeeded()
    try checkTitlebar(toolTip == nil, "Renaming a tab clears a redundant tooltip immediately")
    name = "a-very-long-project-name"
    displayLabel = name
    layoutSubtreeIfNeeded()
    try checkTitlebar(toolTip == displayLabel, "An ellipsized tab reveals its full label")
    frame.size.width = 400
    needsLayout = true
    layoutSubtreeIfNeeded()
    try checkTitlebar(toolTip == nil, "Widening a tab removes a now-redundant tooltip")

    let symbol = SwarmPlainIconButton(frame: NSRect(x: 0, y: 0, width: 32, height: 28))
    symbol.image = HarnessControlSymbols.image("plus")
    let hover = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    let symbolResting = symbol.renderedPixels()
    symbol.mouseEntered(with: hover)
    try checkTitlebar(symbol.renderedPixels() != symbolResting &&
      symbol.renderedBitmap().colorAt(x: 1, y: 1)!.alphaComponent == 0,
      "Symbols emphasize their ink on hover without changing weight or adding a background")
    activity = HarnessNativeActivity(["mark": "⠋", "label": "Working", "color": Int64(0xff64d2ff)])
    layoutSubtreeIfNeeded()
    let width = preferredWidth
    let resting = renderedPixels()
    mouseEntered(with: hover)
    let hovered = renderedBitmap()
    try checkTitlebar(hovered.colorAt(x: 20, y: 12)!.alphaComponent > 0 && renderedPixels() != resting,
      "Hover quietly marks the tab surface")
    try checkTitlebar(preferredWidth == width && !closeButton.isHidden && drawnFont == labelFont,
      "Hover reveals the right close button without changing tab width or type")
    mouseExited(with: hover)
    selectButton.highlight(true)
    try checkTitlebar(renderedPixels() != resting, "Pressing a tab uses the same visible highlight")
    selectButton.highlight(false)
    actionsEnabled = false
    mouseEntered(with: hover)
    try checkTitlebar(renderedPixels() == resting, "Disabled tabs never show an actionable hover")
    actionsEnabled = true
    selected = true
    let active = renderedBitmap()
    let selectedRows = (0..<active.pixelsHigh).filter { active.colorAt(x: 1, y: $0)!.alphaComponent > 0 }
    try checkTitlebar(selectedRows.count > 0 && selectedRows.count < 8 &&
      active.colorAt(x: 1, y: 20)!.alphaComponent == 0 &&
      active.colorAt(x: 20, y: 20)!.alphaComponent == 1 &&
      active.colorAt(x: 20, y: 0)!.alphaComponent == 0,
      "The selected tab has upper curves, lower outward shoulders and a clear top inset")
    mouseExited(with: hover)
    for argb in [Int64(0xff282828), Int64(0xfff0f2f5)] {
      palette = SwarmNativePalette(["workspace": argb])
      let background = palette.workspace
      let pixel = renderedBitmap().colorAt(x: 20, y: Int(bounds.height) - 2)!.usingColorSpace(.sRGB)!
      try checkTitlebar(abs(pixel.redComponent - background.redComponent) < 0.01 &&
        abs(pixel.greenComponent - background.greenComponent) < 0.01 &&
        abs(pixel.blueComponent - background.blueComponent) < 0.01,
        "Active tabs join the workspace color in light and dark palettes")
    }
  }

  func checkHoverClose() throws {
    // Room for the name beside 20pt targets 8pt apart (they were 16pt and 3pt apart, in 240pt).
    frame = NSRect(x: 0, y: 0, width: 260, height: 40)
    contentCenterY = 20
    name = "1: Release planning"
    displayLabel = name
    activity = HarnessNativeActivity(["mark": "⠋", "label": "Working", "color": Int64(0xff64d2ff)])
    layoutSubtreeIfNeeded()
    let title = titleRect, width = preferredWidth
    try checkTitlebar(closeButton.isHidden, "Resting tabs show no close icon")
    var events: [String] = []
    emit = { method, _ in events.append(method) }
    let hover = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    mouseEntered(with: hover)
    try checkTitlebar(!closeButton.isHidden && closeButton.frame.minX > title.maxX &&
      closeButton.frame.size == NSSize(width: 20, height: 32) && closeButton.frame.maxX == bounds.maxX - 8,
      "A hovered tab exposes a separate 20x32pt close target at its right inset")
    try checkTitlebar(accessibilityChildren()?.count == 2 && closeButton.accessibilityLabel() == "Close 1: Release planning",
      "The hover close button has the exact custom title as its accessible purpose")
    let closeBitmap = closeButton.renderedBitmap()
    let closeInk = (0..<closeBitmap.pixelsWide).flatMap { x in
      (0..<closeBitmap.pixelsHigh).compactMap { y -> NSPoint? in
        (closeBitmap.colorAt(x: x, y: y)?.alphaComponent ?? 0) > 0.1
          ? NSPoint(x: x, y: y) : nil
      }
    }
    let inkWidth = (closeInk.map(\.x).max() ?? 0) - (closeInk.map(\.x).min() ?? 0) + 1
    let inkHeight = (closeInk.map(\.y).max() ?? 0) - (closeInk.map(\.y).min() ?? 0) + 1
    try checkTitlebar(!closeInk.isEmpty && inkWidth <= 7 && inkHeight <= 7,
      "The tab close paints the pane's 7pt cross inside its target")
    closeButton.performClick(nil)
    try checkTitlebar(events == ["close"], "Clicking the close control closes once without selecting the tab")
    let hoveredBitmap = renderedBitmap()
    let hasActivityInk = (0..<hoveredBitmap.pixelsWide).contains { x in
      (0..<hoveredBitmap.pixelsHigh).contains { y in
        guard let pixel = hoveredBitmap.colorAt(x: x, y: y)?.usingColorSpace(.sRGB),
          pixel.alphaComponent > 0.2 else { return false }
        return pixel.blueComponent - pixel.redComponent > 0.15 &&
          pixel.greenComponent - pixel.redComponent > 0.1
      }
    }
    try checkTitlebar(hasActivityInk && selectButton.accessibilityHelp() == "Working",
      "Hover keeps the activity mark: no shortcut takes its place")
    try checkTitlebar(titleRect == title && activityRect.maxX <= closeButton.frame.minX && preferredWidth == width,
      "A name with room keeps its place on hover, its status clear of ×")
    mouseExited(with: hover)
    try checkTitlebar(closeButton.isHidden && accessibilityChildren()?.count == 1,
      "Leaving returns the tab to its quiet state")
    actionsEnabled = false
    mouseEntered(with: hover)
    closeButton.performClick(nil)
    try checkTitlebar(closeButton.isHidden && events == ["close"], "A modal disables and hides the hover close action")
    emit = nil
  }

  func drawWithHoverControl() {
    draw(bounds)
    for control in [selectButton, closeButton] where !control.isHidden {
      NSGraphicsContext.saveGraphicsState()
      let transform = NSAffineTransform()
      transform.translateX(by: control.frame.minX, yBy: control.frame.minY)
      transform.concat()
      control.draw(control.bounds)
      NSGraphicsContext.restoreGraphicsState()
    }
  }

  func checkSelectionSurface() throws {
    frame = NSRect(x: 0, y: 0, width: 256, height: 40)
    layoutSubtreeIfNeeded()
    let hover = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    for appearance in [NSAppearance.Name.darkAqua, .aqua] {
      self.appearance = NSAppearance(named: appearance)
      for active in [false, true] {
        selected = active
        for pressed in [false, true] {
          selectButton.performClick(nil)
          selectButton.mouseEntered(with: hover)
          selectButton.highlight(pressed)
          let bitmap = selectButton.renderedBitmap()
          let paintsOverTab = (0..<bitmap.pixelsWide).contains { x in
            (0..<bitmap.pixelsHigh).contains { y in
              (bitmap.colorAt(x: x, y: y)?.alphaComponent ?? 0) > 0
            }
          }
          try checkTitlebar(!paintsOverTab,
            "The selection target adds no second surface over the tab in \(appearance.rawValue), selected=\(active), pressed=\(pressed)")
          selectButton.highlight(false)
          selectButton.mouseExited(with: hover)
        }
      }
    }
  }

  func checkClickOwnership() throws {
    let originalEmit = emit
    var actions: [String] = []
    emit = { method, _ in actions.append(method) }
    defer { emit = originalEmit }
    func click(_ count: Int, time: TimeInterval) -> NSEvent {
      NSEvent.mouseEvent(with: .leftMouseDown, location: NSPoint(x: 60, y: 20),
        modifierFlags: [], timestamp: time, windowNumber: 0,
        context: nil, eventNumber: count, clickCount: count, pressure: 1)!
    }
    // AppKit can carry its click count across tabs, including a tab that
    // moved into the pointer's position after its neighbor was closed.
    selectButton.mouseDown(with: click(2, time: 10))
    try checkTitlebar(actions == ["select"],
      "A second click originating on another tab selects this tab instead of renaming it")
    actions.removeAll()
    selectButton.mouseDown(with: click(1, time: 20))
    selectButton.mouseDown(with: click(2, time: 20.1))
    try checkTitlebar(actions == ["select", "rename"],
      "A double-click that starts and ends on this tab still renames it")
  }

  func checkDoubleClickIsolation() throws {
    let parent = nextResponder
    let originalEmit = emit
    let probe = TitlebarMouseUpProbe()
    nextResponder = probe
    var actions: [String] = []
    emit = { method, _ in actions.append(method) }
    defer {
      nextResponder = parent
      emit = originalEmit
      actionsEnabled = true
    }
    func event(_ type: NSEvent.EventType, _ count: Int) -> NSEvent {
      NSEvent.mouseEvent(with: type, location: NSPoint(x: 60, y: 20),
        modifierFlags: [], timestamp: Double(count) / 10, windowNumber: 0,
        context: nil, eventNumber: count, clickCount: count, pressure: 1)!
    }
    selectButton.mouseDown(with: event(.leftMouseDown, 1))
    selectButton.mouseUp(with: event(.leftMouseUp, 1))
    selectButton.mouseDown(with: event(.leftMouseDown, 2))
    // Rename can open a modal before the second mouse-up arrives.
    actionsEnabled = false
    selectButton.mouseUp(with: event(.leftMouseUp, 2))
    mouseUp(with: event(.leftMouseUp, 2))
    try checkTitlebar(actions == ["select", "rename"], "A tab double-click selects and renames exactly once")
    try checkTitlebar(probe.mouseUps == 0,
      "Tab mouse-up events cannot reach the window's titlebar double-click handler")
    try checkTitlebar(!selectButton.mouseDownCanMoveWindow,
      "Tab action buttons opt out of automatic window movement")
  }

  func checkAccessibility(expectedName: String, active: Bool) throws {
    try checkTitlebar(accessibilityLabel() == expectedName, "Tab group name is available before paint")
    let children = accessibilityChildren()?.compactMap { $0 as? NSButton } ?? []
    try checkTitlebar(children.count == 1, "A tab exposes one full-width selection button")
    try checkTitlebar(children[0].accessibilityLabel() == "Select \(expectedName)", "Selection button has current name")
    try checkTitlebar(children[0].accessibilityValue() as? String == (active ? "Selected" : ""), "Selection value is current")
  }

  func checkEnabled(_ enabled: Bool) throws {
    try checkTitlebar(selectButton.isEnabled == enabled, "Select button obeys modal state")
    for item in menu?.items ?? [] {
      try checkTitlebar(validateMenuItem(item) == enabled, "Tab context menu obeys modal state")
    }
  }

  func checkCenteredLabel() throws {
    let paragraph = label.attribute(.paragraphStyle, at: 0, effectiveRange: nil) as? NSParagraphStyle
    let resting = titleRect(revealed: false)
    let group = trailingSpace == 0 ? resting
      : NSRect(x: resting.minX, y: 0, width: resting.width + trailingSpace, height: bounds.height)
    try checkTitlebar(paragraph?.alignment == .center && abs(group.midX - bounds.midX) < 0.01,
      "The resting title and adjacent status form a centered group")
    let revealed = titleRect(revealed: true)
    try checkTitlebar(revealed.minX <= resting.minX && revealed.minX >= 14 &&
      revealed.maxX + trailingSpace <= closeButton.frame.minX + 0.01,
      "Revealed, the title moves only left, and only as far as × needs")
    try checkTitlebar(selectButton.frame == bounds, "Selection and drag retain the whole tab target beneath its accessory")
    try checkTitlebar(subviews.count == 2 && closeButton.frame.maxX == bounds.maxX - 8,
      "The close action alone stays inset inside the tab's curved body")
  }

  /// Agent Messages (docs/design/2026-10-10-agent-messages.md) has nothing on
  /// a tab: no 💬 at rest or hovered, and no on/off row in its menu, since the
  /// 💬 beside × was easy to hit by mistake and turned the tab off, failing a
  /// waiting message (owner, 2026-10-12). Its menu keeps Show Agent Messages.
  func checkAgentMessages() throws {
    frame = NSRect(x: 0, y: 0, width: 240, height: 40)
    contentCenterY = 20
    name = "app"
    displayLabel = "app"
    activity = nil
    layoutSubtreeIfNeeded()
    var events: [(String, String?)] = []
    emit = { method, args in events.append((method, (args as? [String: Any])?["id"] as? String)) }
    defer { emit = nil }
    let hover = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    let rows = menu?.items ?? []
    // Shared tabs' rows (1c) sit around Show Agent Messages; checkSharedTab covers them. Unshared and without
    // Invite People… they are all hidden.
    try checkTitlebar(rows.map(\.title) == ["Rename Tab…", "", "Invite People…", "Show Agent Messages",
      "Stop Sharing Tab", "Move Here", "Leave Tab", "", "Close Tab"],
      "The tab menu: Rename Tab…, the sharing rows around Show Agent Messages, Close Tab; no Agent Messages on/off row")
    try checkTitlebar(rows[1...6].allSatisfy(\.isHidden), "A tab without a log (the Store) lists no Show Agent Messages")
    try checkTitlebar(subviews.count == 2, "A tab holds only its selection target and ×")

    canShowAgentMessages = true
    try checkTitlebar(!rows[1].isHidden && !rows[3].isHidden && [2, 4, 5, 6].allSatisfy { rows[$0].isHidden },
      "A tab with a log lists Show Agent Messages")
    try checkTitlebar(closeButton.isHidden && accessibilityChildren()?.count == 1,
      "At rest a tab shows only its name and status")
    let title = titleRect
    mouseEntered(with: hover)
    try checkTitlebar(!closeButton.isHidden && accessibilityChildren()?.count == 2 && titleRect == title,
      "Hovered, a tab shows its name, status and ×, and nothing else")
    mouseExited(with: hover)

    activity = HarnessNativeActivity(["mark": "?", "label": "Needs your input", "color": Int64(0xffe5e510)])
    layoutSubtreeIfNeeded()
    try checkTitlebar(selectButton.accessibilityHelp() == "Needs your input",
      "The status slot keeps the tab's own status")
    activity = nil
    let show = rows[3]
    try checkTitlebar(validateMenuItem(show) && show.state == .off, "Show Agent Messages is a plain row, never checked")
    _ = (show.target as AnyObject).perform(show.action, with: show)
    try checkTitlebar(events.count == 1 && events[0] == ("showAgentMessages", swarmId),
      "The tab menu's Show Agent Messages opens this tab's log")
    actionsEnabled = false
    _ = (show.target as AnyObject).perform(show.action, with: show)
    try checkTitlebar(!validateMenuItem(show) && events.count == 1, "A modal disables Show Agent Messages")
    actionsEnabled = true
    canShowAgentMessages = false
    _ = (show.target as AnyObject).perform(show.action, with: show)
    try checkTitlebar(rows[3].isHidden && rows[1].isHidden && events.count == 1,
      "Without a log the row is gone and asks nothing")
  }

  /// A tab shared with other accounts (1c): a two-person mark and the
  /// harness count after the name, counted in the title group; its menu by
  /// role. The selection button keeps "Select <name>" as its label (UI tests
  /// find tabs by it) and says it is shared in its help.
  func checkSharedTab() throws {
    frame = NSRect(x: 0, y: 0, width: 240, height: 40)
    contentCenterY = 20
    name = "app"
    displayLabel = "app"
    activity = nil
    canShowAgentMessages = true
    layoutSubtreeIfNeeded()
    var events: [(String, String?)] = []
    emit = { method, args in events.append((method, (args as? [String: Any])?["id"] as? String)) }
    defer { emit = nil }
    let rows = menu?.items ?? []
    func visible() -> [String] { rows.filter { !$0.isHidden && !$0.isSeparatorItem }.map(\.title) }
    func row(_ title: String) -> NSMenuItem { rows.first { $0.title == title }! }

    // Unshared, where shares can be read (or Agent Messages is off): Invite only.
    let unsharedMinimum = minimumWidth
    let unsharedHelp = selectButton.accessibilityHelp()
    try checkTitlebar(visible() == ["Rename Tab…", "Show Agent Messages", "Close Tab"],
      "Without canInvitePeople an unshared tab offers nothing of sharing")
    canInvitePeople = true
    try checkTitlebar(visible() == ["Rename Tab…", "Invite People…", "Show Agent Messages", "Close Tab"],
      "An unshared tab offers Invite People… before Show Agent Messages")
    try checkTitlebar(sharedMarkWidth == 0 && minimumWidth == unsharedMinimum,
      "An unshared tab draws no mark")

    applyShared(["count": 7, "role": "owner", "canMoveHere": true])
    layoutSubtreeIfNeeded()
    try checkTitlebar(sharedMarkWidth > 0 && sharedMarkWidth == sharedLabelLayout!.size.width + Self.sharedImageWidth + Self.sharedImageGap,
      "The mark is the cached person.2.fill image and count label")
    try checkTitlebar(abs(minimumWidth - unsharedMinimum - (sharedMarkWidth + Self.gap)) < 0.01,
      "The tab's width grew by the cached mark's width")
    try checkTitlebar(sharedLabelLayout?.text.string == "7", "The mark's label is the harness count")
    try checkTitlebar(visible() == ["Rename Tab…", "Invite People…", "Show Agent Messages", "Stop Sharing Tab",
      "Move Here", "Close Tab"], "The owner's menu: Invite People…, Stop Sharing Tab and Move Here, in that order")
    try checkTitlebar(accessibilityLabel() == "app", "The tab group's label stays its name")
    try checkTitlebar(selectButton.accessibilityLabel() == "Select app", "The selection button keeps Select <name>")
    try checkTitlebar(selectButton.accessibilityHelp() == "Shared tab, 7 harnesses",
      "The selection button's help says it is shared, and how many harnesses")
    try checkTitlebar(sharedRect.minX >= titleRect.maxX + Self.gap - 0.01 && sharedRect.maxX <= bounds.maxX,
      "The mark sits after the name, inside the tab")
    // The name gives way to the mark; the count is never cut.
    name = String(repeating: "x", count: 300)
    displayLabel = name
    layoutSubtreeIfNeeded()
    let hover = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    mouseEntered(with: hover)
    layoutSubtreeIfNeeded()
    try checkTitlebar(titleRect.width < naturalTitleWidth && sharedRect.width == sharedMarkWidth &&
      sharedRect.maxX <= closeButton.frame.minX + 0.01,
      "A long name is cut before the mark, which stays whole and clear of ×")
    mouseExited(with: hover)
    name = "app"
    displayLabel = "app"
    _ = rendered()

    for title in ["Invite People…", "Stop Sharing Tab", "Move Here"] {
      let item = row(title)
      try checkTitlebar(validateMenuItem(item), "\(title) is enabled")
      _ = (item.target as AnyObject).perform(item.action, with: item)
    }
    try checkTitlebar(events.map(\.0) == ["invitePeople", "stopSharingTab", "moveTabHere"] &&
      events.allSatisfy { $0.1 == swarmId }, "Each row asks for its action on this tab")

    applyShared(["count": 7, "role": "owner", "canMoveHere": false])
    try checkTitlebar(visible() == ["Rename Tab…", "Invite People…", "Show Agent Messages", "Stop Sharing Tab",
      "Close Tab"], "Shared from this computer: no Move Here")

    applyShared(["count": 1, "role": "member", "canMoveHere": false])
    try checkTitlebar(visible() == ["Rename Tab…", "Show Agent Messages", "Leave Tab", "Close Tab"],
      "A member's menu: Show Agent Messages and Leave Tab, no Invite")
    try checkTitlebar(selectButton.accessibilityHelp() == "Shared tab, 1 harness", "One harness is singular")
    events.removeAll()
    let invite = row("Invite People…")
    _ = (invite.target as AnyObject).perform(invite.action, with: invite)
    let leave = row("Leave Tab")
    _ = (leave.target as AnyObject).perform(leave.action, with: leave)
    try checkTitlebar(events.map(\.0) == ["leaveTab"], "A hidden row asks nothing; Leave Tab asks to leave")
    actionsEnabled = false
    _ = (leave.target as AnyObject).perform(leave.action, with: leave)
    try checkTitlebar(!validateMenuItem(leave) && events.count == 1, "A modal disables Leave Tab")
    actionsEnabled = true

    // Unshared again: everything back as it was.
    applyShared(nil)
    canInvitePeople = false
    layoutSubtreeIfNeeded()
    try checkTitlebar(sharedMarkWidth == 0 && minimumWidth == unsharedMinimum &&
      selectButton.accessibilityHelp() == unsharedHelp && selectButton.accessibilityLabel() == "Select app",
      "Unshared, the tab has no mark and its accessibility is as before")
    try checkTitlebar(visible() == ["Rename Tab…", "Show Agent Messages", "Close Tab"],
      "Unshared, without Invite, the menu is as before")
  }

  private func rendered() -> Data { renderedPixels() }

  /// At the 128pt minimum, a tab draws its whole name beside a status at
  /// rest, and still 16pt of it when hovered.
  func checkMinimumWidth() throws {
    frame = NSRect(x: 0, y: 0, width: SwarmTabButton.minimumLayoutWidth, height: 40)
    contentCenterY = 20
    name = "website"
    displayLabel = "website"
    activity = HarnessNativeActivity(["mark": "⠋", "label": "Working", "working": true, "color": Int64(0xff64d2ff)])
    layoutSubtreeIfNeeded()
    try checkTitlebar(bounds.width >= minimumWidth && titleRect.width == naturalTitleWidth,
      "At rest a 128pt tab shows \"website\" whole beside its status")
    let hover = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    mouseEntered(with: hover)
    layoutSubtreeIfNeeded()
    try checkTitlebar(titleRect.width >= 16 && titleRect.maxX + statusSpace <= closeButton.frame.minX + 0.01,
      "Hovered, a 128pt tab keeps 16pt of its name, clear of ×")
    mouseExited(with: hover)
    try checkCenteredLabel()
  }

  func checkNameFitsOrHasTooltip() throws {
    try checkTitlebar(titleRect.width >= naturalTitleWidth ||
      toolTip?.components(separatedBy: "\n").contains(displayLabel) == true,
      "Equal-width tabs retain the full name in a tooltip when the title truncates")
  }

  func clickBothActions() {
    selectButton.performClick(nil)
    closeSwarm()
  }
}

private extension SwarmTabStrip {
  /// Every frame the bar lays out, apart from the daemon's own.
  func barFrames() -> [NSRect] {
    [scroll.frame, document.frame, newButton.frame, contextButton.frame, subscriptionUsageButton.frame,
     pullRequestButton.frame] + tabs.map { $0.frame }
  }

  /// Daemons off: no key, or `visible: false`, lays out exactly the bar from
  /// before daemons existed, at every width. On, the slot waits for a quiet
  /// moment before it takes its space, and nothing moves meanwhile.
  func checkDaemonOff() throws {
    let originalSize = frame.size
    defer { setFrameSize(originalSize); daemonMayAppear = nil; updateDaemon([:]) }
    let base: [String: Any] = ["enabled": true, "activeId": "off-3",
      "tabs": (0..<7).map { ["id": "off-\($0)", "name": "Off \($0)", "label": "\($0 + 1):code"] },
      "focusedContext": ["text": "Codex  M2:project  (main)", "canSelectModel": true],
      "subscriptionUsage": ["text": "gpt-5", "paneId": 3, "agentId": "a1"],
      "pullRequest": ["text": "PR #298 · Merged", "url": "https://github.com/acme/repo/pull/298"]]
    daemonMayAppear = { true }
    for width in [480.0, 720.0, 1024.0, 1440.0, 1920.0] {
      setFrameSize(NSSize(width: width, height: 52))
      update(base)
      layoutSubtreeIfNeeded()
      let before = barFrames()
      var hidden = base
      hidden["daemon"] = ["visible": false, "glyph": "\\_O_/", "voice": "pip: hello"]
      update(hidden)
      layoutSubtreeIfNeeded()
      try checkTitlebar(barFrames() == before && daemonButton.isHidden && daemonButton.frame.width == 0,
        "A hidden daemon reserves nothing at \(Int(width))pt: the bar is the one from before daemons")
      try checkTitlebar(voiceLabel.isHidden && !contextButton.isHidden,
        "A hidden daemon never speaks over the context at \(Int(width))pt")
    }
    setFrameSize(NSSize(width: 1024, height: 52))
    update(base)
    layoutSubtreeIfNeeded()
    let quiet = barFrames()
    var allowed = false
    daemonMayAppear = { allowed }
    var shown = base
    shown["daemon"] = ["visible": true, "glyph": "\\_O_/"]
    update(shown)
    layoutSubtreeIfNeeded()
    try checkTitlebar(daemonButton.isHidden && pendingDaemon != nil && barFrames() == quiet,
      "While a button is held or the pointer is on the bar, the slot waits and no tab moves")
    retryPendingDaemon()
    try checkTitlebar(daemonButton.isHidden && barFrames() == quiet, "It keeps waiting until the bar is quiet")
    allowed = true
    retryPendingDaemon()
    layoutSubtreeIfNeeded()
    try checkTitlebar(!daemonButton.isHidden && pendingDaemon == nil && daemonButton.frame.width > 0 &&
      daemonButton.frame.maxX <= bounds.width, "At the first quiet moment the slot takes its space")
    // Off again while one waits: the wait is dropped, nothing appears.
    update(base)
    allowed = false
    update(shown)
    update(base)
    allowed = true
    retryPendingDaemon()
    layoutSubtreeIfNeeded()
    try checkTitlebar(daemonButton.isHidden && pendingDaemon == nil && barFrames() == quiet,
      "A slot withdrawn before it appeared never appears")
  }

  func checkDaemonArtwork() throws {
    let originalArt = daemonArt, originalEmit = emit
    defer { daemonArt = originalArt; emit = originalEmit; daemonMayAppear = nil; update([:]) }
    let directory = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("daemon-art-\(UUID().uuidString)")
    try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
    defer { try? FileManager.default.removeItem(at: directory) }
    // A large source verifies eager thumbnail decoding, aspect ratio and cache
    // behavior independently of the exact shape of the generated illustration.
    for (name, color) in [("fixture_egg-00", NSColor.systemYellow), ("fixture_tim-00", NSColor.systemPurple)] {
      let bitmap = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: 256, pixelsHigh: 128,
        bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
        colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)!
      NSGraphicsContext.saveGraphicsState()
      NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: bitmap)
      color.setFill()
      NSRect(x: 0, y: 0, width: 256, height: 128).fill()
      NSGraphicsContext.restoreGraphicsState()
      try bitmap.representation(using: .png, properties: [:])!.write(to: directory.appendingPathComponent(name + ".png"))
    }
    var reads = 0
    daemonArt = SwarmDaemonArt(assetURL: { asset in
      reads += 1
      return directory.appendingPathComponent((asset as NSString).lastPathComponent)
    })
    let egg = "assets/daemon-art/slot/fixture_egg-00.png"
    let tim = "assets/daemon-art/slot/fixture_tim-00.png"
    guard let eggImage = daemonArt.image(asset: egg) else {
      throw TitlebarCheckFailure(message: "The bundled PNG fixture must load")
    }
    try checkTitlebar(eggImage.size == NSSize(width: 32, height: 16) &&
      eggImage.representations.allSatisfy { $0.pixelsWide <= 64 && $0.pixelsHigh <= 64 },
      "Native creature artwork preserves aspect ratio in an eagerly decoded 64px maximum thumbnail")
    try checkTitlebar(daemonArt.image(asset: egg) === eggImage && reads == 1,
      "Repeated animation frames reuse the same decoded native image")
    let invalid = ["https://example.org/tim.png", "/tmp/tim.png", "assets/daemon-art/slot/../tim.png",
      "assets/daemon-art/slot/sub/tim.png", "assets/daemon-art/slot/.png",
      "assets/daemon-art/slot/tim.svg", "assets/daemon-art/slot/tim 0.png", "assets/daemon-art/slot/tím.png",
      "assets/daemon-art/slot/%2e%2e.png", "assets/daemon-art/slot/tim.png?x=1",
      "assets/daemon-art/portrait/tim.png", "assets/daemon-art/slot/" + String(repeating: "a", count: 160) + ".png"]
    for asset in invalid {
      try checkTitlebar(!SwarmDaemonArt.opens(asset) && daemonArt.image(asset: asset) == nil,
        "Untrusted or non-slot artwork is rejected before a file lookup: \(asset)")
    }
    try checkTitlebar(reads == 1, "Invalid artwork paths never touch the file resolver")
    let missing = "assets/daemon-art/slot/not_bundled.png"
    try checkTitlebar(daemonArt.image(asset: missing) == nil && daemonArt.image(asset: missing) == nil && reads == 2,
      "Missing bundled frames are cached as misses")
    daemonMayAppear = { true }
    var state: [String: Any] = ["enabled": true, "tabs": [["id": "art", "name": "Art"]], "activeId": "art"]
    var daemon: [String: Any] = ["visible": true, "glyph": "[oo]", "art": egg]
    state["daemon"] = daemon
    update(state)
    let geometry = barFrames(), frame = daemonButton.frame
    let eggPixels = daemonButton.renderedPixels()
    try checkTitlebar(daemonButton.art === eggImage && daemonButton.frame.width == 44,
      "The daemon payload selects cached artwork in the fixed native slot")
    daemon["art"] = tim
    updateDaemon(daemon)
    let timPixels = daemonButton.renderedPixels()
    try checkTitlebar(timPixels != eggPixels && barFrames() == geometry && daemonButton.frame == frame,
      "Egg, hatch and growth frames repaint without moving a native control")
    daemon["art"] = invalid[0]
    updateDaemon(daemon)
    try checkTitlebar(daemonButton.art == nil && daemonButton.glyph == "[oo]" &&
      daemonButton.renderedPixels() != timPixels && barFrames() == geometry,
      "Missing or invalid art falls back to the existing safe ASCII face")
    daemon["art"] = tim
    updateDaemon(daemon)
    var messages: [(String, Bool?)] = []
    emit = { method, args in messages.append((method, (args as? [String: Bool])?["hovered"])) }
    let pointer = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    daemonButton.mouseEntered(with: pointer)
    try checkTitlebar(messages.map(\.0) == ["daemonLook", "daemonHover"] && messages.last?.1 == true,
      "Native hover sends look before asking Flutter to show the full-size portrait")
    daemonButton.performClick(nil)
    try checkTitlebar(messages.suffix(2).map(\.0) == ["daemonHover", "daemon"] && messages.dropLast().last?.1 == false,
      "A click dismisses the hover preview before opening the existing daemon action")
    daemonButton.mouseExited(with: pointer)
    let afterClick = messages.count
    daemonButton.mouseExited(with: pointer)
    try checkTitlebar(messages.count == afterClick, "Repeated exits do not send duplicate hover closures")
    daemonButton.mouseEntered(with: pointer)
    updateDaemon([:])
    try checkTitlebar(messages.last?.0 == "daemonHover" && messages.last?.1 == false && daemonButton.art == nil,
      "Hiding the creature closes the Flutter preview and releases the selected image")
    updateDaemon(daemon)
    daemonButton.mouseEntered(with: pointer)
    NotificationCenter.default.post(name: NSApplication.didResignActiveNotification, object: NSApp)
    try checkTitlebar(messages.last?.0 == "daemonHover" && messages.last?.1 == false,
      "Application deactivation closes the creature preview")
    daemonButton.mouseEntered(with: pointer)
    state["enabled"] = false
    state["daemon"] = daemon
    update(state)
    try checkTitlebar(messages.last?.0 == "daemonHover" && messages.last?.1 == false && !daemonButton.isEnabled,
      "Opening a modal disables the slot and closes its preview")
  }

  func checkBundledDaemonArtwork() throws {
    guard let root = ProcessInfo.processInfo.environment["HARNESS_TITLEBAR_ASSETS"] else {
      throw TitlebarCheckFailure(message: "Native artwork checks require the bundled asset directory")
    }
    let assets = URL(fileURLWithPath: root)
    let slotDirectory = assets.appendingPathComponent("daemon-art/slot")
    let files = try FileManager.default.contentsOfDirectory(at: slotDirectory,
      includingPropertiesForKeys: nil).filter { $0.pathExtension == "png" }
    try checkTitlebar(!files.isEmpty, "The illustrated daemon's slot frames are bundled")
    let art = SwarmDaemonArt(assetURL: { asset in
      assets.appendingPathComponent(String(asset.dropFirst("assets/".count)))
    })
    for file in files {
      let path = "assets/daemon-art/slot/" + file.lastPathComponent
      guard let image = art.image(asset: path) else {
        throw TitlebarCheckFailure(message: "Bundled daemon frame failed native decoding: \(file.lastPathComponent)")
      }
      try checkTitlebar(image.size.width <= 32 && image.size.height <= 32 &&
        image.representations.allSatisfy { $0.pixelsWide <= 64 && $0.pixelsHigh <= 64 },
        "Native PNG decoding bounds \(file.lastPathComponent) to its slot-sized bitmap")
    }
    let species = ["tim", "gnu", "lynx", "mutt", "yak", "gopher", "bug", "tux", "auk", "beastie"]
    for id in species {
      let asset = "assets/daemon-art/slot/\(id)_baby_idle_0.png"
      let normal = art.image(asset: asset)!
      let styled = art.image(asset: asset, style: ["colour": 2, "mark": 1])!
      let other = art.image(asset: asset, style: ["colour": 4, "mark": 3])!
      try checkTitlebar(styled !== normal && other !== styled && styled.tiffRepresentation != normal.tiffRepresentation,
        "Rolled coat and markings change native pixels for \(id)")
      try checkTitlebar(art.image(asset: asset, style: ["colour": 2, "mark": 1]) === styled,
        "An individual reuses its decoded native frame for \(id)")
    }
    let stages = ["egg_first_p0_0", "egg_first_p4_0", "egg_first_burst_0", "egg_first_open_0"] +
      species.flatMap { id in ["baby", "young", "adult"].map { "\(id)_\($0)_idle_0" } }
    let bar = SwarmTabStrip(frame: NSRect(x: 0, y: 0, width: 900, height: 40))
    bar.daemonMayAppear = { true }
    bar.statusBar.frame = NSRect(x: 0, y: 0, width: 900, height: 40)
    bar.daemonArt = art
    bar.storeButton.image = SwarmHistoryIcons(assetURL: { asset in
      assets.appendingPathComponent(String(asset.dropFirst("assets/".count)))
    }).image(engine: "store", asset: "assets/store/polymath.png")
    let previousAppearance = NSApp.appearance
    defer { NSApp.appearance = previousAppearance }
    for light in [false, true] {
      let foreground: Int64 = light ? 0xff242424 : 0xffe8e8e8
      let background: Int64 = light ? 0xfff4f4f4 : 0xff222222
      var state: [String: Any] = ["enabled": true,
        "tabs": [["id": "design", "label": "1:design", "name": "Design"],
                 ["id": "firmware", "label": "2:firmware", "name": "Firmware"]],
        "activeId": "design", "barStyle": ["size": 13, "foreground": foreground],
        "palette": ["tabBar": background, "workspace": background, "foreground": foreground,
                    "dark": light ? 0 : 1]]
      var pictures: Set<Data> = []
      for stage in stages {
        state["daemon"] = ["visible": true, "glyph": "[oo]", "art": "assets/daemon-art/slot/\(stage).png"]
        bar.update(state)
        try checkTitlebar(bar.daemonButton.art != nil && bar.daemonButton.frame.width == 44 &&
          bar.daemonButton.superview === bar.statusBar && bar.daemonButton.frame.minX == bar.subscriptionUsageButton.frame.maxX,
          "\(stage) is illustrated in the footer on the \(light ? "light" : "dark") native bar")
        pictures.insert(bar.daemonButton.renderedPixels())
        let bitmap = bar.statusBar.renderedTree(background: bar.palette.workspace)
        if stage == "egg_first_p0_0" || stage.hasSuffix("idle_0") {
          var top = bitmap.pixelsHigh, bottom = -1
          for y in 0..<bitmap.pixelsHigh {
            for x in Int(bar.daemonButton.frame.minX * 2)..<Int(bar.daemonButton.frame.maxX * 2) {
              guard let c = bitmap.colorAt(x: x, y: y)?.usingColorSpace(.deviceRGB) else { continue }
              let channels = [c.redComponent, c.greenComponent, c.blueComponent]
              if channels.max()! - channels.min()! > 0.10 {
                top = min(top, y); bottom = max(bottom, y)
              }
            }
          }
          try checkTitlebar(bottom >= top && abs(Double(top + bottom + 1) / 2 - Double(bitmap.pixelsHigh) / 2) <= 2,
            "\(stage) centres its visible artwork on the bar, independent of transparent padding")
        }
        if let capture = ProcessInfo.processInfo.environment["HARNESS_DAEMON_NATIVE_CAPTURE_DIR"] {
          let directory = URL(fileURLWithPath: capture)
          try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
          try bitmap.representation(using: .png, properties: [:])!.write(to:
            directory.appendingPathComponent("native-\(light ? "light" : "dark")-\(stage).png"))
        }
      }
      try checkTitlebar(pictures.count == stages.count, "Native egg, hatch and three growth stages are visually distinct")
    }
  }

  func checkDaemon() throws {
    let originalSize = frame.size
    let originalEmit = emit
    daemonMayAppear = { true }
    defer { setFrameSize(originalSize); emit = originalEmit; daemonMayAppear = nil }
    var events: [String] = []
    emit = { method, _ in events.append(method) }
    var daemon: [String: Any] = ["visible": true, "glyph": "\\_O_/",
      "foreground": 0xffdfc38b, "label": "Egg", "detail": "2 of 3 habits",
      "tooltip": "A daemon is incubating: 2 of 3 habits.\nClick to see them."]
    var state: [String: Any] = ["enabled": true, "activeId": "pet-11",
      "tabs": (0..<12).map { ["id": "pet-\($0)", "name": "Pet \($0)", "label": "\($0 + 1):code"] },
      "focusedContext": ["text": "Codex  M2:project  (main)", "canSelectModel": true],
      "subscriptionUsage": ["text": "gpt-5", "paneId": 3, "agentId": "a1"],
      "pullRequest": ["text": "PR #298 · Merged", "url": "https://github.com/acme/repo/pull/298"],
      "daemon": daemon]
    update(state)
    try checkTitlebar(daemonButton.title.isEmpty && daemonButton.glyph == "\\_O_/",
      "Before hatch the ASCII nest is drawn without a text label")
    try checkTitlebar(daemonButton.toolTip?.contains("incubating") == true && daemonButton.toolTip?.contains("2 of 3") == true,
      "Habit progress remains available in the tooltip")
    try checkTitlebar(daemonButton.foreground == statusColor(0xffdfc38b, fallback: .clear),
      "Native daemon colours come from the same values as Flutter")
    daemonButton.performClick(nil)
    try checkTitlebar(events == ["daemon"], "The slot opens its panel exactly once")
    let eggs = ["\\_O_/", "~\\_O_/~", "\\_.._/", "\\_o.o_/"]
    let sprites = ["[oo]", "\\[o|o]/", "><(((o>", ";:(oo):;", "U(oo)U~", "o   o", "\\[-|-]/z"]
    for size in [13.0, 22.0] {
      state["barStyle"] = ["family": "Menlo", "size": size]
      for glyph in eggs + sprites {
        daemon["glyph"] = glyph
        state["daemon"] = daemon
        for width in [CGFloat(320), CGFloat(640), CGFloat(900), CGFloat(1280)] {
          setFrameSize(NSSize(width: width, height: originalSize.height))
          update(state)
          try checkTitlebar(daemonButton.glyph == glyph, "Every nest stage and drop 1 sprite is drawn as sent")
          try checkTitlebar(daemonButton.font == contextButton.font,
            "The daemon shares the workspace bar font at \(size)pt")
          try checkTitlebar(daemonButton.frame.maxX <= bounds.width &&
            daemonButton.frame.maxX <= contextButton.frame.minX &&
            contextButton.frame.maxX <= pullRequestButton.frame.minX &&
            contextButton.superview === statusBar && daemonButton.superview === statusBar &&
            newButton.frame.maxX < searchButton.frame.minX,
            "Footer controls and top navigation do not overlap at \(width)px / \(size)pt")
          try checkTitlebar(daemonButton.frame.width == 44,
            "Every growth stage keeps a 44pt footer slot at \(size)pt")
          try checkActiveVisible()
        }
      }
    }
    let tab = tabs[0]
    try checkTitlebar(daemonButton.frame.maxX <= contextButton.frame.minX &&
      daemonButton.frame.height == subscriptionUsageButton.frame.height &&
      daemonButton.frame.midY == subscriptionUsageButton.frame.midY,
      "The daemon precedes focused context and shares the controls' height and inner gutters")
    scroll.contentView.scroll(to: .zero)
    let scrollFrame = scroll.frame, visible = scroll.documentVisibleRect
    let documentFrame = document.frame, daemonFrame = daemonButton.frame
    let contextFrame = contextButton.frame, prFrame = pullRequestButton.frame
    let modelFrame = subscriptionUsageButton.frame
    func unmoved() -> Bool {
      tabs[0] === tab && scroll.frame == scrollFrame && scroll.documentVisibleRect == visible &&
        document.frame == documentFrame && daemonButton.frame == daemonFrame &&
        contextButton.frame == contextFrame && pullRequestButton.frame == prFrame &&
        subscriptionUsageButton.frame == modelFrame
    }
    for glyph in eggs + sprites + ["\\[=|=]/", "|[=|=]|", "/[=|=]\\", "-[=|=]-", "[==] |"] {
      daemon["glyph"] = glyph
      updateDaemon(daemon)
      try checkTitlebar(unmoved(), "Moods and work frames keep the same 44pt slot and move nothing else")
    }
    // Anything that is not at most eight printable ASCII cells draws nothing, never a stand-in.
    daemon["glyph"] = "[oo]"
    updateDaemon(daemon)
    let drawn = daemonButton.renderedPixels()
    daemon["glyph"] = ""
    updateDaemon(daemon)
    let blank = daemonButton.renderedPixels()
    try checkTitlebar(drawn != blank, "A sprite draws ink")
    for invalid in ["=^o.o^=~~", "[ö ö]", "[o\to]", "🥚"] {
      daemon["glyph"] = invalid
      updateDaemon(daemon)
      try checkTitlebar(daemonButton.glyph.isEmpty && daemonButton.renderedPixels() == blank,
        "An invalid glyph (\(invalid.unicodeScalars.count) scalars) draws nothing")
    }
    daemon["glyph"] = "\\_o.o_/"
    daemon["busy"] = true
    updateDaemon(daemon)
    events.removeAll()
    daemonButton.performClick(nil)
    try checkTitlebar(!daemonButton.isEnabled && daemonButton.busy && events.isEmpty,
      "A hatch in flight disables the slot")
    let busyPixels = daemonButton.renderedPixels()
    daemon["busy"] = false
    state["enabled"] = false
    state["daemon"] = daemon
    update(state)
    try checkTitlebar(busyPixels != blank && busyPixels != daemonButton.renderedPixels(),
      "A busy slot keeps full ink; only a disabled one dims")
    state["enabled"] = true
    update(state)
    daemon["busy"] = false
    daemon["glyph"] = "\\[-|-]/z"
    daemon["label"] = "Pip"
    daemon["detail"] = "tim 2.0, napping"
    daemon["open"] = true
    updateDaemon(daemon)
    try checkTitlebar(unmoved(), "A mood change does not rebuild tabs, scroll them, or move status text")
    try checkTitlebar(daemonButton.title.isEmpty && daemonButton.accessibilityLabel() == "Pip" &&
      (daemonButton.accessibilityValue() as? String)?.contains("napping") == true,
      "The daemon's name and mood reach accessibility without appearing in the bar")
    // Hover: "I see you", once per entry, through the plain channel path.
    let pointer = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    events.removeAll()
    daemonButton.mouseEntered(with: pointer)
    daemonButton.mouseExited(with: pointer)
    daemonButton.mouseEntered(with: pointer)
    try checkTitlebar(events == ["daemonLook", "daemonHover", "daemonHover", "daemonLook", "daemonHover"],
      "Each hover entry looks first, then opens the Flutter preview; exit closes it")
    daemonButton.mouseExited(with: pointer)
    // Voice: tmux's message line replaces the status, and nothing moves.
    daemon["voice"] = "pip: two agents idle. nothing needs you."
    daemon["voiceColor"] = 0xffd7af5f
    updateDaemon(daemon)
    try checkTitlebar(!voiceLabel.isHidden && voiceLabel.text == "pip: two agents idle. nothing needs you." &&
      voiceLabel.color == statusColor(0xffd7af5f, fallback: .clear) &&
      voiceLabel.accessibilityLabel() == voiceLabel.text && voiceLabel.accessibilityRole() == .staticText,
      "The daemon's line shows in the message colour")
    try checkTitlebar(contextButton.isHidden && pullRequestButton.isHidden && !subscriptionUsageButton.isHidden,
      "Its line replaces the right context while subscription usage stays available on the left")
    try checkTitlebar(unmoved(), "Speaking moves no tab, status control or the slot")
    try checkTitlebar(voiceLabel.frame.minX >= daemonButton.frame.maxX &&
      voiceLabel.frame.maxX <= statusBar.bounds.width,
      "The line spans the status area after the slot")
    let voicePixels = voiceLabel.renderedPixels()
    daemon["voice"] = String(repeating: "a very long line ", count: 40)
    updateDaemon(daemon)
    try checkTitlebar(voiceLabel.renderedPixels() != voicePixels && unmoved(),
      "A long line truncates in place")
    state["daemon"] = daemon
    update(state)
    try checkTitlebar(contextButton.isHidden && pullRequestButton.isHidden && !subscriptionUsageButton.isHidden && !voiceLabel.isHidden,
      "A workspace update while it speaks keeps the message line")
    try checkTitlebar(unmoved(), "A workspace update while it speaks lays out as if the status were shown")
    // The pair brain's line, exactly as sent, keys first: each offered key in
    // the leading bracket is clickable, nothing else is.
    daemon["voice"] = "[y/n/g] codex@m2 wants to run the migration."
    daemon["voiceActions"] = [["key": "y", "label": "run it"], ["key": "n", "label": "not now"],
      ["key": "zz\u{e9}", "label": "bad key"]]
    updateDaemon(daemon)
    _ = voiceLabel.renderedPixels()
    let lineWidth = min(ceil(NSAttributedString(string: voiceLabel.text,
      attributes: [.font: voiceLabel.font, .ligature: 0]).size().width), voiceLabel.bounds.width)
    let lineStart = voiceLabel.bounds.width - lineWidth
    let voiceCell = ceil(("m" as NSString).size(withAttributes: [.font: voiceLabel.font]).width)
    func prefixWidth(_ prefix: String) -> CGFloat {
      ceil(NSAttributedString(string: prefix, attributes: [.font: voiceLabel.font, .ligature: 0]).size().width)
    }
    try checkTitlebar(voiceLabel.text == "[y/n/g] codex@m2 wants to run the migration." &&
      voiceLabel.actions.map(\.key) == ["y", "n"] && voiceLabel.actionRects.map(\.key) == ["y", "n"] &&
      abs(voiceLabel.actionRects[0].rect.minX - (lineStart + prefixWidth("["))) < 1 &&
      abs(voiceLabel.actionRects[1].rect.minX - (lineStart + prefixWidth("[y/"))) < 1 &&
      voiceLabel.actionRects[1].rect.maxX < lineStart + prefixWidth("[y/n/g] "),
      "The line is drawn as sent; its offered keys, first, are the buttons; [g] not offered and a malformed key are not")
    events.removeAll()
    var answered: [Any?] = []
    let previousEmit = emit
    emit = { method, arguments in events.append(method); answered.append(arguments) }
    let yes = voiceLabel.actionRects[0].rect
    voiceLabel.answer(at: NSPoint(x: yes.midX, y: yes.midY))
    // The rest of the line is words, not a button.
    let onLine = NSPoint(x: voiceLabel.bounds.width - 1, y: 1)
    voiceLabel.answer(at: onLine)
    emit = previousEmit
    try checkTitlebar(events == ["daemonAnswer"] && (answered.first as? [String: String])?["key"] == "y",
      "Clicking a key sends exactly that key, and the line itself is not a button")
    try checkTitlebar(voiceLabel.hitTest(voiceLabel.convert(onLine, to: statusBar)) == nil &&
      voiceLabel.hitTest(voiceLabel.convert(NSPoint(x: yes.midX, y: yes.midY), to: statusBar)) === voiceLabel,
      "Only the offered keys take the pointer")
    try checkTitlebar(unmoved(), "Keys move nothing in the bar")
    // Until the line is armed (drawn, with its detail, a moment ago) its keys
    // are drawn faint and take no click; [g] opens at any time.
    daemon["voice"] = "[y/n/g] codex@m2 wants to run the migration."
    daemon["voiceActions"] = [["key": "y", "label": "run it"], ["key": "n", "label": "not now"],
      ["key": "g", "label": "open"]]
    daemon["voiceArmed"] = false
    updateDaemon(daemon)
    let unarmedPixels = voiceLabel.renderedPixels()
    try checkTitlebar(voiceLabel.actionRects.map(\.key) == ["g"] && voiceLabel.arming.map(\.key) == ["y", "n"] &&
      voiceLabel.arming[0].range == NSRange(location: 1, length: 1) &&
      voiceLabel.hitTest(voiceLabel.convert(NSPoint(x: yes.midX, y: yes.midY), to: statusBar)) == nil,
      "An unarmed line's y and n are faint and take no click; its g still opens")
    daemon["voiceArmed"] = true
    updateDaemon(daemon)
    try checkTitlebar(voiceLabel.renderedPixels() != unarmedPixels && voiceLabel.actionRects.map(\.key) == ["y", "n", "g"] &&
      voiceLabel.arming.isEmpty && unmoved(),
      "Armed, the same line's keys are buttons, redrawn in place")
    // The pair harness speaking: its <nick> first, no keys.
    daemon["voice"] = "<tim> api waits on you, 40m."
    daemon["voiceActions"] = []
    updateDaemon(daemon)
    _ = voiceLabel.renderedPixels()
    try checkTitlebar(voiceLabel.text == "<tim> api waits on you, 40m." && voiceLabel.actionRects.isEmpty &&
      voiceLabel.hitTest(voiceLabel.convert(NSPoint(x: voiceLabel.bounds.width - 1, y: 1), to: self)) == nil,
      "The pair's own words are drawn with its nick and carry no key")
    daemon["voiceArmed"] = nil
    daemon["voiceActions"] = [["key": "y", "label": "run it"], ["key": "n", "label": "not now"]]
    // A long line truncates at its tail: the keys stay.
    daemon["voice"] = "[y/n] " + String(repeating: "a very long line ", count: 40)
    updateDaemon(daemon)
    _ = voiceLabel.renderedPixels()
    try checkTitlebar(voiceLabel.actionRects.map(\.key) == ["y", "n"] && voiceLabel.actionRects[0].rect.minX < voiceCell * 2,
      "A line too long for the bar keeps its keys, first")
    daemon["voiceActions"] = nil
    daemon["voice"] = nil
    updateDaemon(daemon)
    try checkTitlebar(voiceLabel.isHidden && !contextButton.isHidden && !pullRequestButton.isHidden &&
      !subscriptionUsageButton.isHidden && unmoved(),
      "Silence restores the status exactly where it was")
    // The ten cells as Flutter drew them and the grue's patch. No count labels.
    daemon["glyph"] = "[o o]"
    daemon["cell"] = "  [o o]   "
    updateDaemon(daemon)
    try checkTitlebar(daemonButton.cells == "  [o o]   " && unmoved(),
      "The slot fits the ten legacy ASCII cells into its fixed footprint")
    let plainCell = daemonButton.renderedPixels()
    daemon["cell"] = "  [= =] / "
    updateDaemon(daemon)
    try checkTitlebar(unmoved() && daemonButton.renderedPixels() != plainCell,
      "A work step repaints the slot and moves nothing")
    daemon["cell"] = "*  [o o]  "
    updateDaemon(daemon)
    try checkTitlebar(daemonButton.cells.hasPrefix("*") && unmoved(), "A shiny daemon's * sits in the gutter")
    let spriteOnly = daemonButton.renderedPixels()
    // Obsolete count fields cannot widen the slot or draw extra labels.
    daemon["tally"] = "3 done, 1 egg"
    daemon["tallyCells"] = 13
    daemon["tallyColor"] = 0xff808080
    updateDaemon(daemon)
    try checkTitlebar(daemonButton.frame.width == 44 && unmoved() &&
      daemonButton.renderedPixels() == spriteOnly,
      "Only the creature occupies the native top slot, without count labels or extra reserved space")
    daemon["tally"] = nil
    daemon["tallyCells"] = nil
    daemon["tallyColor"] = nil
    daemon["cell"] = "   .   .  "
    daemon["patch"] = 0xff000000
    updateDaemon(daemon)
    let patched = daemonButton.renderedPixels()
    daemon["patch"] = nil
    updateDaemon(daemon)
    try checkTitlebar(daemonButton.patch == nil && patched != daemonButton.renderedPixels() && unmoved(),
      "The grue's black patch on a light theme draws behind its eight cells")
    for invalid in ["  [o o]    x", "[\u{f6} \u{f6}]"] {
      daemon["cell"] = invalid
      updateDaemon(daemon)
      try checkTitlebar(daemonButton.cells.isEmpty, "An invalid cell draws nothing")
    }
    daemon["cell"] = nil
    updateDaemon(daemon)
    state["daemon"] = daemon
    state["pullRequest"] = nil
    state["subscriptionUsage"] = nil
    update(state)
    daemon["voice"] = "pip: pong."
    updateDaemon(daemon)
    daemon["voice"] = ""
    updateDaemon(daemon)
    try checkTitlebar(pullRequestButton.isHidden && subscriptionUsageButton.isHidden && !contextButton.isHidden,
      "Silence restores only what the status holds")
    state["enabled"] = false
    update(state)
    events.removeAll()
    daemonButton.performClick(nil)
    daemonButton.mouseEntered(with: pointer)
    daemonButton.mouseExited(with: pointer)
    try checkTitlebar(events.isEmpty && !daemonButton.isEnabled,
      "A modal disables the daemon's click and look")
    update([:])
    try checkTitlebar(daemonButton.isHidden && !daemonButton.isEnabled && voiceLabel.isHidden,
      "Workspace teardown hides the daemon")
  }

  /// Tab Activity's button, just after New Tab: shown only where the active tab can show the sidebar, its
  /// tooltip and label saying Show or Hide as the sidebar stands, and a click toggles it in Flutter.
  func checkTabActivityButton() throws {
    var state: [String: Any] = ["enabled": true, "tabs": [["id": "work", "name": "Desktop"]], "activeId": "work"]
    var calls: [String] = []
    emit = { method, _ in calls.append(method) }
    setFrameSize(NSSize(width: 900, height: 40))
    update(state)
    layout()
    try checkTitlebar(tabActivityButton.isHidden && !tabActivityButton.isEnabled &&
      accessibilityChildren()?.contains(where: { $0 as? NSView === tabActivityButton }) == false,
      "No Tab Activity button where the tab cannot show it")
    let budgetWithout = scroll.frame.width
    for (open, title) in [(false, "Show Tab Activity"), (true, "Hide Tab Activity")] {
      state["canShowTabActivity"] = true
      state["tabActivityOpen"] = open
      state["tabActivityTooltip"] = "\(title) · ⇧⌘Y"
      update(state)
      layout()
      try checkTitlebar(!tabActivityButton.isHidden && tabActivityButton.isEnabled &&
        tabActivityButton.image != nil &&
        tabActivityButton.frame.minX == newButton.frame.maxX && tabActivityButton.frame.width > 0 &&
        tabActivityButton.frame.maxX < searchButton.frame.minX &&
        tabActivityButton.accessibilityLabel() == title &&
        tabActivityButton.toolTip == "\(title) · ⇧⌘Y" &&
        tabActivityButton.accessibilityHelp() == tabActivityButton.toolTip &&
        accessibilityChildren()?.contains(where: { $0 as? NSView === tabActivityButton }) == true,
        "Tab Activity sits after New Tab and says \(title)")
    }
    try checkTitlebar(scroll.frame.width <= budgetWithout, "The button takes its room from the tabs, not over them")
    tabActivityButton.performClick(nil)
    try checkTitlebar(calls == ["toggleTabActivity"], "Tab Activity's button toggles the sidebar in Flutter")
    state["enabled"] = false
    update(state)
    tabActivityButton.performClick(nil)
    try checkTitlebar(!tabActivityButton.isEnabled && calls.count == 1, "A modal disables Tab Activity's button")
  }

  func checkTopActions() throws {
    var state: [String: Any] = ["enabled": true,
      "searchTooltip": "Open Harness · ⌘P", "addPhoneTooltip": "Add Phone", "storeTooltip": "Explore Harness Store · ⌃⌘S",
      "tabs": [["id": "work", "name": "Desktop"]], "activeId": "work",
      "focusedContext": ["text": "Office project", "segments": [["text": "Office project"]]]]
    var calls: [String] = []
    emit = { method, _ in calls.append(method) }
    for width in [CGFloat(360), CGFloat(640), CGFloat(1280)] {
      setFrameSize(NSSize(width: width, height: 40))
      update(state)
      try checkTitlebar(searchButton.isEnabled && searchButton.frame.width > 0 &&
        phoneButton.isEnabled && phoneButton.frame.width == searchButton.frame.width &&
        searchButton.frame.maxX == phoneButton.frame.minX &&
        phoneButton.frame.maxX == storeButton.frame.minX &&
        storeButton.frame.width == searchButton.frame.width &&
        storeButton.frame.maxX == bounds.width - 12 &&
        newButton.frame.maxX < searchButton.frame.minX &&
        contextButton.superview === statusBar,
        "Search, Add Phone and Store are icons side by side after the tabs at width \(width)")
      try checkTitlebar(accountButton.isHidden && !accountButton.isEnabled &&
        accessibilityChildren()?.contains(where: { $0 as? NSView === accountButton }) == false,
        "No account control is drawn until the app says which")
      try checkTitlebar(!subviews.contains { $0.accessibilityLabel() == "Notifications" },
        "Notifications live in the macOS menu bar, with no duplicate titlebar bell")
      try checkTitlebar(devicesButton.isHidden && !devicesButton.isEnabled &&
        accessibilityChildren()?.contains(where: { $0 as? NSView === devicesButton }) == false,
        "Devices stays hidden and outside accessibility navigation until opted in")
      try checkTitlebar(searchButton.toolTip == state["searchTooltip"] as? String &&
        searchButton.accessibilityHelp() == searchButton.toolTip &&
        phoneButton.toolTip == state["addPhoneTooltip"] as? String &&
        phoneButton.accessibilityHelp() == phoneButton.toolTip &&
        phoneButton.accessibilityLabel() == "Add Phone" &&
        storeButton.toolTip == state["storeTooltip"] as? String &&
        storeButton.accessibilityHelp() == storeButton.toolTip,
        "Top actions retain supplied shortcut hints and accessible help after layout")
    }
    searchButton.performClick(nil)
    phoneButton.performClick(nil)
    storeButton.performClick(nil)
    try checkTitlebar(calls == ["sessions", "addPhone", "store"],
      "Top actions open search, Add Phone (the app menu's own action) and Store")
    state["devicesVisible"] = true
    for width in [CGFloat(360), CGFloat(640), CGFloat(1280)] {
      setFrameSize(NSSize(width: width, height: 40))
      update(state)
      try checkTitlebar(!devicesButton.isHidden && devicesButton.isEnabled &&
        searchButton.frame.maxX == phoneButton.frame.minX &&
        phoneButton.frame.maxX == devicesButton.frame.minX &&
        devicesButton.frame.maxX == storeButton.frame.minX &&
        devicesButton.frame.width == searchButton.frame.width &&
        newButton.frame.maxX < searchButton.frame.minX &&
        devicesButton.font == storeButton.font &&
        accessibilityChildren()?.contains(where: { $0 as? NSView === devicesButton }) == true,
        "Opted-in navigation reads Search, Add Phone, Devices, Store at width \(width)")
      try captureTabPresentation("native-tabs-tools-\(Int(width))")
    }
    devicesButton.performClick(nil)
    try checkTitlebar(calls == ["sessions", "addPhone", "store", "devices"],
      "Devices opens its workspace through the native action")
    state["enabled"] = false
    update(state)
    searchButton.performClick(nil)
    phoneButton.performClick(nil)
    storeButton.performClick(nil)
    devicesButton.performClick(nil)
    try checkTitlebar(!searchButton.isEnabled && !phoneButton.isEnabled && !storeButton.isEnabled &&
      !devicesButton.isEnabled && calls.count == 4,
      "A modal prevents toolbar actions")
    state["enabled"] = true
    state["devicesVisible"] = false
    update(state)
    devicesButton.performClick(nil)
    try checkTitlebar(devicesButton.isHidden && !devicesButton.isEnabled && calls.count == 4,
      "Turning the experiment off removes and disables the native entry")
    // The row's last control: Sign in while this computer holds no account,
    // and the account's initial once it does.
    state["devicesVisible"] = true
    state["account"] = ["kind": "signIn", "tooltip": "Sign in to Harness"]
    for width in [CGFloat(360), CGFloat(640), CGFloat(1280)] {
      setFrameSize(NSSize(width: width, height: 40))
      update(state)
      try checkTitlebar(!accountButton.isHidden && accountButton.isEnabled &&
        accountButton.kind == .signIn &&
        accountButton.frame.width == accountButton.preferredWidth &&
        accountButton.frame.maxX == bounds.width - 12 &&
        storeButton.frame.maxX + 8 == accountButton.frame.minX &&
        searchButton.frame.maxX == phoneButton.frame.minX &&
        phoneButton.frame.maxX == devicesButton.frame.minX &&
        devicesButton.frame.maxX == storeButton.frame.minX &&
        newButton.frame.maxX < searchButton.frame.minX &&
        accountButton.accessibilityLabel() == "Sign in" &&
        accountButton.toolTip == "Sign in to Harness" &&
        accessibilityChildren()?.contains(where: { $0 as? NSView === accountButton }) == true,
        "Sign in ends the row after Search, Add Phone, Devices and Store at width \(width)")
      try captureTabPresentation("native-tabs-sign-in-\(Int(width))")
    }
    accountButton.performClick(nil)
    try checkTitlebar(calls == ["sessions", "addPhone", "store", "devices", "signIn"],
      "Sign in opens the sign-in through the native action")
    state["account"] = ["kind": "avatar", "initial": "Dee", "tooltip": "Dee · [email protected]"]
    for width in [CGFloat(360), CGFloat(640), CGFloat(1280)] {
      setFrameSize(NSSize(width: width, height: 40))
      update(state)
      try checkTitlebar(!accountButton.isHidden && accountButton.isEnabled &&
        accountButton.kind == .avatar && accountButton.initial == "D" &&
        accountButton.frame.width == 32 &&
        accountButton.frame.maxX == bounds.width - 12 &&
        storeButton.frame.maxX + 8 == accountButton.frame.minX &&
        accountButton.accessibilityLabel() == "Account" &&
        accountButton.toolTip == "Dee · [email protected]",
        "Signed in, the same place is the account's one initial at width \(width)")
      try captureTabPresentation("native-tabs-account-\(Int(width))")
    }
    accountButton.performClick(nil)
    try checkTitlebar(calls == ["sessions", "addPhone", "store", "devices", "signIn", "account"],
      "The avatar opens the account menu through the native action")
    state["account"] = ["kind": "avatar", "initial": ""]
    update(state)
    try checkTitlebar(!accountButton.isHidden && accountButton.initial.isEmpty,
      "An account whose profile is unread still has its place")
    state["enabled"] = false
    update(state)
    accountButton.performClick(nil)
    try checkTitlebar(!accountButton.isEnabled && calls.count == 6, "A modal prevents the account control")
    state["enabled"] = true
    state["account"] = ["kind": "billing"]
    update(state)
    accountButton.performClick(nil)
    try checkTitlebar(accountButton.isHidden && !accountButton.isEnabled && calls.count == 6 &&
      storeButton.frame.maxX == bounds.width - 12,
      "A kind this build does not know draws nothing, and Store ends the row")
    state["account"] = nil
    state["devicesVisible"] = false
    update([:])
    try checkTitlebar(!searchButton.isEnabled && !storeButton.isEnabled &&
      devicesButton.isHidden && !devicesButton.isEnabled &&
      accountButton.isHidden && !accountButton.isEnabled,
      "Teardown disables the toolbar")
  }

  func checkShareAction() throws {
    let target: [String: Any] = ["text": "[ Share ]", "label": "Share Website launch",
      "tooltip": "Share Website launch · ⇧⌘S", "enabled": true,
      "paneId": 7, "machineId": "office", "agentId": "website",
      "foreground": Int64(0xffffffff), "background": Int64(0xff2f5bea)]
    var state: [String: Any] = ["enabled": true, "shareAction": target,
      "tabs": [["id": "work", "name": "Work"]], "activeId": "work",
      "focusedContext": ["text": "Office project", "segments": [["text": "Office project"]]]]
    var calls: [(String, [String: Any])] = []
    emit = { method, args in calls.append((method, args as? [String: Any] ?? [:])) }
    for width in [CGFloat(360), CGFloat(520), CGFloat(1280)] {
      setFrameSize(NSSize(width: width, height: 40))
      var hidden = state
      hidden.removeValue(forKey: "shareAction")
      update(hidden)
      let withoutShare = barFrames()
      try checkTitlebar(shareButton.isHidden && !shareButton.isEnabled && shareButton.frame.width == 0,
        "The Share experiment reserves no space while off at width \(width)")
      update(state)
      try checkTitlebar(!shareButton.isHidden && shareButton.isEnabled &&
        shareButton.frame.width == shareButton.preferredWidth &&
        shareButton.frame.maxX <= bounds.width &&
        shareButton.frame.maxX <= contextButton.frame.minX &&
        shareButton.superview === statusBar,
        "Share stays in the footer without overlapping context at width \(width)")
      update(hidden)
      try checkTitlebar(barFrames() == withoutShare,
        "Turning Share off restores the original toolbar layout at width \(width)")
      update(state)
    }
    try checkTitlebar(shareButton.accessibilityLabel() == "Share Website launch" &&
      shareButton.toolTip == "Share Website launch · ⇧⌘S",
      "Share identifies the selected harness and its live shortcut")
    let pixel = shareButton.renderedBitmap().colorAt(x: 1, y: 1)!.usingColorSpace(.sRGB)!
    try checkTitlebar(abs(pixel.blueComponent - 234.0 / 255.0) < 0.02,
      "The primary Share action paints the color supplied by Flutter")
    shareButton.performClick(nil)
    try checkTitlebar(calls.last?.0 == "shareAgent" && calls.last?.1["paneId"] as? Int == 7 &&
      calls.last?.1["machineId"] as? String == "office" && calls.last?.1["agentId"] as? String == "website",
      "The native Share button sends the exact displayed agent identity")
    let count = calls.count
    state["enabled"] = false
    update(state)
    shareButton.performClick(nil)
    try checkTitlebar(!shareButton.isEnabled && calls.count == count,
      "Share is disabled behind a modal and cannot dispatch")
    state["enabled"] = true
    var disabled = target
    disabled["enabled"] = false
    state["shareAction"] = disabled
    update(state)
    shareButton.performClick(nil)
    try checkTitlebar(!shareButton.isHidden && !shareButton.isEnabled && calls.count == count,
      "An empty or view-only pane keeps Share visible and inactive")
    update([:])
    try checkTitlebar(shareButton.isHidden, "Workspace teardown clears the Share control")
  }

  func checkAgentIdentity() throws {
    func show(_ count: Int, engine: String? = nil) {
      var row: [String: Any] = ["id": "agent-tab", "name": "Login flow", "agentCount": count]
      if let engine { row["engine"] = engine }
      update(["tabs": [row], "activeId": "agent-tab", "enabled": true])
    }
    func marks() -> Bool { tabs[0].subviews.contains { $0 is NSImageView } }
    show(1, engine: "claude")
    let tab = tabs[0]
    try checkTitlebar(!marks(), "A tab of one harness is its name alone, without the engine's mark")
    show(2)
    try checkTitlebar(tabs[0] === tab && !marks(), "A tab of several harnesses is its name alone, and keeps the tab control")
    show(0)
    try checkTitlebar(tabs[0] === tab && !marks(), "An empty tab is its name alone")
  }

  func checkSharedTypography() throws {
    let menuFont = NSFont.menuFont(ofSize: 0)
    update(["tabs": [["id": "font-tab", "name": "Typography", "label": "1:code"],
                    ["id": "other", "name": "Other", "label": "2:blender"]],
            "activeId": "font-tab", "enabled": true,
            "barStyle": ["family": ".AppleSystemUIFontMonospaced", "size": 13.0,
                              "foreground": Int64(0xffd0d0d0), "selection": Int64(0xff444444)]])
    try checkTitlebar(tabs[0].labelFont == NSFont.systemFont(ofSize: 13, weight: .regular),
      "Tabs use 13 pt regular system UI type")
    try checkTitlebar(tabs[0].drawnFont == tabs[1].drawnFont && contextButton.font != tabs[0].labelFont,
      "Tab navigation is independent of the chosen status-line font")
    for control in [newButton, storeButton] {
      try checkTitlebar(control.font == tabs[0].labelFont && control.frame.midY == bounds.midY &&
        contextButton.frame.midY == statusBar.bounds.midY,
        "Navigation uses system type and each control centers within its own row")
    }
    try checkTitlebar(tabs[0].frame.width == tabs[0].preferredWidth &&
      tabs[1].frame.width == tabs[1].preferredWidth &&
      tabs[0].frame.width == tabs[1].frame.width,
      "Tabs share a capped width independent of their labels")
    try checkTitlebar(tabs[0].menu?.font == menuFont, "Native context menus retain the system menu font")

  }

  func checkTabPresentationAndCapture() throws {
    setFrameSize(NSSize(width: 900, height: 40))
    let rows: [[String: Any]] = [
      ["id": "new", "name": "New Tab", "label": "New Tab", "shortcutHint": "⌘1"],
      ["id": "work", "name": "Desktop", "label": "Desktop", "shortcutHint": "⌘2",
       "activity": ["mark": "⠋", "label": "Working", "working": true, "color": Int64(0xff64d2ff)]],
      ["id": "custom", "name": "1: Release planning", "label": "1: Release planning", "shortcutHint": "⌃⌥⌘R"],
      ["id": "unbound", "name": "Notes", "label": "Notes", "shortcutHint": NSNull()],
    ]
    var messageRows = rows
    messageRows[0]["canShowAgentMessages"] = true
    messageRows[1]["canShowAgentMessages"] = true
    update(["enabled": true, "activeId": "work", "tabs": messageRows])
    try checkTitlebar(tabs.map(\.canShowAgentMessages) == [true, true, false, false],
      "Each row's Show Agent Messages reaches its tab; a row without one has none")
    try checkTitlebar(tabs.allSatisfy { $0.subviews.count == 2 },
      "No tab gains a control for Agent Messages")
    try checkTitlebar(!visibleAnimatedTabs.contains { $0 === tabs[0] },
      "A tab with an Agent Messages log animates nothing; only a working tab's spinner moves")
    // Shared tabs (1c): each row's `shared` and `canInvitePeople` reach its tab; a row without them clears them.
    var sharedRows = rows
    sharedRows[0]["canInvitePeople"] = true
    sharedRows[1]["shared"] = ["count": 7, "role": "owner", "canMoveHere": true]
    sharedRows[2]["shared"] = ["count": 2, "role": "member", "canMoveHere": false]
    sharedRows[3]["shared"] = ["count": 3, "role": "visitor"]
    update(["enabled": true, "activeId": "work", "tabs": sharedRows])
    try checkTitlebar(tabs.map(\.sharedCount) == [nil, 7, 2, nil] &&
      tabs.map(\.sharedRole) == [nil, "owner", "member", nil] && tabs.map(\.canMoveHere) == [false, true, false, false] &&
      tabs.map(\.canInvitePeople) == [true, false, false, false],
      "Each row's shared mark and role reach its tab; an unknown role is no share")
    update(["enabled": true, "activeId": "work", "tabs": rows])
    try checkTitlebar(tabs.allSatisfy { $0.sharedCount == nil && $0.sharedRole == nil && !$0.canInvitePeople },
      "A row without them unshares its tab")
    try checkTitlebar(tabs.map(\.displayLabel) == ["New Tab", "Desktop", "1: Release planning", "Notes"],
      "Clean labels preserve numeric-looking custom names and omit generated prefixes")
    try checkTitlebar(tabs.map(\.shortcutHint) == ["⌘1", "⌘2", "⌃⌥⌘R", nil],
      "Hints use current bindings and keep an explicitly unbound tab empty")
    let initial = tabs.map(\.frame)
    var statusChanged: [String: Any] = ["enabled": true, "activeId": "work", "tabs": rows,
      "barStyle": ["family": "Menlo", "size": 28.0, "foreground": Int64(0xffdddddd)]]
    update(statusChanged)
    try checkTitlebar(tabs.map(\.frame) == initial && tabs.allSatisfy { $0.labelFont.pointSize == 13 },
      "A large custom status font cannot resize system-font tab navigation")
    statusChanged["barStyle"] = ["family": "Menlo", "size": 13.0, "foreground": Int64(0xffdddddd)]
    let hover = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    for (name, values) in [
      ("dark", ["workspace": Int64(0xff282828), "tabBar": Int64(0xff1c1c1c), "accent": Int64(0xffbdcbdc)]),
      ("light", ["workspace": Int64(0xfff0f2f5), "tabBar": Int64(0xffe4e6e9), "accent": Int64(0xff3d5a8a)]),
    ] {
      statusChanged["palette"] = values
      update(statusChanged)
      let ink = tabs[0].foreground.usingColorSpace(.sRGB)!
      try checkTitlebar(name == "light" ? ink.redComponent < 0.2 : ink.redComponent > 0.8,
        "Tab labels remain readable on the UI surface with an independently chosen status foreground")
      try captureTabPresentation("native-tabs-\(name)-rest")
      setShortcutHintsVisible(true)
      try checkTitlebar(tabs.map(\.drawsShortcut) == [true, true, true, false] &&
        tabs.map(\.frame) == initial,
        "Command reveals the same small status-slot hints without resizing tabs")
      try captureTabPresentation("native-tabs-\(name)-command")
      setShortcutHintsVisible(false)
      try checkTitlebar(tabs.allSatisfy { !$0.drawsShortcut },
        "Releasing Command restores each tab's status")
      tabs[1].mouseEntered(with: hover)
      try checkTitlebar(tabs.map(\.frame) == initial && tabs.filter(\.isRevealed).count == 1,
        "Hover reveals one tab without moving any")
      try captureTabPresentation("native-tabs-\(name)-hover")
      tabs[1].mouseExited(with: hover)
    }
    setShortcutHintsVisible(true)
    update(["enabled": false, "activeId": "work", "tabs": rows])
    try checkTitlebar(tabs.allSatisfy { !$0.drawsShortcut }, "A modal hides tab shortcuts")
    update(["enabled": true, "activeId": "work", "tabs": rows])
    try checkTitlebar(tabs[0].drawsShortcut, "Hints follow a held Command when controls re-enable")
    NotificationCenter.default.post(name: NSApplication.didResignActiveNotification, object: NSApp)
    try checkTitlebar(tabs.allSatisfy { !$0.drawsShortcut }, "Losing app focus clears held hints")
    let crowdedNames = ["New Tab", "Desktop", "Docs", "Web", "Notes", "API", "Tests", "Release"]
    let crowdedRows: [[String: Any]] = crowdedNames.enumerated().map { index, name in
      ["id": "dense-\(index)", "name": name, "label": name, "shortcutHint": "⌘\(index + 1)",
       "activity": ["mark": index == 1 ? "⠋" : "", "label": index == 1 ? "Working" : "Idle",
                    "color": Int64(0xff64d2ff)]]
    }
    setFrameSize(NSSize(width: 1440, height: 40))
    for (name, values) in [
      ("dark", ["workspace": Int64(0xff282828), "tabBar": Int64(0xff1c1c1c), "accent": Int64(0xffbdcbdc)]),
      ("light", ["workspace": Int64(0xfff0f2f5), "tabBar": Int64(0xffe4e6e9), "accent": Int64(0xff3d5a8a)]),
    ] {
      update(["enabled": true, "activeId": "dense-1", "tabs": crowdedRows, "palette": values])
      try captureTabPresentation("native-tabs-eight-\(name)-rest")
      try checkTitlebar(tabs.last!.frame.maxX <= scroll.bounds.width,
        "Eight ordinary tab names with inset close targets fit a 1440pt strip: \(tabs.last!.frame.maxX) in \(scroll.bounds.width)")
      for tab in tabs {
        try tab.checkCenteredLabel()
        try tab.checkNameFitsOrHasTooltip()
      }
      tabs[1].mouseEntered(with: hover)
      try captureTabPresentation("native-tabs-eight-\(name)-hover")
      tabs[1].mouseExited(with: hover)
    }
  }

  func captureTabPresentation(_ name: String) throws {
    guard let directory = ProcessInfo.processInfo.environment["HARNESS_TAB_CAPTURE_DIR"] else { return }
    if let root = ProcessInfo.processInfo.environment["HARNESS_TITLEBAR_ASSETS"] {
      let assets = URL(fileURLWithPath: root)
      let icons = SwarmHistoryIcons(assetURL: { asset in
        assets.appendingPathComponent(String(asset.dropFirst("assets/".count)))
      })
      storeButton.image = icons.image(engine: "store", asset: "assets/store/polymath.png")
      devicesButton.image = icons.image(engine: "devices",
        asset: "assets/devices/harness-mark.png", pointSize: devicesButton.markSize)
    }
    let height = Int(bounds.height) + 40
    let bitmap = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(bounds.width), pixelsHigh: height,
      bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
      colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)!
    NSGraphicsContext.saveGraphicsState()
    NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: bitmap)
    palette.tabBar.setFill()
    NSRect(x: 0, y: 0, width: bounds.width, height: CGFloat(height)).fill()
    palette.workspace.setFill()
    NSRect(x: 0, y: 0, width: bounds.width, height: 40).fill()
    for tab in tabs {
      NSGraphicsContext.saveGraphicsState()
      let transform = NSAffineTransform()
      transform.translateX(by: scroll.frame.minX + tab.frame.minX, yBy: 40)
      transform.concat()
      tab.drawWithHoverControl()
      NSGraphicsContext.restoreGraphicsState()
    }
    for control in [newButton, searchButton, devicesButton, storeButton, accountButton] where !control.isHidden {
      NSGraphicsContext.saveGraphicsState()
      let transform = NSAffineTransform()
      transform.translateX(by: control.frame.minX, yBy: control.frame.minY + 40)
      transform.concat()
      control.draw(control.bounds)
      NSGraphicsContext.restoreGraphicsState()
    }
    NSGraphicsContext.restoreGraphicsState()
    let destination = URL(fileURLWithPath: directory)
    try FileManager.default.createDirectory(at: destination, withIntermediateDirectories: true)
    try bitmap.representation(using: .png, properties: [:])!.write(to: destination.appendingPathComponent(name + ".png"))
  }

  func checkStartupPalette(_ expected: SwarmNativePalette) throws {
    try checkTitlebar(palette == expected, "Startup uses the saved workspace palette")
    try checkTitlebar(tabs.isEmpty && !actionsEnabled && !newButton.isEnabled,
      "Palette setup does not create or enable workspace controls")
  }
  func checkWindowGeometry(_ window: NSWindow) throws {
    let stripFrame = convert(bounds, to: nil)
    let close = window.standardWindowButton(.closeButton)!
    let zoom = window.standardWindowButton(.zoomButton)!
    let closeFrame = close.convert(close.bounds, to: nil)
    let zoomFrame = zoom.convert(zoom.bounds, to: nil)
    let newFrame = newButton.convert(newButton.bounds, to: nil)
    try checkTitlebar(bounds.height >= 40, "Native title bar leaves room around the pill actions")
    // 10, which is where a Mac app starts its first control after the buttons: Safari's sidebar
    // toggle and Chrome's first tab both sit about there. It was 12 while the notifications bell
    // still led the strip.
    try checkTitlebar(stripFrame.minX >= zoomFrame.maxX + 10 && stripFrame.minX <= zoomFrame.maxX + 12,
      "Tab row starts a Mac-standard gap after the native traffic lights")
    try checkTitlebar(abs(newFrame.midY - closeFrame.midY) <= 1, "Tab controls align vertically with native traffic lights")
    try checkTitlebar(abs(stripFrame.minY - window.contentLayoutRect.maxY) <= 1, "Tab row meets content without a second toolbar row")
    try checkActiveVisible()
  }

  func checkActiveVisible() throws {
    guard let active = tabs.first(where: { $0.swarmId == activeId }) else {
      throw TitlebarCheckFailure(message: "Selected tab exists")
    }
    let visible = scroll.documentVisibleRect
    try checkTitlebar(active.frame.minX >= visible.minX - 1, "Selected tab's leading edge is visible after layout")
    try checkTitlebar(active.frame.maxX <= visible.maxX + 1, "Selected tab's trailing edge is visible after layout")
  }

  func runChecks() throws {
    let rows = (0..<24).map { ["id": "swarm-\($0)", "name": "Tab \($0)", "label": "\($0 + 1):code"] }
    var events: [String] = []
    emit = { method, _ in events.append(method) }
    func state(_ rows: [[String: String]], active: String, enabled: Bool = true) -> [String: Any] {
      ["tabs": rows, "activeId": active, "enabled": enabled,
       "focusedContext": ["text": "OpenAI  M2:~/code/harness  (main)",
                          "segments": [["text": "OpenAI  M2:~/code/harness  (main)", "foreground": Int64(0xffdddddd)]],
                          "detail": "Full project context", "interactive": false]]
    }
    var prState = state(rows, active: "swarm-11")
    prState["pullRequest"] = ["text": "#298 Merged", "segmented": true,
      "segments": [["text": "#298 Merged", "foreground": Int64(0xffeeeeee), "background": Int64(0xffbc3fbc)]],
      "url": "https://github.com/acme/repo/pull/298", "interactive": true]
    update(prState)
    try checkTitlebar(!pullRequestButton.isHidden && pullRequestButton.isEnabled,
      "A focused PR has a separate enabled action")
    try checkTitlebar(contextButton.frame.maxX < pullRequestButton.frame.minX &&
      pullRequestButton.frame.maxX <= bounds.width, "Plain context keeps a space before its PR action")
    prState["focusedContext"] = ["text": "OpenAI M2  app  main", "segmented": true,
      "segments": [
        ["text": "OpenAI M2", "foreground": Int64(0xffeeeeee), "background": Int64(0xff000000)],
        ["text": "app", "foreground": Int64(0xffeeeeee), "background": Int64(0xff3465a4)],
        ["text": "main", "foreground": Int64(0xff000000), "background": Int64(0xff4e9a06)],
      ], "interactive": false]
    let originalSize = frame.size
    for width in [CGFloat(520), CGFloat(1280)] {
      setFrameSize(NSSize(width: width, height: originalSize.height))
      update(prState)
      try checkTitlebar(abs(contextButton.frame.maxX - pullRequestButton.frame.minX) < 0.01,
        "Powerline context and PR are adjacent at width \(width)")
      try checkTitlebar(contextButton.nextBackground == pullRequestButton.firstBackground,
        "The closing arrow joins into the PR background")
      try checkTitlebar(contextButton.superview === statusBar && pullRequestButton.superview === statusBar &&
        contextButton.frame.minX >= 0 && pullRequestButton.frame.maxX <= statusBar.bounds.width,
        "Joined status stays inside its separate footer")
    }
    setFrameSize(originalSize)
    update(prState)
    let mergedPixels = pullRequestButton.renderedPixels()
    let mergedBitmap = pullRequestButton.renderedBitmap()
    let prFrame = pullRequestButton.frame
    let prWidth = pullRequestButton.preferredWidth
    try checkTitlebar(!mergedPixels.isEmpty && pullRequestButton.accessibilityValue() as? String == "#298 Merged",
      "Segmented PR renders and exposes its full state")
    pullRequestButton.performClick(nil)
    try checkTitlebar(events.last == "focusedPullRequest", "PR opens its own action rather than the model picker")
    let pointer = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    pullRequestButton.mouseEntered(with: pointer)
    try checkTitlebar(pullRequestButton.renderedPixels() != mergedPixels, "A PR uses the same bold hover cue")
    let mergedHover = pullRequestButton.renderedBitmap()
    try checkTitlebar((0..<mergedBitmap.pixelsWide).allSatisfy {
      mergedBitmap.colorAt(x: $0, y: 1) == mergedHover.colorAt(x: $0, y: 1)
    }, "PR hover leaves the space around the ribbon untouched")
    try checkTitlebar(mergedBitmap.colorAt(x: 1, y: 14) == mergedHover.colorAt(x: 1, y: 14) &&
      pullRequestButton.frame == prFrame && pullRequestButton.preferredWidth == prWidth,
      "PR hover preserves the segment background and geometry")
    pullRequestButton.mouseExited(with: pointer)
    let join = SwarmContextButton(frame: NSRect(x: 0, y: 0, width: 100, height: 28))
    join.font = contextButton.font
    join.update(["text": "main", "segmented": true, "interactive": true,
      "segments": [["text": "main", "foreground": Int64(0xff000000), "background": Int64(0xff4e9a06)]]], enabled: true)
    join.setFrameSize(NSSize(width: join.preferredWidth + 0.5, height: 28))
    join.nextBackground = pullRequestButton.firstBackground
    let pixels = join.renderedBitmap()
    try checkTitlebar((pixels.colorAt(x: 0, y: 14)?.alphaComponent ?? 0) > 0.99,
      "Fractional segment padding never leaves a vertical seam before its text")
    let tail = pixels.colorAt(x: pixels.pixelsWide - 1, y: 19)?.usingColorSpace(.sRGB)
    let expectedJoin = pullRequestButton.firstBackground?.usingColorSpace(.sRGB)
    try checkTitlebar(tail != nil && expectedJoin != nil && tail!.alphaComponent > 0.99 &&
      abs(tail!.redComponent - expectedJoin!.redComponent) < 0.02 &&
      abs(tail!.greenComponent - expectedJoin!.greenComponent) < 0.02 &&
      abs(tail!.blueComponent - expectedJoin!.blueComponent) < 0.02,
      "The last arrow paints through to the adjacent PR background without a dark divider")
    try join.checkThemeSymbolsAndCaps()
    try SwarmContextButton().checkPullRequestIcons()
    let contextFields: [[String: Any]] = ["machine", "project", "branch"].map { field in
      ["text": field, "field": field, "paneId": 7, "interactive": true,
       "detail": "Find harnesses: \(field)",
       "segments": [["text": field, "foreground": Int64(0xffdddddd)]]]
    }
    var linked = prState
    linked["focusedContext"] = ["text": "machine project branch", "fields": contextFields]
    var clickedFields: [(String, Int)] = []
    emit = { method, args in
      if method == "focusedContext", let args = args as? [String: Any],
         let field = args["field"] as? String, let pane = args["paneId"] as? Int {
        clickedFields.append((field, pane))
      }
    }
    update(linked)
    try checkTitlebar(contextButton.fieldButtons.count == 3, "Context fields have separate native controls")
    for control in contextButton.fieldButtons {
      let before = control.renderedPixels()
      control.mouseEntered(with: pointer)
      try checkTitlebar(control.renderedPixels() != before, "Each context field emphasizes its own text")
      control.mouseExited(with: pointer)
      control.performClick(nil)
      try checkTitlebar(control.toolTip?.hasPrefix("Find harnesses") == true, "Context tooltip explains navigation")
      try checkTitlebar(contextButton.hitTest(contextButton.convert(NSPoint(x: control.frame.midX, y: control.frame.midY), to: contextButton.superview)) === control, "Context child is clickable inside its informational parent")
    }
    try checkTitlebar(clickedFields.map { $0.0 } == ["machine", "project", "branch"] &&
      clickedFields.allSatisfy { $0.1 == 7 }, "Each context action preserves field and pane identity")
    linked["subscriptionUsage"] = ["text": "Claude 0%  Codex 13%", "interactive": true,
      "detail": "Remaining subscription usage",
      "segments": [["text": "Claude ", "foreground": Int64(0xffdddddd)],
                   ["text": "0%", "foreground": Int64(0xffff6b6b)],
                   ["text": "  Codex ", "foreground": Int64(0xffdddddd)],
                   ["text": "13%", "foreground": Int64(0xffffc857)]]]
    var usageClicks = 0
    emit = { method, _ in
      if method == "subscriptions" { usageClicks += 1 }
    }
    update(linked)
    try checkTitlebar(!subscriptionUsageButton.isHidden && subscriptionUsageButton.isEnabled,
      "Subscription usage has its own enabled status control")
    try checkTitlebar(subscriptionUsageButton.frame.maxX < contextButton.frame.minX &&
      subscriptionUsageButton.frame.height == contextButton.frame.height &&
      subscriptionUsageButton.frame.maxX <= statusBar.bounds.width,
      "Usage sits on the left opposite machine/repo/branch/PR")
    try checkTitlebar(subscriptionUsageButton.toolTip == "Remaining subscription usage",
      "Usage hint explains that percentages are remaining")
    let usageResting = subscriptionUsageButton.renderedPixels()
    subscriptionUsageButton.mouseEntered(with: pointer)
    try checkTitlebar(subscriptionUsageButton.renderedPixels() != usageResting &&
      subscriptionUsageButton.renderedBitmap().colorAt(x: 1, y: 1)!.alphaComponent == 0,
      "Usage uses hover feedback without a background well")
    subscriptionUsageButton.mouseExited(with: pointer)
    subscriptionUsageButton.performClick(nil)
    try checkTitlebar(usageClicks == 1,
      "Usage opens subscriptions without a pane or retarget action")
    linked["enabled"] = false
    update(linked)
    subscriptionUsageButton.performClick(nil)
    try checkTitlebar(!subscriptionUsageButton.isEnabled && usageClicks == 1,
      "Disabled usage controls cannot dispatch")
    linked["enabled"] = false
    update(linked)
    for control in contextButton.fieldButtons {
      try checkTitlebar(!control.isEnabled, "Modal state disables each context link")
      control.performClick(nil)
    }
    try checkTitlebar(clickedFields.count == 3, "Disabled context links cannot dispatch")
    emit = { method, _ in events.append(method) }

    prState["enabled"] = false
    update(prState)
    try checkTitlebar(!pullRequestButton.isEnabled, "A modal disables the PR action")
    update(state(rows, active: "swarm-11"))
    try checkTitlebar(pullRequestButton.actionURL == nil && pullRequestButton.accessibilityValue() as? String == "",
      "Changing to a pane without a PR clears the old link and state")
    var covered = state(rows, active: "swarm-11")
    covered["footerCovered"] = true
    update(covered)
    try checkTitlebar(statusBar.isHidden,
      "Flutter's modal backdrop covers the native footer too")
    try checkTitlebar(accessibilityChildren()?.contains(where: { $0 as? NSView === statusBar }) == false,
      "The covered footer is removed from the native accessibility tree")
    covered["footerCovered"] = false
    update(covered)
    try checkTitlebar(!statusBar.isHidden,
      "Closing the modal restores the native footer")
    try checkTitlebar(accessibilityChildren()?.contains(where: { $0 as? NSView === statusBar }) == true,
      "Closing the modal restores the footer's accessibility controls")
    covered["footerPassive"] = true
    covered["enabled"] = false
    update(covered)
    try checkTitlebar(!statusBar.isHidden,
      "Status customization keeps the footer visible as a passive preview")
    try checkTitlebar(accessibilityChildren()?.contains(where: { $0 as? NSView === statusBar }) == false,
      "A passive footer is excluded from modal accessibility traversal")
    try checkTitlebar(!subscriptionUsageButton.isEnabled && !pullRequestButton.isEnabled,
      "Passive footer controls cannot activate underlying commands")
    covered["footerPassive"] = false
    covered["enabled"] = true
    update(covered)
    try checkTitlebar(contextButton.nextBackground == nil, "A missing PR clears the joined background")
    try checkTitlebar(contextButton.fieldButtons.isEmpty, "Leaving a context clears its former link controls")
    try SwarmTabButton(id: "click-owner").checkClickOwnership()
    try tabs[0].checkDoubleClickIsolation()
    try checkTitlebar(tabs.count == 24 && newButton.isEnabled, "All overflow tabs and New Tab remain available")
    try checkTitlebar(scroll.frame.maxX <= newButton.frame.minX &&
      newButton.frame.maxX < searchButton.frame.minX, "Tabs precede the search and Store controls")
    try checkTitlebar(tabs[0].frame.width < 136 && tabs[0].displayLabel == "code",
      "Overflow tabs keep readable names without persistent number prefixes")
    // Tabs, New Tab, search, Add Phone and Store; Devices and the account are hidden here.
    try checkTitlebar(subviews.filter { !$0.isHidden }.count == 5 && devicesButton.isHidden && statusBar.subviews.count == 10 && machineResourcesLabel.isHidden && machinesButton.isHidden && modelsButton.isHidden && pullRequestButton.isHidden && subscriptionUsageButton.isHidden && harnessMonitorButton.isHidden && daemonButton.isHidden && voiceLabel.isHidden && shareButton.isHidden,
      "Navigation lives in the titlebar and focused context lives in the footer")
    let controls = [newButton]
    for control in controls {
      try checkTitlebar(control.image != nil && control.title.isEmpty && !control.isBordered,
        "Management controls use the regular outline family without a resting button well")
      try checkTitlebar(control.frame.height >= 28 && control.frame.width > HarnessControlSymbols.size,
        "The new-tab control keeps the shared click height and padding around its icon")
      try checkTitlebar(control.accessibilityLabel() == "New Tab" && control.toolTip?.contains("New Tab") == true,
        "Every symbol explains its action through a tooltip and accessible name")
      let resting = control.renderedPixels()
      let event = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
        timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
      control.mouseEntered(with: event)
      try checkTitlebar(control.renderedPixels() != resting, "Hover visibly marks a clickable symbol")
      control.mouseExited(with: event)
      try checkTitlebar(control.renderedPixels() == resting, "Leaving the symbol restores its plain appearance")
      control.highlight(true)
      try checkTitlebar(control.renderedPixels() != resting, "Pressing a symbol gives visual feedback")
      control.highlight(false)
    }
    for (left, right) in zip(controls, controls.dropFirst()) {
      try checkTitlebar(right.frame.midX - left.frame.midX == left.frame.width,
        "Symbols are equally spaced without hidden gaps")
    }
    try checkTitlebar(contextButton.accessibilityValue() as? String == "OpenAI  M2:~/code/harness  (main)",
      "The full focused context is accessible")
    for (index, tab) in tabs.enumerated() {
      try tab.checkAccessibility(expectedName: "Tab \(index)", active: index == 11)
    }
    try checkActiveVisible()
    let hover = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    tabs[1].mouseEntered(with: hover)
    try tabs[1].checkCenteredLabel()
    tabs[1].mouseExited(with: hover)
    try tabs[1].checkCenteredLabel()
    scroll.contentView.scroll(to: .zero)
    scroll.reflectScrolledClipView(scroll.contentView)
    let browsingOrigin = scroll.documentVisibleRect.origin
    update(state(rows, active: "swarm-11"))
    try checkTitlebar(scroll.documentVisibleRect.origin == browsingOrigin,
      "Background updates preserve deliberate tab scrolling")
    update(state(rows, active: "swarm-23"))
    try checkActiveVisible()
    setFrameSize(NSSize(width: 320, height: 52))
    needsLayout = true
    layoutSubtreeIfNeeded()
    try checkActiveVisible()
    try checkTitlebar(newButton.frame.maxX < searchButton.frame.minX &&
      contextButton.frame.maxX <= statusBar.bounds.width, "Narrow windows have no overlapping controls")
    let original = tabs[0]
    let reversed = Array(rows.reversed())
    update(state(reversed, active: "swarm-0"))
    try checkTitlebar(tabs.last === original, "Reordering keeps existing native tab controls")
    let accessibleTabs = document.accessibilityChildren()?.compactMap { $0 as? SwarmTabButton } ?? []
    try checkTitlebar(accessibleTabs.map(\.swarmId) == tabs.map(\.swarmId), "Accessible order follows visual order")
    try checkActiveVisible()
    update(state([["id": "swarm-0", "name": "Custom name", "label": "1:blender"]], active: "swarm-0"))
    try checkTitlebar(tabs[0] === original && original.displayLabel == "blender",
      "Type changes update the existing tab without renaming its saved workspace")
    try original.checkAccessibility(expectedName: "Custom name", active: true)
    try checkTitlebar(newButton.toolTip == "New Tab", "New Tab has a label before its keymap arrives")
    events.removeAll()
    contextButton.performClick(nil)
    newButton.performClick(nil)
    original.clickBothActions()
    try checkTitlebar(events == ["new", "select", "close"], "Visible controls dispatch their actions once")
    events.removeAll()
    update(state(rows, active: "swarm-0", enabled: false))
    try checkTitlebar(!contextButton.isEnabled && !newButton.isEnabled,
      "Modal state disables status-bar actions")
    try original.checkEnabled(false)
    contextButton.performClick(nil)
    newButton.performClick(nil)
    original.clickBothActions()
    try checkTitlebar(events.isEmpty, "Disabled controls cannot act behind a modal")
    update(["tabs": rows, "activeId": "swarm-0", "enabled": true])
    try checkTitlebar(contextButton.accessibilityValue() as? String == "" && !contextButton.isEnabled,
      "An empty tab clears the previous pane's context and model action")
    try checkDragOperations()
  }

}

private extension SwarmTabStrip {
  func checkTabKeyboardFocus(_ window: NSWindow, messenger: TitlebarCheckMessenger) throws {
    update(["enabled": true, "activeId": "keyboard-23",
      "tabs": (0..<24).map { ["id": "keyboard-\($0)", "name": "Keyboard \($0)"] }])
    let tab = tabs[0]
    let hover = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    tab.mouseEntered(with: hover)
    let buttons = tab.accessibilityChildren()!.compactMap { $0 as? NSButton }
    try checkTitlebar(buttons.count == 2, "Hover exposes selection and close to native accessibility")
    for button in buttons {
      let beforeFocus = tab.renderedPixels()
      try checkTitlebar(window.makeFirstResponder(button), "An enabled tab action accepts keyboard focus")
      if button === buttons[0] {
        try checkTitlebar(tab.renderedPixels() != beforeFocus,
          "Explicit keyboard focus still outlines the native tab control")
      }
      try tab.checkCenteredLabel()
      try checkTitlebar(scroll.documentVisibleRect.contains(tab.frame),
        "Keyboard focus reveals the entire overflowed tab")
      try checkTitlebar(activeId == "keyboard-23", "Focusing a tab control does not activate its tab")
      let before = messenger.calls.count
      messenger.holdReplies = true
      button.performClick(nil)
      try checkTitlebar(window.firstResponder === button,
        "Typing stays out of the old workspace until the tab action is acknowledged")
      messenger.finishNextReply()
      try checkTitlebar(window.firstResponder === window.contentInput,
        "Activating a tab action returns the next key to Flutter content")
      try window.checkContentCommand()
      try tab.checkCenteredLabel()
      try checkTitlebar(messenger.calls.count == before + 1, "Each native tab activation sends one action")
    }
    tab.mouseExited(with: hover)
    tab.attention = true
    try checkTitlebar(buttons[0].accessibilityHelp()?.contains("Needs your input") == true,
      "The attention dot has an accessible description")
    tab.attention = false
    try checkTitlebar(buttons[0].accessibilityHelp() == nil, "Resolved attention clears its accessible description")
    try checkTitlebar(window.makeFirstResponder(buttons[0]), "Rename starts from an actual focused control")
    let rename = tab.menu!.items.first!
    NSApp.sendAction(rename.action!, to: rename.target, from: rename)
    messenger.finishNextReply()
    try checkTitlebar(window.firstResponder === window.contentInput && messenger.calls.last?.method == "rename",
      "Renaming gives the Flutter form native keyboard ownership")
    let stale = tab
    update(["enabled": true, "activeId": "survivor", "tabs": [["id": "survivor", "name": "Survivor"]]])
    let beforeStale = messenger.calls.count
    stale.clickBothActions()
    try checkTitlebar(messenger.calls.count == beforeStale, "A removed tab's retained controls cannot dispatch actions")
    let unfocusedNew = newButton.renderedPixels()
    try checkTitlebar(window.makeFirstResponder(newButton), "New Tab accepts keyboard focus")
    try checkTitlebar(newButton.hasKeyboardFocus && newButton.renderedPixels() != unfocusedNew,
      "Keyboard focus gives the new-tab symbol the same bold emphasis as hover")
    newButton.performClick(nil)
    messenger.finishNextReply()
    try checkTitlebar(window.firstResponder === window.contentInput && messenger.calls.last?.method == "new",
      "New Tab returns keyboard ownership to the workspace")
    updateDaemon(["visible": true, "glyph": "\\_O_/"])
    try checkTitlebar(window.makeFirstResponder(daemonButton), "The daemon accepts keyboard focus")
    daemonButton.performClick(nil)
    try checkTitlebar(window.firstResponder === daemonButton,
      "Daemon activation waits for the Flutter panel before releasing keyboard focus")
    messenger.finishNextReply()
    try checkTitlebar(window.firstResponder === window.contentInput && messenger.calls.last?.method == "daemon",
      "The daemon panel receives the next keystroke")
    let beforeLook = messenger.calls.count, pendingReplies = messenger.replies.count
    daemonButton.mouseEntered(with: hover)
    daemonButton.mouseExited(with: hover)
    let expectedHoverCalls = NSApp.isActive ? 3 : 0
    try checkTitlebar(messenger.calls.count == beforeLook + expectedHoverCalls &&
      (!NSApp.isActive || messenger.calls.suffix(3).map(\.method) == ["daemonLook", "daemonHover", "daemonHover"]) &&
      messenger.replies.count == pendingReplies && window.firstResponder === window.contentInput,
      "Hovering an active native window toggles preview without focus; inactive windows stay quiet")
    updateDaemon([:])
    let current = tabs[0].accessibilityChildren()!.first as! NSButton
    window.makeFirstResponder(current)
    current.performClick(nil)
    window.makeFirstResponder(newButton)
    newButton.performClick(nil)
    messenger.finishNextReply()
    try checkTitlebar(window.firstResponder === newButton,
      "A delayed tab reply cannot steal focus from a newer search action")
    messenger.finishNextReply()
    try checkTitlebar(window.firstResponder === window.contentInput,
      "The search button hands the next keystroke to the shared Flutter picker")
    window.makeFirstResponder(current)
    current.performClick(nil)
    current.performClick(nil)
    messenger.finishNextReply()
    try checkTitlebar(window.firstResponder === current, "An older tab action cannot release a newer action's focus")
    messenger.finishNextReply()
    try checkTitlebar(window.firstResponder === window.contentInput,
      "The latest acknowledged tab action restores content focus")
    messenger.holdReplies = false
  }
}

private extension SwarmTabStrip {
  /// Dart's `tabsFocused`: Close parks keys without adding a visual outline.
  func checkKeyboardOnTabs() throws {
    let rows = (0..<3).map { ["id": "strip-\($0)", "name": "Strip \($0)", "label": "\($0 + 1):work"] }
    update(["enabled": true, "activeId": "strip-1", "tabs": rows])
    let resting = tabs.map { $0.renderedPixels() }
    try checkTitlebar(tabs.allSatisfy { !$0.keyboardFocus }, "Tabs start without the strip's keyboard focus")
    update(["enabled": true, "activeId": "strip-1", "tabs": rows, "tabsFocused": true])
    try checkTitlebar(tabs.map(\.keyboardFocus) == [false, true, false],
      "Only the selected tab shows the strip's keyboard focus")
    try checkTitlebar(tabs[1].renderedPixels() == resting[1] &&
      tabs[0].renderedPixels() == resting[0] && tabs[2].renderedPixels() == resting[2],
      "Closing a tab keeps the normal selected appearance without an automatic focus ring")
    let select = tabs[1].accessibilityChildren()!.compactMap { $0 as? NSButton }.first!
    try checkTitlebar(select.accessibilityHelp()?.contains("Return") == true,
      "VoiceOver hears that Return types in the selected tab")
    update(["enabled": false, "activeId": "strip-1", "tabs": rows, "tabsFocused": true])
    try checkTitlebar(tabs[1].renderedPixels() == resting[1], "A strip behind a modal draws no keyboard focus")
    update(["enabled": true, "activeId": "strip-2", "tabs": rows, "tabsFocused": true])
    try checkTitlebar(tabs.map(\.keyboardFocus) == [false, false, true],
      "The strip's keyboard focus follows the selected tab")
    update(["enabled": true, "activeId": "strip-1", "tabs": rows])
    try checkTitlebar(tabs.allSatisfy { !$0.keyboardFocus } && tabs[1].renderedPixels() == resting[1] &&
      select.accessibilityHelp() == nil, "Going into the tab clears the strip's focus")
  }
}

private final class TitlebarCheckDrag: NSObject, NSDraggingInfo {
  var draggingDestinationWindow: NSWindow?
  var draggingSourceOperationMask: NSDragOperation = .move
  var draggingLocation = NSPoint.zero
  var draggedImageLocation = NSPoint.zero
  var draggedImage: NSImage? { nil }
  let draggingPasteboard = NSPasteboard.withUniqueName()
  var draggingSource: Any?
  var draggingSequenceNumber: Int { 1 }
  var draggingFormation: NSDraggingFormation = .none
  var animatesToDestination = false
  var numberOfValidItemsForDrop = 1
  var springLoadingHighlight: NSSpringLoadingHighlight { .none }
  func slideDraggedImage(to screenPoint: NSPoint) {}
  override func namesOfPromisedFilesDropped(atDestination dropDestination: URL) -> [String]? { nil }
  func resetSpringLoading() {}
  func enumerateDraggingItems(options: NSDraggingItemEnumerationOptions, for view: NSView?, classes: [AnyClass],
    searchOptions: [NSPasteboard.ReadingOptionKey: Any], using block: (NSDraggingItem, Int, UnsafeMutablePointer<ObjCBool>) -> Void) {}
  deinit { draggingPasteboard.releaseGlobally() }
}

private extension SwarmTabStrip {
  func checkDragOperations() throws {
    setFrameSize(NSSize(width: 900, height: 52))
    let rows = (0..<4).map { ["id": "drag-\($0)", "name": "Drag \($0)"] }
    update(["tabs": rows, "activeId": "drag-0", "enabled": true])
    var moves: [[String: Any]] = []
    emit = { method, args in if method == "reorder", let args = args as? [String: Any] { moves.append(args) } }
    let info = TitlebarCheckDrag()
    info.draggingSource = tabs[0]
    try checkTitlebar(info.draggingPasteboard.setString("drag-0", forType: swarmPasteboardType),
      "The native fixture can write its isolated drag pasteboard")
    info.draggingLocation = document.convert(NSPoint(x: tabs[2].frame.midX + 1, y: 20), to: nil)
    try checkTitlebar(draggingEntered(info) == .move && draggingUpdated(info) == .move,
      "An owned tab can move within the visible tab area: \(scroll.frame), \(scroll.documentVisibleRect), \(info.draggingLocation)")
    try checkTitlebar(performDragOperation(info) && moves.last?["index"] as? Int == 2,
      "Moving right accounts for removing the source tab first")
    moves.removeAll()
    info.draggingLocation = document.convert(NSPoint(x: tabs[0].frame.midX - 1, y: 20), to: nil)
    try checkTitlebar(performDragOperation(info) && moves.isEmpty, "Dropping in place performs no redundant reorder")
    info.draggingSource = tabs[3]
    info.draggingPasteboard.setString("drag-3", forType: swarmPasteboardType)
    try checkTitlebar(performDragOperation(info) && moves.last?["index"] as? Int == 0,
      "Moving left preserves the requested first position")
    moves.removeAll()
    func rejected(_ reason: String) throws {
      try checkTitlebar(draggingEntered(info).isEmpty && draggingUpdated(info).isEmpty && !performDragOperation(info), reason)
      try checkTitlebar(moves.isEmpty, "Rejected drag emits no reorder")
    }
    info.draggingLocation = convert(NSPoint(x: newButton.frame.midX, y: 20), to: nil)
    try rejected("New Tab is not a tab drop target")
    info.draggingLocation = document.convert(NSPoint(x: tabs[0].frame.midX, y: 20), to: nil)
    info.draggingSource = SwarmTabButton(id: "drag-3")
    try rejected("A foreign tab with a matching ID cannot reorder this strip")
    info.draggingSource = tabs[3]
    info.draggingSourceOperationMask = .copy
    try rejected("A copy-only source is not advertised as movable")
    info.draggingSourceOperationMask = .move
    info.draggingPasteboard.setString("drag-0", forType: swarmPasteboardType)
    try rejected("The pasteboard identity must match the actual dragged tab")
    info.draggingPasteboard.setString("drag-3", forType: swarmPasteboardType)
    update(["tabs": rows, "activeId": "drag-0", "enabled": false])
    try rejected("A modal rejects a pending tab drop")
    update(["tabs": Array(rows.prefix(3)), "activeId": "drag-0", "enabled": true])
    try rejected("A removed source cannot finish its pending drag")
  }
}

// No engine, account, terminal or transport is involved in native layout.
private final class TitlebarCheckMessenger: NSObject, FlutterBinaryMessenger {
  var calls: [FlutterMethodCall] = []
  private var handlers: [String: FlutterBinaryMessageHandler] = [:]
  var holdReplies = false
  var replies: [FlutterBinaryReply] = []
  func finishNextReply() {
    replies.removeFirst()(FlutterStandardMethodCodec.sharedInstance().encodeSuccessEnvelope(nil))
  }
  func send(onChannel channel: String, message: Data?) {
    if let message { calls.append(FlutterStandardMethodCodec.sharedInstance().decodeMethodCall(message)) }
  }
  func send(onChannel channel: String, message: Data?, binaryReply callback: FlutterBinaryReply?) {
    send(onChannel: channel, message: message)
    if let callback {
      if holdReplies { replies.append(callback) }
      else { callback(FlutterStandardMethodCodec.sharedInstance().encodeSuccessEnvelope(nil)) }
    }
  }
  func setMessageHandlerOnChannel(_ channel: String, binaryMessageHandler handler: FlutterBinaryMessageHandler?) -> FlutterBinaryMessengerConnection {
    handlers[channel] = handler
    return 1
  }
  func receive(_ method: String, arguments: [String: Any]) throws -> Data? {
    guard let handler = handlers["harness/swarm_tabs"] else {
      throw TitlebarCheckFailure(message: "Native channel handler is installed")
    }
    var reply: Data?
    let message = FlutterStandardMethodCodec.sharedInstance().encode(
      FlutterMethodCall(methodName: method, arguments: arguments))
    handler(message) { reply = $0 }
    return reply
  }
  func cleanUpConnection(_ connection: FlutterBinaryMessengerConnection) {}
}

private extension SwarmTitlebar {
  func checkKeymapRuntime(_ fixture: [String: [String: Any]], messenger: TitlebarCheckMessenger) throws {
    guard let window, let defaults = HarnessNativeKeymap(fixture["defaults"]!),
          let changed = HarnessNativeKeymap(fixture["changed"]!) else {
      throw TitlebarCheckFailure(message: "Exported runtime keymaps exist")
    }
    let original = NSApp.mainMenu!
    let edit = original.item(withTitle: "Edit")!.submenu!
    let agent = original.item(withTitle: "File")!.submenu!
    let newSwarm = agent.items.first(where: { $0.representedObject as? String == "new" })!
    let addHarness = agent.items.first(where: { $0.representedObject as? String == "addAgent" })!
    let models = original.item(withTitle: "View")!.submenu!.items.first { $0.representedObject as? String == "models" }!
    let nativeCopy = NSMenuItem(title: "Copy", action: #selector(NSText.copy(_:)), keyEquivalent: "c")
    edit.addItem(nativeCopy)
    setKeymap(defaults)
    let main = NSApp.mainMenu as! HarnessKeymapMenu
    try checkTitlebar(main !== original && main.item(withTitle: "Edit")?.submenu === edit,
      "The main-menu dispatcher retains the actual Edit submenu and its targets")
    try checkTitlebar(newSwarm.keyEquivalent == "t" && newSwarm.toolTip == nil,
      "Native shortcuts display in the menu without duplicate hover hints")
    try checkTitlebar(addHarness.keyEquivalent == "o" && addHarness.keyEquivalentModifierMask == [.command],
      "The exported keymap keeps Open Project on Command-O")
    for (action, key) in [("splitRight", "r"), ("splitDown", "d")] {
      let split = agent.items.first(where: { $0.representedObject as? String == action })!
      try checkTitlebar(split.keyEquivalent == key && split.keyEquivalentModifierMask == [.command],
        "The exported keymap preserves the native \(action) shortcut")
    }
    try checkTitlebar(models.keyEquivalent == "i" && models.keyEquivalentModifierMask == [.command],
      "The exported keymap gives View Models the same Command-I shortcut")
    let remappedModels = HarnessNativeKeymap(["version": 1, "contexts": Dictionary(uniqueKeysWithValues:
      HarnessNativeKeymap.contexts.map { ($0, [["keys": ["cmd+u"], "command": "models.list",
        "hint": "⌘U", "repeatable": false, "menuAction": "models"]]) })])!
    setKeymap(remappedModels)
    try checkTitlebar(models.keyEquivalent == "u",
      "Remapping Models updates the View menu")
    setKeymap(changed)
    try checkTitlebar(NSApp.mainMenu === main && newSwarm.keyEquivalent == "o",
      "Hot reload updates the existing menu to the remapped key")
    try checkTitlebar(nativeCopy.keyEquivalent == "c" && nativeCopy.action == #selector(NSText.copy(_:)),
      "Standard native editing remains intact")
    // The inherited default is still first; a sequence is not falsely shown
    // as a second one-stroke accelerator in AppKit's shortcut column.
    let onlySequence = HarnessNativeKeymap(["version": 1, "contexts": Dictionary(uniqueKeysWithValues:
      HarnessNativeKeymap.contexts.map { ($0, [["keys": ["cmd+k", "n"], "command": "swarm.new",
        "hint": "⌘K N", "repeatable": false, "menuAction": "new"]]) })])!
    setKeymap(onlySequence)
    try checkTitlebar(strip.newButton.toolTip == "New Tab · ⌘K N",
      "The plus tooltip teaches the current shortcut sequence")
    try checkTitlebar(newSwarm.keyEquivalent.isEmpty && newSwarm.toolTip == nil,
      "Sequences add no hover hints or misleading first-key menu shortcut")
    setKeymap(HarnessNativeKeymap(["version": 1, "contexts": ["workspace": [], "terminal": [], "picker": [], "project": [], "swarmChat": []]])!)
    try checkTitlebar(strip.newButton.toolTip == "New Tab",
      "Unbinding removes the plus tooltip's shortcut without leaving a separator")
    try checkTitlebar(newSwarm.keyEquivalent.isEmpty && newSwarm.toolTip == nil,
      "Unbinding clears the old native shortcut and hint")
    try checkTitlebar(models.keyEquivalent.isEmpty,
      "Unbinding Models clears its View menu shortcut")
    rebuildHistoryMenu()
    try checkTitlebar(historyMenu.items.allSatisfy { $0.keyEquivalent.isEmpty },
      "Rebuilt History rows retain effective unbindings")
    setKeymap(changed)
    actionsEnabled = true
    func event(_ text: String, _ code: UInt16, _ flags: NSEvent.ModifierFlags = []) -> NSEvent {
      NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: flags,
        timestamp: 0, windowNumber: window.windowNumber, context: nil, characters: text,
        charactersIgnoringModifiers: text, isARepeat: false, keyCode: code)!
    }
    let open = event("o", 31, .command)
    let oldOpen = event("t", 17, .command)
    try checkTitlebar(main.defersToInput(open) && !main.defersToInput(oldOpen),
      "The menu yields the remapped search shortcut to Flutter")
    try checkTitlebar(!main.performKeyEquivalent(with: open), "Menu equivalents defer before input dispatch")
    setKeymap(defaults)
    try checkTitlebar(strip.newButton.toolTip == "New Tab · ⌘T", "Keymap reload restores the current New Tab hint")
    try checkTitlebar(strip.newButton.accessibilityLabel() == "New Tab", "The plus announces New Tab")
    try checkTitlebar(main.defersToInput(event("n", 45, .command)) && main.defersToInput(event("t", 17, .command)),
      "Command-N and Command-T reach creation and New Tab")
    try checkTitlebar(main.defersToInput(event("p", 35, .command)), "Command-P reaches Harnesses")
    try checkTitlebar(main.defersToInput(event("p", 35, [.command, .shift])), "Command-Shift-P reaches commands")
    try checkTitlebar(main.defersToInput(event("o", 31, .command)), "Command-O reaches the project picker")
    try checkTitlebar(!main.defersToInput(event(";", 41, .command)) &&
      !main.performKeyEquivalent(with: event(";", 41, .command)),
      "Command-semicolon no longer opens Models")
    try checkTitlebar(main.defersToInput(event("i", 34, .command)) &&
      !main.performKeyEquivalent(with: event("i", 34, .command)),
      "Command-I reaches Flutter exactly once")
    try checkTitlebar(main.defersToInput(event("m", 46, .command)) &&
      !main.performKeyEquivalent(with: event("m", 46, .command)),
      "Command-M reaches Machines exactly once instead of invoking a native window action")
    try checkTitlebar(main.defersToInput(event("s", 1, .command)) &&
      !main.performKeyEquivalent(with: event("s", 1, .command)),
      "Command-S reaches Store exactly once")
    try checkTitlebar(!main.defersToInput(event("u", 32, .command)), "Command-U has no default workspace action")
    try checkTitlebar(!main.defersToInput(event("y", 16, .command)), "Command-Y has no default workspace action")
    flutterKeyContext = "picker"
    syncMenuKeys()
    try checkTitlebar(!main.performKeyEquivalent(with: event("\u{f701}", 125)), "Result arrows are owned by the shared picker")
    try checkTitlebar(!main.defersToInput(event("a", 0, .command)), "Standard select-all retains native text-editing dispatch")
  }

  func checkViewerShortcutRuntime(_ defaults: HarnessNativeKeymap, messenger: TitlebarCheckMessenger) throws {
    guard let window else { throw TitlebarCheckFailure(message: "Native viewer test window exists") }
    let originalMenu = NSApp.mainMenu
    let originalKeymap = keymap
    let originalContext = flutterKeyContext
    let originalEnabled = actionsEnabled
    NSApp.mainMenu = NSMenu(title: "Isolated viewer shortcut test")
    defer {
      keymap = originalKeymap
      flutterKeyContext = originalContext
      actionsEnabled = originalEnabled
      NSApp.mainMenu = originalMenu
    }
    actionsEnabled = true
    // Orchestrator has no default chord now. Exercise an explicit user binding;
    // Cmd-O opens projects; Cmd-Shift-P opens commands through the exported default keymap.
    let viewerMap = HarnessNativeKeymap(["version": 1, "contexts": [
      "workspace": [["keys": ["cmd+y"], "command": "project.orchestrate", "hint": "⌘Y", "repeatable": false]],
      "terminal": [], "picker": [], "project": [], "swarmChat": [],
    ]])!
    setKeymap(viewerMap)
    let main = NSApp.mainMenu as! HarnessKeymapMenu
    func event(_ text: String, _ code: UInt16, _ flags: NSEvent.ModifierFlags = []) -> NSEvent {
      NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: flags,
        timestamp: 0, windowNumber: window.windowNumber, context: nil, characters: text,
        charactersIgnoringModifiers: text, isARepeat: false, keyCode: code)!
    }
    // Exercise the actual native responder walk, not just the keymap lookup.
    // No navigation, windows shown, app state, or external services are involved.
    let web = WKWebView(frame: NSRect(x: 0, y: 0, width: 320, height: 200))
    let webInput = TitlebarCheckInputView(frame: web.bounds)
    web.addSubview(webInput)
    window.contentViewController!.view.addSubview(web)
    defer { web.removeFromSuperview(); window.makeFirstResponder(window.contentInput) }
    try checkTitlebar(window.makeFirstResponder(webInput), "A native viewer descendant can own test focus")
    flutterKeyContext = "workspace"
    setKeymap(viewerMap)
    let beforeViewer = messenger.calls.count
    try checkTitlebar(main.performKeyEquivalent(with: event("y", 16, .command)),
      "The custom Orchestrator shortcut is consumed by the focused native viewer bridge")
    try checkTitlebar(messenger.calls.count == beforeViewer + 1 && messenger.calls.last?.method == "keymapCommand" &&
      (messenger.calls.last?.arguments as? [String: String])?["command"] == "project.orchestrate",
      "The native viewer dispatches exactly one Orchestrator command to Flutter")
    let repeated = NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: .command,
      timestamp: 0, windowNumber: window.windowNumber, context: nil, characters: "y",
      charactersIgnoringModifiers: "y", isARepeat: true, keyCode: 16)!
    try checkTitlebar(main.performKeyEquivalent(with: repeated) && messenger.calls.count == beforeViewer + 1,
      "Holding the shortcut never launches repeated projects")
    _ = main.performKeyEquivalent(with: event("b", 11, .command))
    try checkTitlebar(messenger.calls.count == beforeViewer + 1, "The viewer bridge does not hijack Command-B")
    actionsEnabled = false
    _ = main.performKeyEquivalent(with: event("y", 16, .command))
    try checkTitlebar(messenger.calls.count == beforeViewer + 1, "Modal state blocks native viewer launch dispatch")
    actionsEnabled = true
    let remapped = HarnessNativeKeymap(["version": 1, "contexts": [
      "workspace": [["keys": ["cmd+x"], "command": "project.orchestrate", "hint": "⌘X", "repeatable": false]],
      "terminal": [], "picker": [], "project": [], "swarmChat": [],
    ]])!
    setKeymap(remapped)
    _ = main.performKeyEquivalent(with: event("y", 16, .command))
    try checkTitlebar(messenger.calls.count == beforeViewer + 1, "The old viewer shortcut stays unbound after remapping")
    try checkTitlebar(main.performKeyEquivalent(with: event("x", 7, .command)) && messenger.calls.count == beforeViewer + 2,
      "The viewer bridge follows the actual remapped two-key shortcut")
    setKeymap(HarnessNativeKeymap(["version": 1, "contexts": ["workspace": [], "terminal": [], "picker": [], "project": [], "swarmChat": []]])!)
    _ = main.performKeyEquivalent(with: event("x", 7, .command))
    try checkTitlebar(messenger.calls.count == beforeViewer + 2, "Unbinding disables viewer launch dispatch")
    setKeymap(defaults)
  }

  func checkNativeContainer(messenger: TitlebarCheckMessenger) throws {
    guard let window else { throw TitlebarCheckFailure(message: "Native test window exists") }
    let main = NSMenu()
    let appItem = NSMenuItem(title: "Harness", action: nil, keyEquivalent: "")
    appItem.submenu = NSMenu(title: "Harness")
    appItem.submenu?.addItem(NSMenuItem(title: "Preferences…", action: nil, keyEquivalent: ","))
    main.addItem(appItem)
    let edit = NSMenuItem(title: "Edit", action: nil, keyEquivalent: "")
    edit.submenu = NSMenu(title: "Edit")
    let find = NSMenuItem(title: "Find", action: nil, keyEquivalent: "")
    find.submenu = NSMenu(title: "Find")
    find.submenu?.addItem(NSMenuItem(title: "Find and Replace…", action: nil, keyEquivalent: "f"))
    edit.submenu?.addItem(find)
    main.addItem(edit)
    for title in ["View", "Window", "Help"] {
      let item = NSMenuItem(title: title, action: nil, keyEquivalent: "")
      item.submenu = NSMenu(title: title)
      main.addItem(item)
    }
    NSApp.mainMenu = main
    let startupColors: [String: Any] = [
      "tabBar": Int64(0xff1b2720), "workspace": Int64(0xff2b3b31),
      "search": Int64(0xff293a31), "accent": Int64(0xffb4d8be),
    ]
    let reply = try messenger.receive("configure", arguments: ["palette": startupColors])
    try checkTitlebar(reply == FlutterStandardMethodCodec.sharedInstance().encodeSuccessEnvelope(true),
      "Native configure acknowledges the initial palette synchronously")
    let startupPalette = SwarmNativePalette(startupColors)
    try checkTitlebar(strip.palette == startupPalette && window.backgroundColor == startupPalette.tabBar,
      "The saved palette reaches native chrome before any workspace update")
    try strip.checkStartupPalette(startupPalette)
    configure()
    try checkTitlebar(strip.palette == startupPalette && window.titlebarAccessoryViewControllers.count == 1,
      "Repeated configuration preserves the saved palette and one titlebar accessory")
    _ = try messenger.receive("update", arguments: [
      "tabs": [["id": "startup-check", "name": "Synthetic tab"]],
      "activeId": "startup-check", "enabled": true, "palette": startupColors,
    ])
    window.contentView?.layoutSubtreeIfNeeded()
    try checkTitlebar(strip.statusBar.superview === window.contentView && !strip.statusBar.isHidden &&
      strip.statusBar.frame.width == window.contentView!.bounds.width &&
      strip.statusBar.frame.minY == window.contentView!.bounds.minY &&
      strip.statusBar.frame.height == strip.preferredStatusBarHeight,
      "The native footer spans the content bottom without adding another titlebar row")
    try checkTitlebar(strip.statusBar.isAccessibilityElement() &&
      strip.statusBar.accessibilityParent() as? NSView === strip &&
      strip.accessibilityChildren()?.contains(where: { $0 as? NSView === strip.statusBar }) == true,
      "The footer remains in the native accessibility tree outside Flutter-owned children")
    // SwarmScreen.dispose sends this when sign-in or setup takes its place.
    _ = try messenger.receive("update", arguments: ["tabs": [], "enabled": false])
    try checkTitlebar(window.backgroundColor == startupPalette.tabBar && strip.statusBar.isHidden,
      "Leaving the workspace hides the footer and preserves the saved native background")
    try strip.checkStartupPalette(startupPalette)
    try window.checkContentCommand()
    try checkTitlebar(window.firstResponder === window.contentInput,
      "Adding toolbar buttons does not take initial keyboard focus from the workspace")
    try checkTitlebar(main.items.map(\.title) == ["Harness", "File", "Edit", "View", "History", "Window", "Help"], "Models and Machines live in the toolbar and View menu")
    let settings = appItem.submenu!.items.first { $0.representedObject as? String == "settings" }!
    try checkTitlebar(settings.title == "Settings…" && settings.representedObject as? String == "settings", "Settings stays in the application menu")
    let agent = main.item(withTitle: "File")!.submenu!
    let addHarness = agent.items.first(where: { $0.representedObject as? String == "addAgent" })!
    try checkTitlebar(addHarness.title == "Open Project" && addHarness.keyEquivalent == "o" && addHarness.keyEquivalentModifierMask == [.command],
      "Open Project advertises Command-O")
    try checkTitlebar(!agent.items.contains { $0.representedObject as? String == "newTerminal" },
      "New Terminal stays off the File menu; its chord lives in the keymap")
    let historyMenu = main.item(withTitle: "History")!.submenu!
    try checkTitlebar(agent.items.contains { $0.title == "Clone Harness" && $0.representedObject as? String == "cloneAgent" }, "Clone Harness preserves its action")
    try checkTitlebar(agent.items.map { $0.isSeparatorItem ? "separator" : ($0.representedObject as? String ?? "") } == ["newAgent", "sessions", "addAgent", "cloneAgent", "restartAgent", "shareAgent", "removeFromTab", "toggleLeader", "separator", "new", "renameActive", "closeActive", "invitePeople", "showAgentMessages", "stopSharingTab", "moveTabHere", "leaveTab", "separator", "splitRight", "splitDown", "zoomPane", "movePaneToTab", "closePane"], "File groups harness, tab and pane actions, harnesses first")
    for (action, title, menu) in [
      ("restartAgent", "Restart Harness", agent),
      ("shareAgent", "Share Harness", agent),
      ("removeFromTab", "Remove from Tab", agent),
      ("toggleLeader", "Make Leader", agent),
      ("toggleViewer", "Toggle Viewer", main.item(withTitle: "View")!.submenu!),
      ("toggleComposer", "Toggle Message Composer", main.item(withTitle: "View")!.submenu!),
    ] {
      let item = menu.items.first { $0.representedObject as? String == action }!
      try checkTitlebar(item.title == title, "\(title) has the right destination")
      actionsEnabled = true
      paneActions[action] = false
      try checkTitlebar(!validateMenuItem(item), "\(title) is unavailable without an eligible focused pane")
      paneActions[action] = true
      try checkTitlebar(validateMenuItem(item), "\(title) is enabled for the focused pane")
      menuAction(item)
      try checkTitlebar(messenger.calls.last?.method == action, "\(title) dispatches to Flutter")
      actionsEnabled = false
      try checkTitlebar(!validateMenuItem(item), "\(title) respects modal state")
    }
    // Remove from Tab is hidden unless Dart offers it for the focused pane.
    let removeRow = agent.items.first { $0.representedObject as? String == "removeFromTab" }!
    for offered in [true, false] {
      _ = try messenger.receive("update", arguments: ["enabled": true, "tabs": [], "paneActions": ["removeFromTab": offered]])
      try checkTitlebar(removeRow.isHidden == !offered, "File shows Remove from Tab only where the focused harness can leave its tab")
    }
    // Make Leader / Remove Leader: one row whose title follows what Dart offers the focused pane, hidden when neither.
    let leaderRow = agent.items.first { $0.representedObject as? String == "toggleLeader" }!
    for (offered, action, title) in [(true, "make", "Make Leader"), (true, "remove", "Remove Leader"), (false, "make", "Make Leader"), (true, "none", "Make Leader")] {
      var update: [String: Any] = ["enabled": true, "tabs": [], "paneActions": ["toggleLeader": offered]]
      if action != "none" { update["leaderAction"] = action }
      _ = try messenger.receive("update", arguments: update)
      try checkTitlebar(leaderRow.title == title, "File says \(title) for a \(action) offer")
      try checkTitlebar(leaderRow.isHidden == !(offered && action != "none"), "File shows Make Leader / Remove Leader only where Dart offers one")
    }
    // File's sharing rows (1c) for the active tab: shown and enabled only as Dart offers them.
    let shareRows = ["invitePeople": "Invite People", "stopSharingTab": "Stop Sharing Tab", "moveTabHere": "Move Here",
                     "leaveTab": "Leave Tab"]
    for (offered, enabled) in [(["invitePeople"], true), (["invitePeople", "stopSharingTab", "moveTabHere"], true),
                               (["leaveTab"], true), ([], true), (["invitePeople"], false)] {
      _ = try messenger.receive("update", arguments: ["enabled": enabled, "tabs": [],
        "tabShareActions": Dictionary(uniqueKeysWithValues: offered.map { ($0, true) })])
      for (action, title) in shareRows {
        let item = agent.items.first { $0.representedObject as? String == action }!
        try checkTitlebar(item.title == title && item.isHidden == !offered.contains(action),
          "File shows \(title) only where the active tab offers it")
        try checkTitlebar(validateMenuItem(item) == (enabled && offered.contains(action)),
          "\(title) is enabled only where offered, and never under a modal")
        if validateMenuItem(item) {
          menuAction(item)
          try checkTitlebar(messenger.calls.last?.method == action && messenger.calls.last?.arguments == nil,
            "\(title) asks Flutter for the active tab")
        }
      }
    }
    _ = try messenger.receive("update", arguments: ["tabs": [], "enabled": false])
    try checkTitlebar(agent.items.first?.title == "New Harness" && agent.items.first?.keyEquivalent == "n" && agent.items.first?.keyEquivalentModifierMask == [.command],
      "New Harness leads File on Command-N")
    for (action, title, key) in [("splitRight", "Split Right", "r"), ("splitDown", "Split Down", "d")] {
      let split = agent.items.first(where: { $0.representedObject as? String == action })!
      try checkTitlebar(split.title == title && split.keyEquivalent == key && split.keyEquivalentModifierMask == [.command],
        "\(title) advertises its directional split shortcut")
      actionsEnabled = true
      canFind = false
      try checkTitlebar(!validateMenuItem(split), "\(title) needs a focused pane")
      canFind = true
      try checkTitlebar(validateMenuItem(split), "\(title) is available with a focused pane")
      menuAction(split)
      try checkTitlebar(messenger.calls.last?.method == action, "\(title) reaches the Flutter split picker")
    }
    try checkTitlebar(agent.items.filter { !$0.isSeparatorItem }.allSatisfy { $0.image != nil && $0.toolTip == nil },
      "Every File action has a native icon and no hover hint")
    try checkTitlebar(agent.items.contains { $0.title == "Rename Tab" && $0.representedObject as? String == "renameActive" }, "Rename Tab preserves its command")
    let movePane = agent.items.first(where: { $0.representedObject as? String == "movePaneToTab" })!
    try checkTitlebar(movePane.title == "Move Pane to Tab" && movePane.keyEquivalent == "m" && movePane.keyEquivalentModifierMask == [.command, .shift],
      "Move Pane to Tab advertises Command-Shift-M")
    actionsEnabled = true
    canFind = false
    try checkTitlebar(!validateMenuItem(movePane), "Move Pane to Tab needs a focused pane")
    canFind = true
    try checkTitlebar(validateMenuItem(movePane), "Move Pane to Tab is available with a focused pane")
    try checkTitlebar(agent.items.contains { $0.title == "Close Tab" && $0.representedObject as? String == "closeActive" }, "Close Tab preserves its command")
    // The active tab's Agent Messages log. No on/off row: that is Settings ▸ Experimental's.
    try checkTitlebar(!agent.items.contains { $0.title == "Agent Messages" },
      "File has no Agent Messages on/off row")
    let showMessagesRow = agent.items.first { $0.representedObject as? String == "showAgentMessages" }!
    try checkTitlebar(showMessagesRow.title == "Show Agent Messages", "File names Show Agent Messages")
    actionsEnabled = true
    canShowAgentMessages = false
    try checkTitlebar(!validateMenuItem(showMessagesRow),
      "A tab without a log (the Store) leaves Show Agent Messages unavailable")
    canShowAgentMessages = true
    try checkTitlebar(validateMenuItem(showMessagesRow) && showMessagesRow.state == .off,
      "Show Agent Messages is available for a tab with a log, and never checked")
    menuAction(showMessagesRow)
    try checkTitlebar(messenger.calls.last?.method == "showAgentMessages",
      "Show Agent Messages dispatches to Flutter for the active tab")
    actionsEnabled = false
    try checkTitlebar(!validateMenuItem(showMessagesRow), "Show Agent Messages respects modal state")
    actionsEnabled = true
    // View's Show Tab Activity / Hide Tab Activity for the active tab: shown and enabled where the tab has Agent
    // Messages, titled by whether its sidebar is open.
    let tabActivityRow = main.item(withTitle: "View")!.submenu!.items.first { $0.representedObject as? String == "toggleTabActivity" }!
    try checkTitlebar(tabActivityRow.image != nil, "Tab Activity has a native icon")
    for (log, open) in [(true, false), (true, true), (false, false), (false, true)] {
      _ = try messenger.receive("update", arguments: ["enabled": true, "tabs": [], "canShowAgentMessages": true, "canShowTabActivity": log, "tabActivityOpen": open])
      try checkTitlebar(tabActivityRow.isHidden == !log, "View shows Tab Activity only where Dart's toggle would act")
      try checkTitlebar(tabActivityRow.title == (open ? "Hide Tab Activity" : "Show Tab Activity"),
        "View says Show or Hide Tab Activity as the sidebar stands")
      try checkTitlebar(validateMenuItem(tabActivityRow) == log, "Tab Activity is enabled only where Dart's toggle would act, not with Agent Messages alone")
    }
    _ = try messenger.receive("update", arguments: ["enabled": true, "tabs": [], "canShowTabActivity": true])
    menuAction(tabActivityRow)
    try checkTitlebar(messenger.calls.last?.method == "toggleTabActivity", "Tab Activity dispatches to Flutter for the active tab")
    _ = try messenger.receive("update", arguments: ["enabled": false, "tabs": [], "canShowTabActivity": true])
    try checkTitlebar(!validateMenuItem(tabActivityRow), "Tab Activity respects modal state")
    actionsEnabled = true
    canShowAgentMessages = true
    let closeTabShortcut = agent.items.first { $0.representedObject as? String == "closeActive" }!
    let closePaneShortcut = agent.items.first { $0.representedObject as? String == "closePane" }!
    try checkTitlebar(closeTabShortcut.keyEquivalent == "w" && closeTabShortcut.keyEquivalentModifierMask == [.command], "Close Tab defaults to Command-W")
    try checkTitlebar(closePaneShortcut.keyEquivalent == "w" && closePaneShortcut.keyEquivalentModifierMask == [.command, .shift], "Close Pane defaults to Command-Shift-W")
    let commands = edit.submenu!.items.first(where: { $0.representedObject as? String == "commands" })!
    try checkTitlebar(commands.keyEquivalent == "p" && commands.keyEquivalentModifierMask == [.command, .shift], "Command search keeps its native menu owner")
    let harnesses = agent.items.first { $0.representedObject as? String == "sessions" }!
    try checkTitlebar(harnesses.title == "Open Harness" && harnesses.keyEquivalent == "p" && harnesses.keyEquivalentModifierMask == [.command],
      "Command-P keeps harness search; commands use Command-Shift-P")
    try checkTitlebar(edit.submenu!.items.allSatisfy { $0.representedObject as? String != "jump" }, "Edit has no Navigate action")
    try checkTitlebar(agent.items.contains { $0.title == "New Tab" && $0.keyEquivalent == "t" && $0.representedObject as? String == "new" }, "New Tab opens the chooser with Command-T")
    let reopen = historyMenu.items.first(where: { $0.representedObject as? String == "reopen" })!
    actionsEnabled = true
    canReopen = false
    try checkTitlebar(!validateMenuItem(reopen), "Closed-Tab recovery is disabled with an empty history")
    canReopen = true
    try checkTitlebar(validateMenuItem(reopen), "Closed-Tab recovery becomes available")
    let closePane = agent.items.first(where: { $0.representedObject as? String == "closePane" })!
    canClosePane = false
    try checkTitlebar(!validateMenuItem(closePane), "Remove Agent is disabled in New Tab")
    canClosePane = true
    try checkTitlebar(validateMenuItem(closePane), "Remove Agent is enabled for a focused pane")
    let create = agent.items.first(where: { $0.representedObject as? String == "new" })!
    try checkTitlebar(validateMenuItem(create), "Native New Tab remains available without the retired tab capacity")
    let machineRows: [[String: Any]] = [
      ["id": "office", "name": "iMac – Office", "status": "Online", "presence": "Online", "local": true, "agentCount": 2,
       "agents": [["id": "one", "title": "App work", "engine": "codex", "canOpen": true],
                  ["id": "two", "title": "Unavailable session", "canOpen": false]]],
      ["id": "home", "name": "iMac – Home", "status": "Offline", "presence": "Offline", "local": false, "agentCount": 0],
    ]
    _ = try messenger.receive("machinesState", arguments: ["machines": machineRows])
    let machineMenu = main.item(withTitle: "View")!.submenu!
    let manager = machineMenu.items.first { $0.representedObject as? String == "machineList" }!
    let manage = machineMenu.items.first { $0.representedObject as? String == "manageMachines" }!
    try checkTitlebar(manager.title == "Machines" && manager.keyEquivalent == "m" &&
      manager.keyEquivalentModifierMask == [.command], "View exposes Machines on Command-M")
    try checkTitlebar(main.item(withTitle: "Machines") == nil, "The old Machines menu is replaced")
    menuAction(manage)
    try checkTitlebar(messenger.calls.last?.method == "manageMachines", "Machine Monitor remains available in View")
    menuAction(manager)
    try checkTitlebar(messenger.calls.last?.method == "machineList", "Machines opens the Flutter panel")
    actionsEnabled = true
    var recentRows: [[String: Any]] = (0..<20).map { index -> [String: Any] in
      ["id": "agent:\(index)", "title": "Agent \(index) — Machine",
       "detail": "Project \(index)", "machineName": "M2", "current": index == 0,
       "engine": index == 0 ? "claude" : "codex"]
    }
    recentRows.append(["id": "swarm:recent", "title": "Recent Tab", "swarm": true])
    let closedRows: [[String: Any]] = (0..<14).map {
      ["id": "closed-\($0)", "title": "Closed Tab \($0)", "detail": "3 agents", "swarm": true, "canReopen": true]
    }
    updateHistory(recentRows, closed: closedRows)
    menuWillOpen(historyMenu)
    let recentItems = historyMenu.items.filter { $0.action == #selector(historyAction(_:)) }
    let closedItems = historyMenu.items.filter { $0.action == #selector(closedHistoryAction(_:)) }
    try checkTitlebar(recentItems.count == 15 && closedItems.count == 10, "Chrome-style direct History sections remain bounded")
    try checkTitlebar(historyMenu.items.filter { !$0.isSeparatorItem }.prefix(2).map(\.title) == ["Back", "Forward"], "History begins with Back and Forward")
    try checkTitlebar(historyMenu.items.last?.title == "Show Full History" && historyMenu.items.last?.keyEquivalent.isEmpty == true, "Full History remains available without a default shortcut")
    try checkTitlebar(historyMenu.items.allSatisfy { $0.submenu == nil }, "Recent work is available without nested menus")
    let recent = recentItems[0]
    let closed = closedItems[0]
    try checkTitlebar(historyMenu.size.width < 504 && historyMenu.minimumWidth > 0, "History adds twenty percent reading room")
    let recentView = recent.view as! SwarmHistoryMenuRow
    try checkTitlebar(recentView.machineFrame.maxX == recentView.bounds.width - 18,
      "History machine names align at the right edge beyond the command shortcut column")
    menu(historyMenu, willHighlight: recent)
    try checkTitlebar(recentView.highlighted, "Keyboard and mouse menu highlight reaches the full History row")
    let historyCallCount = messenger.calls.count
    try checkTitlebar(recentView.accessibilityPerformPress() && messenger.calls.count == historyCallCount + 1 &&
      messenger.calls.last?.method == "historyDestination", "The full-width History row opens the same native destination")
    actionsEnabled = false
    menuWillOpen(historyMenu)
    try checkTitlebar(!recent.isEnabled && !recentView.accessibilityPerformPress(),
      "A modal disables full-width History rows and their accessibility action")
    actionsEnabled = true
    menuWillOpen(historyMenu)
    try checkTitlebar(recent.isEnabled, "History rows become available again after the modal closes")
    try checkTitlebar(recent.image?.size == NSSize(width: 16, height: 16) && recent.image?.isTemplate == false,
      "History uses the colored Claude mark at native menu size")
    try checkTitlebar(recent.state == .on && recent.toolTip == nil, "History adds no hover hints")
    updateHistory(recentRows, closed: closedRows)
    try checkTitlebar(historyMenu.items.contains(where: { $0 === recent }), "Unchanged history retains native menu items")
    try checkTitlebar(validateMenuItem(closed), "A specific closed Tab can be restored")
    canReopen = false
    try checkTitlebar(validateMenuItem(closed), "A chosen closure uses its own capacity, independently of the latest closure")
    var unavailableRows = closedRows
    unavailableRows[0]["canReopen"] = false
    updateHistory(recentRows, closed: unavailableRows)
    try checkTitlebar(!validateMenuItem(closed), "Specific restore respects its destination capacity")
    updateHistory(recentRows, closed: closedRows)
    canReopen = true
    let back = historyMenu.items[0]
    let forward = historyMenu.items[1]
    canGoBack = false
    canGoForward = true
    try checkTitlebar(!validateMenuItem(back) && validateMenuItem(forward), "Back and Forward have independent navigation availability")
    try checkTitlebar(validateMenuItem(recent), "Recent navigation is available in the shell")
    actionsEnabled = false
    try checkTitlebar(!validateMenuItem(recent) && !validateMenuItem(commands) && !validateMenuItem(settings), "History, commands and Settings cannot act behind a modal")
    for item in agent.items where !item.isSeparatorItem {
      try checkTitlebar(!validateMenuItem(item), "Workspace commands cannot act behind a modal")
    }
    actionsEnabled = true
    let iconRows: [[String: Any]] = [
      ["id": "single-harness", "title": "Architecture", "swarm": true,
       "agentCount": 1, "engine": "claude"],
      ["id": "group-harness", "title": "Project", "swarm": true,
       "agentCount": 2],
    ]
    updateHistory(iconRows, closed: iconRows)
    let singleItems = historyMenu.items.filter { $0.representedObject as? String == "single-harness" }
    let groupItems = historyMenu.items.filter { $0.representedObject as? String == "group-harness" }
    try checkTitlebar(singleItems.count == 2 && singleItems.allSatisfy {
      $0.image === historyIcons.image(engine: "claude", asset: nil)
    }, "Recently visited and closed single-agent agents show their agent icon")
    try checkTitlebar(groupItems.count == 2 && groupItems.allSatisfy {
      $0.image === SwarmIdentity.menuIcon
    }, "Multiple-agent agents retain the group icon")
    updateHistory([])
    try checkTitlebar(!validateMenuItem(recent), "A stale recent menu item cannot dispatch after its view disappears")
    try checkTitlebar(!validateMenuItem(closed), "A stale closed entry cannot restore another Tab")
    try checkTitlebar(historyMenu.items.first(where: { $0.title == "No Recent Visits" })?.isEnabled == false, "An empty history is an inert placeholder")
    guard let menu = main.items.first(where: { $0.title == "View" })?.submenu,
          let attention = menu.items.first(where: { $0.representedObject as? String == "notifications" }) else {
      throw TitlebarCheckFailure(message: "View menu exposes agents needing input")
    }
    try checkTitlebar(attention.title == "Harnesses Needing Input…", "Native command names its destination")
    try checkTitlebar(attention.keyEquivalent == "i" && attention.keyEquivalentModifierMask == [.command, .shift], "Native attention shortcut matches Flutter")
    try checkTitlebar(attention.target === self && attention.action == #selector(menuAction(_:)), "Native attention command uses the guarded channel handler")
    actionsEnabled = false
    try checkTitlebar(!validateMenuItem(attention), "Native attention shortcut is disabled behind a modal")
    actionsEnabled = true
    try checkTitlebar(validateMenuItem(attention), "Native attention shortcut returns when the modal closes")
    try checkTitlebar(main.items.compactMap(\.submenu).flatMap(\.items).allSatisfy {
      !["Next Tab", "Previous Tab"].contains($0.title)
    }, "Next and Previous Tab have no redundant menu rows")
    try checkTitlebar(main.item(withTitle: "Models") == nil,
      "Models has no duplicate top-level menu")
    let openModels = menu.items.first(where: { $0.representedObject as? String == "models" })!
    try checkTitlebar(openModels.title == "Models" && openModels.submenu == nil,
      "View offers one direct Models command")
    try checkTitlebar(openModels.keyEquivalent == "i" && openModels.keyEquivalentModifierMask == [.command],
      "View Models advertises Command-I")
    try checkTitlebar(openModels.target === self && openModels.action == #selector(menuAction(_:)),
      "View Models dispatches the same action as the toolbar")
    try checkTitlebar(openModels.identifier?.rawValue == HarnessKeymapMenu.actionPrefix + "models",
      "View Models retains its keymap identity")
    installWorkspaceMenus()
    try checkTitlebar(menu.items.filter { $0.representedObject as? String == "models" }.count == 1,
      "Repeated setup keeps a single Models command")
    for rows in [machineRows, [machineRows[0]], []] {
      _ = try messenger.receive("machinesState", arguments: ["machines": rows])
      try checkTitlebar(menu.items.first(where: { $0.representedObject as? String == "models" }) === openModels,
        "Machine updates retain the same Models command")
    }
    let menuCalls = messenger.calls.count
    menuAction(openModels)
    try checkTitlebar(messenger.calls.count == menuCalls + 1 && messenger.calls.last?.method == "models",
      "View Models opens the shared panel exactly once")
    let modelCalls = messenger.calls.count
    _ = try messenger.receive("update", arguments: ["enabled": false])
    menuAction(openModels)
    try checkTitlebar(messenger.calls.count == modelCalls && !validateMenuItem(openModels),
      "The Models menu cannot dispatch behind a modal")
    actionsEnabled = true

    let findItems = find.submenu?.items ?? []
    try checkTitlebar(findItems.map(\.title) == ["Find in Terminal…", "Find Next", "Find Previous"], "Find replaces the unused editor actions with terminal commands")
    try checkTitlebar(findItems.map(\.keyEquivalent) == ["f", "g", "g"], "Native find shortcuts match Flutter")
    try checkTitlebar(findItems.last?.keyEquivalentModifierMask == [.command, .shift], "Previous match uses Shift-Command-G")
    for item in findItems {
      canFind = false
      try checkTitlebar(!validateMenuItem(item), "Find is disabled without a focused terminal")
      canFind = true
      try checkTitlebar(validateMenuItem(item), "Find is enabled for a focused terminal")
      actionsEnabled = false
      try checkTitlebar(!validateMenuItem(item), "Find cannot run behind a modal")
      actionsEnabled = true
      try checkTitlebar(item.target === self && item.action == #selector(menuAction(_:)), "Find uses the guarded channel handler")
    }
    strip.update([
      "enabled": true, "activeId": "swarm-11",
      "tabs": (0..<12).map { ["id": "swarm-\($0)", "name": "Tab \($0)"] },
    ])
    for width in [880.0, 1280.0, 1920.0] {
      window.setContentSize(NSSize(width: width, height: 700))
      window.contentView?.superview?.layoutSubtreeIfNeeded()
      resize()
      window.contentView?.superview?.layoutSubtreeIfNeeded()
      strip.layoutSubtreeIfNeeded()
      try strip.checkWindowGeometry(window)
    }
    try strip.checkTabKeyboardFocus(window, messenger: messenger)
    let editor = TitlebarCheckInputView()
    window.contentViewController!.view.addSubview(editor)
    window.makeFirstResponder(editor)
    messenger.holdReplies = true
    sendTabAction("new", arguments: nil)
    messenger.finishNextReply()
    try checkTitlebar(window.firstResponder === editor,
      "An acknowledged menu action preserves an already focused content editor")
    messenger.holdReplies = false
    editor.removeFromSuperview()
    try checkTitlebar(!window.isVisible, "Native layout check never displays its window")
  }
}

private final class TitlebarCheckContentController: NSViewController {
  override var acceptsFirstResponder: Bool { true }
}

private final class TitlebarCheckInputView: NSView {
  var keys: [UInt16] = []
  override var acceptsFirstResponder: Bool { true }
  override func keyDown(with event: NSEvent) { keys.append(event.keyCode) }
}

/// Flutter's wrapper dispatches key equivalents only when its input view owns
/// focus. A bare accepting controller lets ordinary keys through but misses
/// that condition, so test the wrapper/input relationship as well as focus.
private final class TitlebarCheckContentView: NSView {
  let input = TitlebarCheckInputView()
  override init(frame: NSRect) {
    super.init(frame: frame)
    input.frame = bounds
    input.autoresizingMask = [.width, .height]
    addSubview(input)
  }
  required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
  override func performKeyEquivalent(with event: NSEvent) -> Bool {
    guard window?.firstResponder === input else { return false }
    input.keyDown(with: event)
    return true
  }
}

private extension NSWindow {
  var contentInput: TitlebarCheckInputView {
    (contentViewController!.view as! TitlebarCheckContentView).input
  }
  func checkContentCommand() throws {
    let count = contentInput.keys.count
    let event = NSEvent.keyEvent(with: .keyDown, location: .zero,
      modifierFlags: .command, timestamp: 1, windowNumber: windowNumber,
      context: nil, characters: "n", charactersIgnoringModifiers: "n",
      isARepeat: false, keyCode: 45)!
    try checkTitlebar(performKeyEquivalent(with: event),
      "The content wrapper accepts Command-N after native focus handoff")
    try checkTitlebar(contentInput.keys.count == count + 1 && contentInput.keys.last == 45,
      "Command-N reaches content exactly once")
  }
}

let titlebarCheckApp = NSApplication.shared
titlebarCheckApp.setActivationPolicy(.prohibited)
titlebarCheckApp.appearance = NSAppearance(named: .darkAqua)
do {
  let paletteValues: [String: Any] = [
    "tabBar": Int64(0xff1b2030), "workspace": Int64(0xff252d43),
    "search": Int64(0xff262f46), "accent": Int64(0xffb1c7f5),
  ]
  let palette = SwarmNativePalette(paletteValues)
  let searchColor = palette.search.usingColorSpace(.sRGB)!
  try checkTitlebar(abs(searchColor.redComponent - 38.0 / 255) < 0.0001 && abs(searchColor.blueComponent - 70.0 / 255) < 0.0001,
    "Native search uses the exact palette channels supplied by Flutter")
  try checkTitlebar(SwarmNativePalette(["search": -1, "tabBar": "invalid"]) == SwarmNativePalette(),
    "Malformed palette data retains readable native defaults")
  let historyRow = SwarmHistoryEntry([
    "id": "agent", "title": "Build a toy", "machineName": "MacBook Pro M2", "engine": "codex",
  ])!
  let label = historyRow.menuTitle()
  try checkTitlebar(label.string == "Build a toy\tMacBook Pro M2", "Agent and machine occupy separate native menu columns")
  let paragraph = label.attribute(.paragraphStyle, at: 0, effectiveRange: nil) as! NSParagraphStyle
  try checkTitlebar(paragraph.tabStops.count == 1 && paragraph.tabStops[0].alignment == .right && paragraph.tabStops[0].location < 300,
    "Machine labels use a compact right-aligned column sized to the text")
  let longRow = SwarmHistoryEntry(["id": "long", "title": String(repeating: "Long title ", count: 100), "machineName": "Mac"])!
  try checkTitlebar(longRow.menuTitle().string.contains("…\tMac") && longRow.menuTitle().size().width < 380,
    "Long titles truncate before the machine column without widening the menu")
  let swarmRow = SwarmHistoryEntry(["id": "swarm", "title": "My tab", "swarm": true])!
  try checkTitlebar(swarmRow.menuTitle().string == "My tab", "Empty tab rows have no invented machine label")
  let sharedSwarm = SwarmHistoryEntry(["id": "shared", "title": "Workshop", "swarm": true, "machineName": "2 machines"])!
  try checkTitlebar(sharedSwarm.menuTitle().string == "Workshop\t2 machines", "Tab machine counts use the same trailing column as agent machines")
  for button in [SwarmIconButton()] {
    button.frame = NSRect(x: 0, y: 0, width: 28, height: 28)
    button.image = NSImage(systemSymbolName: "plus", accessibilityDescription: nil)
    button.isBordered = false
    let rest = button.renderedPixels()
    let hover = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
      timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
    button.mouseEntered(with: hover)
    try checkTitlebar(button.renderedPixels() != rest, "Native icon buttons draw a hover background")
    button.mouseExited(with: hover)
    try checkTitlebar(button.renderedPixels() == rest, "Native icon backgrounds clear when the pointer leaves")
    button.isEnabled = false
    let disabled = button.renderedPixels()
    button.mouseEntered(with: hover)
    try checkTitlebar(button.renderedPixels() == disabled, "Disabled native icons do not highlight")
  }
  var assetReads = 0
  let icons = SwarmHistoryIcons(assetURL: { asset in
    assetReads += 1
    guard let root = ProcessInfo.processInfo.environment["HARNESS_TITLEBAR_ASSETS"] else { return nil }
    return URL(fileURLWithPath: root).appendingPathComponent(String(asset.dropFirst("assets/".count)))
  })
  for engine in ["codex", "grok", "cursor", "opencode"] {
    let asset = "assets/engine-icons/\(engine).png"
    let icon = icons.image(engine: engine, asset: asset)
    try checkTitlebar(icon.size == NSSize(width: 16, height: 16) && !icon.isTemplate, "\(engine) uses its colored bundled mark")
    try checkTitlebar(icons.image(engine: engine, asset: asset) === icon, "Repeated \(engine) history reuses its decoded icon")
  }
  try checkTitlebar(assetReads == 4, "Native history loads each bundled mark only once")
  for name in ["machines", "models", "harnesses"] {
    let icon = icons.image(engine: name, asset: "assets/\(name).svg", pointSize: 20)
    try checkTitlebar(icon.size == NSSize(width: 20, height: 20) && icon.isTemplate,
      "\(name) uses a monochrome toolbar SVG")
    try checkTitlebar(!icon.representations.isEmpty &&
      icon.representations.allSatisfy { !($0 is NSBitmapImageRep) },
      "The native \(name) icon retains a vector representation")
    try checkTitlebar(icons.image(engine: name, asset: "assets/\(name).svg", pointSize: 20) === icon,
      "\(name) reuses its loaded SVG")
  }
  let unknown = icons.image(engine: "custom", asset: nil)
  try checkTitlebar(unknown.isTemplate && unknown.size == NSSize(width: 16, height: 16), "Unknown engines have a native-size adaptive initial")
  let strip = SwarmTabStrip(frame: NSRect(x: 0, y: 0, width: 900, height: 52))
  try strip.runChecks()
  try SwarmTabStrip(frame: NSRect(x: 0, y: 0, width: 900, height: 52)).checkKeyboardOnTabs()
  try strip.checkAgentIdentity()
  try strip.checkSharedTypography()
  try strip.checkShareAction()
  try strip.checkHarnessMonitor()
  try strip.checkInventoryFooter()
  try strip.checkTopActions()
  try SwarmTabStrip(frame: NSRect(x: 0, y: 0, width: 900, height: 40)).checkTabActivityButton()
  try checkIconTooltipTracking()
  try strip.checkActivityMarks()
  try SwarmTabButton(id: "label-measurements").checkLabelMeasurementChanges()
  try SwarmTabButton(id: "hover-close").checkHoverClose()
  try SwarmTabButton(id: "agent-messages").checkAgentMessages()
  try SwarmTabButton(id: "shared-tab").checkSharedTab()
  try SwarmTabButton(id: "minimum-width").checkMinimumWidth()
  try SwarmTabButton(id: "selection-surface").checkSelectionSurface()
  try strip.checkTabPresentationAndCapture()
  try SwarmTabButton(id: "hover-fixture").checkHoverStyleAndTooltips()
  try strip.checkDaemon()
  try strip.checkDaemonOff()
  try strip.checkDaemonArtwork()
  try strip.checkBundledDaemonArtwork()
  try checkTitlebar(titlebarCheckApp.windows.isEmpty, "Checks never open an application window")
  if CommandLine.arguments.contains("--window-layout") {
    let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 700),
      styleMask: [.titled, .closable, .miniaturizable, .resizable], backing: .buffered, defer: false)
    window.isReleasedWhenClosed = false
    let content = TitlebarCheckContentController()
    content.view = TitlebarCheckContentView(frame: NSRect(x: 0, y: 0, width: 1280, height: 700))
    window.contentViewController = content
    let messenger = TitlebarCheckMessenger()
    let titlebar = SwarmTitlebar(window: window, messenger: messenger, installStatusItem: false)
    if let path = ProcessInfo.processInfo.environment["HARNESS_TITLEBAR_KEYMAP_FIXTURE"] {
      let fixture = try JSONSerialization.jsonObject(with: Data(contentsOf: URL(fileURLWithPath: path))) as! [String: [String: Any]]
      let before = titlebarCheckCount
      try titlebar.checkViewerShortcutRuntime(HarnessNativeKeymap(fixture["defaults"]!)!, messenger: messenger)
      print("Native Orchestrator viewer dispatch: \(titlebarCheckCount - before) checks passed with a focused WKWebView descendant; no window displayed.")
    }
    try titlebar.checkNativeContainer(messenger: messenger)
    if let path = ProcessInfo.processInfo.environment["HARNESS_TITLEBAR_KEYMAP_FIXTURE"] {
      let fixture = try JSONSerialization.jsonObject(with: Data(contentsOf: URL(fileURLWithPath: path))) as! [String: [String: Any]]
      try titlebar.checkKeymapRuntime(fixture, messenger: messenger)
    }
    window.close()
    print("AppKit Tab titlebar: \(titlebarCheckCount) checks passed, including native window layout; no windows displayed.")
  } else {
    print("AppKit Tab titlebar: \(titlebarCheckCount) checks passed; no windows opened.")
  }
} catch {
  let message = (error as? TitlebarCheckFailure)?.message ?? String(describing: error)
  FileHandle.standardError.write(Data("AppKit Tab titlebar failed: \(message)\n".utf8))
  exit(1)
}

private extension SwarmTabStrip {
  func checkInventoryFooter() throws {
    if let root = ProcessInfo.processInfo.environment["HARNESS_TITLEBAR_ASSETS"] {
      SwarmContextButton.statusIcons = SwarmHistoryIcons(assetURL: { asset in
        URL(fileURLWithPath: root).appendingPathComponent(String(asset.dropFirst("assets/".count)))
      })
    }
    func count(_ text: String) -> [String: Any] {
      ["text": text, "detail": text, "segments": [["text": text]], "interactive": true]
    }
    for dark in [true, false] {
      let ink = Int64(dark ? 0xfff5f5f5 : 0xff1f1f1d)
      let low = Int64(dark ? 0xffc4a46c : 0xff87632a)
      let critical = Int64(dark ? 0xffff7068 : 0xffb3261e)
      let fields: [[String: Any]] = [
        ("claude", "a", "68%", ink), ("claude", "b", "18%", low),
        ("codex", "c", "4%", critical), ("codex", "d", "82%", ink)
      ].map { engine, account, figure, color in
        ["text": figure, "iconEngine": engine,
         "iconAsset": engine == "codex" ? "assets/engine-icons/codex.png" : "",
         "field": account, "paneId": 0, "interactive": true,
         "iconColor": Int64(dark ? 0xffcc7c5e : 0xffc2633f),
         "detail": "Account: \(account)\n\(figure) remaining\nResets in 2h",
         "segments": [["text": figure, "foreground": color]]]
      }
      var state: [String: Any] = ["enabled": true,
        "palette": ["dark": dark ? 1 : 0, "workspace": Int64(dark ? 0xff282828 : 0xffe6e6e3),
          "tabBar": Int64(dark ? 0xff1c1c1c : 0xffeeeeec), "foreground": ink],
        "barStyle": ["size": 13, "foreground": ink],
        "tabs": [["id": "work", "name": "Work"]], "activeId": "work",
        "harnessMonitor": count("Harnesses 8"), "footerMachines": count("Machines 3"),
        "footerModels": count("Models 4"), "focusedContext": count("Mac Studio > harness > main"),
        "subscriptionUsage": ["text": "Subscriptions", "interactive": true,
          "paddedFields": true, "overflowFields": true, "fields": fields]]
      var calls: [(String, Any?)] = []
      emit = { calls.append(($0, $1)) }
      for width in [CGFloat(1280), CGFloat(640), CGFloat(360)] {
        setFrameSize(NSSize(width: width, height: 40))
        update(state)
        try checkTitlebar(!machinesButton.isHidden && !modelsButton.isHidden && !subscriptionUsageButton.isHidden,
          "Inventory and subscription controls remain available at \(width)")
        let controls = [harnessMonitorButton, machinesButton, modelsButton, subscriptionUsageButton, contextButton]
        for (left, right) in zip(controls, controls.dropFirst()) {
          try checkTitlebar(left.frame.maxX <= right.frame.minX,
            "Footer groups do not overlap at \(width)")
        }
        let visible = subscriptionUsageButton.fieldButtons.filter { !$0.isHidden }
        for account in visible {
          try checkTitlebar(account.frame.width == account.preferredWidth &&
            account.frame.maxX <= subscriptionUsageButton.bounds.width,
            "A visible account always has its full icon and percentage")
          try checkTitlebar(account.toolTip?.contains("Account:") == true,
            "Each subscription owns its account tooltip")
        }
        if width == 1280 {
          try checkTitlebar(visible.count == 4, "Multiple accounts each get their own provider icon")
          machinesButton.performClick(nil)
          modelsButton.performClick(nil)
          visible[1].performClick(nil)
          try checkTitlebar(calls.map { $0.0 } == ["machineControls", "runLocalModel", "subscriptions"],
            "Footer actions use the existing resource destinations")
          try checkTitlebar((calls.last?.1 as? [String: String])?["id"] == "b",
            "Subscription clicks preserve the selected account")
        } else if width == 360 {
          try checkTitlebar(visible.count < 4 && subscriptionUsageButton.subviews.contains { view in
            !view.isHidden && !subscriptionUsageButton.fieldButtons.contains(where: { $0 === view })
          }, "A narrow footer offers the remaining accounts through overflow")
        }
        if let capture = ProcessInfo.processInfo.environment["HARNESS_FOOTER_CAPTURE_DIR"] {
          let root = URL(fileURLWithPath: capture)
          try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
          try statusBar.renderedTree(background: palette.workspace).representation(using: .png, properties: [:])!
            .write(to: root.appendingPathComponent("native-footer-\(dark ? "dark" : "light")-\(Int(width)).png"))
        }
      }
      state["enabled"] = false
      update(state)
      let before = calls.count
      machinesButton.performClick(nil)
      modelsButton.performClick(nil)
      for account in subscriptionUsageButton.fieldButtons { account.performClick(nil) }
      try checkTitlebar(calls.count == before, "Covered footers cannot navigate or select accounts")
    }
  }

  func checkHarnessMonitor() throws {
    var state: [String: Any] = ["enabled": true,
      "harnessMonitor": ["text": "Harnesses 10", "segments": [["text": "Harnesses 10"]], "label": "Harness Monitor", "detail": "View running harnesses", "interactive": true],
      "machineResources": ["text": "CPU 20%   RAM 10 GB   GPU 10%   SSD 1 GB", "label": "Harness resources", "detail": "Resources used by active harnesses", "interactive": true,
        "segments": [["text": "CPU 20%   RAM 10 GB   GPU 10%   SSD 1 GB"]],
        "noStorageSegments": [["text": "CPU 20%   RAM 10 GB   GPU 10%"]],
        "compactSegments": [["text": "CPU 20%   RAM 10 GB"]],
        "minimalSegments": [["text": "CPU 20%"]]],
      "subscriptionUsage": ["text": "Claude 100%   Codex 90%", "segments": [["text": "Claude 100%   Codex 90%"]], "interactive": true],
      "focusedContext": ["text": "M2 > openharness > main", "segments": [["text": "M2 > openharness > main"]], "interactive": true],
      "tabs": [["id": "work", "name": "Work"]], "activeId": "work"]
    var calls: [String] = []
    emit = { method, _ in calls.append(method) }
    for width in [CGFloat(360), CGFloat(520), CGFloat(760), CGFloat(1280)] {
      setFrameSize(NSSize(width: width, height: 40))
      update(state)
      try checkTitlebar(!harnessMonitorButton.isHidden && harnessMonitorButton.isEnabled &&
        harnessMonitorButton.frame.minX > 0 && harnessMonitorButton.frame.width > 0 &&
        harnessMonitorButton.frame.maxX <= contextButton.frame.minX,
        "Resource monitor stays at bottom left without overlapping context at width \(width)")
      try checkTitlebar(!machineResourcesLabel.isHidden && machineResourcesLabel.isEnabled &&
        machineResourcesLabel.accessibilityRole() == .button &&
        machineResourcesLabel.frame.minX >= harnessMonitorButton.frame.maxX &&
        machineResourcesLabel.frame.maxX <= contextButton.frame.minX,
        "Harness resources never overlap count or focused context at width \(width): hidden=\(machineResourcesLabel.isHidden), enabled=\(machineResourcesLabel.isEnabled), role=\(String(describing: machineResourcesLabel.accessibilityRole())), resources=\(machineResourcesLabel.frame), count=\(harnessMonitorButton.frame), context=\(contextButton.frame)")
      try checkTitlebar(harnessMonitorButton.accessibilityLabel() == "Harness Monitor",
        "The resource counter names its action for VoiceOver")
      try checkTitlebar(subscriptionUsageButton.isHidden == (width < 1050),
        "Narrow footers retain the monitor while subscriptions remain accessible through Models")
      if let capture = ProcessInfo.processInfo.environment["HARNESS_RESOURCE_CAPTURE_DIR"] {
        let root = URL(fileURLWithPath: capture)
        try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
        try statusBar.renderedTree(background: NSColor(white: 0.12, alpha: 1))
          .representation(using: .png, properties: [:])!.write(to:
            root.appendingPathComponent("native-resources-\(Int(width)).png"))
      }
    }
    machineResourcesLabel.performClick(nil)
    harnessMonitorButton.performClick(nil)
    try checkTitlebar(calls == ["resourceMonitor", "resourceMonitor"], "Count and every resource open the same Harness Monitor tab")
    state["enabled"] = false
    update(state)
    harnessMonitorButton.performClick(nil)
    machineResourcesLabel.performClick(nil)
    try checkTitlebar(calls.count == 2, "A covered or modal footer cannot open the session monitor")
    state["enabled"] = true
    state.removeValue(forKey: "machineResources")
    state.removeValue(forKey: "subscriptionUsage")
    for width in [CGFloat(360), CGFloat(520), CGFloat(1280)] {
      setFrameSize(NSSize(width: width, height: 40))
      update(state)
      try checkTitlebar(!harnessMonitorButton.isHidden && harnessMonitorButton.isEnabled &&
        harnessMonitorButton.frame.maxX <= contextButton.frame.minX &&
        machineResourcesLabel.isHidden && subscriptionUsageButton.isHidden,
        "The count-only footer preserves context and hides omitted resource and usage groups")
      if let capture = ProcessInfo.processInfo.environment["HARNESS_RESOURCE_CAPTURE_DIR"] {
        try statusBar.renderedTree(background: NSColor(white: 0.12, alpha: 1))
          .representation(using: .png, properties: [:])!.write(to:
            URL(fileURLWithPath: capture).appendingPathComponent("native-count-\(Int(width)).png"))
      }
    }
    update([:])
    try checkTitlebar(harnessMonitorButton.isHidden && !harnessMonitorButton.isEnabled && machineResourcesLabel.isHidden && !machineResourcesLabel.isEnabled,
      "Clearing workspace state clears the counter, hardware and actions")
  }
}