Files
plak/Tests/PlakCoreTests/PagePruningTests.swift
2026-11-25 07:14:49 +01:00

132 lines
3.4 KiB
Swift

import Foundation
import PlakCore
import Testing
@Suite struct PagePruningTests {
private let zone = TimeZone(identifier: "Europe/Amsterdam")!
private var now: Date {
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = zone
return calendar.date(from: DateComponents(year: 2026, month: 11, day: 12, hour: 12, minute: 0))!
}
/// The page file text after pruning at `now`.
private func pruned(_ text: String) -> String {
PageFile(text: text, timeZone: zone).pruned(now: now, timeZone: zone).text(timeZone: zone)
}
@Test("a block of 13 days stays, a block of 15 days goes with the blank line above it")
func olderThan14Days() {
let text = """
<!-- 2026-10-30 12:00 -->
thirteen days
<!-- 2026-10-28 12:00 -->
fifteen days
<!-- 2026-11-12 11:00 -->
today
"""
#expect(pruned(text) == """
<!-- 2026-10-30 12:00 -->
thirteen days
<!-- 2026-11-12 11:00 -->
today
""")
}
@Test("a block of exactly 14 days stays, one minute older goes")
func boundary() {
let text = """
<!-- 2026-10-29 12:00 -->
exactly fourteen days
<!-- 2026-10-29 11:59 -->
one minute older
"""
#expect(pruned(text) == "<!-- 2026-10-29 12:00 -->\nexactly fourteen days\n")
}
@Test("a block without a date line goes")
func undated() {
let text = """
no date
<!-- 2026-11-10 09:00 -->
dated
"""
#expect(pruned(text) == "<!-- 2026-11-10 09:00 -->\ndated\n")
}
@Test("a fenced block with a blank line in it stays whole, or goes whole")
func fencedBlock() {
let text = """
<!-- 2026-11-01 10:00 -->
```php
<?php
echo 'kept';
```
<!-- 2026-10-01 10:00 -->
```json
{
"old": true
}
```
"""
let page = PageFile(text: text, timeZone: zone)
#expect(page.lines.count == 3)
#expect(pruned(text) == """
<!-- 2026-11-01 10:00 -->
```php
<?php
echo 'kept';
```
""")
}
@Test("an old bookmark block goes like any block")
func oldBookmark() {
let text = """
<!-- 2026-10-20 08:00 -->
=old bookmark
under it
<!-- 2026-11-11 08:00 -->
=new bookmark
"""
#expect(pruned(text) == "<!-- 2026-11-11 08:00 -->\n=new bookmark\n")
}
@Test("a page with nothing old comes back unchanged")
func nothingOld() {
let text = "\n<!-- 2026-11-01 10:00 -->\na\n\n\n<!-- 2026-11-12 11:59 -->\nb\n![](images/x.png)\n\n"
let page = PageFile(text: text, timeZone: zone)
#expect(page.pruned(now: now, timeZone: zone) == page)
#expect(pruned(text) == text)
}
@Test("a page with only old blocks becomes an empty page")
func allOld() {
#expect(PageFile(text: "<!-- 2026-01-01 10:00 -->\nold\n\nundated\n", timeZone: zone).pruned(now: now, timeZone: zone) == PageFile())
}
@Test("the image paths are those of the image lines")
func imagePaths() {
let page = PageFile(text: "![](images/a.png)\ntext images/b.png\n\n![](images/c.jpg)\n", timeZone: zone)
#expect(page.imagePaths == ["images/a.png", "images/c.jpg"])
}
}