Tests/OrgIndexTests/TextFileRecordTests.swift
54 lines · 3126 bytes
1import Foundation
2import OrgCore
3import Testing
4@testable import OrgIndex
5
6/// Markdown and plain text files in the index: searched, titled, never org.
7struct TextFileRecordTests {
8 @Test func markdownHeadingsAndTitle() {
9 let text = "---\ntitle: Field notes\n---\nThe heron came back.\n# Birds\nherons\n## Waders\n* TODO not org\n```\n# not a heading\n```\n# Fish\n"
10 let file = record("/notes/a.md", text, kind: .text)
11 #expect(file.title == "Field notes")
12 #expect(file.preamble == "---\ntitle: Field notes\n---\nThe heron came back.\n")
13 #expect(file.headings.map(\.title) == ["Birds", "Waders", "Fish"])
14 #expect(file.headings.map(\.level) == [1, 2, 1])
15 #expect(file.headings.map(\.parent) == [nil, 0, nil])
16 #expect(file.headings[1].outlinePath == ["Birds", "Waders"])
17 #expect(file.headings[0].body == "herons\n")
18 #expect(file.headings[1].body == "* TODO not org\n```\n# not a heading\n```\n")
19 #expect(file.headings.allSatisfy { $0.todo == nil && $0.tags.isEmpty && $0.timestamps.isEmpty && $0.links.isEmpty })
20 #expect(file.fileID == nil && file.links.isEmpty)
21 #expect(record("/notes/b.md", "Intro\n## First\n", kind: .text).title == "First")
22 }
23
24 @Test func plainTextIsOneBody() {
25 let file = record("/notes/a.txt", "* not a heading\n#+title: not a title\n", kind: .text)
26 #expect(file.headings.isEmpty && file.title == nil)
27 #expect(file.preamble == "* not a heading\n#+title: not a title\n")
28 }
29
30 /// Denote's links are read from Markdown and text files, and a text note is titled by its
31 /// Denote front matter.
32 @Test func denoteLinksAndTitles() {
33 let md = record("/notes/20261011T120000--a.md", "Intro [x](denote:20261011T120001)\n# H\n[[denote:20261011T120002][y]] [z](https://z)\n", kind: .text)
34 #expect(md.links.map(\.target) == ["denote:20261011T120001"])
35 #expect(md.headings[0].links.map(\.target) == ["denote:20261011T120002"])
36 let txt = record("/notes/20261011T120003--b.txt", "title: Plain Note\n---------------------------\n\n[x](denote:1) [[denote:20261011T120001]]\n", kind: .text)
37 #expect(txt.title == "Plain Note" && txt.links.map(\.target) == ["denote:20261011T120001"])
38 #expect(record("/notes/b.txt", "title: Not a note\n", kind: .text).title == nil)
39 }
40
41 @Test func searchFindsTextFiles() throws {
42 let store = try IndexStore()
43 try store.write(record("/notes/a.md", "---\ntitle: Pond\n---\nOtters swim.\n# Herons\nThey wade.\n", kind: .text))
44 try store.write(record("/notes/b.txt", "A list of kingfishers.\n", kind: .text))
45 #expect(try store.search("otters").map(\.title) == ["Pond"])
46 let heading = try store.search("wade")
47 #expect(heading.map(\.title) == ["Herons"])
48 #expect(heading.first?.start == 33)
49 #expect(try store.search("kingfish").map(\.title) == ["b.txt"])
50 // Not org: no outline, nodes or link targets.
51 #expect(try store.outline(maxLevel: 3).isEmpty)
52 #expect(try store.linkTargets().isEmpty)
53 }
54}