From 006f35c57cc5a5fc5a55ac330e8d0dc454cd227d Mon Sep 17 00:00:00 2001 From: Mark Dorison Date: Sun, 1 Mar 2026 17:26:26 -0500 Subject: [PATCH] macos: add unit tests for QuickTerminalSize Tests cover Size.toPixels() (percentage conversion, pixel passthrough, 0% and 100% boundaries) and calculate() for all five positions: left, right, top, bottom, and center (both landscape and portrait orientations). Verifies that primary and secondary axes map to the correct screen dimensions per position, and that nil sizes fall back to the documented defaults (400px primary, full screen dimension for secondary). Part of #7879. --- macos/Tests/QuickTerminalSizeTests.swift | 159 +++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 macos/Tests/QuickTerminalSizeTests.swift diff --git a/macos/Tests/QuickTerminalSizeTests.swift b/macos/Tests/QuickTerminalSizeTests.swift new file mode 100644 index 00000000000..66c78b9e8b1 --- /dev/null +++ b/macos/Tests/QuickTerminalSizeTests.swift @@ -0,0 +1,159 @@ +// +// QuickTerminalSizeTests.swift +// GhosttyTests +// +// Tests for QuickTerminalSize.calculate() and QuickTerminalSize.Size.toPixels(). +// + +import Testing +@testable import Ghostty + +struct QuickTerminalSizeTests { + // MARK: - Size.toPixels + + @Test func percentageConversionHorizontal() { + let size = QuickTerminalSize.Size.percentage(50) + #expect(size.toPixels(parentDimension: 1920) == 960) + } + + @Test func percentageConversionVertical() { + let size = QuickTerminalSize.Size.percentage(30) + #expect(size.toPixels(parentDimension: 1080) == 324) + } + + @Test func percentageBoundaryZero() { + let size = QuickTerminalSize.Size.percentage(0) + #expect(size.toPixels(parentDimension: 1000) == 0) + } + + @Test func percentageBoundaryOneHundred() { + let size = QuickTerminalSize.Size.percentage(100) + #expect(size.toPixels(parentDimension: 1000) == 1000) + } + + @Test func pixelPassthrough() { + let size = QuickTerminalSize.Size.pixels(400) + // parentDimension should have no effect for pixels + #expect(size.toPixels(parentDimension: 1920) == 400) + #expect(size.toPixels(parentDimension: 800) == 400) + } + + // MARK: - calculate: left / right + + @Test func leftWithPercentagePrimary() { + let qtSize = QuickTerminalSize(primary: .percentage(40), secondary: .percentage(80)) + let screen = CGSize(width: 1920, height: 1080) + let result = qtSize.calculate(position: .left, screenDimensions: screen) + // left: width = primary(40% of 1920) = 768, height = secondary(80% of 1080) = 864 + #expect(result.width == 768) + #expect(result.height == 864) + } + + @Test func rightWithPixelPrimary() { + let qtSize = QuickTerminalSize(primary: .pixels(500), secondary: .pixels(900)) + let screen = CGSize(width: 1920, height: 1080) + let result = qtSize.calculate(position: .right, screenDimensions: screen) + #expect(result.width == 500) + #expect(result.height == 900) + } + + @Test func leftDefaultsWhenNil() { + let qtSize = QuickTerminalSize() + let screen = CGSize(width: 1920, height: 1080) + let result = qtSize.calculate(position: .left, screenDimensions: screen) + // primary nil → width defaults to 400; secondary nil → height defaults to full screen height + #expect(result.width == 400) + #expect(result.height == 1080) + } + + @Test func rightDefaultsWhenNil() { + let qtSize = QuickTerminalSize() + let screen = CGSize(width: 2560, height: 1440) + let result = qtSize.calculate(position: .right, screenDimensions: screen) + #expect(result.width == 400) + #expect(result.height == 1440) + } + + // MARK: - calculate: top / bottom + + @Test func topWithPercentagePrimary() { + let qtSize = QuickTerminalSize(primary: .percentage(50), secondary: .percentage(100)) + let screen = CGSize(width: 1920, height: 1080) + let result = qtSize.calculate(position: .top, screenDimensions: screen) + // top: width = secondary(100% of 1920) = 1920, height = primary(50% of 1080) = 540 + #expect(result.width == 1920) + #expect(result.height == 540) + } + + @Test func bottomWithPixelPrimary() { + let qtSize = QuickTerminalSize(primary: .pixels(300), secondary: .pixels(1280)) + let screen = CGSize(width: 1920, height: 1080) + let result = qtSize.calculate(position: .bottom, screenDimensions: screen) + #expect(result.width == 1280) + #expect(result.height == 300) + } + + @Test func topDefaultsWhenNil() { + let qtSize = QuickTerminalSize() + let screen = CGSize(width: 1920, height: 1080) + let result = qtSize.calculate(position: .top, screenDimensions: screen) + // primary nil → height defaults to 400; secondary nil → width defaults to full screen width + #expect(result.width == 1920) + #expect(result.height == 400) + } + + @Test func bottomDefaultsWhenNil() { + let qtSize = QuickTerminalSize() + let screen = CGSize(width: 2560, height: 1440) + let result = qtSize.calculate(position: .bottom, screenDimensions: screen) + #expect(result.width == 2560) + #expect(result.height == 400) + } + + // MARK: - calculate: center (landscape) + + @Test func centerLandscapeWithPercentages() { + let qtSize = QuickTerminalSize(primary: .percentage(60), secondary: .percentage(40)) + let screen = CGSize(width: 1920, height: 1080) // landscape: width >= height + let result = qtSize.calculate(position: .center, screenDimensions: screen) + // landscape: width = primary(60% of 1920) = 1152, height = secondary(40% of 1080) = 432 + #expect(result.width == 1152) + #expect(result.height == 432) + } + + @Test func centerLandscapeDefaultsWhenNil() { + let qtSize = QuickTerminalSize() + let screen = CGSize(width: 1920, height: 1080) + let result = qtSize.calculate(position: .center, screenDimensions: screen) + #expect(result.width == 800) + #expect(result.height == 400) + } + + @Test func centerSquareCountsAsLandscape() { + // width == height: the code uses >=, so this is treated as landscape + let qtSize = QuickTerminalSize() + let screen = CGSize(width: 1000, height: 1000) + let result = qtSize.calculate(position: .center, screenDimensions: screen) + #expect(result.width == 800) + #expect(result.height == 400) + } + + // MARK: - calculate: center (portrait) + + @Test func centerPortraitWithPercentages() { + let qtSize = QuickTerminalSize(primary: .percentage(60), secondary: .percentage(40)) + let screen = CGSize(width: 800, height: 1280) // portrait: height > width + let result = qtSize.calculate(position: .center, screenDimensions: screen) + // portrait: width = secondary(40% of 800) = 320, height = primary(60% of 1280) = 768 + #expect(result.width == 320) + #expect(result.height == 768) + } + + @Test func centerPortraitDefaultsWhenNil() { + let qtSize = QuickTerminalSize() + let screen = CGSize(width: 800, height: 1280) + let result = qtSize.calculate(position: .center, screenDimensions: screen) + #expect(result.width == 400) + #expect(result.height == 800) + } +}