adds claude-generated commit messages and a recent commits panel to the git view - adds log tailing in the explorer, branch merging and a clean up button
This commit is contained in:
@@ -22,9 +22,8 @@ import Testing
|
||||
#expect(ContextMenuOverlay.tint(for: .rename("a.php", isDirectory: false)) == .rename)
|
||||
}
|
||||
|
||||
@Test func theGitViewItemsKeepThePlainIcon() {
|
||||
@Test func theGitViewRowItemKeepsThePlainIcon() {
|
||||
#expect(ContextMenuOverlay.tint(for: .openFile("a.php")) == .plain)
|
||||
#expect(ContextMenuOverlay.tint(for: .commit(andPush: true)) == .plain)
|
||||
}
|
||||
|
||||
@Test func copyReferenceKeepsThePlainIconThatTurnsAmberOnHighlight() {
|
||||
@@ -34,6 +33,8 @@ import Testing
|
||||
|
||||
@Test func destructiveActionsAreAmberDeep() {
|
||||
#expect(ContextMenuOverlay.tint(for: .gitDiscard(["a.php"])) == .danger)
|
||||
#expect(ContextMenuOverlay.tint(for: .gitCleanUp) == .danger)
|
||||
#expect(ContextAction.gitCleanUp.isDestructive)
|
||||
#expect(ContextMenuOverlay.tint(for: .delete("a", isDirectory: true)) == .danger)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +70,24 @@ import Testing
|
||||
#expect(MenuCursor.step(3, by: 1, in: entries) == 0)
|
||||
#expect(MenuCursor.step(0, by: -1, in: entries) == 3)
|
||||
}
|
||||
|
||||
@Test func arrowsSkipDisabledEntries() {
|
||||
let entries: [ContextMenuEntry] = [
|
||||
.action(.copyReference("a"), label: "Copy reference"),
|
||||
.disabled(.mergeBranch("b"), label: "Merge into main", hint: "Commit or discard your changes first"),
|
||||
.action(.delete("a", isDirectory: false), label: "Delete file"),
|
||||
]
|
||||
#expect(MenuCursor.step(0, by: 1, in: entries) == 2)
|
||||
#expect(MenuCursor.step(2, by: -1, in: entries) == 0)
|
||||
}
|
||||
|
||||
@Test func aMenuWithOnlyADisabledEntryHighlightsNothing() {
|
||||
let entries: [ContextMenuEntry] = [
|
||||
.disabled(.mergeBranch("b"), label: "Merge into main", hint: "Commit or discard your changes first"),
|
||||
]
|
||||
#expect(MenuCursor.first(in: entries) == nil)
|
||||
#expect(MenuCursor.step(-1, by: 1, in: entries) == nil)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct NamePromptTextTests {
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import CoreGraphics
|
||||
@testable import HelderUI
|
||||
import Testing
|
||||
|
||||
@Suite struct GitStripLayoutTests {
|
||||
@Test func aNarrowStripSplitsEvenlyWithoutTheCommitList() {
|
||||
let layout = GitStripLayout(width: 1201)
|
||||
#expect(layout.console == 600)
|
||||
#expect(layout.branches == 600)
|
||||
#expect(layout.commits == nil)
|
||||
}
|
||||
|
||||
@Test func bothPanelsStopAt820AndASmallRestStaysEmpty() {
|
||||
let layout = GitStripLayout(width: 1800)
|
||||
#expect(layout.console == GitMetrics.consoleMaxWidth)
|
||||
#expect(layout.branches == GitMetrics.branchesMaxWidth)
|
||||
#expect(layout.commits == nil)
|
||||
}
|
||||
|
||||
@Test func theCommitListTakesTheRestFrom260() {
|
||||
let edge = GitMetrics.consoleMaxWidth + GitMetrics.branchesMaxWidth + 2 + GitMetrics.commitsMinWidth
|
||||
#expect(GitStripLayout(width: edge - 1).commits == nil)
|
||||
#expect(GitStripLayout(width: edge).commits == GitMetrics.commitsMinWidth)
|
||||
#expect(GitStripLayout(width: 2560).commits == CGFloat(2560 - 1640 - 2))
|
||||
}
|
||||
}
|
||||
@@ -274,6 +274,73 @@ import Testing
|
||||
#expect(texts(store) == ["$ git restore -- src/b.php src/c.php", "$ git clean -f -- new.txt", "Removing new.txt"])
|
||||
}
|
||||
|
||||
// MARK: Clean up
|
||||
|
||||
@Test func cleanUpRevertsTheWorkingTreeAndRemovesNewFiles() async {
|
||||
let (store, git, recorder) = await makeStore()
|
||||
#expect(store.cleanUpPathCount == 5)
|
||||
#expect(store.canCleanUp)
|
||||
await store.cleanUp()
|
||||
#expect(git.executed == [["restore", "--worktree", "--", "."], ["clean", "-fd"]])
|
||||
#expect(recorder.toastTitles == ["Cleaned up 5 files"])
|
||||
}
|
||||
|
||||
@Test func cleanUpRunsOnlyWhatTheTreeNeeds() async {
|
||||
let (onlyNew, newGit, _) = await makeStore(snapshot([("new.txt", .added, false), ("a.php", .modified, true)]))
|
||||
await onlyNew.cleanUp()
|
||||
#expect(newGit.executed == [["clean", "-fd"]])
|
||||
|
||||
let (onlyTracked, trackedGit, recorder) = await makeStore(snapshot([("a.php", .modified, false)]))
|
||||
await onlyTracked.cleanUp()
|
||||
#expect(trackedGit.executed == [["restore", "--worktree", "--", "."]])
|
||||
#expect(recorder.toastTitles == ["Cleaned up 1 file"])
|
||||
}
|
||||
|
||||
@Test func cleanUpDoesNothingWhenOnlyStagedChangesAreLeft() async {
|
||||
let (store, git, recorder) = await makeStore(snapshot([("a.php", .modified, true), ("b.php", .added, true)]))
|
||||
#expect(!store.canCleanUp)
|
||||
#expect(store.confirmation(for: .gitCleanUp) == nil)
|
||||
await store.cleanUp()
|
||||
#expect(git.executed.isEmpty)
|
||||
#expect(recorder.toastTitles.isEmpty)
|
||||
}
|
||||
|
||||
@Test func cleanUpIsRefusedDuringAConflict() async {
|
||||
let (store, git, _) = await makeStore(snapshot([("both.txt", .unmerged, false), ("src/b.php", .modified, false)]))
|
||||
#expect(!store.canCleanUp)
|
||||
await store.cleanUp()
|
||||
#expect(git.executed.isEmpty)
|
||||
}
|
||||
|
||||
@Test func cleanUpIsRefusedWhileAPushRuns() async {
|
||||
let (store, git, _) = await makeStore()
|
||||
git.setRun(FakeGitRun(delay: .milliseconds(300)), forCommand: "push")
|
||||
let push = Task { await store.push() }
|
||||
#expect(await eventually { store.busy == .push })
|
||||
#expect(!store.canCleanUp)
|
||||
await store.cleanUp()
|
||||
await push.value
|
||||
#expect(git.executed == [["push"]])
|
||||
}
|
||||
|
||||
@Test func aFailedCleanUpToastsGitsError() async {
|
||||
let (store, git, recorder) = await makeStore(snapshot([("new.txt", .added, false)]))
|
||||
git.setRun(FakeGitRun(exitCode: 1, lines: [("warning: failed to remove new.txt: Permission denied", true)]), forCommand: "clean")
|
||||
await store.cleanUp()
|
||||
#expect(recorder.toastTitles == ["Clean up failed"])
|
||||
#expect(recorder.toasts.first?.1 == "warning: failed to remove new.txt: Permission denied")
|
||||
}
|
||||
|
||||
@Test func cleanUpAlwaysAsksWithTheCount() async {
|
||||
let (store, _, _) = await makeStore()
|
||||
store.config.confirmDiscard = false
|
||||
let request = store.confirmation(for: .gitCleanUp)
|
||||
#expect(request?.title == "Clean up 5 files?")
|
||||
#expect(request?.body == "New files are removed from disk and unstaged changes are reverted. Staged changes stay. This cannot be undone.")
|
||||
#expect(request?.confirmLabel == "Clean up")
|
||||
#expect(request?.action == .gitCleanUp)
|
||||
}
|
||||
|
||||
// MARK: Commit
|
||||
|
||||
@Test func commitAlwaysRunsGitEvenWithNothingStagedOrNoMessage() async {
|
||||
@@ -350,6 +417,54 @@ import Testing
|
||||
#expect(failingGit.executed == [["commit", "-m", "ship it"]])
|
||||
}
|
||||
|
||||
@Test func stageAllAndCommitStagesEveryUnstagedPathThenCommits() async {
|
||||
let (store, git, recorder) = await makeStore()
|
||||
#expect(store.canStageAllAndCommit)
|
||||
store.setCommitMessage("ship it")
|
||||
await store.stageAllAndCommit()
|
||||
#expect(git.executed == [
|
||||
["add", "--", "src/b.php", "README.md", "src/a.php", "src/c.php", "new.txt"],
|
||||
["commit", "-m", "ship it"],
|
||||
])
|
||||
#expect(store.commitMessage.isEmpty)
|
||||
#expect(recorder.toastTitles.first?.hasPrefix("Committed") == true)
|
||||
}
|
||||
|
||||
@Test func stageAllAndCommitWithoutAMessageRunsNothingAndFocusesTheMessage() async {
|
||||
let (store, git, recorder) = await makeStore()
|
||||
var focused = 0
|
||||
store.focusMessage = { focused += 1 }
|
||||
store.setCommitMessage(" ")
|
||||
await store.stageAllAndCommit()
|
||||
#expect(git.executed.isEmpty)
|
||||
#expect(focused == 1)
|
||||
#expect(recorder.toastTitles.isEmpty)
|
||||
}
|
||||
|
||||
@Test func stageAllAndCommitIsRefusedDuringAConflict() async {
|
||||
let (store, git, _) = await makeStore(snapshot([("both.txt", .unmerged, false), ("src/b.php", .modified, false)]))
|
||||
#expect(!store.canStageAllAndCommit)
|
||||
store.setCommitMessage("ship it")
|
||||
await store.stageAllAndCommit()
|
||||
#expect(git.executed.isEmpty)
|
||||
}
|
||||
|
||||
@Test func stageAllAndCommitIsOffWithoutChanges() async {
|
||||
let (store, git, _) = await makeStore(snapshot([]))
|
||||
#expect(!store.canStageAllAndCommit)
|
||||
store.setCommitMessage("ship it")
|
||||
await store.stageAllAndCommit()
|
||||
#expect(git.executed.isEmpty)
|
||||
}
|
||||
|
||||
@Test func stageAllAndCommitDoesNotCommitWhenStagingFails() async {
|
||||
let (store, git, _) = await makeStore()
|
||||
git.setRun(FakeGitRun(exitCode: 128, lines: [("fatal: Unable to create '.git/index.lock': File exists.", true)]), forCommand: "add")
|
||||
store.setCommitMessage("ship it")
|
||||
await store.stageAllAndCommit()
|
||||
#expect(git.executed.map(\.first) == ["add"])
|
||||
}
|
||||
|
||||
// MARK: Push and pull
|
||||
|
||||
@Test func aPushWithoutUpstreamRetriesWithTheBranch() async {
|
||||
@@ -441,6 +556,60 @@ import Testing
|
||||
#expect(git.executed.isEmpty)
|
||||
}
|
||||
|
||||
@Test func mergingIsRefusedWhileDirtyWithoutAskingGit() async {
|
||||
let (store, git, recorder) = await makeStore()
|
||||
#expect(await !store.mergeBranch("feature"))
|
||||
#expect(git.executed.isEmpty)
|
||||
#expect(recorder.toastTitles == ["— Commit or discard 6 files before you merge."])
|
||||
}
|
||||
|
||||
@Test func aMergeRunsNoEditIntoTheCurrentBranchAndToasts() async {
|
||||
let (store, git, recorder) = await makeStore(snapshot([]))
|
||||
#expect(await store.mergeBranch("feature"))
|
||||
#expect(git.executed == [["merge", "--no-edit", "feature"]])
|
||||
#expect(texts(store).first == "$ git merge --no-edit feature")
|
||||
#expect(recorder.toastTitles == ["Merged feature into main"])
|
||||
}
|
||||
|
||||
@Test func theCurrentBranchIsNoMerge() async {
|
||||
let (store, git, _) = await makeStore(snapshot([]))
|
||||
#expect(await !store.mergeBranch("main"))
|
||||
#expect(git.executed.isEmpty)
|
||||
}
|
||||
|
||||
@Test func aMergeOnACleanTreeAsksFirst() async {
|
||||
let (store, git, _) = await makeStore(snapshot([]))
|
||||
let request = store.confirmation(for: .mergeBranch("feature"))
|
||||
#expect(request?.title == "Merge feature into main?")
|
||||
#expect(request?.body == "Git merges the commits of feature into main. A conflict stops the merge for you to resolve.")
|
||||
#expect(request?.confirmLabel == "Merge")
|
||||
#expect(request?.action == .mergeBranch("feature"))
|
||||
#expect(git.executed.isEmpty)
|
||||
}
|
||||
|
||||
@Test func aMergeThatWouldBeRefusedAsksNothing() async {
|
||||
let (dirty, _, _) = await makeStore()
|
||||
#expect(dirty.confirmation(for: .mergeBranch("feature")) == nil)
|
||||
let (clean, _, _) = await makeStore(snapshot([]))
|
||||
#expect(clean.confirmation(for: .mergeBranch("main")) == nil)
|
||||
}
|
||||
|
||||
@Test func aMergeThatLeavesConflictsSaysSo() async {
|
||||
let (store, git, recorder) = await makeStore(snapshot([]))
|
||||
git.setRun(FakeGitRun(exitCode: 1, lines: [("CONFLICT (content): Merge conflict in a.txt", false)]), forCommand: "merge")
|
||||
git.set(snapshot([("a.txt", .unmerged, false)]))
|
||||
#expect(await !store.mergeBranch("feature"))
|
||||
#expect(recorder.toastTitles == ["Merge conflict — resolve, then commit"])
|
||||
}
|
||||
|
||||
@Test func aFailedMergeWithoutConflictsShowsGitsError() async {
|
||||
let (store, git, recorder) = await makeStore(snapshot([]))
|
||||
git.setRun(FakeGitRun(exitCode: 1, lines: [("merge: nope - not something we can merge", true)]), forCommand: "merge")
|
||||
#expect(await !store.mergeBranch("nope"))
|
||||
#expect(recorder.toasts.last?.0 == "Merge failed")
|
||||
#expect(recorder.toasts.last?.1 == "merge: nope - not something we can merge")
|
||||
}
|
||||
|
||||
// MARK: Console
|
||||
|
||||
@Test func clearEmptiesTheLogWithoutGit() async {
|
||||
@@ -505,6 +674,23 @@ import Testing
|
||||
#expect(texts(store).first == "$ git fetch origin")
|
||||
}
|
||||
|
||||
@Test func openingTheViewAndEveryCommandReadTheRecentCommits() async {
|
||||
let (store, git, _) = await makeStore()
|
||||
let first = [GitCommitSummary(hash: "a1b2c3d", subject: "first", author: "Jonathan",
|
||||
date: Date(timeIntervalSince1970: 1_700_000_000))]
|
||||
git.setRecentCommits(first)
|
||||
#expect(store.recentCommits.isEmpty)
|
||||
await store.viewOpened()
|
||||
#expect(store.recentCommits == first)
|
||||
|
||||
let second = [GitCommitSummary(hash: "b2c3d4e", subject: "second", author: "Jonathan",
|
||||
date: Date(timeIntervalSince1970: 1_700_000_100))] + first
|
||||
git.setRecentCommits(second)
|
||||
await store.stage(["README.md"])
|
||||
#expect(store.recentCommits == second)
|
||||
#expect(git.recentCommitsReads == 2)
|
||||
}
|
||||
|
||||
@Test func anUpstreamOnOriginIsEnoughToFetch() async {
|
||||
let tracked = GitSnapshot(branch: "main", ahead: 1, canPush: true, upstream: "origin/main", changes: [])
|
||||
let (store, git, _) = await makeStore(tracked)
|
||||
@@ -536,3 +722,81 @@ import Testing
|
||||
#expect(!store.isPolling)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct SourceControlGenerateMessageTests {
|
||||
private func makeStore(_ rows: [(String, GitStatus, Bool)] = [("a.txt", .modified, true)])
|
||||
async -> (SourceControlStore, FakeGit, FakeCommitMessages, Recorder, () -> Int) {
|
||||
let git = FakeGit(snapshot(rows))
|
||||
let generator = FakeCommitMessages()
|
||||
let store = SourceControlStore(git: git, commitMessages: generator, derive: TestDerivation.derive)
|
||||
let recorder = Recorder()
|
||||
store.notify = recorder.notify
|
||||
var focused = 0
|
||||
store.focusMessage = { focused += 1 }
|
||||
await store.refresh()
|
||||
return (store, git, generator, recorder, { focused })
|
||||
}
|
||||
|
||||
@Test func successReplacesTheMessageAndFocusesTheField() async {
|
||||
let (store, git, generator, recorder, focused) = await makeStore()
|
||||
git.setStagedDiff(StagedDiff(stat: " a.txt | 1 +\n", patch: "+the change\n"))
|
||||
generator.setResult(.success("Adds the change"))
|
||||
store.setCommitMessage("old draft")
|
||||
await store.generateCommitMessage()
|
||||
#expect(store.commitMessage == "adds the change")
|
||||
#expect(focused() == 1)
|
||||
#expect(recorder.toasts.isEmpty)
|
||||
#expect(!store.isGeneratingMessage)
|
||||
let prompt = generator.prompts.first ?? ""
|
||||
#expect(prompt.contains(" a.txt | 1 +"))
|
||||
#expect(prompt.contains("+the change"))
|
||||
}
|
||||
|
||||
@Test func failureToastsWithTheDetailAndKeepsTheDraft() async {
|
||||
let (store, _, generator, recorder, focused) = await makeStore()
|
||||
generator.setResult(.failure(.notFound))
|
||||
store.setCommitMessage("my draft")
|
||||
await store.generateCommitMessage()
|
||||
#expect(store.commitMessage == "my draft")
|
||||
#expect(recorder.toasts.map(\.0) == ["Couldn't generate a commit message"])
|
||||
#expect(recorder.toasts.first?.1 == "claude not found")
|
||||
#expect(focused() == 0)
|
||||
#expect(!store.isGeneratingMessage)
|
||||
}
|
||||
|
||||
@Test func nothingStagedDoesNothing() async {
|
||||
let (store, _, generator, recorder, _) = await makeStore([("a.txt", .modified, false)])
|
||||
#expect(!store.canGenerateMessage)
|
||||
await store.generateCommitMessage()
|
||||
#expect(generator.prompts.isEmpty)
|
||||
#expect(recorder.toasts.isEmpty)
|
||||
#expect(store.commitMessage == "")
|
||||
}
|
||||
|
||||
@Test func anEmptyStagedDiffToastsWithoutAskingClaude() async {
|
||||
let (store, git, generator, recorder, _) = await makeStore()
|
||||
git.setStagedDiff(StagedDiff(stat: "", patch: ""))
|
||||
await store.generateCommitMessage()
|
||||
#expect(generator.prompts.isEmpty)
|
||||
#expect(recorder.toasts.map(\.0) == ["Couldn't generate a commit message"])
|
||||
}
|
||||
|
||||
@Test func aClickWhileGeneratingIsIgnored() async {
|
||||
let (store, _, generator, _, _) = await makeStore()
|
||||
generator.setDelay(.milliseconds(200))
|
||||
let first = Task { await store.generateCommitMessage() }
|
||||
#expect(await eventually { store.isGeneratingMessage })
|
||||
#expect(!store.canGenerateMessage)
|
||||
await store.generateCommitMessage()
|
||||
await first.value
|
||||
#expect(generator.prompts.count == 1)
|
||||
#expect(!store.isGeneratingMessage)
|
||||
}
|
||||
|
||||
@Test func withoutAGeneratorTheButtonStaysOff() async {
|
||||
let store = SourceControlStore(git: FakeGit(snapshot([("a.txt", .modified, true)])), derive: TestDerivation.derive)
|
||||
await store.refresh()
|
||||
#expect(!store.canGenerateMessage)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,6 +294,39 @@ import Testing
|
||||
#expect(harness.model.editor?.text(for: "a.php") == "committed")
|
||||
}
|
||||
|
||||
@Test func cleanUpAsksFirstThenRunsGitAndReloadsTheFile() async {
|
||||
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, false), ("new.txt", .added, false)]),
|
||||
contents: ["a.php": "edited"])
|
||||
await harness.start()
|
||||
harness.model.open("a.php")
|
||||
await harness.model.settle()
|
||||
await harness.perform(.context(.gitCleanUp))
|
||||
guard case let .confirm(request) = harness.model.overlay.current else {
|
||||
Issue.record("expected a confirm overlay")
|
||||
return
|
||||
}
|
||||
#expect(request.title == "Clean up 2 files?")
|
||||
#expect(harness.git.executed.isEmpty)
|
||||
harness.files.set("a.php", "staged")
|
||||
await harness.perform(.confirmPending)
|
||||
#expect(harness.git.executed == [["restore", "--worktree", "--", "."], ["clean", "-fd"]])
|
||||
#expect(await eventually { harness.model.editor?.text(for: "a.php") == "staged" })
|
||||
}
|
||||
|
||||
@Test func mergeAsksFirstThenRunsGit() async {
|
||||
let harness = WorkspaceHarness(git: snapshot([]))
|
||||
await harness.start()
|
||||
await harness.perform(.context(.mergeBranch("feature")))
|
||||
guard case let .confirm(request) = harness.model.overlay.current else {
|
||||
Issue.record("expected a confirm overlay")
|
||||
return
|
||||
}
|
||||
#expect(request.confirmLabel == "Merge")
|
||||
#expect(harness.git.executed.isEmpty)
|
||||
await harness.perform(.confirmPending)
|
||||
#expect(harness.git.executed == [["merge", "--no-edit", "feature"]])
|
||||
}
|
||||
|
||||
@Test func stagingRunsGitStraightAway() async {
|
||||
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, false), ("b.php", .modified, false)]))
|
||||
await harness.start()
|
||||
@@ -353,6 +386,46 @@ import Testing
|
||||
#expect(harness.toastTitles == ["Committed 1 file", "Pushed to origin/main"])
|
||||
}
|
||||
|
||||
@Test func theCommitButtonCommitsWithoutPushingAndCommitAndPushPushes() async {
|
||||
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, true)]))
|
||||
await harness.start()
|
||||
harness.model.sourceControl?.setCommitMessage("fix it")
|
||||
await harness.perform(.commit)
|
||||
#expect(harness.git.executed == [["commit", "-m", "fix it"]])
|
||||
|
||||
let pushing = WorkspaceHarness(git: snapshot([("a.php", .modified, true)]))
|
||||
await pushing.start()
|
||||
pushing.model.sourceControl?.setCommitMessage("ship it")
|
||||
await pushing.perform(.commitAndPush)
|
||||
#expect(pushing.git.executed == [["commit", "-m", "ship it"], ["push"]])
|
||||
}
|
||||
|
||||
@Test func theStageAllAndCommitCommandStagesThenCommitsWithoutPushing() async {
|
||||
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, false), ("new.txt", .added, false)]))
|
||||
await harness.start()
|
||||
harness.model.sourceControl?.setCommitMessage("ship it")
|
||||
await harness.perform(.stageAllAndCommit)
|
||||
#expect(harness.git.executed == [["add", "--", "a.php", "new.txt"], ["commit", "-m", "ship it"]])
|
||||
}
|
||||
|
||||
@Test func theGenerateCommitMessageCommandFillsTheMessageAndFocusesIt() async {
|
||||
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, true)]))
|
||||
await harness.start()
|
||||
harness.commitMessages.setResult(.success("Fixed the login form"))
|
||||
await harness.perform(.generateCommitMessage)
|
||||
#expect(harness.model.sourceControl?.commitMessage == "fixed the login form")
|
||||
#expect(harness.model.focusRequest?.target == .commitMessage)
|
||||
#expect(harness.git.executed.isEmpty)
|
||||
}
|
||||
|
||||
@Test func aFailedGenerationToastsOnce() async {
|
||||
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, true)]))
|
||||
await harness.start()
|
||||
harness.commitMessages.setResult(.failure(.timedOut))
|
||||
await harness.perform(.generateCommitMessage)
|
||||
#expect(harness.toastTitles == ["Couldn't generate a commit message"])
|
||||
}
|
||||
|
||||
@Test func commitWithoutAMessageStillAsksGitThenFocusesTheMessage() async {
|
||||
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, true)]))
|
||||
await harness.start()
|
||||
@@ -637,6 +710,46 @@ import Testing
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct TailLogModelTests {
|
||||
@Test func tailLogShowsTheBlockAndCloseRemovesIt() async {
|
||||
let harness = WorkspaceHarness()
|
||||
await harness.perform(.context(.tailLog("storage/logs/laravel.log")))
|
||||
#expect(harness.model.terminal.tailedPath == "storage/logs/laravel.log")
|
||||
let tail = FakeTerminal()
|
||||
harness.model.terminal.start(.tail, on: tail)
|
||||
await harness.perform(.closeTail)
|
||||
#expect(tail.terminations == 1)
|
||||
#expect(harness.model.terminal.tailedPath == nil)
|
||||
}
|
||||
|
||||
@Test func tailingASecondLogReplacesTheFirst() async {
|
||||
let harness = WorkspaceHarness()
|
||||
await harness.perform(.context(.tailLog("a.log")))
|
||||
let first = FakeTerminal()
|
||||
harness.model.terminal.start(.tail, on: first)
|
||||
await harness.perform(.context(.tailLog("b.log")))
|
||||
#expect(first.terminations == 1)
|
||||
#expect(harness.model.terminal.tailedPath == "b.log")
|
||||
}
|
||||
|
||||
@Test func aProjectSwitchEndsTheTailAndOpensWithout() async {
|
||||
let harness = WorkspaceHarness()
|
||||
await harness.start()
|
||||
await harness.perform(.context(.tailLog("a.log")))
|
||||
let tail = FakeTerminal()
|
||||
harness.model.terminal.start(.tail, on: tail)
|
||||
await harness.model.openProject(URL(fileURLWithPath: "/fake/other"))
|
||||
#expect(tail.terminations == 1)
|
||||
#expect(harness.model.terminal.tailedPath == nil)
|
||||
}
|
||||
|
||||
@Test func theTailLogItemUsesThePlainTint() {
|
||||
#expect(ContextMenuOverlay.tint(for: .tailLog("a.log")) == .plain)
|
||||
#expect(ContextMenuOverlay.symbol(for: .tailLog("a.log")) == "terminal")
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct TerminalContextMenuModelTests {
|
||||
@Test func aRightClickInATerminalOpensTheAppMenuAtThePointer() {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import Foundation
|
||||
import HelderServices
|
||||
|
||||
/// Answers `generate` with a scripted message or error and records every prompt.
|
||||
final class FakeCommitMessages: CommitMessageGenerating, @unchecked Sendable {
|
||||
private struct State {
|
||||
var result: Result<String, CommitMessageError> = .success("Adds a generated message")
|
||||
var delay: Duration = .zero
|
||||
var prompts: [String] = []
|
||||
}
|
||||
|
||||
private let state = Locked(State())
|
||||
|
||||
var prompts: [String] { state.current.prompts }
|
||||
|
||||
func setResult(_ result: Result<String, CommitMessageError>) { state.mutate { $0.result = result } }
|
||||
/// How long each `generate` takes before it answers.
|
||||
func setDelay(_ delay: Duration) { state.mutate { $0.delay = delay } }
|
||||
|
||||
func generate(prompt: String) async throws -> String {
|
||||
let (result, delay) = state.mutate { state in
|
||||
state.prompts.append(prompt)
|
||||
return (state.result, state.delay)
|
||||
}
|
||||
if delay > .zero { try await Task.sleep(for: delay) }
|
||||
return try result.get()
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,9 @@ final class FakeGit: GitServicing, @unchecked Sendable {
|
||||
var runsByCommand: [String: FakeGitRun] = [:]
|
||||
var branches = GitBranchList.empty
|
||||
var author: String?
|
||||
var recentCommits: [GitCommitSummary] = []
|
||||
var recentCommitsReads = 0
|
||||
var stagedDiff: StagedDiff? = StagedDiff(stat: " a.txt | 1 +\n", patch: "+a\n")
|
||||
}
|
||||
|
||||
private let state: Locked<State>
|
||||
@@ -54,6 +57,10 @@ final class FakeGit: GitServicing, @unchecked Sendable {
|
||||
func setRun(_ run: FakeGitRun, forCommand command: String) { state.mutate { $0.runsByCommand[command] = run } }
|
||||
func setBranches(_ value: GitBranchList) { state.mutate { $0.branches = value } }
|
||||
func setAuthor(_ value: String?) { state.mutate { $0.author = value } }
|
||||
func setRecentCommits(_ value: [GitCommitSummary]) { state.mutate { $0.recentCommits = value } }
|
||||
var recentCommitsReads: Int { state.current.recentCommitsReads }
|
||||
/// What `stagedDiff` returns; nil plays a failed git read.
|
||||
func setStagedDiff(_ value: StagedDiff?) { state.mutate { $0.stagedDiff = value } }
|
||||
/// Makes every `load` throw this error, or read normally again with nil.
|
||||
func setLoadError(_ error: Error?) { state.mutate { $0.loadError = error } }
|
||||
|
||||
@@ -86,6 +93,13 @@ final class FakeGit: GitServicing, @unchecked Sendable {
|
||||
|
||||
func branches() async -> GitBranchList { state.current.branches }
|
||||
func author() async -> String? { state.current.author }
|
||||
func recentCommits() async -> [GitCommitSummary] {
|
||||
state.mutate { state in
|
||||
state.recentCommitsReads += 1
|
||||
return state.recentCommits
|
||||
}
|
||||
}
|
||||
func stagedDiff() async -> StagedDiff? { state.current.stagedDiff }
|
||||
}
|
||||
|
||||
/// A snapshot with one change per row: (path, status, staged).
|
||||
|
||||
@@ -20,6 +20,7 @@ struct WorkspaceHarness {
|
||||
let speech: FakeSpeech
|
||||
let schema: FakeSchema
|
||||
let schemaLayouts: FakeSchemaLayouts
|
||||
let commitMessages: FakeCommitMessages
|
||||
let watchers: WatcherBox
|
||||
let agent = FakeTerminal()
|
||||
|
||||
@@ -42,13 +43,15 @@ struct WorkspaceHarness {
|
||||
let speech = FakeSpeech()
|
||||
let schema = FakeSchema()
|
||||
let schemaLayouts = FakeSchemaLayouts()
|
||||
let commitMessages = FakeCommitMessages()
|
||||
let watchers = WatcherBox()
|
||||
let app = AppServices(
|
||||
recentProjects: recentStore, session: session, layouts: layouts, schemaLayouts: schemaLayouts,
|
||||
system: system, speech: speech,
|
||||
makeWorkspace: { _ in
|
||||
WorkspaceServices(git: git, files: files, search: FakeSearch(), symbols: symbols,
|
||||
config: config, recentFiles: recentFiles, schema: schema)
|
||||
config: config, recentFiles: recentFiles, schema: schema,
|
||||
commitMessages: commitMessages)
|
||||
},
|
||||
makeWatcher: {
|
||||
let watcher = FakeWatcher()
|
||||
@@ -70,6 +73,7 @@ struct WorkspaceHarness {
|
||||
self.speech = speech
|
||||
self.schema = schema
|
||||
self.schemaLayouts = schemaLayouts
|
||||
self.commitMessages = commitMessages
|
||||
self.watchers = watchers
|
||||
model.terminal.attach(agent, as: .agent)
|
||||
}
|
||||
|
||||
@@ -171,6 +171,84 @@ import Testing
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct TerminalStoreTailTests {
|
||||
private let environment = ["SHELL": "/bin/zsh", "HOME": "/Users/me"]
|
||||
|
||||
@Test func aTailRunsTailOnThePathInTheProjectWithoutALoginShell() {
|
||||
let store = TerminalStore(projectRoot: "/code/app", environment: environment)
|
||||
store.startTail("storage/logs/laravel.log")
|
||||
let tail = FakeTerminal()
|
||||
store.start(.tail, on: tail)
|
||||
#expect(store.tailedPath == "storage/logs/laravel.log")
|
||||
#expect(store.tail === tail)
|
||||
#expect(tail.starts.first?.launch.executable == "/usr/bin/tail")
|
||||
#expect(tail.starts.first?.launch.arguments == ["-f", "storage/logs/laravel.log"])
|
||||
#expect(tail.starts.first?.launch.workingDirectory == "/code/app")
|
||||
#expect(tail.starts.first?.argv0 == "tail")
|
||||
}
|
||||
|
||||
@Test func withoutATailedPathThePaneSpawnsNothing() {
|
||||
let store = TerminalStore(projectRoot: "/p", environment: environment)
|
||||
let tail = FakeTerminal()
|
||||
store.start(.tail, on: tail)
|
||||
#expect(tail.starts.isEmpty)
|
||||
#expect(store.tail == nil)
|
||||
}
|
||||
|
||||
@Test func tailingAnotherFileTerminatesTheOldTailAndAsksForAFreshPane() {
|
||||
let store = TerminalStore(projectRoot: "/p", environment: environment)
|
||||
store.startTail("a.log")
|
||||
let first = FakeTerminal()
|
||||
store.start(.tail, on: first)
|
||||
let generation = store.tailGeneration
|
||||
store.startTail("b.log")
|
||||
#expect(first.terminations == 1)
|
||||
#expect(store.tail == nil)
|
||||
#expect(store.tailedPath == "b.log")
|
||||
#expect(store.tailGeneration != generation)
|
||||
let second = FakeTerminal()
|
||||
store.start(.tail, on: second)
|
||||
#expect(second.starts.first?.launch.arguments == ["-f", "b.log"])
|
||||
#expect(first.starts.count == 1)
|
||||
}
|
||||
|
||||
@Test func closeTerminatesTheTailAndRemovesTheBlock() {
|
||||
let store = TerminalStore(projectRoot: "/p", environment: environment)
|
||||
store.startTail("a.log")
|
||||
let tail = FakeTerminal()
|
||||
store.start(.tail, on: tail)
|
||||
store.closeTail()
|
||||
#expect(tail.terminations == 1)
|
||||
#expect(store.tail == nil)
|
||||
#expect(store.tailedPath == nil)
|
||||
}
|
||||
|
||||
@Test func terminateAllEndsTheTailToo() {
|
||||
let store = TerminalStore(projectRoot: "/p", environment: environment)
|
||||
let agent = FakeTerminal(), shell = FakeTerminal(), tail = FakeTerminal()
|
||||
store.start(.agent, on: agent)
|
||||
store.start(.shell, on: shell)
|
||||
store.startTail("a.log")
|
||||
store.start(.tail, on: tail)
|
||||
store.terminateAll()
|
||||
#expect([agent, shell, tail].allSatisfy { $0.terminations == 1 })
|
||||
#expect(store.tail == nil)
|
||||
#expect(store.tailedPath == nil)
|
||||
store.release(tail)
|
||||
#expect(tail.terminations == 1)
|
||||
}
|
||||
|
||||
@Test func aTailExitIsReportedOnItsOwn() {
|
||||
let store = TerminalStore(projectRoot: "/p", environment: environment)
|
||||
store.startTail("a.log")
|
||||
store.start(.tail, on: FakeTerminal())
|
||||
store.processExited(.tail, exitCode: 1)
|
||||
#expect(store.hasExited(.tail))
|
||||
#expect(!store.hasExited(.shell))
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite struct TerminalPseudoTerminalTests {
|
||||
@Test(.tags(.realProcess), .timeLimit(.minutes(1)))
|
||||
|
||||
Reference in New Issue
Block a user