import Foundation import PlakCore import Testing @Suite struct PageFileTests { private let zone = TimeZone(identifier: "Europe/Amsterdam")! private func date(_ year: Int, _ month: Int, _ day: Int, _ hour: Int, _ minute: Int) -> Date { var calendar = Calendar(identifier: .gregorian) calendar.timeZone = zone return calendar.date(from: DateComponents(year: year, month: month, day: day, hour: hour, minute: minute))! } private func roundTrip(_ text: String) -> String { PageFile(text: text, timeZone: zone).text(timeZone: zone) } @Test("text, runs of blank lines, date lines and image lines read and write back byte for byte") func roundTripExact() { let text = """ \n\n # title **bold** stays as typed no date \t ![](images/2026-10-16-085512-a3f0.png) ![](images/b.jpg) under the image """ #expect(roundTrip(text) == text) } @Test("only the trailing newline and CRLF are normalised") func normalised() { #expect(roundTrip("a\r\n\r\nb\r\n") == "a\n\nb\n") #expect(roundTrip("a") == "a\n") #expect(roundTrip("a\n\n\n") == "a\n\n\n") #expect(roundTrip("") == "") #expect(roundTrip("a\rb\n") == "a\rb\n") } @Test("date lines become block dates and image lines become images") func reads() { let file = PageFile(text: "\none\n![](images/x.png)\n\n\ntwo\n", timeZone: zone) #expect(file.lines == [.text("one"), .image("images/x.png"), .text(""), .text(""), .text("two")]) #expect(file.dates == [0: date(2026, 10, 16, 8, 55)]) } @Test("three paragraphs give three blocks; a run of blank lines is one separator") func blocks() { let file = PageFile(text: "one\nline two\n\ntwo\n\n\n\nthree\n") #expect(file.blocks == [0..<2, 3..<4, 7..<8]) #expect(PageFile(text: "\n\n").blocks.isEmpty) #expect(PageFile(text: "a\n \nb\n").blocks.count == 2) } @Test("a date line that does not match the exact pattern stays text") func otherCommentsStayText() { let comments = [ "", "", " ", " ", "", "", "", ] for comment in comments { let text = "\(comment)\npara\n" let file = PageFile(text: text, timeZone: zone) #expect(file.dates.isEmpty, "\(comment)") #expect(file.lines == [.text(comment), .text("para")]) #expect(file.text(timeZone: zone) == text) } } @Test("a date line inside a block, or with no line under it, stays text") func dateLineOutOfPlace() { let line = "" let text = "a\n\(line)\n\n\(line)\n\nb\n\n\(line)\n" let file = PageFile(text: text, timeZone: zone) #expect(file.dates.isEmpty) #expect(file.lines.count == 8) #expect(file.text(timeZone: zone) == text) } @Test("a second date line under a date line is the first line of the block") func twoDateLines() { let text = "\n\nx\n" let file = PageFile(text: text, timeZone: zone) #expect(file.dates == [0: date(2026, 10, 16, 8, 55)]) #expect(file.lines.first == .text("")) #expect(file.text(timeZone: zone) == text) } @Test("a line that is not exactly an image line stays text") func otherImagesStayText() { let lines = [ "![a cat](images/x.png)", " ![](images/x.png)", "![](images/x.png) ", "![](other/x.png)", "![](images/a/b.png)", "![](images/a b.png)", "![](https://example.com/x.png)", "see ![](images/x.png)", "![](images/)", ] for line in lines { #expect(PageFile(text: line + "\n").lines == [.text(line)], "\(line)") } #expect(PageFile(text: "![](images/2026-10-16-085512-a3f0.heic)\n").lines == [.image("images/2026-10-16-085512-a3f0.heic")]) } @Test("a dated block writes its date line directly above its first line, a date elsewhere is not written") func writesDates() { let created = date(2026, 1, 2, 3, 4) let file = PageFile(lines: [.text("a"), .text("b"), .text(""), .image("images/x.png")], dates: [0: created, 1: created, 3: created]) #expect(file.text(timeZone: zone) == "\na\nb\n\n\n![](images/x.png)\n") } }