Tests/OrgCoreTests/ArchiveTests.swift

359666677e3403c6c38599c91ff884b7aa3787cd
orgstar/Tests/OrgCoreTests/ArchiveTests.swift history · blame · raw

112 lines · 7067 bytes

  1import Foundation
  2import Testing
  3@testable import OrgCore
  4
  5struct ArchiveTests {
  6    static let now = Date(timeIntervalSince1970: 1_789_000_000)
  7    static var utc: Calendar {
  8        var calendar = Calendar(identifier: .gregorian)
  9        calendar.timeZone = TimeZone(identifier: "UTC")!
 10        return calendar
 11    }
 12
 13    func context(_ text: String, _ caret: Int) -> EditContext {
 14        EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret], now: Self.now, calendar: Self.utc)
 15    }
 16
 17    @Test func destinations() throws {
 18        let plain = try Archive.destination(context("* a\n", 0), sourcePath: "/n/a.org").get()
 19        #expect(plain == Archive.Destination(path: "/n/a.org_archive", heading: nil, isSameFile: false))
 20        let keyword = try Archive.destination(context("#+ARCHIVE: done.org::* %s\n* a\n", 27), sourcePath: "/n/a.org").get()
 21        #expect(keyword == Archive.Destination(path: "/n/done.org", heading: "* a.org", isSameFile: false))
 22        let local = try Archive.destination(context("* a\n:PROPERTIES:\n:ARCHIVE: ::* Old\n:END:\n", 0), sourcePath: "/n/a.org").get()
 23        #expect(local.isSameFile && local.heading == "* Old")
 24    }
 25
 26    @Test func archivesToANewFile() throws {
 27        let text = "* P\n** TODO a :t:\nbody\n** b\n"
 28        let ctx = context(text, 6)
 29        let destination = try Archive.destination(ctx, sourcePath: "/n/a.org").get()
 30        let edits = try Archive.run(ctx, sourcePath: "/n/a.org", destination: destination, archiveText: nil).get()
 31        #expect(edits.source.edits.reduce(text) { $1.apply(to: $0) } == "* P\n** b\n")
 32        let archived = try #require(edits.archive).apply(to: "")
 33        #expect(archived.hasPrefix("\nArchived entries from file /n/a.org\n\n\n* TODO a"))
 34        #expect(archived.contains(":ARCHIVE_OLPATH: P\n"))
 35        #expect(archived.contains(":ARCHIVE_TIME: 2026-09-10 Thu 00:26\n"))
 36    }
 37
 38    static let sources = [
 39        "* a\nbody\n* b\n",
 40        "#+FILETAGS: :f:\n* P :p:\n** TODO a :t:\nbody\n** DONE b\n",
 41        "* P [1/2]\n** TODO a\n** DONE b\n",
 42        "#+CATEGORY: work\n* a [[https://x.org][link]]\n* b\n",
 43        "#+ARCHIVE: ::* Archive\n* a :x:\n* Archive\n** old\n",
 44        "* P\n:PROPERTIES:\n:ARCHIVE: ::* Old :t:\n:CATEGORY: c\n:END:\n** a\n* Old :t:\n",
 45        "* a\nSCHEDULED: <2026-01-01 Thu>\n:PROPERTIES:\n:ID: 1\n:END:\n* b",
 46    ]
 47    static let archives: [String?] = [nil, "", "* existing\n", "\n#+TITLE: arch\n\n* x\n:PROPERTIES:\n:ARCHIVE_TIME: t\n:END:\n\n"]
 48
 49    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
 50    func archiveMatchesEmacs() throws {
 51        try #require(EmacsOracle.isAvailable)
 52        let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-archive-\(UUID().uuidString)")
 53        try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
 54        defer { try? FileManager.default.removeItem(at: folder) }
 55        var cases: [EmacsOracle.Case] = []
 56        var ours: [(label: String, text: String, caret: Int, failed: Bool)] = []
 57        var index = 0
 58        for source in Self.sources {
 59            let tree = OrgParser.parse(source)
 60            let carets = tree.root.descendants().filter { $0.kind == .heading }.flatMap { [$0.range.lowerBound, $0.range.lowerBound + 2] }
 61            for caret in carets {
 62                for archiveText in Self.archives {
 63                    index += 1
 64                    let path = folder.appendingPathComponent("s\(index).org").path
 65                    let ctx = context(source, caret)
 66                    guard case .success(let destination) = Archive.destination(ctx, sourcePath: path) else { continue }
 67                    if destination.isSameFile, archiveText != nil { continue }
 68                    let archivePath = destination.path
 69                    let escaped = { (s: String) in s.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"") }
 70                    let writeArchive = archiveText.map { "(with-temp-file \"\(escaped(archivePath))\" (insert \"\(escaped($0))\"))" } ?? "(when (file-exists-p \"\(escaped(archivePath))\") (delete-file \"\(escaped(archivePath))\"))"
 71                    let form = """
 72                        (let ((p (point)) (src (buffer-string)) s1 s2)
 73                          \(writeArchive)
 74                          (with-temp-file "\(escaped(path))" (insert src))
 75                          (with-current-buffer (find-file-noselect "\(escaped(path))")
 76                            (org-fold-show-all) (goto-char p)
 77                            (cl-letf* ((orig (symbol-function 'format-time-string))
 78                                       ((symbol-function 'format-time-string) (lambda (f &optional time zone) (funcall orig f (or time 1789000000) zone))))
 79                              (org-archive-subtree))
 80                            (setq p (point) s1 (buffer-string))
 81                            (set-buffer-modified-p nil) (kill-buffer))
 82                          (let ((b (get-file-buffer "\(escaped(archivePath))")))
 83                            (when b (with-current-buffer b (setq s2 (buffer-string)) (set-buffer-modified-p nil) (kill-buffer))))
 84                          (erase-buffer) (insert s1 "\\n@@@@\\n" (or s2 "")) (goto-char p))
 85                        """
 86                    cases.append(EmacsOracle.Case(text: source, point: EmacsOracle.point(caret, in: source), form: form))
 87                    let label = "\(caret) in \(source.debugDescription) to \(archiveText.debugDescription)"
 88                    switch Archive.run(ctx, sourcePath: path, destination: destination, archiveText: archiveText) {
 89                    case .success(let edits):
 90                        let newSource = edits.source.edits.reduce(source) { $1.apply(to: $0) }
 91                        let newArchive = destination.isSameFile ? "" : (edits.archive.map { $0.apply(to: archiveText ?? "") } ?? (archiveText ?? ""))
 92                        ours.append((label, newSource + "\n@@@@\n" + newArchive, edits.source.selection?.first?.lowerBound ?? caret, false))
 93                    case .failure:
 94                        ours.append((label, "", 0, true))
 95                    }
 96                }
 97            }
 98        }
 99        var mismatches = 0
100        for (mine, emacs) in zip(ours, try EmacsOracle.run(cases)) {
101            let failed = !emacs.error.isEmpty
102            let caret = EmacsOracle.offset(emacs.point, in: emacs.text)
103            guard mine.failed != failed || (!failed && (mine.text != emacs.text || mine.caret != caret)) else { continue }
104            mismatches += 1
105            if mismatches <= 8 {
106                let (a, b) = excerpts(mine.text, emacs.text)
107                Issue.record("\(mine.label)\n  ours:  \(mine.failed ? "failed" : "\(a.debugDescription) @\(mine.caret)")\n  emacs: \(failed ? emacs.error : "\(b.debugDescription) @\(caret)")")
108            }
109        }
110        #expect(mismatches == 0, "\(mismatches) of \(ours.count) differ")
111    }
112}