Files
museion/MuseionTests/PostsModelTests.swift
T
AlexKucera e55747e7fe
CI / ci (push) Canceled after 0s
feat(library): projects & archive UI — S2 (#108)
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
2026-10-13 22:18:24 +02:00

122 lines
4.6 KiB
Swift

//
// PostsModelTests.swift
// MuseionTests
//
// #108 audit: the model-layer lifecycle the store tests can't see —
// archive clearing the open session, move reopening it at the new parent,
// restore keeping the writer in the piece, New Project's draft targeting.
//
import Foundation
@testable import Museion
import Testing
/// Throwaway Home + model (EditorSessionTests convention: direct @MainActor
/// instantiation, no app gate).
@MainActor
private func makeModelHome() throws -> (PostsModel, URL) {
let home = FileManager.default.temporaryDirectory
.appendingPathComponent("postsmodel-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(at: home, withIntermediateDirectories: true)
let model = PostsModel()
model.bind(home: home)
return (model, home)
}
@Test @MainActor func `archiving the open piece clears selection and session`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Gone Soon\n", home: home)
model.select(post.id, home: home)
#expect(model.session?.postID == post.id)
model.archivePost(post, home: home)
#expect(model.selectedPostID == nil)
#expect(model.session == nil)
#expect(model.lastError == nil)
#expect(Post.scan(home: home).first?.isArchived == true)
}
@Test @MainActor func `moving the open piece reopens its session at the new parent`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Traveler\n", project: "essays", home: home)
model.select(post.id, home: home)
#expect(model.session?.folderURL.path.contains("projects/essays") == true)
model.moveToProject(post, project: "posts", home: home)
#expect(model.session?.postID == post.id) // same piece, fresh session
#expect(model.session?.folderURL.path.contains("projects/posts/\(post.id)") == true)
#expect(model.lastError == nil)
}
@Test @MainActor func `restore reopens the session off the archive path`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Revived\n", home: home)
try PostStore.archive(post, home: home)
model.select(post.id, home: home) // opened from the Archive pane
#expect(model.session?.folderURL.path.contains("archive/") == true)
model.restorePost(post, home: home)
#expect(model.selectedPostID == post.id) // the writer stays in it
#expect(model.session?.folderURL.path.contains("projects/posts/\(post.id)") == true)
#expect(model.lastError == nil)
}
@Test @MainActor func `new project draft lands in the named project`() throws {
let (model, home) = try makeModelHome()
try PostStore.createProject(named: "essays", home: home)
model.newProjectPending(named: "essays")
#expect(model.draft?.project == "essays")
model.updateDraft("# Fresh\n")
model.saveDraft(home: home)
let scanned = Post.scan(home: home)
#expect(scanned.first?.project == "essays")
#expect(scanned.first?.id == "fresh")
}
@Test @MainActor func `abandoning the draft for an external file drops the pending target`() throws {
let (model, home) = try makeModelHome()
try PostStore.createProject(named: "essays", home: home)
model.newProjectPending(named: "essays")
#expect(model.draft?.project == "essays")
// The external guest takes the column; the draft dies — its target must too.
let external = home.appendingPathComponent("outside.md")
try "# foreign\n".write(to: external, atomically: true, encoding: .utf8)
model.openExternal(external)
#expect(model.draft == nil)
model.newPost()
#expect(model.draft?.project == Post.defaultProject) // not the abandoned essays
}
@Test @MainActor func `unarchive flushes the dirty open piece before moving`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Keeper\n", home: home)
try PostStore.archive(post, home: home)
model.select(post.id, home: home)
model.session?.edit("# Keeper\n\nDirty.") // inside the debounce window
model.unarchiveProject("posts", home: home)
// The edits landed on disk (readBody resolves by id — through the move).
#expect(PostStore.readBody(id: post.id, home: home) == "# Keeper\n\nDirty.")
#expect(model.lastError == nil)
// The reopen swapped in a fresh session over the same piece.
#expect(model.session?.postID == post.id)
#expect(model.session?.folderURL.path.contains("projects/posts/\(post.id)") == true)
}
@Test @MainActor func `moveTargets lists empty projects from disk`() throws {
let (model, home) = try makeModelHome()
try PostStore.createProject(named: "essays", home: home) // empty — no pieces
#expect(model.moveTargets(home: home) == ["essays", "posts"])
// "posts" exists too (bind created it); a dot-project never shows.
}