import Foundation import OrgCore import OrgDocument import Testing @testable import OrgApp /// User commands run through the real command host against a session's buffer. @MainActor @Suite(.serialized) struct UserCommandRunTests { static let manifest = """ api = 1 [[command]] name = "upcase-title" title = "Upcase Heading Title" file = "headings.js" function = "upcaseTitle" when = "heading" [[command]] name = "weekly-review" title = "Add Weekly Review" file = "review.js" function = "weeklyReview" [[command]] name = "overlap" file = "bad.js" function = "overlap" [[command]] name = "loop" file = "bad.js" function = "loop" timeout = 1 [[command]] name = "refuse" file = "bad.js" function = "refuse" [[command]] name = "copy" file = "bad.js" function = "copy" """ static let headings = """ import { upper } from "./lib/text.js"; export async function upcaseTitle(org) { const heading = await org.heading(); if (!heading) return org.fail("Not in an entry"); const text = await org.text(); const title = text.slice(heading.titleStart, heading.titleEnd); return { edits: [{ from: heading.titleStart, to: heading.titleEnd, text: upper(title) }], message: `Upcased ${title}`, }; } """ static let review = """ export async function weeklyReview(org) { const heading = await org.heading(); if (!heading) return org.fail("Not on a heading"); const week = await org.prompt({ message: "Week: ", initial: "W48" }); return { edits: [{ from: heading.end, to: heading.end, text: `${"*".repeat(heading.level + 1)} Review ${week}\\n` }], selection: heading.end + heading.level + 2, }; } """ static let bad = """ export function overlap() { return { edits: [{ from: 0, to: 4 }, { from: 2, to: 6 }] }; } export function loop() { for (;;) {} } export async function refuse(org) { await org.fail("Not today"); } export async function copy(org) { org.message("Copying"); return { copy: (await org.file()) ?? "none" }; } """ static let file = "* Plan the week :work:\nbody\n* Next\n" let config: Folder let notes: Folder let path: String let session: DocumentSession let buffer: StateBuffer init() throws { config = try Folder() notes = try Folder() try config.write("commands.toml", Self.manifest) try config.write("commands/headings.js", Self.headings) try config.write("commands/lib/text.js", "export const upper = s => s.toUpperCase();\n") try config.write("commands/review.js", Self.review) try config.write("commands/bad.js", Self.bad) path = try notes.write("a.org", Self.file).path session = DocumentSession(recovery: FileRecoveryStore(directory: notes.url.appendingPathComponent(".recovery"))) session.saveMode = .explicit session.passphrases = FakeKeychain.passphrases(.undecided).passphrases session.userCommands = UserCommands() #expect(UserCommandFile.load(into: session.userCommands, folder: config.url) == []) session.open(path) buffer = StateBuffer(try #require(session.document)) session.attach(buffer) } func run(_ name: String, at caret: Int = 4, ask: @escaping @MainActor (Prompt, @escaping (String?) -> Void) -> Void = { _, reply in reply(nil) }, report: @escaping @MainActor (String) -> Void = { _ in }) async throws -> String? { let command = try #require(session.userCommands.command("user." + name)) let document = buffer.document let context = EditContext(revision: document.revision, text: document.text, tree: document.tree, selection: [caret..