restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
import HelderServices
|
||||
|
||||
/// Records every git call; `load` returns queued snapshots first (each after its delay), then the current one.
|
||||
final class FakeGit: GitServicing, @unchecked Sendable {
|
||||
private struct State {
|
||||
var loads = 0
|
||||
var staged: [[String]] = []
|
||||
var unstaged: [[String]] = []
|
||||
var commits: [String] = []
|
||||
var discarded: [[String]] = []
|
||||
var pushes = 0
|
||||
var queued: [(GitSnapshot?, Duration)] = []
|
||||
var current: GitSnapshot?
|
||||
var failCommit = false
|
||||
var pushResult = PushResult(ok: true, message: "main -> main")
|
||||
}
|
||||
|
||||
private let state: Locked<State>
|
||||
|
||||
init(_ snapshot: GitSnapshot? = nil) {
|
||||
state = Locked(State(current: snapshot))
|
||||
}
|
||||
|
||||
var loads: Int { state.current.loads }
|
||||
var staged: [[String]] { state.current.staged }
|
||||
var unstaged: [[String]] { state.current.unstaged }
|
||||
var commits: [String] { state.current.commits }
|
||||
var discarded: [[String]] { state.current.discarded }
|
||||
var pushes: Int { state.current.pushes }
|
||||
|
||||
func set(_ snapshot: GitSnapshot?) { state.mutate { $0.current = snapshot } }
|
||||
func queue(_ snapshot: GitSnapshot?, after delay: Duration) { state.mutate { $0.queued.append((snapshot, delay)) } }
|
||||
func setFailCommit(_ value: Bool) { state.mutate { $0.failCommit = value } }
|
||||
func setPushResult(_ value: PushResult) { state.mutate { $0.pushResult = value } }
|
||||
|
||||
func load() async throws -> GitSnapshot? {
|
||||
let (snapshot, delay): (GitSnapshot?, Duration) = state.mutate { state in
|
||||
state.loads += 1
|
||||
return state.queued.isEmpty ? (state.current, .zero) : state.queued.removeFirst()
|
||||
}
|
||||
if delay > .zero { try? await Task.sleep(for: delay) }
|
||||
return snapshot
|
||||
}
|
||||
|
||||
func stage(_ paths: [String]) async throws { state.mutate { $0.staged.append(paths) } }
|
||||
func unstage(_ paths: [String]) async throws { state.mutate { $0.unstaged.append(paths) } }
|
||||
|
||||
func commit(message: String) async throws {
|
||||
try state.mutate { state in
|
||||
if state.failCommit { throw FakeError() }
|
||||
state.commits.append(message)
|
||||
}
|
||||
}
|
||||
|
||||
func push() async -> PushResult {
|
||||
state.mutate { state in
|
||||
state.pushes += 1
|
||||
return state.pushResult
|
||||
}
|
||||
}
|
||||
|
||||
func discard(_ paths: [String]) async throws { state.mutate { $0.discarded.append(paths) } }
|
||||
}
|
||||
|
||||
/// A snapshot with one change per row: (path, status, staged).
|
||||
func snapshot(_ rows: [(String, GitStatus, Bool)], branch: String = "main") -> GitSnapshot {
|
||||
GitSnapshot(branch: branch, ahead: 0, canPush: false, changes: rows.map {
|
||||
GitFileChange(path: $0.0, status: $0.1, staged: $0.2, original: "a", updated: "b")
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user