Tests/OrgIndexTests/IndexStoreTests.swift
66 lines · 3203 bytes
1import OrgCore
2import Testing
3@testable import OrgIndex
4
5struct IndexStoreTests {
6 @Test func searchFindsTitlesAndBodies() throws {
7 let store = try IndexStore()
8 try store.write(record("/notes/a.org", "* Groceries\nbuy apples\n* Taxes\nfile forms\n"))
9 #expect(try store.search("appl").map(\.title) == ["Groceries"])
10 #expect(try store.search("tax").map(\.title) == ["Taxes"])
11 #expect(try store.search("file forms").map(\.title) == ["Taxes"])
12 #expect(try store.search("\"oops").isEmpty)
13 #expect(try store.search(" ").isEmpty)
14 }
15
16 @Test func rewritingAFileReplacesItsRows() throws {
17 let store = try IndexStore()
18 try store.write(record("/notes/a.org", "* Old title\n"))
19 try store.write(record("/notes/a.org", "* New title\n"))
20 #expect(try store.search("old").isEmpty)
21 #expect(try store.search("new").count == 1)
22 }
23
24 @Test func removeMoveAndTouch() throws {
25 let store = try IndexStore()
26 try store.write(record("/notes/a.org", "* Alpha\n"))
27 try store.write(record("/notes/b.org", "* Beta\n"))
28 var change = IndexChange()
29 change.removals = ["/notes/a.org"]
30 change.moves = [(from: "/notes/b.org", to: "/notes/c.org", mtime: 5)]
31 try store.apply(change)
32 #expect(try store.search("alpha").isEmpty)
33 #expect(try store.search("beta").map(\.path) == ["/notes/c.org"])
34 #expect(try store.fileStates(root: "/notes")["/notes/c.org"]?.mtime == 5)
35 }
36
37 @Test func overlayReplacesIndexedRows() throws {
38 let store = try IndexStore()
39 try store.write(record("/notes/a.org", "* Saved title\n"))
40 let overlay = ["/notes/a.org": record("/notes/a.org", "* Unsaved title\n")]
41 #expect(try store.search("saved", overlay: overlay).isEmpty)
42 #expect(try store.search("unsaved", overlay: overlay).map(\.title) == ["Unsaved title"])
43 }
44
45 @Test func idsAndDuplicates() throws {
46 let store = try IndexStore()
47 try store.write(record("/notes/a.org", "* A\n:PROPERTIES:\n:ID: x\n:END:\n"))
48 try store.write(record("/notes/b.org", "* B\n:PROPERTIES:\n:ID: y\n:END:\n"))
49 #expect(try store.headings(withID: "x").map(\.path) == ["/notes/a.org"])
50 #expect(try store.duplicateIDs().isEmpty)
51 let overlay = ["/notes/b.org": record("/notes/b.org", "* B\n:PROPERTIES:\n:ID: x\n:END:\n")]
52 #expect(try store.headings(withID: "x", overlay: overlay).map(\.path) == ["/notes/a.org", "/notes/b.org"])
53 #expect(try store.duplicateIDs(overlay: overlay).keys.sorted() == ["x"])
54 }
55
56 @Test func fileStatesAndKinds() throws {
57 let store = try IndexStore()
58 let a = record("/notes/a.org", "* A\n")
59 try store.write(a)
60 try store.write(record("/notes/a.sync-conflict-1.org", "* A\n", kind: .conflict))
61 let states = try store.fileStates(root: "/notes")
62 #expect(states["/notes/a.org"] == FileState(kind: .org, size: a.size, mtime: 1, hash: a.hash, settingsVersion: 1))
63 #expect(try store.files().map(\.kind) == [.org, .conflict])
64 #expect(try store.search("a").map(\.path) == ["/notes/a.org"])
65 }
66}