CI / ci (push) Canceled after 0s
In-place outline→post conversion on the piece: id, folder, Pass State, origin kept, type frontmatter flips to post, body transforms once. The ask picks the transform — Verbatim Bullets (default, lossless) or Headings ladder (roots ##, stepping to the ###### floor, deeper stays bullets), with a warning when the ladder would drop paragraph lines between bullets. The open session flushes first, then reopens routed to prose via proseOverridePostID (lastDraftWasOutline's machinery reversed). An outline that went lossy on disk behind an unwarned ask aborts the convert; an unanswered #67 conflict aborts every flush-first op (shared flushOrAbort seam); rescan no longer hands a same-id reopened session its predecessor's stale disk text; a held #111 guard ask dies with the outline it pointed at. Audit round (finish-audit, three reviewers) included: 13 new pins, 206 tests green, pin discrimination verified. Record: docs/devlog/20261016-issue114-convert-to-post_log.md
560 lines
25 KiB
Swift
560 lines
25 KiB
Swift
//
|
|
// PostStoreTests.swift
|
|
// MuseionTests
|
|
//
|
|
// #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.
|
|
// #108: project moves, archive/restore, project unarchive + creation.
|
|
//
|
|
|
|
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("projects/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")
|
|
#expect(post.project == Post.defaultProject)
|
|
#expect(!post.isArchived)
|
|
#expect(post.type == .post)
|
|
let file = try readPost(home, "my-first-post")
|
|
#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)
|
|
#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("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("projects/posts/my-post", isDirectory: true),
|
|
withIntermediateDirectories: true,
|
|
)
|
|
// The taken set spans every project, not just posts/.
|
|
try FileManager.default.createDirectory(
|
|
at: home.appendingPathComponent("projects/other/My-Post-2", isDirectory: true),
|
|
withIntermediateDirectories: true,
|
|
)
|
|
try "x".write(to: home.appendingPathComponent("projects/posts/my-post/post.md"), atomically: true, encoding: .utf8)
|
|
}
|
|
// 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 `writeNew with type outline stamps scans back and lands in the project`() throws {
|
|
let home = try storeHome()
|
|
let post = try PostStore.writeNew(body: "- Root\n\t- Child", project: "essays", type: .outline, home: home)
|
|
|
|
#expect(post.type == .outline)
|
|
#expect(post.id == "root")
|
|
#expect(post.project == "essays")
|
|
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/essays/root/post.md").path))
|
|
// #59 stamp with type first, outline rawValue.
|
|
#expect(try readPiece(home, root: "projects", project: "essays", post.id)
|
|
== "---\ntype: outline\npass_state: First Draft\norigin: writer\n---\n- Root\n\t- Child")
|
|
|
|
// The #17 reader round-trips the outline type and the project.
|
|
let scanned = try #require(Post.scan(home: home).first)
|
|
#expect(scanned.type == .outline)
|
|
#expect(scanned.project == "essays")
|
|
}
|
|
|
|
@Test func `writeNew default still stamps type post`() throws {
|
|
let home = try storeHome()
|
|
let post = try PostStore.writeNew(body: "# Plain\n", home: home)
|
|
#expect(post.type == .post)
|
|
#expect(try readPost(home, post.id).hasPrefix("---\ntype: post\n"))
|
|
}
|
|
|
|
@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("projects/posts/old-name").path))
|
|
let file = try readPost(home, "new-name")
|
|
#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 {
|
|
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("---\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 {
|
|
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 == "---\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 {
|
|
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("projects/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("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", home: home) == "mine-4")
|
|
_ = try PostStore.writeNew(body: "# Mine\n", home: home)
|
|
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/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("projects/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("---\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("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("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 == "---\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)
|
|
}
|
|
|
|
@Test func `overwrite preserves existing pass_state and origin values`() throws {
|
|
let home = try storeHome { home in
|
|
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)
|
|
}
|
|
|
|
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 default project dir should go: writeNew must fail cleanly.
|
|
let home = try storeHome()
|
|
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)
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|
|
}
|
|
|
|
@Test func `convert flips type transforms the body once and keeps identity`() throws {
|
|
let home = try storeHome()
|
|
let post = try PostStore.writeNew(body: "# Chapter\n- one\n - nested", type: .outline, home: home)
|
|
|
|
// verbatim: body unchanged, type flips, stamp keeps its own values
|
|
try PostStore.convert(id: post.id, headingsLadder: false, home: home)
|
|
let verbatim = try readPost(home, post.id)
|
|
#expect(verbatim.hasPrefix("---\ntype: post\n"))
|
|
#expect(verbatim.contains("pass_state: First Draft"))
|
|
#expect(verbatim.contains("origin: writer"))
|
|
#expect(PostStore.splitFrontmatter(verbatim).body == "# Chapter\n- one\n - nested")
|
|
// folder never moved; scan reads the new type on rescan
|
|
#expect(Post.scan(home: home).first?.id == post.id)
|
|
#expect(Post.scan(home: home).first?.type == .post)
|
|
|
|
// ladder: the headings mapping, preamble above
|
|
try PostStore.convert(id: post.id, headingsLadder: true, home: home)
|
|
let ladder = try readPost(home, post.id)
|
|
#expect(ladder.hasPrefix("---\ntype: post\n"))
|
|
#expect(PostStore.splitFrontmatter(ladder).body == "# Chapter\n## one\n### nested")
|
|
|
|
// a vanished piece throws, never resurrects
|
|
#expect(throws: CocoaError(.fileReadNoSuchFile)) {
|
|
try PostStore.convert(id: "no-such-piece", headingsLadder: true, home: home)
|
|
}
|
|
}
|
|
|
|
@Test func `convert works on an archived piece without moving it`() throws {
|
|
let home = try storeHome()
|
|
let post = try PostStore.writeNew(body: "# Filed\n- one", type: .outline, home: home)
|
|
try PostStore.archive(post, home: home)
|
|
|
|
try PostStore.convert(id: post.id, headingsLadder: true, home: home)
|
|
|
|
let onDisk = try String(contentsOf: home.appendingPathComponent("archive/posts/\(post.id)/post.md"), encoding: .utf8)
|
|
#expect(onDisk.hasPrefix("---\ntype: post\n"))
|
|
#expect(onDisk.contains("## one"))
|
|
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts/\(post.id)").path))
|
|
}
|