feat(library): projects & archive UI — S2 (#108)
CI / ci (push) Canceled after 0s

Variant B per the #104 prototype verdict: the library column groups
pieces by project (serif lowercase heads, collapse toggle, Pass-State
groups inside, 2px edge-bar selection shared with the new Archive
pane via LibraryPostRow). Move to Project / Archive / Move Back /
whole-project Unarchive are folder moves — post.md bytes untouched,
never clobbering, ids globally unique. File ▸ New Post ⌘N and New
Project… ⌥⌘N (CommandGroup, never CommandMenu per #84).

Every folder move flushes pending edits first and aborts when the
write failed — nothing moves without its text. Project membership and
the archived-refusal derive from freshly resolved disk paths, never
the scan snapshot. Slug and project-name comparisons fold through one
canonical key (NFC + lowercase). Two reviewer fanouts this session;
their findings are all fixed or visibly parked.

144/144 tests green (run twice), swiftformat/swiftlint clean.

Closes #108
This commit is contained in:
AlexKucera committed 2026-10-13 22:18:24 +02:00
1 parent 9da3a07f4a
commit e55747e7fe
15 files changed
+1205 -68

No files matched your search

+203
View File
@@ -6,6 +6,7 @@
// 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.
// #108: project moves, archive/restore, project unarchive + creation.
//
import Foundation
@@ -289,3 +290,205 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
_ = try PostStore.writeNew(body: "# Blocked\n", home: home)
}
}
// MARK: Project moves & archive (#108)
/// readPost's path-aware sibling: any piece under any root/project.
private func readPiece(_ home: URL, root: String, project: String, _ id: String) throws -> String {
try String(
contentsOf: home.appendingPathComponent("\(root)/\(project)/\(id)/post.md", isDirectory: false),
encoding: .utf8,
)
}
@Test func `move sends piece to another project`() throws {
let home = try storeHome()
let post = try PostStore.writeNew(body: "# Traveler\n\nBody.", project: "essays", home: home)
let original = try readPiece(home, root: "projects", project: "essays", post.id)
try PostStore.move(post, to: "posts", home: home)
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/essays/\(post.id)").path))
#expect(try readPiece(home, root: "projects", project: "posts", post.id) == original) // bytes untouched
let scanned = try #require(Post.scan(home: home).first)
#expect(scanned.project == "posts")
#expect(!scanned.isArchived)
// Same-project confirm in the picker is a no-op, not a "file exists" error.
try PostStore.move(post, to: "posts", home: home)
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts/\(post.id)/post.md").path))
}
@Test func `archive and restore round-trip a piece`() throws {
let home = try storeHome()
let post = try PostStore.writeNew(body: "# Round Trip\n", home: home)
try PostStore.archive(post, home: home)
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("archive/posts/\(post.id)/post.md").path))
var scanned = try #require(Post.scan(home: home).first)
#expect(scanned.isArchived)
#expect(scanned.project == "posts")
try PostStore.restore(post, home: home)
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts/\(post.id)/post.md").path))
scanned = try #require(Post.scan(home: home).first)
#expect(!scanned.isArchived)
#expect(scanned.project == "posts")
}
@Test func `move refuses an archived piece`() throws {
let home = try storeHome()
let post = try PostStore.writeNew(body: "# No Escape\n", home: home)
try PostStore.archive(post, home: home)
#expect(throws: CocoaError(.featureUnsupported)) {
try PostStore.move(post, to: "essays", home: home)
}
// Still where archive left it.
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("archive/posts/\(post.id)/post.md").path))
}
@Test func `unarchiveProject moves the whole project folder`() throws {
let home = try storeHome { home in
try FileManager.default.createDirectory(
at: home.appendingPathComponent("archive/awards/laurel", isDirectory: true),
withIntermediateDirectories: true,
)
try "# Laurel\n".write(
to: home.appendingPathComponent("archive/awards/laurel/post.md"),
atomically: true,
encoding: .utf8,
)
}
try PostStore.unarchiveProject("awards", home: home)
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/awards/laurel/post.md").path))
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("archive/awards").path))
let scanned = try #require(Post.scan(home: home).first)
#expect(scanned.id == "laurel")
#expect(scanned.project == "awards")
#expect(!scanned.isArchived)
}
@Test func `unarchiveProject merges when the project exists`() throws {
let home = try storeHome { home in
for path in ["projects/awards/kept", "archive/awards/back"] {
try FileManager.default.createDirectory(
at: home.appendingPathComponent(path, isDirectory: true),
withIntermediateDirectories: true,
)
try "# Piece\n".write(
to: home.appendingPathComponent("\(path)/post.md"),
atomically: true,
encoding: .utf8,
)
}
}
try PostStore.unarchiveProject("awards", home: home)
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/awards/kept/post.md").path))
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/awards/back/post.md").path))
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("archive/awards").path))
#expect(Post.scan(home: home).count == 2)
#expect(Post.scan(home: home).allSatisfy { !$0.isArchived && $0.project == "awards" })
}
@Test func `unarchiveProject skips finder dotfiles in the merge`() throws {
let home = try storeHome { home in
for path in ["projects/awards/kept", "archive/awards/back"] {
try FileManager.default.createDirectory(
at: home.appendingPathComponent(path, isDirectory: true),
withIntermediateDirectories: true,
)
try "# Piece\n".write(
to: home.appendingPathComponent("\(path)/post.md"),
atomically: true,
encoding: .utf8,
)
// Finder drops these when it browses a folder.
try "junk".write(
to: home.appendingPathComponent("\(path)/.DS_Store"),
atomically: true,
encoding: .utf8,
)
}
}
try PostStore.unarchiveProject("awards", home: home)
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/awards/back/post.md").path))
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("archive/awards").path)) // the .DS_Stores didn't wedge it open
#expect(Post.scan(home: home).count == 2)
}
@Test func `unarchiveProject keeps hidden user folders instead of destroying them`() throws {
// removeItem is permanent — a Finder-hidden folder inside the archived
// project must survive the unarchive, not die with the emptied source.
let home = try storeHome { home in
for path in ["projects/awards/kept", "archive/awards/back"] {
try FileManager.default.createDirectory(
at: home.appendingPathComponent(path, isDirectory: true),
withIntermediateDirectories: true,
)
try "# Piece\n".write(
to: home.appendingPathComponent("\(path)/post.md"),
atomically: true,
encoding: .utf8,
)
}
try FileManager.default.createDirectory(
at: home.appendingPathComponent("archive/awards/.research", isDirectory: true),
withIntermediateDirectories: true,
)
}
try PostStore.unarchiveProject("awards", home: home)
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/awards/back/post.md").path))
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("archive/awards/.research").path)) // survived
}
@Test func `slug collisions fold unicode normalization`() throws {
// Decomposed-jamo folder on disk vs a precomposed title — one folder to
// normalization-insensitive APFS; the fold makes the -2 fire instead of
// the TOCTOU guard surfacing a baffling "file exists".
let decomposed = "한국".decomposedStringWithCanonicalMapping
let home = try storeHome { home in
try FileManager.default.createDirectory(
at: home.appendingPathComponent("projects/posts/\(decomposed)", isDirectory: true),
withIntermediateDirectories: true,
)
}
#expect(!decomposed.utf8.elementsEqual("한국".utf8)) // two byte sequences, one name — Swift ==/Set already fold; the explicit key pins the contract
#expect(PostStore.availableSlug(for: "한국", home: home) == "한국-2")
}
@Test func `createProject materializes and validates`() throws {
let home = try storeHome()
try PostStore.createProject(named: "foo", home: home)
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/foo").path))
#expect(throws: CocoaError(.fileWriteFileExists)) {
try PostStore.createProject(named: "FOO", home: home) // case-insensitive duplicate
}
// #108 audit 2: NFD duplicate — "café" typed/pasted decomposed reads
// identical but is one folder on normalization-insensitive APFS.
try PostStore.createProject(named: "café", home: home) // NFC variant first
#expect(throws: CocoaError(.fileWriteFileExists)) {
try PostStore.createProject(named: "cafe\u{0301}", home: home) // NFD twin
}
#expect(throws: CocoaError(.fileWriteInvalidFileName)) {
try PostStore.createProject(named: "a:b", home: home) // Finder shows ":" as "/"
}
#expect(throws: CocoaError(.fileWriteInvalidFileName)) {
try PostStore.createProject(named: " ", home: home) // empty after trimming
}
#expect(throws: CocoaError(.fileWriteInvalidFileName)) {
try PostStore.createProject(named: "a/b", home: home) // path separator
}
#expect(throws: CocoaError(.fileWriteInvalidFileName)) {
try PostStore.createProject(named: ".x", home: home) // hidden — scan skips dot-dirs
}
}