Tests/OrgProgramsTests/FileHistoryTests.swift

3aa264c947476955d58c5cccc70e97072bb47877
orgstar/Tests/OrgProgramsTests/FileHistoryTests.swift

59 lines · 3230 bytes

 1import Foundation
 2import OrgCore
 3import OrgDocument
 4import Testing
 5@testable import OrgApp
 6@testable import OrgPrograms
 7
 8@Suite(.enabled(if: git != nil, "git isn't installed"))
 9struct FileHistoryTests {
10    @Test func commitsFollowARename() async throws {
11        let repo = try await Repository()
12        try repo.folder.write("old.org", "* One\n")
13        try await repo.commitAll("First")
14        try await repo.run("mv", "old.org", "new.org")
15        try await repo.commitAll("Rename")
16        try repo.folder.write("new.org", "* One\n* Two\n")
17        try repo.folder.write("other.org", "x\n")
18        try await repo.commitAll("Second")
19
20        let commits = await GitHistory.commits(for: repo.url.appendingPathComponent("new.org"), git: git)
21        #expect(commits.map(\.subject) == ["Second", "Rename", "First"])
22        #expect(commits.map(\.path) == ["new.org", "new.org", "old.org"])
23        #expect(commits.allSatisfy { $0.author == "Test" && $0.hash.count == 40 })
24        #expect(await GitHistory.text(of: commits[2], git: git) == "* One\n")
25        #expect(await GitHistory.commits(for: repo.url.appendingPathComponent("missing.org"), git: git).isEmpty)
26        let outside = try Folder()
27        #expect(await GitHistory.commits(for: outside.url.appendingPathComponent("a.org"), git: git).isEmpty)
28    }
29
30    @Test func recoveryVersionsAndCommitsByDate() {
31        let commit = { (hash: String, seconds: TimeInterval) in
32            FileHistory.Commit(hash: hash, date: Date(timeIntervalSince1970: seconds), author: "A", subject: hash, path: "a.org", repository: URL(fileURLWithPath: "/r"))
33        }
34        let recovery = [(url: URL(fileURLWithPath: "/k/2"), date: Date(timeIntervalSince1970: 200), label: "snapshot"),
35                        (url: URL(fileURLWithPath: "/k/1"), date: Date(timeIntervalSince1970: 50), label: "local")]
36        let merged = FileHistory.merged(recovery: recovery, commits: [commit("c3", 300), commit("c1", 100)])
37        #expect(merged.map(\.date.timeIntervalSince1970) == [300, 200, 100, 50])
38        #expect(merged[1] == .recovery(url: URL(fileURLWithPath: "/k/2"), date: Date(timeIntervalSince1970: 200), label: "snapshot"))
39    }
40
41    @MainActor
42    @Test func restoringACommitIsAnEdit() async throws {
43        let repo = try await Repository()
44        try repo.folder.write("a.org", "* A\nfirst\n")
45        try await repo.commitAll("First")
46        let file = try repo.folder.write("a.org", "* A\nnow\n")
47        let session = DocumentSession(recovery: FileRecoveryStore(directory: repo.url.appendingPathComponent(".git/r")))
48        session.open(file.path)
49        let commits = await GitHistory.commits(for: file, git: git)
50        let first = try #require(commits.first)
51        let text = try #require(await GitHistory.text(of: first, git: git))
52        #expect(session.restore(text: text) == nil)
53        #expect(session.document?.text == "* A\nfirst\n")
54        #expect(session.isDirty)
55        // The buffer as it was went to recovery; the file on disk is unchanged until saved.
56        #expect(session.recoveryVersions().contains { session.recoveryText($0.url) == "* A\nnow\n" })
57        #expect(try String(contentsOf: file, encoding: .utf8) == "* A\nnow\n")
58    }
59}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form