Tests/OrgEditorAppKitTests/WorkspaceGateTests.swift

b5aa2aefb2a4844c3369b8024a32c2f81a59bfd1
orgstar/Tests/OrgEditorAppKitTests/WorkspaceGateTests.swift

116 lines · 6290 bytes

  1import Foundation
  2import OrgApp
  3import OrgCore
  4import Testing
  5
  6/// Gates for a large workspace: the first index build, the agenda refresh, the backlinks of a
  7/// file and a heading, and Lint All Folders. In `GateTests` so they run one at a time with the
  8/// editor gates.
  9extension GateTests {
 10    /// Writes `count` notes of eight headings each, about 2 kB a note. Every heading has an ID
 11    /// and a paragraph; the first `fiveLinks` notes have five links and the rest four. A third of
 12    /// the headings are TODOs, and every hundredth note has one scheduled in the week of
 13    /// 5 October 2026.
 14    static func writeNotes(_ count: Int, fiveLinks: Int, to folder: URL) throws {
 15        let week = ["2026-10-05 Mon", "2026-10-06 Tue", "2026-10-07 Wed", "2026-10-08 Thu", "2026-10-09 Fri", "2026-10-10 Sat", "2026-10-11 Sun"]
 16        try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
 17        for n in 0..<count {
 18            let links = [
 19                "[[id:\((n + 1) % count)-0][note \((n + 1) % count)]]",
 20                "[[file:note-\((n + 2) % count).org::*Heading \((n + 2) % count).1][a heading]]",
 21                "[[*Heading \(n).2]]",
 22                "[[file:note-\((n + 3) % count).org]]",
 23                "[[https://example.com/\(n)][the web]]"
 24            ].prefix(n < fiveLinks ? 5 : 4)
 25            var text = "#+title: Note \(n)\n\n"
 26            for h in 0..<8 {
 27                text += "* \(h % 3 == 0 ? "TODO " : "")Heading \(n).\(h) :tag\(h):\n"
 28                if h == 0, n % 100 == 0 { text += "SCHEDULED: <\(week[n / 100 % 7])>\n" }
 29                text += ":PROPERTIES:\n:ID:       \(n)-\(h)\n:END:\n"
 30                text += "Paragraph of heading \(h) in note \(n), with *bold*, /italic/ and =code=, about as long as a note someone wrote. It goes on for a second sentence about nothing in particular.\n"
 31                if h < links.count { text += "See \(links[h]).\n" }
 32                text += "\n"
 33            }
 34            try Data(text.utf8).write(to: folder.appendingPathComponent("note-\(n).org"))
 35        }
 36    }
 37
 38    /// A folder of `count` notes, indexed on disk as the app does, and how long the first build took.
 39    static func workspace(_ count: Int, fiveLinks: Int, in scratch: URL) async throws -> (model: WorkspaceModel, build: Duration) {
 40        let notes = scratch.appendingPathComponent("notes")
 41        try writeNotes(count, fiveLinks: fiveLinks, to: notes)
 42        let model = try WorkspaceModel(directory: scratch.appendingPathComponent("data"))
 43        let start = ContinuousClock.now
 44        try await model.addRoot(notes)
 45        return (model, ContinuousClock.now - start)
 46    }
 47
 48    /// One run of Lint All Folders over the workspace, and how many files had findings.
 49    static func lintAll(_ model: WorkspaceModel) async throws -> (files: Int, time: Duration) {
 50        let paths = FileLint.folderPaths(model.files)
 51        let environment = FileLint.environment(index: model.index)
 52        var files = 0
 53        let time = try await ContinuousClock().measure {
 54            files = try await FileLint.lint(paths: paths, texts: [:], environment: environment).count
 55        }
 56        return (files, time)
 57    }
 58
 59    static func scratch() throws -> URL {
 60        let url = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-gates-\(UUID().uuidString)")
 61        try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
 62        return url
 63    }
 64
 65    /// One pass of the backlinks pane: the file's backlinks and the heading's.
 66    static func backlinks(_ model: WorkspaceModel) throws -> (found: Int, time: Duration) {
 67        let path = model.files.first { $0.name == "note-0.org" }!.path
 68        var found = 0
 69        let time = try ContinuousClock().measure {
 70            found += try model.backlinks(to: Backlinks.Target(path: path)).count
 71            found += try model.backlinks(to: Backlinks.Target(path: path, title: "Heading 0.1", id: "0-1")).count
 72        }
 73        return (found, time)
 74    }
 75
 76    /// 10,000 notes, 49,400 links.
 77    @Test func workspaceGates() async throws {
 78        let scratch = try Self.scratch()
 79        defer { try? FileManager.default.removeItem(at: scratch) }
 80        let (model, build) = try await Self.workspace(10_000, fiveLinks: 9_400, in: scratch)
 81        #expect(model.files.count == 10_000)
 82
 83        let agenda = AgendaModel(workspace: model)
 84        agenda.agendaFiles = []
 85        agenda.includeSubfolders = false
 86        let now = DateComponents(calendar: .current, year: 2026, month: 10, day: 5, hour: 12).date!
 87        let clock = ContinuousClock()
 88        let cold = await clock.measure { await agenda.refresh(open: [:], now: now) }
 89        let warm = await clock.measure { await agenda.refresh(open: [:], now: now) }
 90        let items = agenda.days.reduce(0) { $0 + $1.items.count }
 91        #expect(items > 0)
 92
 93        let (found, backlinks) = try Self.backlinks(model)
 94        #expect(found > 0)
 95        let lint = try await Self.lintAll(model)
 96        print("GATE 10,000 notes: index build \(build), agenda refresh cold \(cold) / warm \(warm) (\(items) items), backlinks over 49,400 links \(backlinks), lint all folders \(lint.time) (\(lint.files) files with findings)")
 97        #expect(build < .seconds(15), "index build")
 98        #expect(cold < .seconds(10), "agenda refresh, cold")
 99        #expect(warm < .seconds(2), "agenda refresh, warm")
100        #expect(backlinks < .milliseconds(1_500), "backlinks, 49,400 links")
101        #expect(lint.time < .seconds(15), "lint all folders, 10,000 notes")
102    }
103
104    /// 1,000 notes, 5,000 links.
105    @Test func backlinksGate() async throws {
106        let scratch = try Self.scratch()
107        defer { try? FileManager.default.removeItem(at: scratch) }
108        let (model, _) = try await Self.workspace(1_000, fiveLinks: 1_000, in: scratch)
109        let (found, time) = try Self.backlinks(model)
110        #expect(found > 0)
111        let lint = try await Self.lintAll(model)
112        print("GATE 1,000 notes: backlinks over 5,000 links \(time), lint all folders \(lint.time) (\(lint.files) files with findings)")
113        #expect(time < .milliseconds(150), "backlinks, 5,000 links")
114        #expect(lint.time < .seconds(2), "lint all folders, 1,000 notes")
115    }
116}
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