Tests/OrgAppTests/WorkingCopyTests.swift

9954efcbf435b2e8d78a4b59bfbbbb67c1b6c379
orgstar/Tests/OrgAppTests/WorkingCopyTests.swift

64 lines · 4166 bytes

 1import Foundation
 2import Testing
 3@testable import OrgApp
 4
 5struct WorkingCopyTests {
 6    @Test func syncChainsCommitPullAndPush() throws {
 7        let url = WorkingCopy.syncURL(repo: "my notes", key: "k3y+/&=", message: "Update from orgstar")
 8        #expect(url.scheme == "working-copy")
 9        #expect(url.host == "x-callback-url")
10        #expect(url.path == "/chain")
11        let query = try #require(url.query(percentEncoded: true))
12        // Spaces as %20, never +, and reserved characters encoded.
13        #expect(!query.contains("+"))
14        #expect(query.hasPrefix("repo=my%20notes&key=k3y%2B%2F%26%3D&x-success="))
15        #expect(query.hasSuffix("&command=commit&message=Update%20from%20orgstar&limit=999&command=pull&command=push"))
16        let items = try #require(URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems)
17        #expect(items.map(\.name) == ["repo", "key", "x-success", "x-error", "command", "message", "limit", "command", "command"])
18        #expect(items.filter { $0.name == "command" }.map(\.value) == ["commit", "pull", "push"])
19        #expect(items.first { $0.name == "key" }?.value == "k3y+/&=")
20        #expect(items.first { $0.name == "x-success" }?.value == "orgstar://working-copy/success?repo=my%20notes")
21        #expect(items.first { $0.name == "x-error" }?.value == "orgstar://working-copy/error?repo=my%20notes")
22    }
23
24    @Test func callbacksComeBackParsed() throws {
25        let success = try #require(URL(string: "orgstar://working-copy/success?repo=my%20notes"))
26        #expect(WorkingCopy.callback(success) == .success(repo: "my notes"))
27        // Working Copy adds x-callback-url's errorCode and errorMessage.
28        let error = try #require(URL(string: "orgstar://working-copy/error?repo=my%20notes&errorCode=1&errorMessage=Merge%20conflict%20in%20a.org"))
29        #expect(WorkingCopy.callback(error) == .failure(repo: "my notes", message: "Merge conflict in a.org"))
30        let bare = try #require(URL(string: "orgstar://working-copy/error?repo=notes"))
31        #expect(WorkingCopy.callback(bare) == .failure(repo: "notes", message: nil))
32        // The callback the sync asks for, as Working Copy would open it.
33        let built = WorkingCopy.syncURL(repo: "a&b", key: "k")
34        let back = try #require(URLComponents(url: built, resolvingAgainstBaseURL: false)?.queryItems?.first { $0.name == "x-success" }?.value)
35        #expect(WorkingCopy.callback(try #require(URL(string: back))) == .success(repo: "a&b"))
36        // Other links aren't callbacks.
37        #expect(WorkingCopy.callback(try #require(URL(string: "orgstar://agenda"))) == nil)
38        #expect(WorkingCopy.callback(try #require(URL(string: "orgstar://working-copy/other?repo=a"))) == nil)
39        #expect(WorkingCopy.callback(try #require(URL(string: "orgstar://working-copy/success"))) == nil)
40        #expect(AppLink(url: success) == nil)
41    }
42
43    @Test func foldersAreKeptByRootAndFoundByRepository() {
44        let defaults = TestDefaults("WorkingCopyTests")
45        let folders = WorkingCopy.Folders(defaults: defaults)
46        #expect(folders.folder("/notes") == nil)
47        folders.set(WorkingCopy.Folder(repository: "notes"), for: "/notes")
48        folders.set(WorkingCopy.Folder(repository: "work", syncOnOpen: true), for: "/w")
49        #expect(WorkingCopy.Folders(defaults: defaults).folder("/notes") == WorkingCopy.Folder(repository: "notes", syncOnOpen: false))
50        #expect(folders.root(of: "work") == "/w")
51        #expect(folders.root(of: "nope") == nil)
52        folders.set(nil, for: "/notes")
53        #expect(folders.all.keys.sorted() == ["/w"])
54    }
55
56    @Test func aGitFolderIsARepository() throws {
57        let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
58        defer { try? FileManager.default.removeItem(at: root) }
59        try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
60        #expect(!WorkingCopy.isRepository(root.path))
61        try FileManager.default.createDirectory(at: root.appendingPathComponent(".git"), withIntermediateDirectories: true)
62        #expect(WorkingCopy.isRepository(root.path))
63    }
64}
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