feat(library): projects & archive substrate — S1 (#107)
CI / ci (push) Canceled after 0s
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:
1 parent
50eecc6dcb
commit
3ca9d4e601
17 files changed
+608
-197
No files matched your search
@@ -20,13 +20,13 @@ Owned by the root AGENTS.md contract.
|
||||
- `EngineGateTests.swift` builds fake `hermes` shell scripts in temp dirs and runs them through the real `Process` path — copy that pattern for any future child-process testing.
|
||||
- `ACPClientTests.swift` extends it into a full fake ACP *server*: a zsh script speaking real NDJSON JSON-RPC over stdio (`makeFakeACP`), with a per-binary mode file (`crashAfterHandshake`, `hangOnPrompt`, default full) and a base64-encoded fix-card payload — shell quoting can never break the wire JSON. Only `acpTempDir` remains as a helper; crash assertions use Swift Testing's `confirmation` (hook the callback BEFORE triggering the crash). `makeStrictFakeACP` (same file) guards `session/new` the way adapter ≥0.21.5 does — rejecting a missing `mcpServers` with `-32602` — pinning the wire contract the lenient fake can't catch.
|
||||
- `ProfileProvisionerTests.swift` extends it: the fake creates the profile at `<binaryDir>/profiles/<name>` (self-locating, race-free under parallel tests); payload dirs are built at runtime; the bundled-payload test asserts manifest ↔ payload correspondence. #48 failure-path tests live in `ProfileProvisionerFailureTests.swift` (own file — Not-Now defer + re-fail, config.yaml byte-identity + fresh-profile no-config (mcp_servers negative invariant), state.json engine-gate-only, copy-phase catch branch, path-traversal rejection, orphaned-temp sweep). Shared helpers (`makeFakeHermes`, `makePayload`, `defaultSkills`, `provTempDir`) are internal in `ProfileProvisionerTests.swift`. `ProfileBackupTests.swift` (own helpers `backupTempDir`/`makeProfileFixture`) injects profile/backups roots, a fixed clock, and a `psScan` stub — a dev machine may run the real engine, so the ps scan is never trusted in tests.
|
||||
- `PostLibraryTests.swift` (#17) — throwaway Homes via `makeHome`/`writePost` in temp dirs: bare corpus posts (First Draft/writer defaults), lenient + quoted frontmatter (`pass_state`/`origin`), lazy folders / hidden dirs / symlinks / noise not surfacing, localizedStandardCompare title sort, `untitled` edges (blank, marker-only, unterminated frontmatter), leading-blank-line frontmatter, default Home = `~/Documents/Museion`.
|
||||
- `PostStoreTests.swift` (#18) — write side via `storeHome`/`readPost` (same temp-dir pattern): slug algorithm cases (diacritics, CJK, cap, punctuation-only → untitled), lazy first write + #59 stamp round-trip through `Post.scan`, untitled fallback, case-insensitive collision suffixes, rename (retitle + move, insert-title path, same-slug retitle-only, collision -2), trash removes from posts/, overwrite preservation + bare-post normalization, `readBody` stripping, blocked-posts failure path.
|
||||
- `PostLibraryTests.swift` (#17, #107) — throwaway Homes via `makeHome`/`writePiece`/`writePost` in temp dirs: bare corpus posts (First Draft/writer defaults), lenient + quoted frontmatter (`pass_state`/`origin`/`type`), two-level scan across `projects/<name>/<id>/` + `archive/<name>/<id>/` (project/isArchived stamped, noise at both levels), lazy folders / hidden dirs / symlinks not surfacing, localizedStandardCompare title sort, `untitled` edges (blank, marker-only, unterminated frontmatter), leading-blank-line frontmatter, default Home = `~/Documents/Museion`.
|
||||
- `PostStoreTests.swift` (#18, #107) — write side via `storeHome`/`readPost` (same temp-dir pattern): slug algorithm cases (diacritics, CJK, cap, punctuation-only → untitled), lazy first write into `projects/<name>/<id>/` + #59 stamp (type first) round-trip through `Post.scan`, untitled fallback, case-insensitive collision suffixes over the GLOBAL slug space (projects + archive; archived slugs never reused), `writeNew(project:)` routing, rename (retitle + move within the same project, archived stays archived, insert-title path, same-slug retitle-only, collision -2), trash removes from projects/ and archive/, overwrite preservation + bare-post normalization, `readBody` stripping, blocked-project failure path.
|
||||
- `EditorSessionTests.swift` (#19) — session mechanics via `makeSessionHome`/`seedPost`: debounce → flush + sidecar round-trip, clean-flush no-op + ⌘S immediate, #67 conflict (echo suppressed, Yes-reload, No-keep-local → overwrite), write failure → `.failed` + retry.
|
||||
- `ReadingPresenterTests.swift` (#76, ADR-0011) — apex HTML pipeline coverage with inline fixture strings: chunker sentinel stamping, stamper attribute rewrite (multi-digit), `blocks(for:)` line↔range map (monotonic, 1-based lines, exact chunk offsets — the click→edit map), pipeline tests running the REAL bundled apex binary (headings/inline marks, `1. 1. 1.` renumber via native `<ol>`, mixed-lists stay split, table/quote/fence/link/HR constructs with exact `data-source-line` stamps, base64 image embed against `spike/prose`, native footnotes), fallback = escaped source, stylesheet carries the slot CSS vars.
|
||||
- `ProsePresenterTests.swift` (#77) — editing-surface parity pinned: heading ladder sizes (30/22/19/17-bold), half-height blanks (9pt), quote secondary-ink + indent, fence interior mono-14, inline traits (bold/italic/code/link underline + dimmed marks), CSS-1.8 leading calibration tolerance, CommonMark intraword-underscore rule, reattribute with fence-state retrofit.
|
||||
- `FocusModeTests.swift` (#21, map #21) — soft-centering band math pinned: in-band no-op, below-band recenter down, above-band recenter up, document-bottom clamp (parks, then no-ops while parked — no jump), short-document floor at 0.
|
||||
- `ImportExternalTests.swift` (#22, map #22) — import + external buffer via `importHome`/`writeExternal` (temp-dir convention): #95 frontmatter merge (external keys verbatim, our two keys win, original untouched), CRLF file normalized at the boundary (block detected, keys stamped, LF body), bare file → fresh block + #60 collision suffixes, empty file → `untitled`, unreadable file (UTF-16) → `loadFailed`, nothing written, original untouched, external session saves in place with NO sidecar (#93), #67 conflict raised on a foreign write + Yes-reload.
|
||||
- `ImportExternalTests.swift` (#22, map #22) — import + external buffer via `importHome`/`writeExternal` (temp-dir convention): #95 frontmatter merge (external keys verbatim, our keys win — external `type: outline` overwritten to `type: post`, original untouched), CRLF file normalized at the boundary (block detected, keys stamped, LF body), bare file → fresh block + #60 collision suffixes, empty file → `untitled`, unreadable file (UTF-16) → `loadFailed`, nothing written, original untouched, external session saves in place with NO sidecar (#93), #67 conflict raised on a foreign write + Yes-reload. Imports land at `projects/posts/<id>/post.md` (#107).
|
||||
|
||||
## Verification
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ private func makeSessionHome() -> URL {
|
||||
|
||||
@MainActor
|
||||
private func seedPost(_ home: URL, id: String, body: String) {
|
||||
let dir = home.appendingPathComponent("posts/\(id)", isDirectory: true)
|
||||
let dir = home.appendingPathComponent("projects/posts/\(id)", isDirectory: true)
|
||||
try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
||||
try? PostStore.stamped(body: body, passState: .firstDraft, origin: .writer)
|
||||
.write(to: dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
||||
@@ -132,7 +132,7 @@ struct EditorSessionTests {
|
||||
// #83: Keep Local with the post vanished mid-alert — the session must
|
||||
// stay dirty (never a lying "Saved"), ⌘S still attempts the write.
|
||||
try? FileManager.default.removeItem(
|
||||
at: home.appendingPathComponent("posts/eta", isDirectory: true),
|
||||
at: home.appendingPathComponent("projects/posts/eta", isDirectory: true),
|
||||
)
|
||||
session.diskDidChange(diskText: "# Eta\n\nExternal 2.") // alert raised; disk now unreadable
|
||||
session.keepLocal()
|
||||
@@ -148,7 +148,7 @@ struct EditorSessionTests {
|
||||
|
||||
// Vanish the post: overwrite throws (no resurrection), state = failed.
|
||||
try FileManager.default.removeItem(
|
||||
at: home.appendingPathComponent("posts/delta", isDirectory: true),
|
||||
at: home.appendingPathComponent("projects/posts/delta", isDirectory: true),
|
||||
)
|
||||
var reported: String?
|
||||
session.onError = { reported = $0 }
|
||||
@@ -210,4 +210,15 @@ struct EditorSessionTests {
|
||||
#expect(PostStore.readBody(id: "zeta", home: home) == "# Zeta\n\nReworked.") // unwritten
|
||||
#expect(session.saveState != .failed) // no spurious failure fired
|
||||
}
|
||||
|
||||
@Test func `unresolvable id falls back to the default-project path`() {
|
||||
// #107: folderURL is resolved once at init; a piece that exists
|
||||
// nowhere still gets a sane path (image resolution, crumbs) — the
|
||||
// default-project shape, never a crash.
|
||||
let home = makeSessionHome()
|
||||
let session = EditorSession(postID: "ghost", home: home)
|
||||
let expected = home.appendingPathComponent("projects/posts/ghost", isDirectory: true)
|
||||
#expect(session.folderURL == expected)
|
||||
#expect(session.loadFailed) // nothing to read there either
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@ struct ImportExternalTests {
|
||||
#expect(post.id == "my-outside-note")
|
||||
#expect(post.passState == .firstDraft)
|
||||
#expect(post.origin == .ai)
|
||||
let onDisk = try String(contentsOf: home.appendingPathComponent("posts/my-outside-note/post.md"), encoding: .utf8)
|
||||
let onDisk = try String(contentsOf: home.appendingPathComponent("projects/posts/my-outside-note/post.md"), encoding: .utf8)
|
||||
// Our two keys win:
|
||||
#expect(onDisk.contains("pass_state: First Draft"))
|
||||
#expect(onDisk.contains("origin: ai"))
|
||||
@@ -77,7 +77,7 @@ struct ImportExternalTests {
|
||||
let post = try PostStore.importFile(at: ext, origin: .ai, home: home)
|
||||
|
||||
#expect(post.id == "crlf-note")
|
||||
let onDisk = try String(contentsOf: home.appendingPathComponent("posts/crlf-note/post.md"), encoding: .utf8)
|
||||
let onDisk = try String(contentsOf: home.appendingPathComponent("projects/posts/crlf-note/post.md"), encoding: .utf8)
|
||||
#expect(onDisk.contains("pass_state: First Draft"))
|
||||
#expect(onDisk.contains("origin: ai"))
|
||||
#expect(onDisk.contains("date: 2026-10-11"))
|
||||
@@ -113,8 +113,8 @@ struct ImportExternalTests {
|
||||
|
||||
#expect(first.id == "same-title")
|
||||
#expect(second.id == "same-title-2") // #60 machinery
|
||||
let bare = try String(contentsOf: home.appendingPathComponent("posts/same-title/post.md"), encoding: .utf8)
|
||||
#expect(bare.hasPrefix("---\npass_state: First Draft\norigin: writer\n---\n"))
|
||||
let bare = try String(contentsOf: home.appendingPathComponent("projects/posts/same-title/post.md"), encoding: .utf8)
|
||||
#expect(bare.hasPrefix("---\ntype: post\npass_state: First Draft\norigin: writer\n---\n"))
|
||||
}
|
||||
|
||||
@Test func `import empty file is untitled`() throws {
|
||||
@@ -126,6 +126,31 @@ struct ImportExternalTests {
|
||||
#expect(post.title == "untitled")
|
||||
}
|
||||
|
||||
@Test func `import overwrites external type key with post`() throws {
|
||||
let home = try importHome()
|
||||
let ext = try writeExternal("outline.md", """
|
||||
---
|
||||
type: outline
|
||||
date: 2026-10-12
|
||||
---
|
||||
|
||||
# Outside Outline
|
||||
|
||||
Body.
|
||||
""")
|
||||
defer { try? FileManager.default.removeItem(at: ext) }
|
||||
|
||||
let post = try PostStore.importFile(at: ext, origin: .writer, home: home)
|
||||
|
||||
#expect(post.id == "outside-outline")
|
||||
#expect(post.type == .post)
|
||||
let onDisk = try String(contentsOf: home.appendingPathComponent("projects/posts/outside-outline/post.md"), encoding: .utf8)
|
||||
#expect(onDisk.contains("type: post"))
|
||||
#expect(!onDisk.contains("type: outline"))
|
||||
let scanned = try #require(Post.scan(home: home).first)
|
||||
#expect(scanned.type == .post)
|
||||
}
|
||||
|
||||
@Test func `external session saves in place with no sidecar`() async throws {
|
||||
let home = try importHome()
|
||||
let ext = try writeExternal("journal.md", "# Journal\n\nOne.")
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
@@ -2,9 +2,10 @@
|
||||
// PostStoreTests.swift
|
||||
// MuseionTests
|
||||
//
|
||||
// #18: the Post lifecycle write side — slug naming, lazy first write with
|
||||
// the #59 stamp, rename = retitle + move (#60), Trash delete, save-time
|
||||
// normalization.
|
||||
// #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.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
@@ -21,7 +22,7 @@ private func storeHome(_ write: (URL) throws -> Void = { _ in }) throws -> URL {
|
||||
}
|
||||
|
||||
private func readPost(_ home: URL, _ id: String) throws -> String {
|
||||
try String(contentsOf: home.appendingPathComponent("posts/\(id)/post.md"), encoding: .utf8)
|
||||
try String(contentsOf: home.appendingPathComponent("projects/posts/\(id)/post.md"), encoding: .utf8)
|
||||
}
|
||||
|
||||
@Test func `slug algorithm per #60`() {
|
||||
@@ -45,8 +46,11 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
|
||||
#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 == "---\npass_state: First Draft\norigin: writer\n---\n# My First Post\n\nBody.")
|
||||
#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)
|
||||
@@ -61,28 +65,55 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
|
||||
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("posts/untitled/post.md").path))
|
||||
#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("posts/my-post", isDirectory: true),
|
||||
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("posts/My-Post-2", isDirectory: true),
|
||||
at: home.appendingPathComponent("projects/other/My-Post-2", isDirectory: true),
|
||||
withIntermediateDirectories: true,
|
||||
)
|
||||
try "x".write(to: home.appendingPathComponent("posts/my-post/post.md"), atomically: true, encoding: .utf8)
|
||||
try "x".write(to: home.appendingPathComponent("projects/posts/my-post/post.md"), atomically: true, encoding: .utf8)
|
||||
}
|
||||
// existing: my-post, My-Post-2 → next free: my-post-3
|
||||
#expect(PostStore.availableSlug(for: "My Post", in: home.appendingPathComponent("posts")) == "my-post-3")
|
||||
// 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)
|
||||
@@ -90,9 +121,33 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
|
||||
|
||||
#expect(moved.id == "new-name")
|
||||
#expect(moved.title == "New Name")
|
||||
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("posts/old-name").path))
|
||||
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts/old-name").path))
|
||||
let file = try readPost(home, "new-name")
|
||||
#expect(file == "---\npass_state: First Draft\norigin: writer\n---\n# New Name\n\nBody.")
|
||||
#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 {
|
||||
@@ -102,7 +157,7 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
|
||||
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("---\npass_state: First Draft\norigin: writer\n---\n# 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 {
|
||||
@@ -111,7 +166,7 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
|
||||
let moved = try PostStore.rename(original, to: "Errands", home: home)
|
||||
#expect(moved.id == "errands")
|
||||
let file = try readPost(home, "errands")
|
||||
#expect(file == "---\npass_state: First Draft\norigin: writer\n---\n# Errands\nCall mom tomorrow\n\nSecond line.")
|
||||
#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 {
|
||||
@@ -119,7 +174,7 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
|
||||
#expect(throws: CocoaError(.fileReadNoSuchFile)) {
|
||||
try PostStore.overwrite(id: "ghost", body: "# Ghost\n", home: home)
|
||||
}
|
||||
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("posts/ghost").path))
|
||||
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts/ghost").path))
|
||||
}
|
||||
|
||||
@Test func `collision routing consults a fresh listing`() throws {
|
||||
@@ -130,12 +185,12 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
|
||||
// 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("posts/mine-3", isDirectory: true)
|
||||
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", in: home.appendingPathComponent("posts")) == "mine-4")
|
||||
#expect(PostStore.availableSlug(for: "Mine", home: home) == "mine-4")
|
||||
_ = try PostStore.writeNew(body: "# Mine\n", home: home)
|
||||
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("posts/mine-4/post.md").path))
|
||||
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts/mine-4/post.md").path))
|
||||
}
|
||||
|
||||
@Test func `rename to same slug only retitles`() throws {
|
||||
@@ -144,7 +199,7 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
|
||||
// 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("posts/same-name/post.md").path))
|
||||
#expect(FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts/same-name/post.md").path))
|
||||
}
|
||||
|
||||
@Test func `rename collision takes -2`() throws {
|
||||
@@ -154,28 +209,46 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
|
||||
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("---\npass_state: First Draft\norigin: writer\n---\n# Beta"))
|
||||
#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("posts/\(post.id)").path))
|
||||
#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("posts/bare-one", isDirectory: true)
|
||||
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 == "---\npass_state: First Draft\norigin: writer\n---\n# Bare\n\nEdited body.")
|
||||
#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)
|
||||
@@ -183,7 +256,7 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
|
||||
|
||||
@Test func `overwrite preserves existing pass_state and origin values`() throws {
|
||||
let home = try storeHome { home in
|
||||
let dir = home.appendingPathComponent("posts/stamped", isDirectory: true)
|
||||
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)
|
||||
@@ -204,9 +277,13 @@ private func readPost(_ home: URL, _ id: String) throws -> String {
|
||||
}
|
||||
|
||||
@Test func `writeNew failure does not leave a half post`() throws {
|
||||
// A file where the posts dir should go: writeNew must fail cleanly.
|
||||
// A file where the default project dir should go: writeNew must fail cleanly.
|
||||
let home = try storeHome()
|
||||
let postsPath = home.appendingPathComponent("posts").path
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user