restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,284 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
import HelderServices
|
||||
import HelderUI
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
@Suite struct SourceControlStoreTests {
|
||||
private let sample = snapshot([
|
||||
("src/b.php", .modified, false),
|
||||
("src/a.php", .added, true),
|
||||
("README.md", .modified, false),
|
||||
("src/a.php", .modified, false),
|
||||
("config/app.json", .modified, true),
|
||||
("src/c.php", .deleted, false),
|
||||
])
|
||||
|
||||
private func makeStore(_ snapshot: GitSnapshot? = nil) async -> (SourceControlStore, FakeGit, Recorder) {
|
||||
let git = FakeGit(snapshot ?? sample)
|
||||
let store = SourceControlStore(git: git, derive: TestDerivation.derive)
|
||||
let recorder = Recorder()
|
||||
store.notify = recorder.notify
|
||||
await store.refresh()
|
||||
return (store, git, recorder)
|
||||
}
|
||||
|
||||
// MARK: Loading
|
||||
|
||||
@Test func refreshDerivesTheRowsFromGit() async {
|
||||
let (store, _, _) = await makeStore()
|
||||
#expect(store.hasLoaded)
|
||||
#expect(store.state.isRepository)
|
||||
#expect(store.state.stagedCount == 2)
|
||||
#expect(store.state.unstagedCount == 4)
|
||||
}
|
||||
|
||||
@Test func aProjectWithoutGitReadsAsNotARepository() async {
|
||||
let git = FakeGit(nil)
|
||||
let store = SourceControlStore(git: git, derive: TestDerivation.derive)
|
||||
await store.refresh()
|
||||
#expect(store.hasLoaded)
|
||||
#expect(!store.state.isRepository)
|
||||
}
|
||||
|
||||
@Test func anOlderSlowerRefreshCannotOverwriteANewerOne() async {
|
||||
let git = FakeGit()
|
||||
let store = SourceControlStore(git: git, derive: TestDerivation.derive)
|
||||
git.queue(snapshot([("old.txt", .modified, false)]), after: .milliseconds(150))
|
||||
git.queue(snapshot([("new.txt", .modified, false)]), after: .zero)
|
||||
let slow = Task { await store.refresh() }
|
||||
#expect(await eventually { git.loads == 1 })
|
||||
await store.refresh()
|
||||
await slow.value
|
||||
#expect(store.state.changes.map(\.path) == ["new.txt"])
|
||||
}
|
||||
|
||||
@Test func theDerivationRunsOffTheMainThread() async {
|
||||
let onMain = Locked<[Bool]>([])
|
||||
let store = SourceControlStore(git: FakeGit(sample)) { snapshot in
|
||||
onMain.mutate { $0.append(Thread.isMainThread) }
|
||||
return TestDerivation.derive(snapshot)
|
||||
}
|
||||
await store.refresh()
|
||||
#expect(onMain.current == [false])
|
||||
#expect(store.state.changes.count == 6)
|
||||
}
|
||||
|
||||
@Test(.timeLimit(.minutes(1)))
|
||||
func aSlowDerivationOvertakenByANewerRefreshIsDropped() async {
|
||||
let git = FakeGit()
|
||||
git.queue(snapshot([("old.txt", .modified, false)]), after: .zero)
|
||||
git.queue(snapshot([("new.txt", .modified, false)]), after: .zero)
|
||||
let release = DispatchSemaphore(value: 0)
|
||||
let started = Locked(0)
|
||||
let store = SourceControlStore(git: git) { snapshot in
|
||||
if snapshot?.changes.first?.path == "old.txt" {
|
||||
started.mutate { $0 += 1 }
|
||||
release.wait()
|
||||
}
|
||||
return TestDerivation.derive(snapshot)
|
||||
}
|
||||
let slow = Task { await store.refresh() }
|
||||
#expect(await eventually { started.current == 1 })
|
||||
await store.refresh()
|
||||
release.signal()
|
||||
await slow.value
|
||||
#expect(store.state.changes.map(\.path) == ["new.txt"])
|
||||
}
|
||||
|
||||
@Test func loadIfNeededReadsGitOnlyOnce() async {
|
||||
let (store, git, _) = await makeStore()
|
||||
await store.loadIfNeeded()
|
||||
#expect(git.loads == 1)
|
||||
}
|
||||
|
||||
// MARK: Grouping and order
|
||||
|
||||
@Test func rowsWalkStagedFoldersFirstThenUnstagedFoldersInOrderOfFirstAppearance() async {
|
||||
let (store, _, _) = await makeStore()
|
||||
#expect(store.stagedGroups.map(\.dir) == ["src", "config"])
|
||||
#expect(store.unstagedGroups.map(\.dir) == ["src", ""])
|
||||
#expect(store.unstagedGroups[0].rows.map(\.path) == ["src/b.php", "src/a.php", "src/c.php"])
|
||||
#expect(store.rows.map(\.id) == [
|
||||
"s:src/a.php", "s:config/app.json",
|
||||
"w:src/b.php", "w:src/a.php", "w:src/c.php", "w:README.md",
|
||||
])
|
||||
}
|
||||
|
||||
@Test func aFileStagedAndEditedAgainHasTwoRows() async {
|
||||
let (store, _, _) = await makeStore()
|
||||
#expect(store.rows.filter { $0.path == "src/a.php" }.map(\.staged) == [true, false])
|
||||
}
|
||||
|
||||
@Test func theExplorerLetterComesFromTheUnstagedRow() async {
|
||||
let (store, _, _) = await makeStore()
|
||||
#expect(store.fileStatuses["src/a.php"] == .modified)
|
||||
#expect(store.fileStatuses["config/app.json"] == .modified)
|
||||
#expect(store.fileStatuses["src/c.php"] == .deleted)
|
||||
}
|
||||
|
||||
// MARK: Keyboard cursor
|
||||
|
||||
@Test func theCursorStartsAtTheTopClampsAtTheEndsAndFollowsRowIDs() async {
|
||||
let (store, _, _) = await makeStore()
|
||||
store.moveSelection(by: 1)
|
||||
#expect(store.selectedRowID == "s:src/a.php")
|
||||
store.moveSelection(by: -1)
|
||||
#expect(store.selectedRowID == "s:src/a.php")
|
||||
store.moveSelection(by: 100)
|
||||
#expect(store.selectedRowID == "w:README.md")
|
||||
#expect(store.selectedRow?.path == "README.md")
|
||||
}
|
||||
|
||||
@Test func aCursorWhoseRowVanishedStartsOverAtTheTop() async {
|
||||
let (store, git, _) = await makeStore()
|
||||
store.selectedRowID = "w:src/c.php"
|
||||
git.set(snapshot([("x.txt", .modified, true)]))
|
||||
await store.refresh()
|
||||
store.moveSelection(by: 1)
|
||||
#expect(store.selectedRowID == "s:x.txt")
|
||||
}
|
||||
|
||||
// MARK: Staging
|
||||
|
||||
@Test func stagingAndUnstagingSendTheRowPathAndReload() async {
|
||||
let (store, git, _) = await makeStore()
|
||||
await store.stage("src/b.php")
|
||||
await store.unstage("config/app.json")
|
||||
#expect(git.staged == [["src/b.php"]])
|
||||
#expect(git.unstaged == [["config/app.json"]])
|
||||
#expect(git.loads == 3)
|
||||
}
|
||||
|
||||
@Test func stageAllSendsEveryPathWithAnUnstagedRowOnce() async {
|
||||
let (store, git, _) = await makeStore()
|
||||
await store.stageAll()
|
||||
#expect(git.staged == [["src/b.php", "README.md", "src/a.php", "src/c.php"]])
|
||||
}
|
||||
|
||||
@Test func unstageAllSendsEveryStagedPath() async {
|
||||
let (store, git, _) = await makeStore()
|
||||
await store.unstageAll()
|
||||
#expect(git.unstaged == [["src/a.php", "config/app.json"]])
|
||||
}
|
||||
|
||||
@Test func stageAllWithNothingToStageCallsNoGit() async {
|
||||
let (store, git, _) = await makeStore(snapshot([("a.txt", .modified, true)]))
|
||||
await store.stageAll()
|
||||
#expect(git.staged.isEmpty)
|
||||
}
|
||||
|
||||
// MARK: Commit
|
||||
|
||||
@Test func commitNeedsAStagedFileAndAMessage() async {
|
||||
let (store, _, _) = await makeStore()
|
||||
#expect(!store.canCommit)
|
||||
store.setCommitMessage(" ")
|
||||
#expect(!store.canCommit)
|
||||
store.setCommitMessage("fix it")
|
||||
#expect(store.canCommit)
|
||||
|
||||
let (clean, git, _) = await makeStore(snapshot([("a.txt", .modified, false)]))
|
||||
clean.setCommitMessage("fix it")
|
||||
#expect(!clean.canCommit)
|
||||
#expect(await clean.commit() == 0)
|
||||
#expect(git.commits.isEmpty)
|
||||
}
|
||||
|
||||
@Test func commitSendsTheMessageClearsTheDraftAndCountsDistinctFiles() async {
|
||||
let (store, git, recorder) = await makeStore()
|
||||
store.setCommitMessage("add the payment service ")
|
||||
let count = await store.commit()
|
||||
#expect(count == 2)
|
||||
#expect(git.commits == ["add the payment service"])
|
||||
#expect(store.commitMessage.isEmpty)
|
||||
#expect(recorder.toastTitles == ["Committed 2 files"])
|
||||
}
|
||||
|
||||
@Test func aFailedCommitKeepsTheDraft() async {
|
||||
let (store, git, recorder) = await makeStore()
|
||||
git.setFailCommit(true)
|
||||
store.setCommitMessage("wip")
|
||||
#expect(await store.commit() == 0)
|
||||
#expect(store.commitMessage == "wip")
|
||||
#expect(recorder.toastTitles == ["Commit failed"])
|
||||
}
|
||||
|
||||
@Test func dictatedMessagesBecomeOneLowercaseLine() {
|
||||
let store = SourceControlStore(git: FakeGit(), derive: TestDerivation.derive)
|
||||
store.setCommitMessage("Fix The Bug\n And The Test")
|
||||
#expect(store.commitMessage == "fix the bug and the test")
|
||||
}
|
||||
|
||||
// MARK: Push
|
||||
|
||||
@Test func pushTellsTheOutcomeInAToast() async {
|
||||
let (store, git, recorder) = await makeStore()
|
||||
git.setPushResult(PushResult(ok: false, message: "rejected"))
|
||||
let result = await store.push()
|
||||
#expect(!result.ok)
|
||||
#expect(!store.isPushing)
|
||||
#expect(git.pushes == 1)
|
||||
#expect(recorder.toastTitles == ["Pushing…", "Push failed"])
|
||||
#expect(recorder.toasts.last?.1 == "rejected")
|
||||
}
|
||||
|
||||
// MARK: Discard and confirmations
|
||||
|
||||
@Test func discardAsksFirstWhileConfirmDiscardIsOn() async {
|
||||
let (store, _, _) = await makeStore()
|
||||
let request = store.confirmation(for: .discard("src/b.php"))
|
||||
#expect(request?.title == "Discard changes to src/b.php?")
|
||||
#expect(request?.confirmLabel == "Discard")
|
||||
#expect(request?.action == .discard("src/b.php"))
|
||||
|
||||
store.config.confirmDiscard = false
|
||||
#expect(store.confirmation(for: .discard("src/b.php")) == nil)
|
||||
}
|
||||
|
||||
@Test func stagingAsksOnlyWhenTheConfigSaysSo() async {
|
||||
let (store, _, _) = await makeStore()
|
||||
#expect(store.confirmation(for: .stage("a")) == nil)
|
||||
#expect(store.confirmation(for: .unstage("a")) == nil)
|
||||
store.config.confirmStage = true
|
||||
store.config.confirmUnstage = true
|
||||
#expect(store.confirmation(for: .stage("a"))?.title == "Stage a?")
|
||||
#expect(store.confirmation(for: .unstage("a"))?.title == "Unstage a?")
|
||||
#expect(store.confirmation(for: .copyReference("a")) == nil)
|
||||
}
|
||||
|
||||
@Test func performRunsGitActionsAndDeclinesTheRest() async {
|
||||
let (store, git, recorder) = await makeStore()
|
||||
#expect(await store.perform(.discard("src/b.php")))
|
||||
#expect(git.discarded == [["src/b.php"]])
|
||||
#expect(recorder.toasts.last?.0 == "Discarded changes")
|
||||
#expect(await store.perform(.stage("README.md")))
|
||||
#expect(git.staged == [["README.md"]])
|
||||
#expect(await !store.perform(.openDiff("README.md")))
|
||||
#expect(await !store.perform(.copyReference("README.md")))
|
||||
}
|
||||
|
||||
// MARK: Polling
|
||||
|
||||
@Test func pollingReReadsGitOnTheIntervalUntilStopped() async throws {
|
||||
let (store, git, _) = await makeStore()
|
||||
store.config.refreshInterval = 20
|
||||
store.startPolling()
|
||||
#expect(store.isPolling)
|
||||
#expect(await eventually { git.loads >= 3 })
|
||||
store.stopPolling()
|
||||
#expect(!store.isPolling)
|
||||
let loads = git.loads
|
||||
try await Task.sleep(for: .milliseconds(100))
|
||||
// One read may already have been in flight when polling stopped.
|
||||
#expect(git.loads <= loads + 1)
|
||||
}
|
||||
|
||||
@Test func aZeroIntervalDisablesPolling() async {
|
||||
let (store, _, _) = await makeStore()
|
||||
store.config.refreshInterval = 0
|
||||
store.startPolling()
|
||||
#expect(!store.isPolling)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user