Merge pull request 'WP-3a: CaptureRegion geometry + Carbon HotkeyCenter' (#1) from wp3a/region-hotkey-20260831 into feat/shotdeck-20260830

This commit was merged in pull request #1.
This commit is contained in:
2026-08-31 11:01:09 +00:00
3 changed files with 344 additions and 0 deletions
@@ -0,0 +1,171 @@
import Carbon.HIToolbox
import Foundation
import Testing
@testable import ShotdeckCore
@Test("GEO-1 primary-only 1080-tall screen, AppKit rect (100,100,400,300)")
func geo1_primaryOnlyAppKitRectConvertsToExpectedCGRect() {
let region = CaptureRegion.fromAppKit(
rect: CGRect(x: 100, y: 100, width: 400, height: 300),
displayID: 1,
capturedScale: 2,
primaryHeight: 1080
)
#expect(region.rect == CGRect(x: 100, y: 680, width: 400, height: 300))
}
@Test("GEO-2 rect flush to AppKit bottom (y=0), h=300, primaryHeight=1080")
func geo2_flushToAppKitBottomYieldsCGY780() {
let region = CaptureRegion.fromAppKit(
rect: CGRect(x: 50, y: 0, width: 200, height: 300),
displayID: 1,
capturedScale: 1,
primaryHeight: 1080
)
#expect(region.rect.origin.y == 780)
}
@Test("GEO-3 rect flush to AppKit top (y+h=primaryHeight), primaryHeight=1080, h=300")
func geo3_flushToAppKitTopYieldsCGY0() {
let region = CaptureRegion.fromAppKit(
rect: CGRect(x: 50, y: 780, width: 200, height: 300),
displayID: 1,
capturedScale: 1,
primaryHeight: 1080
)
#expect(region.rect.origin.y == 0)
}
@Test("GEO-4 round-trip AppKit→CG via injectable overload, then invert recovers original")
func geo4_invertRecoverOriginalAppKitRect() {
let original = CGRect(x: 100, y: 100, width: 400, height: 300)
let primaryHeight: CGFloat = 1080
let region = CaptureRegion.fromAppKit(
rect: original,
displayID: 1,
capturedScale: 2,
primaryHeight: primaryHeight
)
let appKitY = primaryHeight - region.rect.origin.y - region.rect.height
let recovered = CGRect(
x: region.rect.origin.x,
y: appKitY,
width: region.rect.width,
height: region.rect.height
)
#expect(recovered == original)
}
@Test("GEO-5 isStillValid for displayID 999_999 is false and does not trap")
func geo5_unknownDisplayIsNotStillValid() {
let region = CaptureRegion(
displayID: 999_999,
rect: CGRect(x: 0, y: 0, width: 100, height: 100),
capturedScale: 1
)
#expect(region.isStillValid == false)
}
@Test("GEO-6 CaptureRegion JSONEncoder→JSONDecoder round-trip equals original")
func geo6_codableRoundTripEqualsOriginal() throws {
let original = CaptureRegion(
displayID: 42,
rect: CGRect(x: 10, y: 20, width: 300, height: 400),
capturedScale: 2
)
let data = try JSONEncoder().encode(original)
let decoded = try JSONDecoder().decode(CaptureRegion.self, from: data)
#expect(decoded == original)
}
@Test("GEO-7 secondary screen negative x: AppKit (1800,200,500,400) → CG (1800,480,500,400)")
func geo7_secondaryNegativeXLeavesXUnchanged() {
// Secondary AppKit frame (1920, 0, 1920, 1080); primaryHeight=1080.
let region = CaptureRegion.fromAppKit(
rect: CGRect(x: -1800, y: 200, width: 500, height: 400),
displayID: 2,
capturedScale: 1,
primaryHeight: 1080
)
#expect(region.rect == CGRect(x: -1800, y: 480, width: 500, height: 400))
}
@Test("GEO-8 secondary screen stacked above primary: AppKit (300,1300,600,350) → CG (300,570,600,350)")
func geo8_secondaryPositiveYProducesNegativeCGY() {
// Secondary AppKit frame (0, 1080, 1920, 1080); primaryHeight=1080.
let region = CaptureRegion.fromAppKit(
rect: CGRect(x: 300, y: 1300, width: 600, height: 350),
displayID: 2,
capturedScale: 1,
primaryHeight: 1080
)
#expect(region.rect == CGRect(x: 300, y: -570, width: 600, height: 350))
}
/// Carbon registrations are process-wide, so these cases must not run in parallel.
@Suite(.serialized)
@MainActor
struct HotkeyCenterCarbonTests {
/// kVK_ANSI_2. The app registers this combo once (WP-4a); tests only exercise the registrar.
private let captureKeyCode: UInt32 = 19
private let captureModifiers = UInt32(optionKey) | UInt32(shiftKey)
@Test("HK-1 register id a with Option-Shift-2 returns true")
func hk1_registerReturnsTrue() {
let center = HotkeyCenter()
defer { center.unregisterAll() }
let ok = center.register(id: "a", keyCode: captureKeyCode, modifiers: captureModifiers) {}
if !ok {
Issue.record("HK-1: RegisterEventHotKey returned non-noErr inside swift test (no NSApplication). Carbon registration did not function headlessly.")
return
}
#expect(ok)
}
@Test("HK-2 register same combo under a different id returns false")
func hk2_duplicateComboRejected() {
let center = HotkeyCenter()
defer { center.unregisterAll() }
let first = center.register(id: "a", keyCode: captureKeyCode, modifiers: captureModifiers) {}
if !first {
Issue.record("HK-2: first RegisterEventHotKey failed inside swift test (no NSApplication); cannot evaluate duplicate-combo rejection.")
return
}
let second = center.register(id: "b", keyCode: captureKeyCode, modifiers: captureModifiers) {}
#expect(second == false)
}
@Test("HK-3 unregister frees the combination so a later register succeeds")
func hk3_unregisterFreesCombination() {
let center = HotkeyCenter()
defer { center.unregisterAll() }
let first = center.register(id: "a", keyCode: captureKeyCode, modifiers: captureModifiers) {}
if !first {
Issue.record("HK-3: first RegisterEventHotKey failed inside swift test (no NSApplication); cannot evaluate unregister.")
return
}
center.unregister(id: "a")
let again = center.register(id: "c", keyCode: captureKeyCode, modifiers: captureModifiers) {}
#expect(again)
}
@Test("HK-4 unregisterAll frees combinations so a later register succeeds")
func hk4_unregisterAllFreesCombinations() {
let center = HotkeyCenter()
defer { center.unregisterAll() }
let first = center.register(id: "a", keyCode: captureKeyCode, modifiers: captureModifiers) {}
let other = center.register(
id: "other",
keyCode: UInt32(kVK_ANSI_3),
modifiers: captureModifiers
) {}
if !first {
Issue.record("HK-4: RegisterEventHotKey failed inside swift test (no NSApplication); cannot evaluate unregisterAll.")
return
}
_ = other
center.unregisterAll()
let again = center.register(id: "c", keyCode: captureKeyCode, modifiers: captureModifiers) {}
#expect(again)
}
}