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

+91 -9
View File
@@ -2,14 +2,15 @@
// PostLibraryTests.swift
// MuseionTests
//
// #17: the Posts list mirrors <home>/posts/ on disk.
// #17/#107: the Posts list mirrors <home>/projects/<name>/<id>/ and
// <home>/archive/<name>/<id>/ on disk (two-level scan, global slug space).
//
import Foundation
@testable import Museion
import Testing
/// Builds throwaway Homes in a temp dir: posts written bare (corpus shape,
/// Builds throwaway Homes in a temp dir: pieces written bare (corpus shape,
/// no frontmatter) or with frontmatter; noise entries that must not crash or
/// surface as Posts.
private func makeHome(_ write: (URL) throws -> Void) throws -> URL {
@@ -20,12 +21,17 @@ private func makeHome(_ write: (URL) throws -> Void) throws -> URL {
return home
}
private func writePost(_ home: URL, _ id: String, _ body: String) throws {
let dir = home.appendingPathComponent("posts/\(id)", isDirectory: true)
/// Seeds <home>/<root>/<project>/<id>/post.md (root: "projects" or "archive").
private func writePiece(_ home: URL, _ root: String, _ project: String, _ id: String, _ body: String) throws {
let dir = home.appendingPathComponent("\(root)/\(project)/\(id)", isDirectory: true)
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
try body.write(to: dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
}
private func writePost(_ home: URL, _ id: String, _ body: String) throws {
try writePiece(home, "projects", "posts", id, body)
}
@Test func `bare post is first draft writer with heading title`() throws {
let home = try makeHome { home in
try writePost(home, "the-new-reeder", "## The New Reeder\n\nBody text.")
@@ -71,19 +77,62 @@ private func writePost(_ home: URL, _ id: String, _ body: String) throws {
#expect(Post.scan(home: missing).isEmpty)
}
@Test func `scan enumerates two levels across projects and archive`() throws {
let home = try makeHome { home in
try writePiece(home, "projects", "alpha", "one", "## One\n\nBody.")
try writePiece(home, "projects", "beta", "two", "## Two\n\nBody.")
try writePiece(home, "archive", "gamma", "three", "## Three\n\nBody.")
// Noise at the new level: loose file at projects/ root, lazy piece
// folder, hidden project dir.
try "loose".write(
to: home.appendingPathComponent("projects/loose.md"),
atomically: true,
encoding: .utf8,
)
try FileManager.default.createDirectory(
at: home.appendingPathComponent("projects/alpha/not-yet", isDirectory: true),
withIntermediateDirectories: true,
)
try writePiece(home, "projects", ".trash", "four", "## Four\n")
}
let posts = Post.scan(home: home)
#expect(posts.count == 3)
let one = try #require(posts.first { $0.id == "one" })
#expect(one.project == "alpha")
#expect(!one.isArchived)
let two = try #require(posts.first { $0.id == "two" })
#expect(two.project == "beta")
#expect(!two.isArchived)
let three = try #require(posts.first { $0.id == "three" })
#expect(three.project == "gamma")
#expect(three.isArchived)
}
@Test func `type frontmatter is read leniently`() throws {
let home = try makeHome { home in
try writePost(home, "outlined", "---\ntype: outline\n---\n\n## Outlined\n")
try writePost(home, "untyped", "---\npass_state: First Draft\n---\n\n## Plain\n")
try writePost(home, "essay", "---\ntype: essay\n---\n\n## Essay\n")
}
let posts = Post.scan(home: home)
#expect(posts.first { $0.id == "outlined" }?.type == .outline)
#expect(posts.first { $0.id == "untyped" }?.type == .post) // absent key
#expect(posts.first { $0.id == "essay" }?.type == .post) // unknown value
}
@Test func `lazy folders, hidden dirs and noise are not posts`() throws {
let home = try makeHome { home in
try writePost(home, "one", "One")
// Lazy folder: no post.md yet (created lazily on first write, #18).
try FileManager.default.createDirectory(
at: home.appendingPathComponent("posts/not-yet", isDirectory: true),
at: home.appendingPathComponent("projects/posts/not-yet", isDirectory: true),
withIntermediateDirectories: true,
)
// Hidden dir WITH a post.md — must not surface.
try writePost(home, ".trash", "---\n---\n## Hidden\n")
// Noise: loose file at posts/ root.
// Noise: loose file at the piece level.
try "loose".write(
to: home.appendingPathComponent("posts/loose.md"),
to: home.appendingPathComponent("projects/posts/loose.md"),
atomically: true,
encoding: .utf8,
)
@@ -94,7 +143,7 @@ private func writePost(_ home: URL, _ id: String, _ body: String) throws {
@Test func `symlinked post dir is not followed`() throws {
let home = try makeHome { home in
try writePost(home, "one", "One")
// A post.md outside the Home, linked into posts/ by symlink.
// A post.md outside the Home, linked into the project by symlink.
let outside = home.deletingLastPathComponent()
.appendingPathComponent("postlib-outside-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(at: outside, withIntermediateDirectories: true)
@@ -104,7 +153,7 @@ private func writePost(_ home: URL, _ id: String, _ body: String) throws {
encoding: .utf8,
)
try FileManager.default.createSymbolicLink(
at: home.appendingPathComponent("posts/linked", isDirectory: true),
at: home.appendingPathComponent("projects/posts/linked", isDirectory: true),
withDestinationURL: outside,
)
}
@@ -152,3 +201,36 @@ private func writePost(_ home: URL, _ id: String, _ body: String) throws {
.appendingPathComponent("Documents/Museion", isDirectory: true),
)
}
@Test func `duplicate id on disk dedupes to the folder-of winner`() throws {
// Audit: a Finder copy can duplicate an id across projects (or into
// archive/). scan keeps ONE row — the same projects-before-archive,
// name-sorted winner folder(of:) writes to — so the row shown is
// always the piece written.
let home = try makeHome { home in
try writePiece(home, "projects", "beta", "same-id", "## From Beta\n")
try writePiece(home, "projects", "alpha", "same-id", "## From Alpha\n")
try writePiece(home, "archive", "zeta", "same-id", "## From Archive\n")
}
let posts = Post.scan(home: home)
#expect(posts.count == 1)
#expect(posts.first?.project == "alpha") // alpha < beta; archive never wins
#expect(PostStore.folder(of: "same-id", home: home)?
.lastPathComponent == "same-id")
#expect(PostStore.folder(of: "same-id", home: home)?
.pathComponents.suffix(2).first == "alpha")
}
@Test func `hidden project folder never shadows a visible piece`() throws {
// Audit: folder(of:) skips dot-projects like scan does — a piece under
// projects/.trash/ must not capture writes meant for the visible twin.
let home = try makeHome { home in
try writePiece(home, "projects", ".trash", "mine", "## Hidden\n")
try writePiece(home, "projects", "posts", "mine", "## Visible\n")
}
let posts = Post.scan(home: home)
#expect(posts.map(\.id) == ["mine"])
#expect(posts.first?.title == "Visible")
#expect(PostStore.folder(of: "mine", home: home)?
.pathComponents.suffix(2).first == "posts")
}