Tests/OrgCoreTests/AttachmentExportTests.swift
45 lines · 2005 bytes
3 symbols in this file
1import Foundation
2import Testing
3@testable import OrgCore
4
5/// `org-attach-expand-links` before HTML export.
6struct AttachmentExportTests {
7 static let text = """
8 :PROPERTIES:
9 :ID: 786d6b41-ff99
10 :END:
11 [[attachment:scan.pdf]]
12 [[attachment:photo.jpg]]
13 #+begin_example
14 [[attachment:kept.png]]
15 #+end_example
16
17 * Task
18 :PROPERTIES:
19 :ID: aa11bb22-cc33
20 :END:
21 See [[attachment:in.png][the picture]].
22
23 """
24
25 @Test func attachmentLinksBecomeFileLinksIntoTheEntrysFolder() {
26 let folder = "/notes/data/78/6d6b41-ff99"
27 let expanded = ExportPreprocess.attachments(Self.text, path: "/notes/a.org", settings: .default) { $0 == folder }
28 #expect(expanded.contains("[[file:\(folder)/scan.pdf]]\n[[file:\(folder)/photo.jpg]]\n"))
29 // No link in an example block, and no folder for the second entry: the file's own.
30 #expect(expanded.contains("[[attachment:kept.png]]"))
31 #expect(expanded.contains("See [[file:/notes/in.png][the picture]]."))
32 }
33
34 @Test func htmlShowsAnAttachedImage() throws {
35 let root = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-attach-\(UUID().uuidString)").standardizedFileURL
36 let folder = root.appendingPathComponent("data/78/6d6b41-ff99")
37 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
38 defer { try? FileManager.default.removeItem(at: root) }
39 let html = HTMLExport.page(Self.text, fallbackTitle: "t", path: root.appendingPathComponent("a.org").path, bodyOnly: true)
40 // An absolute path, which is how this exporter writes a file link to one.
41 #expect(html.contains("<a href=\"\(folder.path)/scan.pdf\">\(folder.path)/scan.pdf</a>"))
42 #expect(html.contains("<img src=\"\(folder.path)/photo.jpg\" alt=\"photo.jpg\">"))
43 #expect(html.contains("<a href=\"\(root.path)/in.png\">the picture</a>"))
44 }
45}