Tests/OrgCoreTests/BacklinkTests.swift

58f3b73e8ff337e5a15111fbb32c0d57cf2346da
orgstar/Tests/OrgCoreTests/BacklinkTests.swift history · blame · raw

76 lines · 4716 bytes

6 symbols in this file
 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5struct BacklinkTests {
 6    let heading = Backlinks.Target(path: "/notes/a.org", title: "Fix the sink [1/2]", id: "abc-1", customID: "sink")
 7    let file = Backlinks.Target(path: "/notes/a.org")
 8
 9    @Test func followsLinksAsOrgDoes() {
10        func links(_ link: String, from source: String = "/notes/b.org", to target: Backlinks.Target) -> Bool {
11            Backlinks.links(link, from: source, to: target, home: "/home")
12        }
13        #expect(links("id:abc-1", to: heading) && !links("id:abc-2", to: heading) && !links("id:abc-1", to: file))
14        #expect(links("file:a.org::*Fix the sink", to: heading))
15        #expect(links("file:./a.org::#sink", to: heading))
16        #expect(links("file:/notes/a.org", to: file) && !links("file:/notes/a.org", to: heading))
17        #expect(links("./a.org", to: file) && links("../notes/a.org::*Fix the sink", to: heading))
18        #expect(links("file:sub/../a.org", to: file))
19        #expect(!links("file:c.org::*Fix the sink", to: heading) && !links("https://a.org", to: file))
20        #expect(links("~/x.org", from: "/notes/b.org", to: Backlinks.Target(path: "/home/x.org")))
21        // Within the file.
22        #expect(links("*Fix the sink", from: "/notes/a.org", to: heading) && links("#sink", from: "/notes/a.org", to: heading))
23        #expect(links("Fix the sink", from: "/notes/a.org", to: heading))
24        #expect(!links("*Fix the sink", from: "/notes/b.org", to: heading))
25    }
26
27    /// The patterns are compiled once, not for each link: testing the links takes less time
28    /// than compiling them that often would.
29    @Test func testsManyLinksWithoutCompilingPatterns() throws {
30        let links = (0..<4_000).map { ["id:abc-\($0)", "file:a.org::*Fix the sink", "https://example.org/\($0)", "Fix the sink"][$0 % 4] }
31        let clock = ContinuousClock()
32        // The fastest of several interleaved rounds, which other work on the machine can't slow.
33        var compiling = Duration.seconds(Int64.max)
34        var testing = Duration.seconds(Int64.max)
35        for _ in 0..<5 {
36            compiling = min(compiling, try clock.measure {
37                for index in links.indices {
38                    _ = try NSRegularExpression(pattern: "(\\\\+)(?=$|[\\[\\]])")
39                    if index % 2 == 0 { _ = try Regex("^[a-zA-Z][-a-zA-Z0-9+]*:") }
40                }
41            })
42            var hits = 0
43            testing = min(testing, clock.measure {
44                for link in links where Backlinks.links(link, from: "/notes/b.org", to: heading, home: "/home") { hits += 1 }
45            })
46            #expect(hits == 1_000)
47        }
48        #expect(testing < compiling / 2, "\(testing) against \(compiling)")
49    }
50
51    @Test func followsDenoteLinks() {
52        let file = Backlinks.Target(path: "/notes/20261119T120000--a-note__kw.org")
53        let heading = Backlinks.Target(path: file.path, title: "Inside", customID: "in")
54        #expect(Backlinks.links("denote:20261119T120000", from: "/notes/b.org", to: file))
55        #expect(!Backlinks.links("denote:20261119T120000", from: "/notes/b.org", to: heading))
56        #expect(Backlinks.links("denote:20261119T120000::#in", from: "/notes/b.org", to: heading))
57        #expect(Backlinks.links("denote:20261119T120000::*Inside", from: "/notes/b.org", to: heading))
58        #expect(!Backlinks.links("denote:20261119T120001", from: "/notes/b.org", to: file))
59        #expect(!Backlinks.links("denote:20261119T120000", from: "/notes/b.org", to: Backlinks.Target(path: "/notes/a.org")))
60    }
61
62    @Test func contextLineShowsLinksAsTheyRead() throws {
63        let body = ":PROPERTIES:\n:ID: x\n:END:\nFirst line.\nSee [[id:s-1][the sink]] and [[file:a.org]] today.\n"
64        let context = try #require(Backlinks.context(in: body, link: "id:s-1"))
65        #expect(context.text == "See the sink and file:a.org today.")
66        #expect((context.text as NSString).substring(with: NSRange(context.link)) == "the sink")
67        let plain = try #require(Backlinks.context(in: "Read [[file:a.org::*Sink]].\n", link: "file:a.org::*Sink"))
68        #expect(plain.text == "Read file:a.org::*Sink." && plain.link == 5..<22)
69        #expect(Backlinks.context(in: "nothing here\n", link: "id:zz") == nil)
70        // A long line keeps the part around the link.
71        let long = String(repeating: "word ", count: 60) + "[[id:q][target]]" + String(repeating: " more", count: 60)
72        let cut = try #require(Backlinks.context(in: long, link: "id:q"))
73        #expect(cut.text.utf16.count < 200 && cut.text.hasPrefix("…") && cut.text.hasSuffix("…"))
74        #expect((cut.text as NSString).substring(with: NSRange(cut.link)) == "target")
75    }
76}