Files
helder/Tests/HelderUITests/Support/StoreHarnesses.swift
T
2026-10-30 13:21:45 +01:00

86 lines
3.1 KiB
Swift

import Foundation
import HelderCore
import HelderServices
@testable import HelderUI
/// A simple stand-in for `GitDerivation.derive`: one row per change, no diffs.
enum TestDerivation {
static func derive(_ snapshot: GitSnapshot?) -> GitWorkingState {
guard let snapshot else { return GitWorkingState() }
let changes = snapshot.changes.map {
Change(path: $0.path, status: $0.status, added: $0.updated.isEmpty ? 0 : 1,
removed: $0.original.isEmpty ? 0 : 1, staged: $0.staged, originalPath: $0.originalPath)
}
return GitWorkingState(branch: snapshot.branch, ahead: snapshot.ahead, canPush: snapshot.canPush,
behind: snapshot.behind, upstream: snapshot.upstream, changes: changes, stagedPaths: Set(changes.filter(\.staged).map(\.path)),
isRepository: true)
}
}
/// Collects a store's toasts, plus the pasteboard and Finder calls it makes through `system`.
@MainActor
final class Recorder {
let system = FakeSystem()
var toasts: [(String, String?)] = []
var toastTitles: [String] { toasts.map(\.0) }
var copied: [String] { system.copied }
var revealed: [URL] { system.revealed }
func notify(_ title: String, _ detail: String?) {
toasts.append((title, detail))
}
}
/// A whole-file diff built by hand, for tests that only need "this file changed".
func makeDiff(original: String, updated: String, added: Int = 1, removed: Int = 0,
isAddedFile: Bool = false, isDeletedFile: Bool = false) -> Diff {
Diff(rows: [], left: [], right: [], split: [], added: added, removed: removed,
isDeletedFile: isDeletedFile, isAddedFile: isAddedFile, original: original, updated: updated)
}
/// The git state an EditorStore follows, mutable from a test.
@MainActor
final class GitStateBox {
var state = GitWorkingState.empty
func change(_ path: String, _ diff: Diff) {
state.fileDiffs[path] = diff
}
}
@MainActor
final class OverlayRecorder {
var presented: [Overlay] = []
var diffDismissals = 0
}
/// An EditorStore on fakes, with its overlay hooks recorded.
@MainActor
struct EditorHarness {
let files: FakeFiles
let git = GitStateBox()
let session = FakeSession()
let recent: FakeRecentFiles
let store: EditorStore
let overlays = OverlayRecorder()
init(files: [String: String] = [:], recent: [String] = []) {
self.files = FakeFiles(contents: files)
self.recent = FakeRecentFiles(recent)
let git = self.git
store = EditorStore(root: "/fake", files: self.files, gitState: { git.state },
session: session, recentFiles: self.recent)
let overlays = self.overlays
store.presentOverlay = { overlays.presented.append($0) }
store.dismissDiffOverlay = { overlays.diffDismissals += 1 }
}
}
extension EditorHarness {
/// Unsaved edits on a file that is not open; opening another file never leaves these behind.
func strand(_ path: String, disk: String, buffer: String) {
store.states[path] = FileState(buffer: buffer, disk: disk)
}
}