improvements on the ui

This commit is contained in:
2026-11-25 07:14:49 +01:00
parent 3afdb6d67f
commit b7c72b9e16
249 changed files with 19702 additions and 17358 deletions
@@ -85,6 +85,40 @@ import UniformTypeIdentifiers
let name = ImageStore(base: base).name(ext: "webp")
#expect(name.wholeMatch(of: /\d{4}-\d{2}-\d{2}-\d{6}-[0-9a-f]{4}\.webp/) != nil)
}
@Test("an image file no image line uses is deleted, a used one stays")
func removeUnused() throws {
let images = store()
try FileManager.default.createDirectory(at: images.folder, withIntermediateDirectories: true)
let png = try imageData(.png)
try png.write(to: images.folder.appendingPathComponent("used.png"))
try png.write(to: images.folder.appendingPathComponent("unused.png"))
#expect(images.removeUnused(keeping: ["images/used.png"]) == ["images/unused.png"])
#expect(FileManager.default.fileExists(atPath: images.folder.appendingPathComponent("used.png").path))
#expect(!FileManager.default.fileExists(atPath: images.folder.appendingPathComponent("unused.png").path))
}
@Test("a symbolic link, a hidden file and a file that is no image stay, and so does the file a link points to")
func removeUnusedTouchesOnlyImageFiles() throws {
let images = store()
let manager = FileManager.default
try manager.createDirectory(at: images.folder, withIntermediateDirectories: true)
let outside = base.appendingPathComponent("outside.png")
try imageData(.png).write(to: outside)
try manager.createSymbolicLink(at: images.folder.appendingPathComponent("link.png"), withDestinationURL: outside)
try Data().write(to: images.folder.appendingPathComponent(".hidden.png"))
try Data().write(to: images.folder.appendingPathComponent("notes.txt"))
#expect(images.removeUnused(keeping: []).isEmpty)
#expect(manager.fileExists(atPath: outside.path))
#expect((try? manager.destinationOfSymbolicLink(atPath: images.folder.appendingPathComponent("link.png").path)) != nil)
#expect(manager.fileExists(atPath: images.folder.appendingPathComponent(".hidden.png").path))
#expect(manager.fileExists(atPath: images.folder.appendingPathComponent("notes.txt").path))
}
@Test("a missing image folder deletes nothing")
func removeUnusedWithoutFolder() {
#expect(store().removeUnused(keeping: []).isEmpty)
}
}
private final class Counter: @unchecked Sendable {
@@ -96,6 +96,58 @@ private final class Page {
#expect(store.loaded.lines == [.text("a"), .text(""), .text(""), .text("b")])
}
@Test("pruning at launch writes the page file once, without the old and undated blocks")
func prunesOldBlocks() throws {
let now = Date()
let recent = CreatedDate.line(for: now.addingTimeInterval(-3_600))
let old = CreatedDate.line(for: now.addingTimeInterval(-15 * 86_400))
try PageStore.writeAtomically(Data("\(old)\nold\n\nundated\n\n\(recent)\nrecent\n".utf8), to: url)
let log = WriteLog()
let store = try PageStore(url: url, write: log.record, pruneAt: now)
#expect(store.loaded.lines == [.text("recent")])
#expect(log.texts == ["\(recent)\nrecent\n"])
store.flush()
#expect(log.texts.count == 1)
}
@Test("pruning with nothing old writes nothing")
func prunesNothing() throws {
let now = Date()
let text = "\(CreatedDate.line(for: now.addingTimeInterval(-13 * 86_400)))\nrecent\n"
try PageStore.writeAtomically(Data(text.utf8), to: url)
let log = WriteLog()
let store = try PageStore(url: url, write: log.record, pruneAt: now)
#expect(store.loaded.lines == [.text("recent")])
#expect(log.texts.isEmpty)
#expect(try String(contentsOf: url, encoding: .utf8) == text)
}
@Test("pruning every block leaves an empty page file, not the welcome text")
func prunesToEmpty() throws {
try PageStore.writeAtomically(Data("undated\n".utf8), to: url)
let log = WriteLog()
let store = try PageStore(url: url, write: log.record, pruneAt: Date())
#expect(store.loaded == PageFile())
#expect(log.texts == [""])
#expect(try Data(contentsOf: url).isEmpty)
}
@Test("a failed write of the pruned page is reported and tried again on flush")
func pruneWriteError() throws {
try PageStore.writeAtomically(Data("undated\n".utf8), to: url)
let failing = LockedFlag(true)
let store = try PageStore(url: url, write: { data, url in
if failing.value { throw DiskFull() }
try PageStore.writeAtomically(data, to: url)
}, pruneAt: Date())
#expect(store.saveError != nil)
failing.value = false
store.contents = { "" }
store.flush()
#expect(store.saveError == nil)
#expect(try Data(contentsOf: url).isEmpty)
}
@Test("a page file that is not UTF-8 is not read, so it is never saved over")
func unreadableFile() throws {
try PageStore.writeAtomically(Data([0xFF, 0xFE, 0x00, 0xD8]), to: url)