Tests/OrgAppTests/HandoffTests.swift

c2d503b97e80b560fda12d61ecc99864f2a18a25
orgstar/Tests/OrgAppTests/HandoffTests.swift

99 lines · 5954 bytes

 1import Foundation
 2import Testing
 3@testable import OrgApp
 4
 5struct HandoffTests {
 6    static let text = """
 7    #+TITLE: Notes
 8    * Work
 9    ** Project
10    :PROPERTIES:
11    :ID: project-1
12    :END:
13    first line
14    second line
15    ** Other
16    * Home
17
18    """
19
20    @Test func placeRoundTripsThroughUserInfo() throws {
21        let path = "/Users/a/Library/Mobile Documents/com~apple~CloudDocs/org/notes/todo.org"
22        let caret = (Self.text as NSString).range(of: "second").location + 3
23        let place = try #require(HandoffPlace.make(path: path, text: Self.text, caret: caret, roots: ["/Users/a/Library/Mobile Documents/com~apple~CloudDocs/org"]))
24        #expect(place.folder == "org")
25        #expect(place.file == "notes/todo.org")
26        #expect(place.cloudPath == "com~apple~CloudDocs/org/notes/todo.org")
27        #expect(place.outline == ["Work", "Project"])
28        #expect(place.id == "project-1")
29        #expect(place.line == 8)
30        #expect(place.headingLine == 3)
31        // Through a property list, as Handoff sends it.
32        let data = try PropertyListSerialization.data(fromPropertyList: place.userInfo, format: .binary, options: 0)
33        let info = try #require(try PropertyListSerialization.propertyList(from: data, format: nil) as? [AnyHashable: Any])
34        #expect(HandoffPlace(userInfo: info) == place)
35        #expect(!place.userInfo.values.contains { ($0 as? String)?.contains("second line") == true })
36    }
37
38    @Test func nothingIsAdvertisedOutsideTheFoldersOrForEncryptedFiles() {
39        #expect(HandoffPlace.make(path: "/elsewhere/a.org", text: "", caret: 0, roots: ["/org"]) == nil)
40        #expect(HandoffPlace.make(path: "/org/secret.org.gpg", text: "", caret: 0, roots: ["/org"]) == nil)
41        #expect(HandoffPlace.make(path: "/org/a.org", text: "", caret: 0, roots: ["/org"])?.file == "a.org")
42        // The innermost of nested roots.
43        #expect(HandoffPlace.make(path: "/org/work/a.org", text: "", caret: 0, roots: ["/org", "/org/work"])?.folder == "work")
44    }
45
46    @Test func userInfoMustNameAFileInsideTheFolder() {
47        #expect(HandoffPlace(userInfo: ["folder": "org", "file": "../etc/passwd"]) == nil)
48        #expect(HandoffPlace(userInfo: ["folder": "org", "file": "/etc/passwd"]) == nil)
49        #expect(HandoffPlace(userInfo: ["folder": "org"]) == nil)
50        #expect(HandoffPlace(userInfo: nil) == nil)
51        #expect(HandoffPlace(userInfo: ["folder": "org", "file": "a.org"])?.line == 1)
52    }
53
54    @Test func iCloudFoldersMatchByTheirPathInICloud() {
55        let place = HandoffPlace(folder: "org", cloudPath: "com~apple~CloudDocs/org/notes/todo.org", file: "notes/todo.org",
56                                 outline: [], id: nil, line: 1, headingLine: 0)
57        let phone = "/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/org"
58        #expect(place.resolve(roots: [phone], exists: { _ in true }) == .file(phone + "/notes/todo.org"))
59        // A root higher up in iCloud Drive holds the file too.
60        let drive = "/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs"
61        #expect(place.resolve(roots: [drive], exists: { _ in true }) == .file(drive + "/org/notes/todo.org"))
62        // Another iCloud folder named org is not this one, so the name decides only without a match.
63        let other = "/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/archive/org"
64        #expect(place.resolve(roots: [other, phone], exists: { _ in true }) == .file(phone + "/notes/todo.org"))
65    }
66
67    @Test func otherFoldersMatchByName() {
68        let place = HandoffPlace(folder: "notes", cloudPath: nil, file: "a/b.org", outline: [], id: nil, line: 1, headingLine: 0)
69        let root = "/private/var/mobile/Containers/Shared/AppGroup/1234/File Provider Storage/notes"
70        #expect(place.resolve(roots: ["/x/org", root], exists: { _ in true }) == .file(root + "/a/b.org"))
71        #expect(place.resolve(roots: ["/x/org"], exists: { _ in true }) == .noFolder)
72        #expect(place.resolve(roots: [root], exists: { _ in false }) == .noFile)
73        // Two folders of that name: the one with the file.
74        #expect(place.resolve(roots: ["/y/notes", root], exists: { $0.hasPrefix(root) }) == .file(root + "/a/b.org"))
75        // An iCloud file on a device without that iCloud folder still matches by name.
76        let cloud = HandoffPlace(folder: "notes", cloudPath: "com~apple~CloudDocs/notes/a/b.org", file: "a/b.org", outline: [], id: nil, line: 1, headingLine: 0)
77        #expect(cloud.resolve(roots: [root], exists: { _ in true }) == .file(root + "/a/b.org"))
78    }
79
80    @Test func caretGoesToTheHeadingByIDThenOutlineThenLine() {
81        let place = HandoffPlace(folder: "org", cloudPath: nil, file: "a.org", outline: ["Work", "Project"], id: "project-1", line: 8, headingLine: 3)
82        let ns = Self.text as NSString
83        let second = ns.range(of: "second line").location
84        #expect(place.offset(in: Self.text) == second)
85        // Lines added above the heading on this device: the heading is found by ID.
86        let moved = "* New\nnew text\n" + Self.text
87        #expect(place.offset(in: moved) == (moved as NSString).range(of: "second line").location)
88        // Without the ID, by the outline path.
89        var byOutline = place
90        byOutline.id = "gone"
91        #expect(byOutline.offset(in: moved) == (moved as NSString).range(of: "second line").location)
92        // Neither: the same line.
93        let unrelated = HandoffPlace(folder: "org", cloudPath: nil, file: "a.org", outline: ["Nope"], id: nil, line: 3, headingLine: 2)
94        #expect(unrelated.offset(in: Self.text) == ns.range(of: "** Project").location)
95        // A line past the heading's text stays in it.
96        let long = HandoffPlace(folder: "org", cloudPath: nil, file: "a.org", outline: ["Work", "Project"], id: nil, line: 40, headingLine: 3)
97        #expect(long.offset(in: Self.text) == ns.range(of: "** Other").location - 1)
98    }
99}
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