Tests/OrgIndexTests/IndexStoreTests.swift

design-themes-widgets
orgstar/Tests/OrgIndexTests/IndexStoreTests.swift history · blame · raw

111 lines · 5737 bytes

  1import Foundation
  2import OrgCore
  3import Testing
  4@testable import OrgIndex
  5
  6struct IndexStoreTests {
  7    @Test func searchFindsTitlesAndBodies() throws {
  8        let store = try IndexStore()
  9        try store.write(record("/notes/a.org", "* Groceries\nbuy apples\n* Taxes\nfile forms\n"))
 10        #expect(try store.search("appl").map(\.title) == ["Groceries"])
 11        #expect(try store.search("tax").map(\.title) == ["Taxes"])
 12        #expect(try store.search("file forms").map(\.title) == ["Taxes"])
 13        #expect(try store.search("\"oops").isEmpty)
 14        #expect(try store.search("   ").isEmpty)
 15    }
 16
 17    @Test func searchFindsTextBeforeTheFirstHeading() throws {
 18        let store = try IndexStore()
 19        try store.write(record("/notes/flat.org", "#+TITLE: Field notes\nThe heron came back to the pond.\n"))
 20        try store.write(record("/notes/b.org", "Preamble about otters.\n* Rivers\nwater\n"))
 21        let flat = try store.search("heron")
 22        #expect(flat.map(\.title) == ["Field notes"])
 23        #expect(flat.first?.ordinal == HeadingLocation.fileOrdinal && flat.first?.start == 0)
 24        #expect(try store.search("otters").map(\.path) == ["/notes/b.org"])
 25        #expect(try store.search("water").map(\.title) == ["Rivers"])
 26        // Rewritten and removed files take their preamble rows with them.
 27        try store.write(record("/notes/flat.org", "Nothing here.\n"))
 28        #expect(try store.search("heron").isEmpty)
 29        var change = IndexChange()
 30        change.removals = ["/notes/b.org"]
 31        try store.apply(change)
 32        #expect(try store.search("otters").isEmpty)
 33        // Unsaved text before the first heading is searched too.
 34        let overlay = ["/notes/flat.org": record("/notes/flat.org", "Kingfishers now.\n")]
 35        #expect(try store.search("kingfish", overlay: overlay).map(\.ordinal) == [HeadingLocation.fileOrdinal])
 36    }
 37
 38    @Test func snippetsMarkTheMatches() throws {
 39        let store = try IndexStore()
 40        try store.write(record("/notes/a.org", "* Trip\nWe walked along the river bank and saw a heron fishing in the reeds near the old mill.\n"))
 41        let hit = try #require(try store.search("heron reed").first)
 42        let snippet = try #require(hit.snippet)
 43        let marked = snippet.matches.map { (snippet.text as NSString).substring(with: NSRange($0)) }
 44        #expect(marked == ["heron", "reeds"])
 45        #expect(snippet.text.contains("saw a heron fishing"))
 46        // A title-only match has no snippet.
 47        #expect(try store.search("trip").first?.snippet == nil)
 48    }
 49
 50    @Test func snippetWindow() {
 51        let long = (1...80).map { "word\($0)" }.joined(separator: " ")
 52        let snippet = Snippet(text: long + "\nneedle\n" + long, prefixes: ["needle"])
 53        #expect(snippet != nil)
 54        #expect(snippet!.text.hasPrefix("…") && snippet!.text.hasSuffix("…"))
 55        #expect(snippet!.text.utf16.count < 200)
 56        #expect(!snippet!.text.contains("\n"))
 57        #expect(Snippet(text: "nothing", prefixes: ["zzz"]) == nil)
 58        #expect(Snippet(text: "Héron here", prefixes: ["heron"])?.matches.count == 1)
 59    }
 60
 61    @Test func rewritingAFileReplacesItsRows() throws {
 62        let store = try IndexStore()
 63        try store.write(record("/notes/a.org", "* Old title\n"))
 64        try store.write(record("/notes/a.org", "* New title\n"))
 65        #expect(try store.search("old").isEmpty)
 66        #expect(try store.search("new").count == 1)
 67    }
 68
 69    @Test func removeMoveAndTouch() throws {
 70        let store = try IndexStore()
 71        try store.write(record("/notes/a.org", "* Alpha\n"))
 72        try store.write(record("/notes/b.org", "* Beta\n"))
 73        var change = IndexChange()
 74        change.removals = ["/notes/a.org"]
 75        change.moves = [(from: "/notes/b.org", to: "/notes/c.org", mtime: 5)]
 76        try store.apply(change)
 77        #expect(try store.search("alpha").isEmpty)
 78        #expect(try store.search("beta").map(\.path) == ["/notes/c.org"])
 79        #expect(try store.fileStates(root: "/notes")["/notes/c.org"]?.mtime == 5)
 80    }
 81
 82    @Test func overlayReplacesIndexedRows() throws {
 83        let store = try IndexStore()
 84        try store.write(record("/notes/a.org", "* Saved title\n"))
 85        let overlay = ["/notes/a.org": record("/notes/a.org", "* Unsaved title\n")]
 86        #expect(try store.search("saved", overlay: overlay).isEmpty)
 87        #expect(try store.search("unsaved", overlay: overlay).map(\.title) == ["Unsaved title"])
 88    }
 89
 90    @Test func idsAndDuplicates() throws {
 91        let store = try IndexStore()
 92        try store.write(record("/notes/a.org", "* A\n:PROPERTIES:\n:ID: x\n:END:\n"))
 93        try store.write(record("/notes/b.org", "* B\n:PROPERTIES:\n:ID: y\n:END:\n"))
 94        #expect(try store.headings(withID: "x").map(\.path) == ["/notes/a.org"])
 95        #expect(try store.duplicateIDs().isEmpty)
 96        let overlay = ["/notes/b.org": record("/notes/b.org", "* B\n:PROPERTIES:\n:ID: x\n:END:\n")]
 97        #expect(try store.headings(withID: "x", overlay: overlay).map(\.path) == ["/notes/a.org", "/notes/b.org"])
 98        #expect(try store.duplicateIDs(overlay: overlay).keys.sorted() == ["x"])
 99    }
100
101    @Test func fileStatesAndKinds() throws {
102        let store = try IndexStore()
103        let a = record("/notes/a.org", "* A\n")
104        try store.write(a)
105        try store.write(record("/notes/a.sync-conflict-1.org", "* A\n", kind: .conflict))
106        let states = try store.fileStates(root: "/notes")
107        #expect(states["/notes/a.org"] == FileState(kind: .org, size: a.size, mtime: 1, hash: a.hash, settingsVersion: 1))
108        #expect(try store.files().map(\.kind) == [.org, .conflict])
109        #expect(try store.search("a").map(\.path) == ["/notes/a.org"])
110    }
111}