// // PostStoreTests.swift // MuseionTests // // #18/#107: the Post lifecycle write side — global slug namespace across // projects/ and archive/, writeNew re-rooted to projects///, // the #59 stamp with type first, rename = retitle + move within the same // project (#60), Trash delete, save-time normalization. // 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 `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//, 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) } }