CI / ci (push) Canceled after 0s
Posts become writable end to end per the #18 map decisions: New Post holds an in-memory draft (folder appears only on first save), first save stamps the flat frontmatter block the #17 reader already parses (pass_state First Draft, origin writer, immutable after stamping), and the Post ID is the Title slug with case-insensitive collision suffixes. Explicit Rename retitles the # Title line and moves the folder (post.md stays); Delete goes to Trash. A recursive FSEvents stream keeps the list mirroring posts/ — external Finder edits included. Editor is a deliberate TextEditor stub; #19's TextKit2 editor replaces it in place while the lifecycle machinery below the view survives the swap. Two review rounds (standards/spec + finish-audit fanout) fixed: watcher start-order bug, writeNew clobber race, overwrite resurrecting vanished posts with reset origin, rename destroying non-heading first lines, draft hijacking the editor, debounce starvation under churn. Tests: 75/75 (17 new). Closes #18.
215 lines
9.7 KiB
Swift
215 lines
9.7 KiB
Swift
//
|
|
// PostStoreTests.swift
|
|
// MuseionTests
|
|
//
|
|
// #18: the Post lifecycle write side — slug naming, lazy first write with
|
|
// the #59 stamp, rename = retitle + move (#60), Trash delete, save-time
|
|
// normalization.
|
|
//
|
|
|
|
import Foundation
|
|
@testable import Museion
|
|
import Testing
|
|
|
|
/// Throwaway Home in a temp dir (PostLibraryTests convention).
|
|
private func storeHome(_ write: (URL) throws -> Void = { _ in }) throws -> URL {
|
|
let home = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent("poststore-\(UUID().uuidString)", isDirectory: true)
|
|
try FileManager.default.createDirectory(at: home, withIntermediateDirectories: true)
|
|
try write(home)
|
|
return home
|
|
}
|
|
|
|
private func readPost(_ home: URL, _ id: String) throws -> String {
|
|
try String(contentsOf: home.appendingPathComponent("posts/\(id)/post.md"), encoding: .utf8)
|
|
}
|
|
|
|
@Test func `slug algorithm per #60`() {
|
|
#expect(PostStore.slug(from: "The Cartographer's Error") == "the-cartographers-error")
|
|
#expect(PostStore.slug(from: "A Short Defence of Footnotes") == "a-short-defence-of-footnotes")
|
|
#expect(PostStore.slug(from: "Café naïve résumé") == "cafe-naive-resume") // diacritic-fold
|
|
#expect(PostStore.slug(from: "Hello — World!") == "hello-world") // em-dash dropped, space hyphenated
|
|
#expect(PostStore.slug(from: " leading and trailing ") == "leading-and-trailing")
|
|
#expect(PostStore.slug(from: "Multiple spaces") == "multiple-spaces") // runs → one hyphen
|
|
#expect(PostStore.slug(from: "北京 Notes") == "北京-notes") // CJK kept
|
|
#expect(PostStore.slug(from: String(repeating: "a", count: 100)) == String(repeating: "a", count: 80)) // 80-char cap
|
|
#expect(PostStore.slug(from: "!!!") == "untitled")
|
|
#expect(PostStore.slug(from: "") == "untitled")
|
|
}
|
|
|
|
@Test func `first write is lazy stamps frontmatter and slugs the folder`() throws {
|
|
let home = try storeHome()
|
|
let post = try PostStore.writeNew(body: "# My First Post\n\nBody.", home: home)
|
|
|
|
#expect(post.id == "my-first-post")
|
|
#expect(post.passState == .firstDraft)
|
|
#expect(post.origin == .writer)
|
|
#expect(post.title == "My First Post")
|
|
let file = try readPost(home, "my-first-post")
|
|
#expect(file == "---\npass_state: First Draft\norigin: writer\n---\n# My First Post\n\nBody.")
|
|
|
|
// The #17 reader round-trips what the write side stamps.
|
|
let scanned = Post.scan(home: home)
|
|
#expect(scanned.count == 1)
|
|
#expect(scanned.first?.title == "My First Post")
|
|
#expect(scanned.first?.passState == .firstDraft)
|
|
#expect(scanned.first?.origin == .writer)
|
|
}
|
|
|
|
@Test func `untitled draft first save uses untitled fallback`() throws {
|
|
let home = try storeHome()
|
|
let post = try PostStore.writeNew(body: "\n\n", home: home)
|
|
#expect(post.id == "untitled")
|
|
#expect(post.title == "untitled")
|
|
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("posts/untitled/post.md").path))
|
|
}
|
|
|
|
@Test func `collision appends -2 -3 case-insensitively`() throws {
|
|
let home = try storeHome { home in
|
|
try FileManager.default.createDirectory(
|
|
at: home.appendingPathComponent("posts/my-post", isDirectory: true),
|
|
withIntermediateDirectories: true,
|
|
)
|
|
try FileManager.default.createDirectory(
|
|
at: home.appendingPathComponent("posts/My-Post-2", isDirectory: true),
|
|
withIntermediateDirectories: true,
|
|
)
|
|
try "x".write(to: home.appendingPathComponent("posts/my-post/post.md"), atomically: true, encoding: .utf8)
|
|
}
|
|
// existing: my-post, My-Post-2 → next free: my-post-3
|
|
#expect(PostStore.availableSlug(for: "My Post", in: home.appendingPathComponent("posts")) == "my-post-3")
|
|
|
|
let post = try PostStore.writeNew(body: "# My Post\n", home: home)
|
|
#expect(post.id == "my-post-3")
|
|
}
|
|
|
|
@Test func `rename retitles moves folder and keeps frontmatter`() throws {
|
|
let home = try storeHome()
|
|
let original = try PostStore.writeNew(body: "# Old Name\n\nBody.", home: home)
|
|
let moved = try PostStore.rename(original, to: "New Name", home: home)
|
|
|
|
#expect(moved.id == "new-name")
|
|
#expect(moved.title == "New Name")
|
|
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("posts/old-name").path))
|
|
let file = try readPost(home, "new-name")
|
|
#expect(file == "---\npass_state: First Draft\norigin: writer\n---\n# New Name\n\nBody.")
|
|
}
|
|
|
|
@Test func `rename inserts title line when body had none`() throws {
|
|
let home = try storeHome()
|
|
let original = try PostStore.writeNew(body: "\n\n", home: home)
|
|
#expect(original.id == "untitled")
|
|
let moved = try PostStore.rename(original, to: "Given Title", home: home)
|
|
#expect(moved.id == "given-title")
|
|
let file = try readPost(home, "given-title")
|
|
#expect(file.hasPrefix("---\npass_state: First Draft\norigin: writer\n---\n# Given Title"))
|
|
}
|
|
|
|
@Test func `rename keeps non-heading first line and inserts title above`() throws {
|
|
let home = try storeHome()
|
|
let original = try PostStore.writeNew(body: "Call mom tomorrow\n\nSecond line.", home: home)
|
|
let moved = try PostStore.rename(original, to: "Errands", home: home)
|
|
#expect(moved.id == "errands")
|
|
let file = try readPost(home, "errands")
|
|
#expect(file == "---\npass_state: First Draft\norigin: writer\n---\n# Errands\nCall mom tomorrow\n\nSecond line.")
|
|
}
|
|
|
|
@Test func `overwrite on vanished post throws instead of resurrecting`() throws {
|
|
let home = try storeHome()
|
|
#expect(throws: CocoaError(.fileReadNoSuchFile)) {
|
|
try PostStore.overwrite(id: "ghost", body: "# Ghost\n", home: home)
|
|
}
|
|
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("posts/ghost").path))
|
|
}
|
|
|
|
@Test func `collision routing consults a fresh listing`() throws {
|
|
let home = try storeHome()
|
|
_ = try PostStore.writeNew(body: "# Mine\n", home: home) // mine
|
|
_ = try PostStore.writeNew(body: "# Mine\n", home: home) // mine-2
|
|
// An external write lands a folder the app didn't create — the next
|
|
// slug pick must see it. (The in-writeNew existence guard covers the
|
|
// race where the folder appears after the pick; that window isn't
|
|
// deterministically reachable from a black-box test.)
|
|
let racedDir = home.appendingPathComponent("posts/mine-3", isDirectory: true)
|
|
try FileManager.default.createDirectory(at: racedDir, withIntermediateDirectories: true)
|
|
try "external".write(to: racedDir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
|
#expect(PostStore.availableSlug(for: "Mine", in: home.appendingPathComponent("posts")) == "mine-4")
|
|
_ = try PostStore.writeNew(body: "# Mine\n", home: home)
|
|
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("posts/mine-4/post.md").path))
|
|
}
|
|
|
|
@Test func `rename to same slug only retitles`() throws {
|
|
let home = try storeHome()
|
|
let original = try PostStore.writeNew(body: "# Same Name\n\nBody.", home: home)
|
|
// Title slugs to same id → rewrite # line, no move.
|
|
let moved = try PostStore.rename(original, to: "Same name!", home: home)
|
|
#expect(moved.id == "same-name")
|
|
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("posts/same-name/post.md").path))
|
|
}
|
|
|
|
@Test func `rename collision takes -2`() throws {
|
|
let home = try storeHome()
|
|
let alpha = try PostStore.writeNew(body: "# Alpha\n", home: home)
|
|
try PostStore.writeNew(body: "# Beta\n", home: home)
|
|
let moved = try PostStore.rename(alpha, to: "Beta", home: home)
|
|
#expect(moved.id == "beta-2")
|
|
let renamed = try readPost(home, "beta-2")
|
|
#expect(renamed.hasPrefix("---\npass_state: First Draft\norigin: writer\n---\n# Beta"))
|
|
}
|
|
|
|
@Test func `trash moves folder out of posts recoverably`() throws {
|
|
let home = try storeHome()
|
|
let post = try PostStore.writeNew(body: "# Gone Soon\n", home: home)
|
|
try PostStore.trash(post, home: home)
|
|
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("posts/\(post.id)").path))
|
|
#expect(Post.scan(home: home).isEmpty)
|
|
}
|
|
|
|
@Test func `overwrite preserves pass_state and origin normalizing bare posts`() throws {
|
|
let home = try storeHome()
|
|
// A bare corpus post on disk (no frontmatter).
|
|
let dir = home.appendingPathComponent("posts/bare-one", isDirectory: true)
|
|
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
|
try "# Bare\n\nCorpus body.".write(to: dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
|
|
|
try PostStore.overwrite(id: "bare-one", body: "# Bare\n\nEdited body.", home: home)
|
|
|
|
let file = try readPost(home, "bare-one")
|
|
#expect(file == "---\npass_state: First Draft\norigin: writer\n---\n# Bare\n\nEdited body.")
|
|
let scanned = try #require(Post.scan(home: home).first)
|
|
#expect(scanned.passState == .firstDraft)
|
|
#expect(scanned.origin == .writer)
|
|
}
|
|
|
|
@Test func `overwrite preserves existing pass_state and origin values`() throws {
|
|
let home = try storeHome { home in
|
|
let dir = home.appendingPathComponent("posts/stamped", isDirectory: true)
|
|
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
|
try "---\npass_state: Pass-1 Done\norigin: ai\n---\n# Stamped\n\nBody."
|
|
.write(to: dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
|
}
|
|
|
|
try PostStore.overwrite(id: "stamped", body: "# Stamped\n\nNew body.", home: home)
|
|
|
|
let post = try #require(Post.scan(home: home).first)
|
|
#expect(post.passState == .pass1Done)
|
|
#expect(post.origin == .ai)
|
|
}
|
|
|
|
@Test func `readBody strips frontmatter for editing`() throws {
|
|
let home = try storeHome()
|
|
_ = try PostStore.writeNew(body: "# Editable\n\nSome text.", home: home)
|
|
#expect(PostStore.readBody(id: "editable", home: home) == "# Editable\n\nSome text.")
|
|
#expect(PostStore.readBody(id: "missing", home: home) == nil)
|
|
}
|
|
|
|
@Test func `writeNew failure does not leave a half post`() throws {
|
|
// A file where the posts dir should go: writeNew must fail cleanly.
|
|
let home = try storeHome()
|
|
let postsPath = home.appendingPathComponent("posts").path
|
|
try "blocker".write(to: URL(fileURLWithPath: postsPath), atomically: true, encoding: .utf8)
|
|
#expect(throws: Error.self) {
|
|
_ = try PostStore.writeNew(body: "# Blocked\n", home: home)
|
|
}
|
|
}
|