restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import AppKit
|
||||
import HelderCore
|
||||
@testable import HelderUI
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
@Suite struct DiffCanvasSelectionTests {
|
||||
private func canvas() -> DiffCanvasView {
|
||||
let view = DiffCanvasView(frame: NSRect(x: 0, y: 0, width: 800, height: 400))
|
||||
view.load(rows: [
|
||||
SplitRow(left: SideLine(no: 1, text: "old one"), right: SideLine(no: 1, text: "new one")),
|
||||
SplitRow(left: nil, right: SideLine(no: 2, text: "added", mark: .added)),
|
||||
SplitRow(left: SideLine(no: 2, text: "old two"), right: SideLine(no: 3, text: "new three")),
|
||||
], language: nil, knownSymbols: [], scale: 1)
|
||||
return view
|
||||
}
|
||||
|
||||
private func mouse(_ type: NSEvent.EventType, row: Int, right: Bool) -> NSEvent {
|
||||
let y = DiffOverlayLayout.topPadding + (CGFloat(row) + 0.5) * DiffOverlayLayout.rowHeight
|
||||
return NSEvent.mouseEvent(with: type, location: NSPoint(x: right ? 600 : 200, y: y), modifierFlags: [],
|
||||
timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 1, pressure: 1)!
|
||||
}
|
||||
|
||||
@Test func draggingSelectsWholeLinesOnOneSide() {
|
||||
let view = canvas()
|
||||
view.mouseDown(with: mouse(.leftMouseDown, row: 0, right: true))
|
||||
view.mouseDragged(with: mouse(.leftMouseDragged, row: 2, right: false))
|
||||
#expect(view.selectedText == "new one\nadded\nnew three")
|
||||
}
|
||||
|
||||
@Test func theOtherSidesBlankRowsAreSkipped() {
|
||||
let view = canvas()
|
||||
view.mouseDown(with: mouse(.leftMouseDown, row: 2, right: false))
|
||||
view.mouseDragged(with: mouse(.leftMouseDragged, row: 0, right: false))
|
||||
#expect(view.selectedText == "old one\nold two")
|
||||
}
|
||||
|
||||
@Test func aClickWithoutADragSelectsNothing() {
|
||||
let view = canvas()
|
||||
view.mouseDown(with: mouse(.leftMouseDown, row: 1, right: true))
|
||||
#expect(!view.hasSelection)
|
||||
}
|
||||
|
||||
@Test func selectAllTakesTheSelectedSideElseTheOriginal() {
|
||||
let view = canvas()
|
||||
view.selectAll(nil)
|
||||
#expect(view.selectedText == "old one\nold two")
|
||||
view.mouseDown(with: mouse(.leftMouseDown, row: 0, right: true))
|
||||
view.mouseDragged(with: mouse(.leftMouseDragged, row: 0, right: true))
|
||||
view.selectAll(nil)
|
||||
#expect(view.selectedText == "new one\nadded\nnew three")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
@testable import HelderUI
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
@Suite struct EditorBinaryTests {
|
||||
@Test func aFileWithBinaryContentCannotBeDisplayedWhateverItsName() async {
|
||||
let h = EditorHarness(files: ["data.txt": ""])
|
||||
h.files.setBinary("data.txt")
|
||||
h.store.open("data.txt")
|
||||
await h.store.settle()
|
||||
#expect(h.store.binaryPaths == ["data.txt"])
|
||||
#expect(h.store.presentation(for: "data.txt").kind == .binary)
|
||||
}
|
||||
|
||||
@Test func anEmptyTextFileIsStillText() async {
|
||||
let h = EditorHarness(files: ["empty.txt": ""])
|
||||
h.store.open("empty.txt")
|
||||
await h.store.settle()
|
||||
#expect(h.store.binaryPaths.isEmpty)
|
||||
#expect(h.store.presentation(for: "empty.txt").kind == .text)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct EditorFocusTests {
|
||||
@Test func closingTheDiffHandsTheKeyboardBackToTheText() async {
|
||||
let h = EditorHarness(files: ["a.php": "new"])
|
||||
h.git.change("a.php", makeDiff(original: "old", updated: "new"))
|
||||
h.store.open("a.php")
|
||||
h.store.setMode(.diff, for: "a.php")
|
||||
let before = h.store.textFocusRequest
|
||||
h.store.collapseDiff()
|
||||
#expect(h.store.textFocusRequest > before)
|
||||
#expect(h.overlays.diffDismissals == 1)
|
||||
}
|
||||
|
||||
@Test func movingFocusToTheEditorColumnAsksForTheTextView() async {
|
||||
let harness = WorkspaceHarness(contents: ["a.php": ""])
|
||||
await harness.start()
|
||||
let before = harness.model.editor?.textFocusRequest ?? 0
|
||||
harness.model.perform(.focus(.editor))
|
||||
#expect(harness.model.editor?.textFocusRequest == before + 1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
import CoreGraphics
|
||||
import Foundation
|
||||
import HelderCore
|
||||
@testable import HelderUI
|
||||
import Testing
|
||||
|
||||
@Suite struct EditorPresentationTests {
|
||||
@Test func anUnchangedFileShowsItsEditableCodeViewAndNoButtons() {
|
||||
let view = EditorPresentation(path: "a.php", requested: nil, diff: nil)
|
||||
#expect(view.mode == .code)
|
||||
#expect(view.barButtons.isEmpty)
|
||||
#expect(view.isEditable)
|
||||
}
|
||||
|
||||
@Test func aChangedFileOffersOnlyTheDiffButton() {
|
||||
let view = EditorPresentation(path: "a.php", requested: nil, diff: makeDiff(original: "a", updated: "b"))
|
||||
#expect(view.mode == .actual)
|
||||
#expect(view.barButtons == [.diff])
|
||||
#expect(view.opensDiffOnClick)
|
||||
}
|
||||
|
||||
@Test func unchangedMarkdownOffersOnlyPreview() {
|
||||
let view = EditorPresentation(path: "README.md", requested: nil, diff: nil)
|
||||
#expect(view.barButtons == [.preview])
|
||||
}
|
||||
|
||||
@Test func changedMarkdownOffersPreviewThenDiff() {
|
||||
let view = EditorPresentation(path: "README.md", requested: .preview, diff: makeDiff(original: "a", updated: "b"))
|
||||
#expect(view.barButtons == [.preview, .diff])
|
||||
#expect(view.mode == .preview)
|
||||
#expect(!view.isEditable)
|
||||
}
|
||||
|
||||
@Test func aFileOverTheDiffCapStillShowsItsLiveText() {
|
||||
var diff = makeDiff(original: "", updated: "", added: 0)
|
||||
diff.isTooBig = true
|
||||
let view = EditorPresentation(path: "dump.sql", requested: .actual, diff: diff)
|
||||
#expect(view.placeholder == nil)
|
||||
#expect(view.isEditable)
|
||||
}
|
||||
|
||||
@Test func aDiffRequestOnAnUnchangedFileFallsBackToCode() {
|
||||
let view = EditorPresentation(path: "a.php", requested: .diff, diff: nil)
|
||||
#expect(view.mode == .code)
|
||||
}
|
||||
|
||||
@Test func aCodeModeOnAChangedFileResolvesToActual() {
|
||||
let view = EditorPresentation(path: "a.php", requested: .code, diff: makeDiff(original: "a", updated: "b"))
|
||||
#expect(view.mode == .actual)
|
||||
}
|
||||
|
||||
@Test func theDiffOverlayLeavesActualInThePaneBehindIt() {
|
||||
let view = EditorPresentation(path: "a.php", requested: .diff, diff: makeDiff(original: "a", updated: "b"))
|
||||
#expect(view.mode == .diff)
|
||||
#expect(view.paneMode == .actual)
|
||||
}
|
||||
|
||||
@Test func anAddedFileHasNoClickToDiff() {
|
||||
let diff = makeDiff(original: "", updated: "b", isAddedFile: true)
|
||||
let actual = EditorPresentation(path: "a.php", requested: .actual, diff: diff)
|
||||
#expect(actual.placeholder == nil)
|
||||
#expect(!actual.opensDiffOnClick)
|
||||
#expect(actual.status == .added)
|
||||
}
|
||||
|
||||
@Test func aDeletedFileHasNoUpdatedVersion() {
|
||||
let diff = makeDiff(original: "a", updated: "", isDeletedFile: true)
|
||||
let view = EditorPresentation(path: "a.php", requested: .actual, diff: diff)
|
||||
#expect(view.placeholder == .noUpdated)
|
||||
#expect(!view.isEditable)
|
||||
}
|
||||
|
||||
@Test func aBinaryFileGetsAPlaceholderAndNoButtons() {
|
||||
let view = EditorPresentation(path: "font.woff2", requested: nil, diff: makeDiff(original: "", updated: ""))
|
||||
#expect(view.placeholder == .cannotDisplay)
|
||||
#expect(view.barButtons.isEmpty)
|
||||
#expect(!view.hasDiff)
|
||||
}
|
||||
|
||||
@Test func anImageIsNeitherTextNorDiffed() {
|
||||
let view = EditorPresentation(path: "logo.png", requested: nil, diff: makeDiff(original: "", updated: ""))
|
||||
#expect(view.kind == .image)
|
||||
#expect(view.barButtons.isEmpty)
|
||||
#expect(!view.isEditable)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct LineIndexTests {
|
||||
@Test func linesAreFoundByUTF16Offset() {
|
||||
let index = LineIndex("ab\ncd\n")
|
||||
#expect(index.lineCount == 3)
|
||||
#expect(index.line(containing: 0) == 1)
|
||||
#expect(index.line(containing: 2) == 1)
|
||||
#expect(index.line(containing: 3) == 2)
|
||||
#expect(index.line(containing: 6) == 3)
|
||||
#expect(index.column(of: 4) == 2)
|
||||
#expect(index.start(ofLine: 2) == 3)
|
||||
}
|
||||
|
||||
@Test func anEmptyTextIsOneEmptyLine() {
|
||||
let index = LineIndex("")
|
||||
#expect(index.lineCount == 1)
|
||||
#expect(index.line(containing: 0) == 1)
|
||||
}
|
||||
|
||||
@Test func wrappedRowsShowTheNumberOnlyOnTheirFirstRow() {
|
||||
// Line 1 folds into three rows (starting at 0, 10, 20); line 2 is one row at 31.
|
||||
let index = LineIndex(String(repeating: "x", count: 30) + "\nshort")
|
||||
#expect(GutterNumbers.labels(rowStarts: [0, 10, 20, 31], index: index) == [1, nil, nil, 2])
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct ChangeMarksTests {
|
||||
@Test func actualTintsAddedLinesAndRulesTheEdgeOfAPureDeletion() {
|
||||
let lines = [
|
||||
ViewLine(no: 1, text: "a"),
|
||||
ViewLine(no: 2, text: "b", style: .added),
|
||||
ViewLine(no: 3, text: "c", gap: .above),
|
||||
ViewLine(no: 4, text: "d", gap: .below),
|
||||
]
|
||||
let marks = ChangeMarks.actual(lines)
|
||||
#expect(marks[1] == nil)
|
||||
#expect(marks[2] == CodeLineMark(tint: true, tone: .added))
|
||||
#expect(marks[3] == CodeLineMark(tint: false, tone: nil, gap: .above))
|
||||
#expect(marks[4]?.gap == .below)
|
||||
}
|
||||
}
|
||||
|
||||
/// Marks and layout built from real Core diffs, end to end.
|
||||
@Suite struct EditorRealDiffTests {
|
||||
@Test func actualMarksComeFromTheRealDiff() {
|
||||
let diff = LineDiff.build(original: "a\nb\nc\n", updated: "a\nc\n")
|
||||
#expect(ChangeMarks.actual(DiffViews.actual(diff))[2]?.gap == .above)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct DiffOverlayLayoutTests {
|
||||
private func rows(_ count: Int) -> [SplitRow] {
|
||||
(1...count).map { SplitRow(left: SideLine(no: $0, text: ""), right: SideLine(no: $0, text: "")) }
|
||||
}
|
||||
|
||||
@Test func theClickedLineLandsInTheMiddleOfTheViewport() {
|
||||
let index = DiffOverlayLayout.rowIndex(forUpdatedLine: 100, in: rows(200))!
|
||||
let offset = DiffOverlayLayout.centredOffset(rowIndex: index, rowCount: 200, viewportHeight: 440)
|
||||
let rowMid = 6 + CGFloat(index) * 22 + 11
|
||||
#expect(rowMid - offset == 220)
|
||||
}
|
||||
|
||||
@Test func aLineNearTheTopGoesAsNearTheMiddleAsTheFilePermits() {
|
||||
#expect(DiffOverlayLayout.centredOffset(rowIndex: 1, rowCount: 200, viewportHeight: 440) == 0)
|
||||
}
|
||||
|
||||
@Test func aLineNearTheEndStopsAtTheBottom() {
|
||||
let offset = DiffOverlayLayout.centredOffset(rowIndex: 199, rowCount: 200, viewportHeight: 440)
|
||||
#expect(abs(offset - CGFloat(12 + 200 * 22 - 440)) < 0.001)
|
||||
}
|
||||
|
||||
@Test func fillerRowsOnTheUpdatedSideAreSkipped() {
|
||||
let split = [
|
||||
SplitRow(left: SideLine(no: 1, text: "a", mark: .removed), right: nil),
|
||||
SplitRow(left: nil, right: SideLine(no: 1, text: "b", mark: .added)),
|
||||
]
|
||||
#expect(DiffOverlayLayout.rowIndex(forUpdatedLine: 1, in: split) == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct IndentationTests {
|
||||
@Test func tabOnOneLineInsertsSpacesAtTheCaret() {
|
||||
let edit = Indentation.edit("ab", selection: NSRange(location: 1, length: 0), tabSize: 4, outdent: false)
|
||||
#expect(edit == TextEdit(range: NSRange(location: 1, length: 0), replacement: " ", selection: NSRange(location: 5, length: 0)))
|
||||
}
|
||||
|
||||
@Test func tabOverSeveralLinesIndentsEveryTouchedLine() {
|
||||
let edit = Indentation.edit("a\nb\nc", selection: NSRange(location: 0, length: 3), tabSize: 2, outdent: false)!
|
||||
#expect(edit.replacement == " a\n b")
|
||||
#expect(edit.range == NSRange(location: 0, length: 3))
|
||||
}
|
||||
|
||||
@Test func aSelectionEndingAtColumnZeroLeavesThatLineAlone() {
|
||||
let edit = Indentation.edit("a\nb\nc", selection: NSRange(location: 0, length: 4), tabSize: 2, outdent: false)!
|
||||
#expect(edit.replacement == " a\n b")
|
||||
}
|
||||
|
||||
@Test func shiftTabOutdentsTheCurrentLine() {
|
||||
let edit = Indentation.edit("x\n ab", selection: NSRange(location: 7, length: 0), tabSize: 4, outdent: true)!
|
||||
#expect(edit.replacement == "ab")
|
||||
#expect(edit.selection == NSRange(location: 3, length: 0))
|
||||
}
|
||||
|
||||
@Test func shiftTabOnAnUnindentedLineChangesNothing() {
|
||||
#expect(Indentation.edit("ab", selection: NSRange(location: 1, length: 0), tabSize: 4, outdent: true) == nil)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct EditorReferenceTests {
|
||||
@Test func aSelectionNamesItsFirstAndLastLine() {
|
||||
let index = LineIndex("a\nb\nc\nd\n")
|
||||
let lines = EditorReference.lines(of: NSRange(location: 2, length: 3), in: index)
|
||||
#expect(lines == 2...3)
|
||||
#expect(EditorReference.make(path: "x.php", selectedLines: lines, clickedLine: 9).description == "x.php:2-3")
|
||||
}
|
||||
|
||||
@Test func aSingleLineSelectionIsASingleLineReference() {
|
||||
#expect(EditorReference.make(path: "x.php", selectedLines: 4...4, clickedLine: 9).description == "x.php:4")
|
||||
}
|
||||
|
||||
@Test func withoutASelectionTheClickedLineIsTheReference() {
|
||||
#expect(EditorReference.make(path: "x.php", selectedLines: nil, clickedLine: 42).description == "x.php:42")
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct IncrementalLineIndexTests {
|
||||
private func edited(_ text: String, replacing range: NSRange, with insert: String) -> (LineIndex, String) {
|
||||
var index = LineIndex(text)
|
||||
let next = (text as NSString).replacingCharacters(in: range, with: insert)
|
||||
let insertedLength = (insert as NSString).length
|
||||
index.applyEdit(editedRange: NSRange(location: range.location, length: insertedLength),
|
||||
delta: insertedLength - range.length, inserted: insert)
|
||||
return (index, next)
|
||||
}
|
||||
|
||||
@Test func typingANewlineMatchesAFullRescan() {
|
||||
let (index, text) = edited("ab\ncd\nef", replacing: NSRange(location: 4, length: 0), with: "\n")
|
||||
#expect(index == LineIndex(text))
|
||||
}
|
||||
|
||||
@Test func deletingAcrossLinesMatchesAFullRescan() {
|
||||
let (index, text) = edited("ab\ncd\nef\ngh", replacing: NSRange(location: 1, length: 6), with: "")
|
||||
#expect(index == LineIndex(text))
|
||||
}
|
||||
|
||||
@Test func pastingSeveralLinesMatchesAFullRescan() {
|
||||
let (index, text) = edited("ab\ncd", replacing: NSRange(location: 3, length: 1), with: "x\ny\nz")
|
||||
#expect(index == LineIndex(text))
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct HighlightDiffTests {
|
||||
@Test func typingInsidePlainTextRepaintsOnlyTheEditedRange() {
|
||||
let old = [Token(location: 0, length: 3, role: .keyword), Token(location: 10, length: 2, role: .number)]
|
||||
let new = [Token(location: 0, length: 3, role: .keyword), Token(location: 11, length: 2, role: .number)]
|
||||
let range = HighlightDiff.repaintRange(old: old, new: new, edited: NSRange(location: 5, length: 1), delta: 1, newLength: 14)
|
||||
#expect(range == NSRange(location: 5, length: 1))
|
||||
}
|
||||
|
||||
@Test func growingAStringRepaintsTheWholeToken() {
|
||||
let old = [Token(location: 4, length: 4, role: .string)]
|
||||
let new = [Token(location: 4, length: 5, role: .string)]
|
||||
let range = HighlightDiff.repaintRange(old: old, new: new, edited: NSRange(location: 6, length: 1), delta: 1, newLength: 20)
|
||||
#expect(range == NSRange(location: 4, length: 5))
|
||||
}
|
||||
|
||||
@Test func openingABlockCommentRepaintsEverythingAfterIt() {
|
||||
let old = [Token(location: 10, length: 3, role: .keyword), Token(location: 30, length: 3, role: .keyword)]
|
||||
let new = [Token(location: 0, length: 40, role: .comment)]
|
||||
let range = HighlightDiff.repaintRange(old: old, new: new, edited: NSRange(location: 0, length: 2), delta: 2, newLength: 40)
|
||||
#expect(range == NSRange(location: 0, length: 40))
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct SymbolTargetsTests {
|
||||
private let lookup = SymbolLookup(
|
||||
name: "User",
|
||||
definitions: [SymbolDef(name: "User", path: "app/User.php", line: 7, kind: "class", namespace: "App")],
|
||||
references: [RefGroup(path: "a.php", hits: [ContentHit(line: 3, text: "", matchOffset: 0), ContentHit(line: 9, text: "", matchOffset: 0)])],
|
||||
referenceCount: 2)
|
||||
|
||||
@Test func theKeyboardWalksDeclarationsThenEveryReferenceLine() {
|
||||
#expect(SymbolTargets.flatten(lookup) == [
|
||||
SymbolTarget(path: "app/User.php", line: 7), SymbolTarget(path: "a.php", line: 3), SymbolTarget(path: "a.php", line: 9),
|
||||
])
|
||||
}
|
||||
|
||||
@Test func oneDeclarationIsNamedInFull() {
|
||||
#expect(SymbolTargets.title("User", lookup) == "App\\User")
|
||||
}
|
||||
|
||||
@Test func severalDeclarationsFallBackToTheShortName() {
|
||||
var two = lookup
|
||||
two.definitions.append(SymbolDef(name: "User", path: "b/User.php", line: 1, kind: "class", namespace: "B"))
|
||||
#expect(SymbolTargets.title("User", two) == "User")
|
||||
#expect(SymbolTargets.title("User", nil) == "User")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import AppKit
|
||||
import HelderCore
|
||||
@testable import HelderUI
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
@Suite struct EditorScrollOriginTests {
|
||||
private static let text = (1...60).map { line in
|
||||
line % 3 == 0
|
||||
? "\(line): " + String(repeating: "a long line that runs well past ninety characters ", count: 3)
|
||||
: "\(line): short line"
|
||||
}.joined(separator: "\n")
|
||||
|
||||
private func open(wrap: Bool) -> (CodeEditorController, NSWindow) {
|
||||
let controller = CodeEditorController()
|
||||
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 700, height: 900), styleMask: [.titled],
|
||||
backing: .buffered, defer: false)
|
||||
window.contentView = controller.scrollView
|
||||
controller.apply(CodeEditorConfiguration(documentID: "notes.md", path: "notes.md", text: Self.text, revision: 1,
|
||||
language: nil, isEditable: true, wrap: wrap, tabSize: 4, marksKey: "", makeMarks: { [:] },
|
||||
opensDiffOnClick: false, knownSymbols: [], symbolsVersion: 0, flash: nil, focusRequest: 0,
|
||||
selectAllRequest: 0, fontScale: 1, isShown: true))
|
||||
return (controller, window)
|
||||
}
|
||||
|
||||
@Test(arguments: [true, false])
|
||||
func aNewDocumentStartsRightOfTheGutter(wrap: Bool) {
|
||||
let (controller, window) = open(wrap: wrap)
|
||||
let clip = controller.scrollView.contentView
|
||||
#expect(clip.bounds.origin.x == -clip.contentInsets.left)
|
||||
withExtendedLifetime(window) {}
|
||||
}
|
||||
|
||||
@Test func wrappedTextFitsTheWidthRightOfTheGutter() {
|
||||
let (controller, window) = open(wrap: true)
|
||||
RunLoop.main.run(until: Date().addingTimeInterval(0.2))
|
||||
window.displayIfNeeded()
|
||||
let scrollView = controller.scrollView
|
||||
#expect(controller.textView.frame.width == scrollView.contentSize.width - scrollView.contentView.contentInsets.left)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
@testable import HelderUI
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
@Suite struct EditorStoreSafetyTests {
|
||||
private func autoSaving(_ files: [String: String]) -> EditorHarness {
|
||||
let h = EditorHarness(files: files)
|
||||
var config = HelderConfig.Editor()
|
||||
config.autoSave = true
|
||||
h.store.editorConfig = config
|
||||
return h
|
||||
}
|
||||
|
||||
@Test func autoSaveKeepsAPendingSaveWhenTheTabChanges() async {
|
||||
let h = autoSaving(["a.txt": "a", "b.txt": "b"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("a edited")
|
||||
h.store.open("b.txt")
|
||||
h.store.edit("b edited")
|
||||
await h.store.settle()
|
||||
#expect(h.files.current("a.txt") == "a edited")
|
||||
#expect(h.files.current("b.txt") == "b edited")
|
||||
}
|
||||
|
||||
@Test func pendingAutoSavesAreWrittenBeforeTheUnsavedPrompt() async {
|
||||
let h = autoSaving(["a.txt": "a"])
|
||||
var asked: [String] = []
|
||||
h.store.confirmUnsavedClose = { asked.append($0); return .cancel }
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("typed")
|
||||
#expect(await h.store.resolveUnsaved())
|
||||
#expect(h.files.current("a.txt") == "typed")
|
||||
#expect(asked.isEmpty)
|
||||
}
|
||||
|
||||
@Test func anOlderReadThatLandsLastIsDropped() async {
|
||||
let h = EditorHarness(files: ["a.txt": "old"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
let gate = h.files.holdNextRead("a.txt")
|
||||
let stale = Task { await h.store.filesChanged(nil) }
|
||||
#expect(await eventually { gate.isWaiting })
|
||||
h.files.set("a.txt", "new")
|
||||
await h.store.reloadFromDisk("a.txt")
|
||||
gate.open()
|
||||
await stale.value
|
||||
#expect(h.store.text(for: "a.txt") == "new")
|
||||
}
|
||||
|
||||
@Test func forgettingAFileDropsEveryPieceOfItsState() async {
|
||||
let h = EditorHarness(files: ["a.php": "x"])
|
||||
h.files.setImage("logo.png", Data([1]))
|
||||
h.git.change("a.php", makeDiff(original: "", updated: "x"))
|
||||
h.store.open("a.php", OpenOptions(side: .staged))
|
||||
h.store.open("logo.png")
|
||||
await h.store.settle()
|
||||
h.store.forget("a.php", isDirectory: false)
|
||||
h.store.forget("logo.png", isDirectory: false)
|
||||
#expect(h.store.states.isEmpty)
|
||||
#expect(h.store.modes.isEmpty && h.store.sides.isEmpty && h.store.images.isEmpty)
|
||||
}
|
||||
|
||||
@Test func renamingCarriesImagesAlong() async {
|
||||
let h = EditorHarness()
|
||||
h.files.setImage("img/logo.png", Data([1, 2]))
|
||||
h.store.open("img/logo.png")
|
||||
await h.store.settle()
|
||||
h.store.remap(from: "img", to: "assets", isDirectory: true)
|
||||
#expect(h.store.image(for: "assets/logo.png") == Data([1, 2]))
|
||||
#expect(h.store.image(for: "img/logo.png") == nil)
|
||||
}
|
||||
|
||||
@Test func aReopenedFileNeverReusesAnOldRevision() async {
|
||||
let h = EditorHarness(files: ["a.txt": "one"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
let first = h.store.revision(for: "a.txt")
|
||||
h.store.forget("a.txt", isDirectory: false)
|
||||
h.files.set("a.txt", "two")
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
#expect(h.store.revision(for: "a.txt") != first)
|
||||
#expect(h.store.text(for: "a.txt") == "two")
|
||||
}
|
||||
|
||||
@Test func aFileDeletedOnDiskKeepsItsTextAsUnsavedEdits() async {
|
||||
let h = EditorHarness(files: ["a.txt": "precious"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.files.remove("a.txt")
|
||||
await h.store.filesChanged(nil)
|
||||
#expect(h.store.isDeleted("a.txt"))
|
||||
#expect(h.store.isDirty("a.txt"))
|
||||
#expect(h.store.text(for: "a.txt") == "precious")
|
||||
#expect(h.files.writes.isEmpty)
|
||||
}
|
||||
|
||||
@Test func autoSaveNeverResurrectsADeletedFile() async {
|
||||
let h = autoSaving(["a.txt": "precious"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.files.remove("a.txt")
|
||||
await h.store.filesChanged(nil)
|
||||
h.store.edit("precious, edited")
|
||||
await h.store.settle()
|
||||
#expect(h.files.writes.isEmpty)
|
||||
}
|
||||
|
||||
@Test func savingADeletedFileWritesItBackOnPurpose() async throws {
|
||||
let h = EditorHarness(files: ["a.txt": "precious"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.files.remove("a.txt")
|
||||
await h.store.filesChanged(nil)
|
||||
try await h.store.save()
|
||||
#expect(h.files.current("a.txt") == "precious")
|
||||
#expect(!h.store.isDeleted("a.txt"))
|
||||
#expect(!h.store.isDirty("a.txt"))
|
||||
}
|
||||
|
||||
@Test func aFileThatComesBackIsCleanAgain() async {
|
||||
let h = EditorHarness(files: ["a.txt": "same"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.files.remove("a.txt")
|
||||
await h.store.filesChanged(nil)
|
||||
h.files.set("a.txt", "same")
|
||||
await h.store.filesChanged(nil)
|
||||
#expect(!h.store.isDeleted("a.txt"))
|
||||
#expect(!h.store.isDirty("a.txt"))
|
||||
}
|
||||
|
||||
@Test func theUnsavedPromptAsksForEveryDirtyFileTheOpenOneFirst() async {
|
||||
let h = EditorHarness(files: ["a.txt": "a", "b.txt": "b"])
|
||||
var asked: [String] = []
|
||||
h.store.confirmUnsavedClose = { path in
|
||||
asked.append(path)
|
||||
return path == "a.txt" ? .save : .discard
|
||||
}
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("a2")
|
||||
h.store.open("b.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("b2")
|
||||
#expect(await h.store.resolveUnsaved())
|
||||
#expect(h.store.activePath == "b.txt")
|
||||
#expect(asked == ["b.txt", "a.txt"])
|
||||
#expect(h.files.current("a.txt") == "a2")
|
||||
#expect(h.files.current("b.txt") == "b")
|
||||
#expect(h.store.dirtyPaths.isEmpty)
|
||||
}
|
||||
|
||||
@Test func cancellingThePromptKeepsEveryEdit() async {
|
||||
let h = EditorHarness(files: ["a.txt": "a", "b.txt": "b"])
|
||||
h.store.confirmUnsavedClose = { $0 == "a.txt" ? .discard : .cancel }
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("a2")
|
||||
h.store.open("b.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("b2")
|
||||
#expect(await h.store.resolveUnsaved() == false)
|
||||
#expect(h.store.text(for: "a.txt") == "a2")
|
||||
#expect(h.store.text(for: "b.txt") == "b2")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,533 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
@testable import HelderUI
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
@Suite struct EditorStoreOpenFileTests {
|
||||
@Test func openingAFileMakesItTheActiveFileAndReadsItFromDisk() async {
|
||||
let h = EditorHarness(files: ["a.php": "<?php\n"])
|
||||
h.store.open("a.php")
|
||||
await h.store.settle()
|
||||
#expect(h.store.activePath == "a.php")
|
||||
#expect(h.store.text(for: "a.php") == "<?php\n")
|
||||
#expect(h.store.history == ["a.php"])
|
||||
}
|
||||
|
||||
@Test func openingASecondFileReplacesTheActiveOne() async {
|
||||
let h = EditorHarness(files: ["a.php": "a", "b.php": "b"])
|
||||
h.store.open("a.php")
|
||||
h.store.open("b.php")
|
||||
await h.store.settle()
|
||||
#expect(h.store.activePath == "b.php")
|
||||
#expect(h.store.text(for: "b.php") == "b")
|
||||
#expect(h.store.history == ["b.php", "a.php"])
|
||||
}
|
||||
|
||||
@Test func switchingAwayKeepsADirtyBufferAndItIsStillDirtyOnReturn() async {
|
||||
let h = EditorHarness(files: ["a.php": "a", "b.php": "b"])
|
||||
var asked = false
|
||||
h.store.confirmUnsavedClose = { _ in asked = true; return .cancel }
|
||||
h.store.open("a.php")
|
||||
await h.store.settle()
|
||||
h.store.edit("my edit")
|
||||
h.store.open("b.php")
|
||||
await h.store.settle()
|
||||
#expect(!asked)
|
||||
#expect(h.store.dirtyPaths == ["a.php"])
|
||||
h.store.open("a.php")
|
||||
await h.store.settle()
|
||||
#expect(h.store.activePath == "a.php")
|
||||
#expect(h.store.text(for: "a.php") == "my edit")
|
||||
#expect(h.store.isDirty("a.php"))
|
||||
#expect(h.files.writes.isEmpty)
|
||||
}
|
||||
|
||||
@Test func theOpenFileComesFirstAmongTheDirtyPaths() async {
|
||||
let h = EditorHarness(files: ["a": "a", "b": "b", "c": "c"])
|
||||
for path in ["c", "a", "b"] {
|
||||
h.store.open(path)
|
||||
await h.store.settle()
|
||||
h.store.edit(path + "!")
|
||||
}
|
||||
#expect(h.store.dirtyPaths == ["b", "a", "c"])
|
||||
}
|
||||
|
||||
@Test func composerLockNeverOpensAndFlashesTooBig() {
|
||||
let h = EditorHarness(files: ["composer.lock": "{}"])
|
||||
h.store.open("composer.lock")
|
||||
#expect(h.store.activePath == nil)
|
||||
#expect(h.store.tooBig?.path == "composer.lock")
|
||||
}
|
||||
|
||||
@Test func closingTheFileLeavesNoFileOpen() async {
|
||||
let h = EditorHarness(files: ["a": "", "b": ""])
|
||||
h.store.open("a")
|
||||
h.store.open("b")
|
||||
h.store.close("b")
|
||||
#expect(h.store.activePath == nil)
|
||||
}
|
||||
|
||||
@Test func aLineJumpSetsTheCursorAndFlashesTheLine() async {
|
||||
let h = EditorHarness(files: ["a.php": "1\n2\n3\n"])
|
||||
h.store.open("a.php", OpenOptions(line: 3))
|
||||
#expect(h.store.cursor == Cursor(path: "a.php", line: 3, column: 1))
|
||||
#expect(h.store.flash?.line == 3)
|
||||
}
|
||||
|
||||
@Test func renamingAFolderCarriesTheFileAndItsBufferAlong() async {
|
||||
let h = EditorHarness(files: ["src/a.php": "x"])
|
||||
h.store.open("src/a.php")
|
||||
await h.store.settle()
|
||||
h.store.edit("y")
|
||||
h.store.remap(from: "src", to: "lib", isDirectory: true)
|
||||
#expect(h.store.activePath == "lib/a.php")
|
||||
#expect(h.store.text(for: "lib/a.php") == "y")
|
||||
}
|
||||
|
||||
@Test func deletingTheActiveFileShowsTheEmptyState() async {
|
||||
let h = EditorHarness(files: ["a": "", "b": ""])
|
||||
h.store.open("a")
|
||||
h.store.open("b")
|
||||
h.store.forget("b", isDirectory: false)
|
||||
#expect(h.store.activePath == nil)
|
||||
}
|
||||
|
||||
@Test func deletingAnotherFileLeavesTheOpenOneAlone() async {
|
||||
let h = EditorHarness(files: ["a": "", "b": ""])
|
||||
h.store.open("a")
|
||||
h.store.open("b")
|
||||
h.store.forget("a", isDirectory: false)
|
||||
#expect(h.store.activePath == "b")
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct EditorStoreDirtyTests {
|
||||
@Test func anEditMakesTheFileDirtyAndTypingBackCleansIt() async {
|
||||
let h = EditorHarness(files: ["a.txt": "one"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("two")
|
||||
#expect(h.store.isDirty("a.txt"))
|
||||
h.store.edit("one")
|
||||
#expect(!h.store.isDirty("a.txt"))
|
||||
#expect(h.store.buffers["a.txt"] == nil)
|
||||
}
|
||||
|
||||
@Test func saveWritesTheBufferThroughTheFileServiceAndCleansTheFile() async throws {
|
||||
let h = EditorHarness(files: ["a.txt": "one"])
|
||||
var saved: [String] = []
|
||||
h.store.fileSaved = { saved.append($0) }
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("two")
|
||||
try await h.store.save()
|
||||
#expect(h.files.writes.map(\.text) == ["two"])
|
||||
#expect(!h.store.isDirty("a.txt"))
|
||||
#expect(h.store.diskText["a.txt"] == "two")
|
||||
#expect(saved == ["a.txt"])
|
||||
}
|
||||
|
||||
@Test func saveWithoutChangesWritesNothing() async throws {
|
||||
let h = EditorHarness(files: ["a.txt": "one"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
try await h.store.save()
|
||||
#expect(h.files.writes.isEmpty)
|
||||
}
|
||||
|
||||
@Test func aFailedSaveKeepsTheBufferAndSaysSo() async {
|
||||
let h = EditorHarness(files: ["a.txt": "one"])
|
||||
var toasts: [String] = []
|
||||
h.store.showToast = { title, _ in toasts.append(title) }
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("two")
|
||||
h.files.failWrites = true
|
||||
await #expect(throws: (any Error).self) { try await h.store.save() }
|
||||
#expect(h.store.isDirty("a.txt"))
|
||||
#expect(toasts == ["Save failed"])
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct EditorStoreUnsavedCloseTests {
|
||||
private func dirtyHarness() async -> EditorHarness {
|
||||
let h = EditorHarness(files: ["a.txt": "one"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("two")
|
||||
return h
|
||||
}
|
||||
|
||||
@Test func closingADirtyFileAsksAndCancelKeepsItOpen() async {
|
||||
let h = await dirtyHarness()
|
||||
var asked: [String] = []
|
||||
h.store.confirmUnsavedClose = { asked.append($0); return .cancel }
|
||||
let closed = await h.store.requestCloseActive()
|
||||
#expect(!closed)
|
||||
#expect(asked == ["a.txt"])
|
||||
#expect(h.store.activePath == "a.txt")
|
||||
#expect(h.store.isDirty("a.txt"))
|
||||
}
|
||||
|
||||
@Test func saveWritesThenCloses() async {
|
||||
let h = await dirtyHarness()
|
||||
h.store.confirmUnsavedClose = { _ in .save }
|
||||
let closed = await h.store.requestCloseActive()
|
||||
#expect(closed)
|
||||
#expect(h.files.current("a.txt") == "two")
|
||||
#expect(h.store.activePath == nil)
|
||||
}
|
||||
|
||||
@Test func discardClosesWithoutWriting() async {
|
||||
let h = await dirtyHarness()
|
||||
h.store.confirmUnsavedClose = { _ in .discard }
|
||||
let closed = await h.store.requestCloseActive()
|
||||
#expect(closed)
|
||||
#expect(h.files.writes.isEmpty)
|
||||
#expect(h.store.buffers.isEmpty)
|
||||
#expect(h.store.activePath == nil)
|
||||
}
|
||||
|
||||
@Test func discardingThenReopeningShowsTheDiskTextToTheView() async {
|
||||
let h = await dirtyHarness()
|
||||
let revision = h.store.revision(for: "a.txt")
|
||||
h.store.confirmUnsavedClose = { _ in .discard }
|
||||
await h.store.requestCloseActive()
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
#expect(h.store.text(for: "a.txt") == "one")
|
||||
#expect(h.store.revision(for: "a.txt") != revision)
|
||||
}
|
||||
|
||||
@Test func withoutAPromptADirtyFileNeverClosesSilently() async {
|
||||
let h = await dirtyHarness()
|
||||
let closed = await h.store.requestCloseActive()
|
||||
#expect(!closed)
|
||||
}
|
||||
|
||||
@Test func aCleanFileClosesWithoutAsking() async {
|
||||
let h = EditorHarness(files: ["a.txt": "one"])
|
||||
var asked = false
|
||||
h.store.confirmUnsavedClose = { _ in asked = true; return .cancel }
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
#expect(await h.store.requestCloseActive())
|
||||
#expect(!asked)
|
||||
}
|
||||
|
||||
@Test func closingAsksOnlyAboutTheOpenFile() async {
|
||||
let h = EditorHarness(files: ["a.txt": "one", "b.txt": "b"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("two")
|
||||
h.store.open("b.txt")
|
||||
await h.store.settle()
|
||||
var asked = false
|
||||
h.store.confirmUnsavedClose = { _ in asked = true; return .cancel }
|
||||
#expect(await h.store.requestCloseActive())
|
||||
#expect(!asked)
|
||||
#expect(h.store.isDirty("a.txt"))
|
||||
}
|
||||
|
||||
@Test func resolvingUnsavedSeesABufferThatIsNoLongerOpen() async {
|
||||
let h = EditorHarness(files: ["a.txt": "one", "b.txt": "b"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("two")
|
||||
h.store.open("b.txt")
|
||||
await h.store.settle()
|
||||
var asked: [String] = []
|
||||
h.store.confirmUnsavedClose = { asked.append($0); return .save }
|
||||
#expect(await h.store.resolveUnsaved())
|
||||
#expect(asked == ["a.txt"])
|
||||
#expect(h.files.current("a.txt") == "two")
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct EditorStoreReloadTests {
|
||||
@Test func aWatcherChangeUpdatesACleanBuffer() async {
|
||||
let h = EditorHarness(files: ["a.txt": "one"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
let revision = h.store.revision(for: "a.txt")
|
||||
h.files.set("a.txt", "agent wrote this")
|
||||
await h.store.filesChanged(["a.txt"])
|
||||
#expect(h.store.text(for: "a.txt") == "agent wrote this")
|
||||
#expect(h.store.revision(for: "a.txt") != revision)
|
||||
}
|
||||
|
||||
@Test func aWatcherChangeNeverClobbersADirtyBuffer() async {
|
||||
let h = EditorHarness(files: ["a.txt": "one"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("my edit")
|
||||
h.files.set("a.txt", "agent wrote this")
|
||||
await h.store.filesChanged(nil)
|
||||
#expect(h.store.text(for: "a.txt") == "my edit")
|
||||
#expect(h.store.diskText["a.txt"] == "agent wrote this")
|
||||
#expect(h.store.isDirty("a.txt"))
|
||||
}
|
||||
|
||||
@Test func forcedReloadLetsDiskWinOverTheBuffer() async {
|
||||
let h = EditorHarness(files: ["a.txt": "one"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("my edit")
|
||||
h.files.set("a.txt", "agent wrote this")
|
||||
await h.store.reloadActiveFromDisk()
|
||||
#expect(h.store.text(for: "a.txt") == "agent wrote this")
|
||||
#expect(!h.store.isDirty("a.txt"))
|
||||
}
|
||||
|
||||
@Test func aDiskChangeThatMatchesTheBufferCleansIt() async {
|
||||
let h = EditorHarness(files: ["a.txt": "one"])
|
||||
h.store.open("a.txt")
|
||||
await h.store.settle()
|
||||
h.store.edit("same")
|
||||
h.files.set("a.txt", "same")
|
||||
await h.store.filesChanged(["a.txt"])
|
||||
#expect(!h.store.isDirty("a.txt"))
|
||||
#expect(h.store.buffers["a.txt"] == nil)
|
||||
}
|
||||
|
||||
@Test func anImageIsReadAsBytesNotText() async {
|
||||
let h = EditorHarness()
|
||||
h.files.setImage("logo.png", Data([1, 2, 3]))
|
||||
h.store.open("logo.png")
|
||||
await h.store.settle()
|
||||
#expect(h.store.image(for: "logo.png") == Data([1, 2, 3]))
|
||||
#expect(h.store.diskText["logo.png"] == nil)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct EditorStoreModeTests {
|
||||
@Test func aChangedFileLandsInActual() async {
|
||||
let h = EditorHarness(files: ["a.php": "new"])
|
||||
h.git.change("a.php", makeDiff(original: "old", updated: "new"))
|
||||
h.store.open("a.php")
|
||||
#expect(h.store.mode(for: "a.php") == .actual)
|
||||
#expect(h.overlays.presented.isEmpty)
|
||||
}
|
||||
|
||||
@Test func commandMDoesNothingOnAFileWithoutPreview() {
|
||||
let h = EditorHarness(files: ["a.php": "new", "b.php": "x"])
|
||||
h.git.change("a.php", makeDiff(original: "old", updated: "new"))
|
||||
h.store.open("b.php")
|
||||
h.store.togglePreview()
|
||||
#expect(h.store.mode(for: "b.php") == .code)
|
||||
h.store.open("a.php")
|
||||
h.store.togglePreview()
|
||||
#expect(h.store.mode(for: "a.php") == .actual)
|
||||
#expect(h.overlays.presented.isEmpty)
|
||||
}
|
||||
|
||||
@Test func aDiffDefaultStillLandsInActualWithoutPoppingTheOverlay() {
|
||||
let h = EditorHarness(files: ["a.php": "new"])
|
||||
h.git.change("a.php", makeDiff(original: "old", updated: "new"))
|
||||
h.store.defaultMode = .diff
|
||||
h.store.open("a.php")
|
||||
#expect(h.store.mode(for: "a.php") == .actual)
|
||||
#expect(h.overlays.presented.isEmpty)
|
||||
}
|
||||
|
||||
@Test func unchangedMarkdownTogglesBetweenCodeAndPreview() {
|
||||
let h = EditorHarness(files: ["README.md": "# x"])
|
||||
h.store.open("README.md")
|
||||
h.store.togglePreview()
|
||||
#expect(h.store.mode(for: "README.md") == .preview)
|
||||
h.store.togglePreview()
|
||||
#expect(h.store.mode(for: "README.md") == .code)
|
||||
}
|
||||
|
||||
@Test func changedMarkdownTogglesBetweenActualAndPreviewAndNeverTheDiff() {
|
||||
let h = EditorHarness(files: ["README.md": "# x"])
|
||||
h.git.change("README.md", makeDiff(original: "", updated: "# x"))
|
||||
h.store.open("README.md")
|
||||
h.store.togglePreview()
|
||||
#expect(h.store.mode(for: "README.md") == .preview)
|
||||
h.store.togglePreview()
|
||||
#expect(h.store.mode(for: "README.md") == .actual)
|
||||
#expect(h.overlays.presented.isEmpty)
|
||||
}
|
||||
|
||||
@Test func enteringDiffPresentsTheOverlayWithTheFilesGitSide() {
|
||||
let h = EditorHarness(files: ["a.php": "new"])
|
||||
h.git.change("a.php", makeDiff(original: "old", updated: "new"))
|
||||
h.store.open("a.php", OpenOptions(side: .staged))
|
||||
h.store.setMode(.diff, for: "a.php")
|
||||
#expect(h.overlays.presented == [.diff(DiffOverlayRequest(path: "a.php", side: .staged))])
|
||||
}
|
||||
|
||||
@Test func closingTheDiffReturnsToTheModeItCovered() {
|
||||
let h = EditorHarness(files: ["README.md": "new"])
|
||||
h.git.change("README.md", makeDiff(original: "old", updated: "new"))
|
||||
h.store.open("README.md")
|
||||
h.store.setMode(.preview, for: "README.md")
|
||||
h.store.setMode(.diff, for: "README.md")
|
||||
h.store.closeDiff()
|
||||
#expect(h.store.mode(for: "README.md") == .preview)
|
||||
}
|
||||
|
||||
@Test func togglingPreviewOverTheDiffClosesTheOverlay() {
|
||||
let h = EditorHarness(files: ["README.md": "new"])
|
||||
h.git.change("README.md", makeDiff(original: "old", updated: "new"))
|
||||
h.store.open("README.md")
|
||||
h.store.setMode(.diff, for: "README.md")
|
||||
h.store.togglePreview()
|
||||
#expect(h.overlays.diffDismissals == 1)
|
||||
#expect(h.store.mode(for: "README.md") == .preview)
|
||||
}
|
||||
|
||||
@Test func aClickOnAMarkedLineOpensTheDiffOnHeadVersusDiskAtThatLine() {
|
||||
let h = EditorHarness(files: ["a.php": "new"])
|
||||
h.git.change("a.php", makeDiff(original: "old", updated: "new"))
|
||||
h.store.open("a.php", OpenOptions(side: .unstaged))
|
||||
h.store.openDiff(atLine: 42)
|
||||
#expect(h.overlays.presented == [.diff(DiffOverlayRequest(path: "a.php", side: nil, focusLine: 42))])
|
||||
h.store.collapseDiff()
|
||||
#expect(h.store.mode(for: "a.php") == .actual)
|
||||
#expect(h.overlays.diffDismissals == 1)
|
||||
}
|
||||
|
||||
@Test func showDiffOpensStraightIntoTheOverlay() {
|
||||
let h = EditorHarness(files: ["a.php": "new"])
|
||||
h.git.change("a.php", makeDiff(original: "old", updated: "new"))
|
||||
h.store.open("a.php", OpenOptions(showDiff: true))
|
||||
#expect(h.store.mode(for: "a.php") == .diff)
|
||||
#expect(h.overlays.presented.count == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct EditorStoreSessionTests {
|
||||
@Test func sessionRoundTripsTheActiveFileItsModeAndSide() async {
|
||||
let h = EditorHarness(files: ["a.md": "a", "b.php": "b"])
|
||||
h.git.change("a.md", makeDiff(original: "", updated: "a"))
|
||||
await h.store.restore(restoreSession: true)
|
||||
h.store.open("b.php")
|
||||
h.store.open("a.md", OpenOptions(side: .unstaged))
|
||||
h.store.setMode(.preview, for: "a.md")
|
||||
await h.store.persist()
|
||||
#expect(h.session.sessions["/fake"] == SessionState(active: "a.md", modes: ["a.md": .preview], sides: ["a.md": .unstaged]))
|
||||
|
||||
let next = EditorStore(root: "/fake", files: h.files, gitState: { h.git.state },
|
||||
session: h.session, recentFiles: h.recent)
|
||||
await next.restore(restoreSession: true)
|
||||
#expect(next.activePath == "a.md")
|
||||
#expect(next.mode(for: "a.md") == .preview)
|
||||
#expect(next.sides["a.md"] == .unstaged)
|
||||
#expect(next.history == ["a.md", "b.php"])
|
||||
#expect(next.text(for: "a.md") == "a")
|
||||
}
|
||||
|
||||
@Test func aRestoredDiffModeComesBackAsActual() async {
|
||||
let h = EditorHarness(files: ["a.php": "a"])
|
||||
h.git.change("a.php", makeDiff(original: "", updated: "a"))
|
||||
h.session.sessions["/fake"] = SessionState(active: "a.php", modes: ["a.php": .diff])
|
||||
await h.store.restore(restoreSession: true)
|
||||
#expect(h.store.mode(for: "a.php") == .actual)
|
||||
#expect(h.overlays.presented.isEmpty)
|
||||
}
|
||||
|
||||
@Test func withRestoreOffOnlyTheRecentFilesComeBack() async {
|
||||
let h = EditorHarness(files: ["a.php": "a"], recent: ["x.php"])
|
||||
h.session.sessions["/fake"] = SessionState(active: "z.php")
|
||||
await h.store.restore(restoreSession: false)
|
||||
#expect(h.store.activePath == nil)
|
||||
#expect(h.store.history == ["x.php"])
|
||||
h.store.open("a.php")
|
||||
await h.store.settle()
|
||||
#expect(h.session.sessions["/fake"]?.active == "z.php")
|
||||
#expect(h.recent.paths == ["a.php", "x.php"])
|
||||
}
|
||||
|
||||
@Test func nothingIsWrittenBackBeforeTheSessionWasRead() async {
|
||||
let h = EditorHarness(files: ["a.php": "a"], recent: ["old.php"])
|
||||
h.store.open("a.php")
|
||||
await h.store.settle()
|
||||
#expect(h.recent.paths == ["old.php"])
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct EditorStorePassOnTests {
|
||||
@Test func theCaretLineIsTheReferenceWithoutASelection() async {
|
||||
let h = EditorHarness(files: ["src/a.php": "x"])
|
||||
h.store.open("src/a.php")
|
||||
h.store.updateCursor(line: 42, column: 3)
|
||||
#expect(h.store.currentReference()?.description == "src/a.php:42")
|
||||
}
|
||||
|
||||
@Test func aSelectionIsPassedAsARangeWithItsCode() async {
|
||||
let h = EditorHarness(files: ["src/a.php": "x"])
|
||||
h.store.open("src/a.php")
|
||||
h.store.updateSelection(lines: 42...58, code: "code")
|
||||
let request = h.store.passOnSelectionRequest(anchor: .zero)
|
||||
#expect(request == PassOnRequest(reference: "src/a.php:42-58", code: "code", anchor: .zero))
|
||||
#expect(PassOn.compose(note: "fix", reference: request!.reference, code: request!.code) == "fix src/a.php:42-58\n```\ncode\n```")
|
||||
}
|
||||
|
||||
@Test func withoutASelectionCommandRightPassesNothing() async {
|
||||
let h = EditorHarness(files: ["src/a.php": "x"])
|
||||
h.store.open("src/a.php")
|
||||
#expect(h.store.passOnSelectionRequest(anchor: .zero) == nil)
|
||||
}
|
||||
|
||||
@Test func theRightClickMenuIsPassOnThenCopyReference() {
|
||||
let h = EditorHarness(files: ["src/a.php": "x"])
|
||||
let menu = h.store.contextMenuRequest(reference: AgentReference(path: "src/a.php", startLine: 42), code: nil, anchor: .zero)
|
||||
#expect(menu.header == "src/a.php:42")
|
||||
#expect(menu.entries == [
|
||||
.action(.passOnReference("src/a.php:42", code: nil), label: "Pass on reference"),
|
||||
.action(.copyReference("src/a.php:42"), label: "Copy reference"),
|
||||
])
|
||||
}
|
||||
|
||||
@Test func anEmptyNotePassesTheBareReference() {
|
||||
#expect(PassOn.compose(note: " ", reference: "src/a.php:42") == "src/a.php:42")
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct EditorStoreSymbolTests {
|
||||
@Test func aSymbolLookupOpensThePopupAtOnceAndFillsItLater() async {
|
||||
let h = EditorHarness()
|
||||
let symbols = FakeSymbols()
|
||||
symbols.lookups["User"] = SymbolLookup(name: "User", definitions: [SymbolDef(name: "User", path: "app/User.php", line: 7, kind: "class")])
|
||||
h.store.symbols = symbols
|
||||
h.store.lookupSymbol("User")
|
||||
#expect(h.overlays.presented == [.symbol(name: "User")])
|
||||
#expect(h.store.symbolLookup == nil)
|
||||
await h.store.settle()
|
||||
#expect(h.store.symbolLookup?.definitions.first?.path == "app/User.php")
|
||||
}
|
||||
|
||||
@Test func knownSymbolsComeFromTheSymbolService() async {
|
||||
let h = EditorHarness()
|
||||
let symbols = FakeSymbols()
|
||||
symbols.declared = ["User", "Order"]
|
||||
h.store.symbols = symbols
|
||||
await h.store.loadKnownSymbols()
|
||||
#expect(h.store.knownSymbols == ["User", "Order"])
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct EditorStoreStatusTests {
|
||||
@Test func theStatusBarReadsPositionLanguageAndIndentation() async {
|
||||
let h = EditorHarness(files: ["a.php": "x"])
|
||||
h.store.editorConfig.tabSize = 2
|
||||
h.store.open("a.php")
|
||||
h.store.updateCursor(line: 3, column: 7)
|
||||
#expect(h.store.cursorLine == 3)
|
||||
#expect(h.store.cursorColumn == 7)
|
||||
#expect(h.store.activeLanguage == "PHP")
|
||||
#expect(h.store.indentationLabel == "Spaces: 2")
|
||||
#expect(h.store.activeMode == .code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user