Tests/OrgAppTests/VersionTests.swift
57 lines · 3292 bytes
4 symbols in this file
1import Foundation
2import OrgCore
3import OrgDocument
4import Testing
5@testable import OrgApp
6
7@MainActor
8struct VersionTests {
9 func edit(_ session: DocumentSession, _ path: String, _ text: String) {
10 let current = session.document!.text
11 _ = session.perform(PrecomputedEdit(EditResult(baseRevision: session.document!.revision, edits: lineEdits(from: current, to: text))),
12 path: path, at: 0, expect: nil)
13 }
14
15 @Test func conflictsMergeWithMarkers() throws {
16 let notes = try Folder()
17 let file = try notes.write("a.org", "* A\nline one\nline two\n")
18 let session = DocumentSession(recovery: FileRecoveryStore(directory: notes.url.appendingPathComponent(".r")))
19 session.open(file.path)
20 edit(session, file.path, "* A\nline ONE mine\nline two\n")
21 try notes.write("a.org", "* A\nline one disk\nline two\nadded on disk\n")
22 #expect(!session.save())
23 #expect(session.conflict != nil)
24 let versions = try #require(session.conflictVersions())
25 #expect(versions.base == "* A\nline one\nline two\n" && versions.disk.contains("added on disk"))
26 session.mergeConflictWithMarkers()
27 #expect(session.conflict == nil)
28 #expect(session.document?.text == "* A\n<<<<<<< yours\nline ONE mine\n=======\nline one disk\n>>>>>>> disk\nline two\nadded on disk\n")
29 #expect(session.save())
30 #expect(try String(contentsOf: file, encoding: .utf8) == session.document?.text)
31 // The buffer as it was is kept, and restoring it works as an edit.
32 let kept = session.recoveryVersions()
33 #expect(kept.contains { $0.label == "local" })
34 let local = try #require(kept.first { session.recoveryText($0.url) == "* A\nline ONE mine\nline two\n" })
35 #expect(session.restore(local.url) == nil)
36 #expect(session.document?.text == "* A\nline ONE mine\nline two\n")
37 }
38
39 @Test func resolvesSyncthingCopies() throws {
40 let notes = try Folder()
41 let file = try notes.write("n.org", "* N\nshared\nmine\n")
42 let first = try notes.write("n.sync-conflict-20261005-101010-ABCD123.org", "* N\nshared\ntheirs\n")
43 try notes.write("n.sync-conflict-20261005-121212-ZZZZ999.org", "* N\ncopy two\n")
44 try notes.write("other.sync-conflict-20261005-101010-ABCD123.org", "x\n")
45 let session = DocumentSession(recovery: FileRecoveryStore(directory: notes.url.appendingPathComponent(".r")))
46 session.open(file.path)
47 #expect(session.syncConflictCopies().map(\.lastPathComponent) == ["n.sync-conflict-20261005-101010-ABCD123.org", "n.sync-conflict-20261005-121212-ZZZZ999.org"])
48 #expect(session.resolve(copy: first, .merge) == nil)
49 #expect(session.document?.text == "* N\nshared\n<<<<<<< n.org\nmine\n=======\ntheirs\n>>>>>>> n.sync-conflict-20261005-101010-ABCD123.org\n")
50 #expect(!FileManager.default.fileExists(atPath: first.path))
51 let second = session.syncConflictCopies()[0]
52 #expect(session.resolve(copy: second, .useCopy) == nil)
53 #expect(session.document?.text == "* N\ncopy two\n")
54 #expect(session.syncConflictCopies().isEmpty)
55 #expect(session.recoveryVersions().filter { $0.label == "sync-conflict" }.count == 2)
56 }
57}