From 8fa42c6ec0b2be97148ec7e90c00e3cc58d5c589 Mon Sep 17 00:00:00 2001 From: Nolin McFarland Date: Sat, 16 May 2026 20:05:11 -0400 Subject: [PATCH] feat: add search state unit tests --- .../SurfaceView+SearchStateTests.swift | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 macos/Tests/Ghostty/Surface View/SurfaceView+SearchStateTests.swift diff --git a/macos/Tests/Ghostty/Surface View/SurfaceView+SearchStateTests.swift b/macos/Tests/Ghostty/Surface View/SurfaceView+SearchStateTests.swift new file mode 100644 index 000000000..7b3c1942e --- /dev/null +++ b/macos/Tests/Ghostty/Surface View/SurfaceView+SearchStateTests.swift @@ -0,0 +1,87 @@ +import AppKit +import GhosttyKit +import Testing +@testable import Ghostty + +@MainActor struct SurfaceView_SearchStateTests { + typealias SearchState = Ghostty.OSSurfaceView.SearchState + typealias StartSearch = Ghostty.Action.StartSearch + + /// A unique pasteboard for each test case prevents flakiness. + let pasteboard = OSPasteboard( + name: OSPasteboard.Name(rawValue: UUID().uuidString) + ) + + init() { + pasteboard.setString("pb", forType: .string) + } + + @Test func init_withNilNeedle_readsPasteboardNeedle() { + let sut = SearchState( + from: StartSearch(c: .init(needle: nil)), + pasteboard: pasteboard + ) + #expect(sut.needle == "pb") + } + + @Test func init_withEmptyNeedle_readsPasteboardNeedle() { + "".withCString { needle in + let sut = SearchState( + from: StartSearch(c: .init(needle: needle)), + pasteboard: pasteboard + ) + #expect(sut.needle == "pb") + } + } + + @Test func init_withNeedle_setsNeedle() { + "start".withCString { needle in + let sut = SearchState( + from: StartSearch(c: .init(needle: needle)), + pasteboard: pasteboard + ) + #expect(sut.needle == "start") + } + } + + @Test func init_withNeedle_writesPasteboard() { + "start".withCString { needle in + _ = SearchState( + from: StartSearch(c: .init(needle: needle)), + pasteboard: pasteboard + ) + #expect(pasteboard.string(forType: .string) == "start") + } + } + + @Test func writePasteboardNeedle_writesPasteboard() { + let sut = SearchState( + from: StartSearch(c: .init(needle: nil)), + pasteboard: pasteboard + ) + sut.needle = "sut" + sut.writePasteboardNeedle() + #expect(pasteboard.string(forType: .string) == "sut") + } + + @Test func readPasteboardNeedle_whenPasteboardNeedleIsNil() { + let sut = SearchState( + from: StartSearch(c: .init(needle: nil)), + pasteboard: pasteboard + ) + pasteboard.clearContents() + sut.needle = "sut" + sut.readPasteboardNeedle() + #expect(sut.needle == "sut") + } + + @Test func readPasteboardNeedle_whenPasteboardNeedleIsValid() { + let sut = SearchState( + from: StartSearch(c: .init(needle: nil)), + pasteboard: pasteboard + ) + sut.needle = "sut" + sut.readPasteboardNeedle() + #expect(sut.needle == "pb") + } +}