diff --git a/Sources/tart/Commands/Set.swift b/Sources/tart/Commands/Set.swift index e0016c8..6a8b737 100644 --- a/Sources/tart/Commands/Set.swift +++ b/Sources/tart/Commands/Set.swift @@ -95,9 +95,11 @@ extension VMDisplayConfig: ExpressibleByArgument { var ppi: Int? = nil // Optional "@PPI" pixels-per-inch hint, e.g. "3200x1800px@220". Parsed - // before the unit suffix since it always trails the whole spec. + // before the unit suffix since it always trails the whole spec. Only a + // positive value is kept; zero/negative (or non-numeric) is ignored so the + // display falls back to the 72 default rather than a broken configuration. if let atIndex = argument.lastIndex(of: "@") { - ppi = Int(argument[argument.index(after: atIndex)...]) + ppi = Int(argument[argument.index(after: atIndex)...]).flatMap { $0 > 0 ? $0 : nil } argument = String(argument[.. 0 { + return ppi + } + return 72 } } diff --git a/Tests/TartTests/VMConfigTests.swift b/Tests/TartTests/VMConfigTests.swift index f85740c..b590a84 100644 --- a/Tests/TartTests/VMConfigTests.swift +++ b/Tests/TartTests/VMConfigTests.swift @@ -57,4 +57,19 @@ final class VMConfigTests: XCTestCase { XCTAssertEqual(VMDisplayConfig(width: 3200, height: 1800, unit: .pixel, ppi: nil), config) XCTAssertEqual(72, config.effectivePixelsPerInch) } + + func testRejectsNonPositivePixelsPerInchWhenParsing() throws { + // A zero or negative PPI is meaningless and must not persist — it would + // otherwise reach VZMacGraphicsDisplayConfiguration and fail to build the + // display instead of falling back to the 72 default. + XCTAssertNil(VMDisplayConfig(argument: "3200x1800px@0").ppi) + XCTAssertNil(VMDisplayConfig(argument: "3200x1800px@-5").ppi) + } + + func testEffectivePixelsPerInchIgnoresNonPositiveStoredValue() throws { + // Defense for a hand-edited config.json: a stored value <= 0 falls back to + // 72 rather than being handed to VZMacGraphicsDisplayConfiguration. + XCTAssertEqual(72, VMDisplayConfig(width: 100, height: 100, unit: .pixel, ppi: 0).effectivePixelsPerInch) + XCTAssertEqual(72, VMDisplayConfig(width: 100, height: 100, unit: .pixel, ppi: -5).effectivePixelsPerInch) + } }