// ScratchTextViewTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT #if os(macOS) import Testing import AppKit @testable import LyrebirdLiveCompileUI @Suite("ScratchTextView.rangeForUserCompletion") @MainActor struct ScratchTextViewTests { private func makeTextView(_ text: String, caret: Int) -> ScratchTextView { let tv: ScratchTextView = ScratchTextView(frame: .zero) tv.string = text tv.setSelectedRange(NSRange(location: caret, length: 0)) return tv } /// Right after a `.`, the completion range is EMPTY at the caret — so a /// member completion inserts after the dot instead of replacing the /// receiver expression (the `context.` → erases `context` bug). @Test func afterDotRangeIsEmptyAtCaret() { let tv: ScratchTextView = makeTextView("context.", caret: 8) let range: NSRange = tv.rangeForUserCompletion #expect(range == NSRange(location: 8, length: 0)) } /// A dotted member expression: completing after the second dot must not /// reach back over `context.engine`. @Test func afterNestedDotRangeIsEmptyAtCaret() { let text: String = "context.engine." let tv: ScratchTextView = makeTextView(text, caret: text.utf16.count) let range: NSRange = tv.rangeForUserCompletion #expect(range == NSRange(location: text.utf16.count, length: 0)) } /// Mid-identifier (no preceding dot) falls through to the default behavior, /// which covers the whole partial word so it gets replaced on completion. @Test func partialIdentifierUsesDefaultWordRange() { let tv: ScratchTextView = makeTextView("OscSi", caret: 5) let range: NSRange = tv.rangeForUserCompletion // Default range spans the partial word `OscSi` (length 5 from offset 0). #expect(range == NSRange(location: 0, length: 5)) } } #endif