|
| 1 | +// |
| 2 | +// TerminalClearToStartTests.swift |
| 3 | +// CodeEditTests |
| 4 | +// |
| 5 | +// Created for issue #891 — Clear To Start (⌘K) on the integrated terminal. |
| 6 | +// |
| 7 | + |
| 8 | +import AppKit |
| 9 | +import SwiftTerm |
| 10 | +import XCTest |
| 11 | +@testable import CodeEdit |
| 12 | + |
| 13 | +final class TerminalClearToStartTests: XCTestCase { |
| 14 | + |
| 15 | + // MARK: - Settings |
| 16 | + |
| 17 | + func testClearToStartSettingDefaultsToTrueWhenMissing() throws { |
| 18 | + let json = Data("{}".utf8) |
| 19 | + let settings = try JSONDecoder().decode(SettingsData.TerminalSettings.self, from: json) |
| 20 | + |
| 21 | + XCTAssertTrue( |
| 22 | + settings.clearToStartOnCommandK, |
| 23 | + "⌘K clear should be enabled by default to match Terminal.app / VS Code" |
| 24 | + ) |
| 25 | + } |
| 26 | + |
| 27 | + func testClearToStartSettingDecodesFalse() throws { |
| 28 | + let json = Data(#"{"clearToStartOnCommandK":false}"#.utf8) |
| 29 | + let settings = try JSONDecoder().decode(SettingsData.TerminalSettings.self, from: json) |
| 30 | + |
| 31 | + XCTAssertFalse(settings.clearToStartOnCommandK) |
| 32 | + } |
| 33 | + |
| 34 | + func testClearToStartSettingIsSearchable() { |
| 35 | + let settings = SettingsData.TerminalSettings() |
| 36 | + let keys = settings.searchKeys.map { $0.lowercased() } |
| 37 | + |
| 38 | + XCTAssertTrue( |
| 39 | + keys.contains(where: { $0.contains("clear to start") }), |
| 40 | + "Expected a searchable label for Clear to Start / ⌘K, got: \(settings.searchKeys)" |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + // MARK: - Clear behavior |
| 45 | + |
| 46 | + @MainActor |
| 47 | + func testClearToStartRemovesViewportAndScrollback() { |
| 48 | + let terminalView = CELocalShellTerminalView(frame: NSRect(x: 0, y: 0, width: 800, height: 600)) |
| 49 | + let terminal = terminalView.getTerminal() |
| 50 | + |
| 51 | + // Produce more lines than the viewport so scrollback is non-empty. |
| 52 | + for index in 0..<200 { |
| 53 | + terminalView.feed(text: "line-\(index)\n") |
| 54 | + } |
| 55 | + |
| 56 | + let before = terminal.getText( |
| 57 | + start: Position(col: 0, row: 0), |
| 58 | + end: Position(col: max(0, terminal.cols - 1), row: max(0, terminal.buffer.yDisp + terminal.rows - 1)) |
| 59 | + ) |
| 60 | + XCTAssertTrue(before.contains("line-"), "Precondition: terminal should contain fed output") |
| 61 | + |
| 62 | + terminalView.clearToStart() |
| 63 | + |
| 64 | + XCTAssertEqual(terminal.buffer.x, 0, "Cursor should be at column 0") |
| 65 | + XCTAssertEqual(terminal.buffer.y, 0, "Cursor should be at row 0") |
| 66 | + XCTAssertEqual(terminal.buffer.yDisp, 0, "Viewport should be scrolled to the top") |
| 67 | + |
| 68 | + let after = terminal.getText( |
| 69 | + start: Position(col: 0, row: 0), |
| 70 | + end: Position(col: max(0, terminal.cols - 1), row: max(0, terminal.rows - 1)) |
| 71 | + ) |
| 72 | + XCTAssertFalse( |
| 73 | + after.contains("line-"), |
| 74 | + "Cleared terminal should not still show previous output, got: \(after.prefix(200))" |
| 75 | + ) |
| 76 | + |
| 77 | + let visibleText = terminalView.accessibilityValue() as? String ?? "" |
| 78 | + XCTAssertFalse( |
| 79 | + visibleText.contains("line-"), |
| 80 | + "Accessibility value should also be cleared, got: \(visibleText.prefix(200))" |
| 81 | + ) |
| 82 | + } |
| 83 | +} |
0 commit comments