Tests/OrgEditorAppKitTests/WorkspaceGateTests.swift

58f3b73e8ff337e5a15111fbb32c0d57cf2346da
orgstar/Tests/OrgEditorAppKitTests/WorkspaceGateTests.swift history · blame · raw

100 lines · 5352 bytes

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