Tests/OrgAppTests/SubtreeShareTests.swift

fbc792eb21866a31f8edcffd23f5105df3be2348
orgstar/Tests/OrgAppTests/SubtreeShareTests.swift

92 lines · 3619 bytes

 1import Foundation
 2import OrgCore
 3import Testing
 4@testable import OrgApp
 5
 6struct SubtreeShareTests {
 7    static let text = """
 8    #+TITLE: Notes
 9    #+TODO: TODO WAIT | DONE
10    #+STARTUP: overview
11
12    * Other
13    ** TODO Trip to [[https://example.com][Lisbon]]: plans :travel:
14    SCHEDULED: <2026-12-01 Tue>
15    :PROPERTIES:
16    :DIR: trip
17    :END:
18    Flights.
19    *** Hotel
20    :PROPERTIES:
21    :DIR: hotel
22    :END:
23    Booked.
24    *** Secret :crypt:
25    -----BEGIN PGP MESSAGE-----
26
27    jA0ECQMC
28    -----END PGP MESSAGE-----
29
30
31    ** Next
32    """
33
34    @Test func theEntryIsSharedAsTheFileHoldsItPromoted() throws {
35        let caret = (Self.text as NSString).range(of: "Flights").location
36        let share = try #require(SubtreeShare(text: Self.text, at: caret, path: "/nowhere/notes.org", idDir: "data/", inherit: nil))
37        #expect(share.text == """
38        #+TODO: TODO WAIT | DONE
39        #+STARTUP: overview
40
41        * TODO Trip to [[https://example.com][Lisbon]]: plans :travel:
42        SCHEDULED: <2026-12-01 Tue>
43        :PROPERTIES:
44        :DIR: trip
45        :END:
46        Flights.
47        ** Hotel
48        :PROPERTIES:
49        :DIR: hotel
50        :END:
51        Booked.
52        ** Secret :crypt:
53        -----BEGIN PGP MESSAGE-----
54
55        jA0ECQMC
56        -----END PGP MESSAGE-----
57
58        """)
59        #expect(share.fileName == "Trip to Lisbon- plans.org")
60        #expect(share.attachments.isEmpty)
61        #expect(SubtreeShare(text: Self.text, at: 3, path: "/nowhere/notes.org") == nil)
62    }
63
64    @Test func attachmentsOfTheEntryAndItsChildrenComeAsFiles() throws {
65        let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
66        defer { try? FileManager.default.removeItem(at: folder) }
67        let files = FileManager.default
68        for dir in ["trip", "hotel", "trip/inside", "elsewhere"] { try files.createDirectory(at: folder.appendingPathComponent(dir), withIntermediateDirectories: true) }
69        for file in ["trip/ticket.pdf", "trip/.DS_Store", "hotel/booking.png", "elsewhere/no.txt"] { try Data("x".utf8).write(to: folder.appendingPathComponent(file)) }
70        let path = folder.appendingPathComponent("notes.org").path
71        let caret = (Self.text as NSString).range(of: "Flights").location
72        let share = try #require(SubtreeShare(text: Self.text, at: caret, path: path, idDir: "data/", inherit: nil))
73        #expect(share.attachments.map(\.lastPathComponent) == ["ticket.pdf", "booking.png"])
74        // Only the child's own folder for the child.
75        let child = try #require(SubtreeShare(text: Self.text, at: (Self.text as NSString).range(of: "Booked").location, path: path, idDir: "data/", inherit: nil))
76        #expect(child.attachments.map(\.lastPathComponent) == ["booking.png"])
77
78        let file = try share.writeFile()
79        defer { try? files.removeItem(at: file.deletingLastPathComponent()) }
80        #expect(file.lastPathComponent == "Trip to Lisbon- plans.org")
81        #expect(try String(contentsOf: file, encoding: .utf8) == share.text)
82        #expect(share.files(file: file, withAttachments: false) == [file])
83        #expect(share.files(file: file, withAttachments: true).map(\.lastPathComponent) == ["Trip to Lisbon- plans.org", "ticket.pdf", "booking.png"])
84    }
85
86    @Test func fileNamesAreSafe() {
87        #expect(SubtreeShare.fileName("a/b: c?") == "a-b- c-.org")
88        #expect(SubtreeShare.fileName("  ") == "Subtree.org")
89        #expect(SubtreeShare.fileName("..hidden") == "hidden.org")
90        #expect(SubtreeShare.fileName(String(repeating: "x", count: 100)).count == 84)
91    }
92}
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