import Foundation import OrgCore import OrgDocument import OrgMathJax import PDFKit import Testing @testable import OrgApp /// What Print… sends to the printer: the page PDF export makes of the file, the subtree or the /// selection, and the agenda as shown. @MainActor struct PrintingTests { init() { PDFExport.mathJaxURL = OrgMathJax.script } static let text = "#+TITLE: Notes\n* One\nFirst body.\n** Inner\nInner body.\n* Two\nSecond body.\n" func page(_ scope: PrintScope, text: String = Self.text) -> String? { Printing.page(text, path: "/notes/a.org", scope: scope, settings: .default, codeStyle: nil, language: "en") } func offset(of needle: String, in text: String = Self.text) -> Int { (text as NSString).range(of: needle).location } @Test func eachScope() throws { let file = try #require(page(.file)) #expect(file.contains("Notes") && file.contains("First body.") && file.contains("Second body.")) #expect(file == HTMLExport.page(Self.text, fallbackTitle: "a", settings: .default, path: "/notes/a.org", language: "en")) // As Export Subtree: the heading is the title, its children promoted. let subtree = try #require(page(.subtree(offset(of: "First body.")))) #expect(subtree.contains("One") && subtree.contains("Inner body.")) #expect(!subtree.contains("Second body.")) #expect(page(.subtree(0)) == nil) // The selection with the file's keywords. let start = offset(of: "Second body.") let selection = try #require(page(.selection(start..<(start + 6)))) #expect(selection.contains("Notes") && selection.contains("Second")) #expect(!selection.contains("Second body.") && !selection.contains("First body.")) #expect(page(.selection(start.. (AgendaModel, Folder) { let notes = try Folder() try notes.write("work.org", """ * TODO Call the bank :phone: SCHEDULED: <2026-11-26 Thu 09:00> * TODO [#A] Send the report DEADLINE: <2026-11-20 Fri> * DONE Filed CLOSED: [2026-11-25 Wed 10:00] * TODO Read this SCHEDULED: <2026-11-27 Fri> """) let workspace = try WorkspaceModel(directory: nil) try await workspace.addRoot(notes.url) let agenda = AgendaModel(workspace: workspace) agenda.mode = mode await agenda.refresh(open: [:], now: Self.now, calendar: Self.calendar) return (agenda, notes) } func page(_ agenda: AgendaModel, filter: AgendaFilter = AgendaFilter(), overdueShown: Bool = true, showAllDates: Bool = false) -> String { AgendaPrint.page(title: "Agenda", subtitle: "work", parts: agenda.printParts(showAllDates: showAllDates, filter: filter, overdueShown: overdueShown), today: agenda.today, calendar: Self.calendar, locale: Locale(identifier: "en_US")) } @Test func agendaDaysAsShown() async throws { let (agenda, notes) = try await agenda(.agenda(span: 2, startOffset: nil)) _ = notes let html = page(agenda) #expect(html.contains("Agenda") && html.contains("

work

")) #expect(html.contains("Today · Thursday, November 26, 2026") && html.contains("Tomorrow · Friday, November 27, 2026")) #expect(html.contains("TODO Call the bank")) #expect(html.contains("09:00") && html.contains("work · :phone:")) #expect(html.contains("

Overdue 1

") && html.contains("[#A] Send the report")) // Escaped, not markup. #expect(html.contains("Read <b>this</b>")) // Folded overdue entries stay folded; a filter leaves out what it hides. let folded = page(agenda, overdueShown: false) #expect(folded.contains("Overdue 1") && !folded.contains("Send the report")) var filter = AgendaFilter() filter.tags = ["-phone"] #expect(!page(agenda, filter: filter).contains("Call the bank")) } @Test func agendaListsAndEmptyDays() async throws { let (list, notes) = try await agenda(.todo(keywords: nil)) _ = notes let html = page(list) #expect(html.contains("Call the bank") && html.contains("Send the report") && !html.contains("Filed")) #expect(!html.contains("class=\"gutter\"")) var none = AgendaFilter() none.tags = ["+nothing"] #expect(page(list, filter: none).contains("No entries")) let (days, more) = try await agenda(.agenda(span: 1, startOffset: 3)) _ = more #expect(!page(days).contains("\n

Nothing scheduled

")) } /// The agenda's page goes through the same pages as a file's. @Test func agendaPrintsAsPages() async throws { let (agenda, notes) = try await agenda(.agenda(span: 2, startOffset: nil)) _ = notes let text = try #require(PDFDocument(data: try await Printing.pages(html: page(agenda), directory: nil))).string ?? "" #expect(text.contains("Call the bank") && text.contains("Send the report")) } }