import HelderCore import Testing @Suite struct GitDerivationTests { private func change(_ path: String, _ status: GitStatus, staged: Bool, _ original: String, _ updated: String) -> GitFileChange { GitFileChange(path: path, status: status, staged: staged, original: original, updated: updated) } @Test("a change over the diff cap marks its row and whole-file diffs as too big") func tooBig() { let snapshot = GitSnapshot(branch: "main", ahead: 0, canPush: false, changes: [ GitFileChange(path: "dump.sql", status: .modified, staged: true, original: "", updated: "", isTooBig: false), GitFileChange(path: "dump.sql", status: .modified, staged: false, original: "", updated: "", isTooBig: true), change("a.txt", .modified, staged: false, "1\n", "2\n"), ]) let state = GitDerivation.derive(snapshot) #expect(state.changes.map(\.path) == ["dump.sql", "dump.sql", "a.txt"]) #expect(state.rowDiffs["s:dump.sql"]?.isTooBig == false) #expect(state.rowDiffs["w:dump.sql"]?.isTooBig == true) #expect(state.fileDiffs["dump.sql"]?.isTooBig == true) #expect(state.fileDiffs["a.txt"]?.isTooBig == false) } @Test("no snapshot means not a repository") func notARepository() { let state = GitDerivation.derive(nil) #expect(!state.isRepository) #expect(state.branch == "—") #expect(state.changes.isEmpty) } @Test("one row per change, with its line counts and row diff") func rows() { let snapshot = GitSnapshot(branch: "main", ahead: 1, canPush: true, changes: [ change("a.txt", .modified, staged: false, "1\n2\n3\n", "1\nX\n3\n4\n"), ]) let state = GitDerivation.derive(snapshot) #expect(state.isRepository) #expect(state.branch == "main") #expect(state.ahead == 1) #expect(state.canPush) #expect(state.changes == [Change(path: "a.txt", status: .modified, added: 2, removed: 1, staged: false)]) #expect(state.rowDiffs["w:a.txt"]?.added == 2) #expect(state.fileDiffs["a.txt"]?.updated == "1\nX\n3\n4\n") #expect(state.stagedPaths.isEmpty) } @Test("a staged-then-edited file compares HEAD with disk for the whole-file views") func twoRowsWholeFile() { let snapshot = GitSnapshot(branch: "main", ahead: 0, canPush: false, changes: [ change("a.txt", .modified, staged: true, "1\n2\n3\n", "1\nSTAGED\n3\n"), change("a.txt", .modified, staged: false, "1\nSTAGED\n3\n", "1\nSTAGED\n3\n4\n"), ]) let state = GitDerivation.derive(snapshot) #expect(state.changes.map(\.id) == ["s:a.txt", "w:a.txt"]) #expect(state.stagedPaths == ["a.txt"]) let whole = state.fileDiffs["a.txt"] #expect(whole?.original == "1\n2\n3\n") #expect(whole?.updated == "1\nSTAGED\n3\n4\n") #expect(state.rowDiffs["s:a.txt"]?.updated == "1\nSTAGED\n3\n") #expect(state.rowDiffs["w:a.txt"]?.original == "1\nSTAGED\n3\n") } @Test("the unstaged row owns the file status: staged as modified but deleted on disk reads deleted") func unstagedOwnsStatus() { let snapshot = GitSnapshot(branch: "main", ahead: 0, canPush: false, changes: [ change("a.txt", .modified, staged: true, "old\n", "new\n"), change("a.txt", .deleted, staged: false, "new\n", ""), ]) let state = GitDerivation.derive(snapshot) #expect(state.fileDiffs["a.txt"]?.isDeletedFile == true) #expect(state.fileDiffs["a.txt"]?.original == "old\n") #expect(state.changes.last?.isDeleted == true) } @Test("the unstaged row first and the staged row second still pair HEAD with disk") func reversedRowOrder() { let snapshot = GitSnapshot(branch: "main", ahead: 0, canPush: false, changes: [ change("a.txt", .modified, staged: false, "index\n", "disk\n"), change("a.txt", .added, staged: true, "", "index\n"), ]) let whole = GitDerivation.derive(snapshot).fileDiffs["a.txt"] #expect(whole?.original == "") #expect(whole?.updated == "disk\n") #expect(whole?.isAddedFile == false) } @Test("a renamed row keeps the path it was renamed from") func renameSource() { var renamed = change("new.txt", .renamed, staged: true, "a\n", "a\n") renamed.originalPath = "old.txt" let state = GitDerivation.derive(GitSnapshot(branch: "main", ahead: 0, canPush: false, changes: [renamed])) #expect(state.changes.first?.originalPath == "old.txt") #expect(state.fileDiffs["new.txt"] == state.rowDiffs["s:new.txt"]) } @Test("a new file is an added file diff") func newFile() { let snapshot = GitSnapshot(branch: "main", ahead: 0, canPush: true, changes: [change("n.txt", .added, staged: false, "", "fresh\n")]) let state = GitDerivation.derive(snapshot) #expect(state.fileDiffs["n.txt"]?.isAddedFile == true) #expect(state.totals == (added: 1, removed: 0)) #expect(state.unstagedPaths == ["n.txt"]) } } @Suite struct SourceControlTextTests { private func change(_ path: String, staged: Bool = false) -> Change { Change(path: path, status: .modified, added: 1, removed: 0, staged: staged) } @Test("one group per folder, even when git returns the folder in separate runs") func oneGroupPerFolder() { let groups = SourceControlText.groupByDir([ change("tests/Feature/AlphaTest.php"), change("app/Models/User.php"), change("tests/Feature/BetaTest.php"), ]) #expect(groups.map(\.dir) == ["tests/Feature", "app/Models"]) #expect(groups[0].rows.map(\.path) == ["tests/Feature/AlphaTest.php", "tests/Feature/BetaTest.php"]) } @Test("every row is kept, so the keyboard cursor walks the full list") func keepsEveryRow() { let list = ["a/one.ts", "b/two.ts", "a/three.ts", "four.ts"].map { change($0) } let flat = SourceControlText.groupByDir(list).flatMap(\.rows) #expect(flat.count == list.count) #expect(Set(flat.map(\.id)).count == list.count) } @Test("a project-root file sits in the empty-string group") func rootGroup() { #expect(SourceControlText.groupByDir([change("README.md")]) == [ChangeGroup(dir: "", rows: [change("README.md")])]) } @Test("navigation walks staged groups before unstaged ones") func navigationOrder() { let order = SourceControlText.navigationOrder([change("b/x"), change("a/y", staged: true), change("b/z")]) #expect(order.map(\.path) == ["a/y", "b/x", "b/z"]) } @Test("a dictated commit message becomes one lowercase line") func commitMessage() { #expect(SourceControlText.normalizeCommitMessage("Fix The Bug\n In Parser \r\n") == "fix the bug in parser ") } @Test("the push tooltip counts commits") func pushTooltip() { #expect(SourceControlText.pushTooltip(canPush: false, ahead: 0) == "Nothing to push (⌘P)") #expect(SourceControlText.pushTooltip(canPush: true, ahead: 1) == "Push 1 commit to remote (⌘P)") #expect(SourceControlText.pushTooltip(canPush: true, ahead: 3) == "Push 3 commits to remote (⌘P)") #expect(SourceControlText.pushTooltip(canPush: true, ahead: 0) == "Push the branch to remote (⌘P)") } }