import Foundation import OrgApp import OrgCore import Testing /// Gates for a large workspace: the first index build, the agenda refresh and the backlinks of /// a file and a heading. In `GateTests` so they run one at a time with the editor gates. extension GateTests { /// Writes `count` notes of eight headings each, about 2 kB a note. Every heading has an ID /// and a paragraph; the first `fiveLinks` notes have five links and the rest four. A third of /// the headings are TODOs, and every hundredth note has one scheduled in the week of /// 5 October 2026. static func writeNotes(_ count: Int, fiveLinks: Int, to folder: URL) throws { 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"] try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) for n in 0.. (model: WorkspaceModel, build: Duration) { let notes = scratch.appendingPathComponent("notes") try writeNotes(count, fiveLinks: fiveLinks, to: notes) let model = try WorkspaceModel(directory: scratch.appendingPathComponent("data")) let start = ContinuousClock.now try await model.addRoot(notes) return (model, ContinuousClock.now - start) } static func scratch() throws -> URL { let url = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-gates-\(UUID().uuidString)") try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true) return url } /// One pass of the backlinks pane: the file's backlinks and the heading's. static func backlinks(_ model: WorkspaceModel) throws -> (found: Int, time: Duration) { let path = model.files.first { $0.name == "note-0.org" }!.path var found = 0 let time = try ContinuousClock().measure { found += try model.backlinks(to: Backlinks.Target(path: path)).count found += try model.backlinks(to: Backlinks.Target(path: path, title: "Heading 0.1", id: "0-1")).count } return (found, time) } /// 10,000 notes, 49,400 links. @Test func workspaceGates() async throws { let scratch = try Self.scratch() defer { try? FileManager.default.removeItem(at: scratch) } let (model, build) = try await Self.workspace(10_000, fiveLinks: 9_400, in: scratch) #expect(model.files.count == 10_000) let agenda = AgendaModel(workspace: model) agenda.agendaFiles = [] agenda.includeSubfolders = false let now = DateComponents(calendar: .current, year: 2026, month: 10, day: 5, hour: 12).date! let clock = ContinuousClock() let cold = await clock.measure { await agenda.refresh(open: [:], now: now) } let warm = await clock.measure { await agenda.refresh(open: [:], now: now) } let items = agenda.days.reduce(0) { $0 + $1.items.count } #expect(items > 0) let (found, backlinks) = try Self.backlinks(model) #expect(found > 0) print("GATE 10,000 notes: index build \(build), agenda refresh cold \(cold) / warm \(warm) (\(items) items), backlinks over 49,400 links \(backlinks)") #expect(build < .seconds(15), "index build") #expect(cold < .seconds(10), "agenda refresh, cold") #expect(warm < .seconds(2), "agenda refresh, warm") #expect(backlinks < .milliseconds(1_500), "backlinks, 49,400 links") } /// 1,000 notes, 5,000 links. @Test func backlinksGate() async throws { let scratch = try Self.scratch() defer { try? FileManager.default.removeItem(at: scratch) } let (model, _) = try await Self.workspace(1_000, fiveLinks: 1_000, in: scratch) let (found, time) = try Self.backlinks(model) #expect(found > 0) print("GATE 1,000 notes: backlinks over 5,000 links \(time)") #expect(time < .milliseconds(150), "backlinks, 5,000 links") } }