updates plug with all sorts of awesome improvements
This commit is contained in:
@@ -29,8 +29,8 @@ private final class Page {
|
||||
var url: URL { folder.appendingPathComponent("plak/page.md") }
|
||||
|
||||
private func makeStore(debounce: Duration = .seconds(60), write: @escaping @Sendable (Data, URL) throws -> Void = PageStore.writeAtomically,
|
||||
page: Page) throws -> PageStore {
|
||||
let store = try PageStore(url: url, debounce: debounce, write: write)
|
||||
page: Page, firstPage: PageFile = PageFile()) throws -> PageStore {
|
||||
let store = try PageStore(url: url, debounce: debounce, write: write, firstPage: firstPage)
|
||||
store.contents = { page.text }
|
||||
return store
|
||||
}
|
||||
@@ -51,17 +51,42 @@ private final class Page {
|
||||
#expect(try String(contentsOf: url, encoding: .utf8) == "change 5\n")
|
||||
}
|
||||
|
||||
@Test("a missing page file gives an empty page, and the first save creates it")
|
||||
@Test("a missing page file gives the welcome text, and the first flush writes it")
|
||||
func missingFile() throws {
|
||||
#expect(!FileManager.default.fileExists(atPath: url.path))
|
||||
let now = Date(timeIntervalSince1970: 1_793_000_000)
|
||||
let welcome = WelcomeText.page(createdAt: now)
|
||||
let log = WriteLog()
|
||||
let page = Page()
|
||||
let store = try makeStore(page: page)
|
||||
#expect(store.loaded == PageFile())
|
||||
let store = try makeStore(write: log.record, page: page, firstPage: welcome)
|
||||
#expect(store.loaded == welcome)
|
||||
#expect(!FileManager.default.fileExists(atPath: url.path))
|
||||
page.text = "hello\n"
|
||||
store.changed()
|
||||
page.text = store.loaded.text()
|
||||
store.flush()
|
||||
#expect(try String(contentsOf: url, encoding: .utf8) == "hello\n")
|
||||
let expected = "\(CreatedDate.line(for: now))\n\(WelcomeText.text)\n"
|
||||
#expect(log.texts == [expected])
|
||||
#expect(try String(contentsOf: url, encoding: .utf8) == expected)
|
||||
store.flush()
|
||||
#expect(log.texts.count == 1)
|
||||
}
|
||||
|
||||
@Test("by default a missing page file gives the welcome text")
|
||||
func missingFileDefault() throws {
|
||||
let store = try PageStore(url: url, write: { _, _ in })
|
||||
#expect(store.loaded.lines == [.text(WelcomeText.text)])
|
||||
#expect(store.loaded.dates.count == 1)
|
||||
}
|
||||
|
||||
@Test("an existing empty page file stays empty, and nothing is written")
|
||||
func emptyFile() throws {
|
||||
try PageStore.writeAtomically(Data(), to: url)
|
||||
let log = WriteLog()
|
||||
let store = try PageStore(url: url, write: log.record)
|
||||
store.contents = { "should not be written\n" }
|
||||
#expect(store.loaded == PageFile())
|
||||
store.flush()
|
||||
#expect(log.texts.isEmpty)
|
||||
#expect(try Data(contentsOf: url).isEmpty)
|
||||
}
|
||||
|
||||
@Test("an existing page file is read into lines and dates")
|
||||
|
||||
Reference in New Issue
Block a user