feat(library): projects & archive substrate — S1 (#107)
CI / ci (push) Canceled after 0s

Pieces re-root from posts/<id>/ to projects/<name>/<id>/ with an
archive/ twin root (ADR-0012): scan walks both levels, New Piece and
Import land in the selected project else the default posts, the slug
namespace is global across projects and archive (an archived slug is
never reused), and every piece carries type: post frontmatter —
stamped by New/Import, read leniently (absent or unknown reads as
post), the file stays post.md for every content type. Write paths
resolve the folder from the id alone (folder(of:)); sessions and
sidecars never thread (project, id).

Finish-audit hardening: duplicate ids on disk (Finder copy) dedupe in
scan to the same first-match folder(of:) writes to; folder(of:) skips
hidden/symlinked projects like scan does.

Dev posts moved by hand to projects/posts/ — no migration machinery,
the app never reads the old posts/ root (#103). Full suite green
(128 unit + UI), swiftlint clean on changed files.

Closes #107
This commit is contained in:
AlexKucera committed 2026-10-13 17:44:44 +02:00
1 parent 50eecc6dcb
commit 3ca9d4e601
17 files changed
+608 -197

No files matched your search

+104 -27
View File
@@ -2,9 +2,10 @@
// 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.
// #18/#107: the Post lifecycle write side — global slug namespace across
// projects/ and archive/, writeNew re-rooted to projects/<name>/<id>/,
// the #59 stamp with type first, rename = retitle + move within the same
// project (#60), Trash delete, save-time normalization.
//
import Foundation
@@ -21,7 +22,7 @@ private func storeHome(_ write: (URL) throws -> Void = { _ in }) throws -> URL {
}
private func readPost(_ home: URL, _ id: String) throws -> String {
try String(contentsOf: home.appendingPathComponent("posts/\(id)/post.md"), encoding: .utf8)
try String(contentsOf: home.appendingPathComponent("projects/posts/\(id)/post.md"), encoding: .utf8)
}
@Test func `slug algorithm per #60`() {
@@ -45,8 +46,11 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
#expect(post.passState == .firstDraft)
#expect(post.origin == .writer)
#expect(post.title == "My First Post")
#expect(post.project == Post.defaultProject)
#expect(!post.isArchived)
#expect(post.type == .post)
let file = try readPost(home, "my-first-post")
#expect(file == "---\npass_state: First Draft\norigin: writer\n---\n# My First Post\n\nBody.")
#expect(file == "---\ntype: post\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)
@@ -61,28 +65,55 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
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))
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/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),
at: home.appendingPathComponent("projects/posts/my-post", isDirectory: true),
withIntermediateDirectories: true,
)
// The taken set spans every project, not just posts/.
try FileManager.default.createDirectory(
at: home.appendingPathComponent("posts/My-Post-2", isDirectory: true),
at: home.appendingPathComponent("projects/other/My-Post-2", isDirectory: true),
withIntermediateDirectories: true,
)
try "x".write(to: home.appendingPathComponent("posts/my-post/post.md"), atomically: true, encoding: .utf8)
try "x".write(to: home.appendingPathComponent("projects/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")
// existing: posts/my-post, other/My-Post-2 → next free: my-post-3
#expect(PostStore.availableSlug(for: "My Post", home: home) == "my-post-3")
let post = try PostStore.writeNew(body: "# My Post\n", home: home)
#expect(post.id == "my-post-3")
}
@Test func `archived slug is never reused`() throws {
let home = try storeHome { home in
try FileManager.default.createDirectory(
at: home.appendingPathComponent("archive/done/my-post", isDirectory: true),
withIntermediateDirectories: true,
)
try "x".write(to: home.appendingPathComponent("archive/done/my-post/post.md"), atomically: true, encoding: .utf8)
try FileManager.default.createDirectory(
at: home.appendingPathComponent("projects/posts/other-piece", isDirectory: true),
withIntermediateDirectories: true,
)
}
// Archive counts as taken — restore of the archived piece must not collide.
#expect(PostStore.availableSlug(for: "My Post", home: home) == "my-post-2")
}
@Test func `writeNew lands in the given project`() throws {
let home = try storeHome()
let post = try PostStore.writeNew(body: "# Mine\n", project: "essays", home: home)
#expect(post.project == "essays")
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/essays/mine/post.md").path))
let scanned = Post.scan(home: home)
#expect(scanned.count == 1)
#expect(scanned.first?.project == "essays")
}
@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)
@@ -90,9 +121,33 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
#expect(moved.id == "new-name")
#expect(moved.title == "New Name")
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("posts/old-name").path))
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/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.")
#expect(file == "---\ntype: post\npass_state: First Draft\norigin: writer\n---\n# New Name\n\nBody.")
}
@Test func `rename moves within the same project and archive stays archived`() throws {
let home = try storeHome()
// Same project: rename must not bounce the piece to the default project.
let original = try PostStore.writeNew(body: "# Old Essay\n", project: "essays", home: home)
let moved = try PostStore.rename(original, to: "New Essay", home: home)
#expect(moved.project == "essays")
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/essays/new-essay/post.md").path))
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts/new-essay").path))
// Archived piece: stays under archive/<name>/, still flagged archived.
try FileManager.default.createDirectory(
at: home.appendingPathComponent("archive/done/old-thing", isDirectory: true),
withIntermediateDirectories: true,
)
try "---\ntype: post\npass_state: First Draft\norigin: writer\n---\n# Old Thing\n"
.write(to: home.appendingPathComponent("archive/done/old-thing/post.md"), atomically: true, encoding: .utf8)
let archived = try #require(Post.scan(home: home).first { $0.id == "old-thing" })
let renamed = try PostStore.rename(archived, to: "New Thing", home: home)
#expect(renamed.isArchived)
#expect(renamed.project == "done")
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("archive/done/new-thing/post.md").path))
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("archive/done/old-thing").path))
}
@Test func `rename inserts title line when body had none`() throws {
@@ -102,7 +157,7 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
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"))
#expect(file.hasPrefix("---\ntype: post\npass_state: First Draft\norigin: writer\n---\n# Given Title"))
}
@Test func `rename keeps non-heading first line and inserts title above`() throws {
@@ -111,7 +166,7 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
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.")
#expect(file == "---\ntype: post\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 {
@@ -119,7 +174,7 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
#expect(throws: CocoaError(.fileReadNoSuchFile)) {
try PostStore.overwrite(id: "ghost", body: "# Ghost\n", home: home)
}
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("posts/ghost").path))
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts/ghost").path))
}
@Test func `collision routing consults a fresh listing`() throws {
@@ -130,12 +185,12 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
// 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)
let racedDir = home.appendingPathComponent("projects/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")
#expect(PostStore.availableSlug(for: "Mine", home: home) == "mine-4")
_ = try PostStore.writeNew(body: "# Mine\n", home: home)
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("posts/mine-4/post.md").path))
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts/mine-4/post.md").path))
}
@Test func `rename to same slug only retitles`() throws {
@@ -144,7 +199,7 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
// 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))
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts/same-name/post.md").path))
}
@Test func `rename collision takes -2`() throws {
@@ -154,28 +209,46 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
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"))
#expect(renamed.hasPrefix("---\ntype: post\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(!FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts/\(post.id)").path))
#expect(Post.scan(home: home).isEmpty)
}
@Test func `trash resolves pieces in the archive`() throws {
let home = try storeHome { home in
try FileManager.default.createDirectory(
at: home.appendingPathComponent("archive/done/doomed", isDirectory: true),
withIntermediateDirectories: true,
)
try "# Doomed\n".write(
to: home.appendingPathComponent("archive/done/doomed/post.md"),
atomically: true,
encoding: .utf8,
)
}
let doomed = try #require(Post.scan(home: home).first)
try PostStore.trash(doomed, home: home)
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("archive/done/doomed").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)
let dir = home.appendingPathComponent("projects/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.")
#expect(file == "---\ntype: post\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)
@@ -183,7 +256,7 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
@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)
let dir = home.appendingPathComponent("projects/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)
@@ -204,9 +277,13 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
}
@Test func `writeNew failure does not leave a half post`() throws {
// A file where the posts dir should go: writeNew must fail cleanly.
// A file where the default project dir should go: writeNew must fail cleanly.
let home = try storeHome()
let postsPath = home.appendingPathComponent("posts").path
try FileManager.default.createDirectory(
at: home.appendingPathComponent("projects", isDirectory: true),
withIntermediateDirectories: true,
)
let postsPath = home.appendingPathComponent("projects/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)