refactor(posts): one frontmatter read/write codec
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
#180: one PostFrontmatter module now owns the lenient read (parse/split/decoded) and every write posture — fresh stamp, merge-import, update-keys — behind one known-key table (type, pass_state, origin; one insertion order). The three near-twin walkers are deleted byte-identically: PostStore.stamped/splitFrontmatter/ mergedFrontmatter, LearningDecay.stampingFrontmatter, Post.frontmatter(of:) — 21 call sites swept across seven production files and eight test files. The Unaided First Draft eligibility is named once and consumed by both gates: isUnaidedFirstDraftPair (writer ∧ post — the scan's read-back gate) and isCaptureEligible (pair ∧ First Draft — the capture gate at pass invocation). The lenient ?? default stamp decode is extracted with it (the third copy's deferred extraction). One deliberate divergence from the absorbed writer, doc-commented and pinned: mergingImport now preserves hand-edit lines above the opening --- on import where the old split-first shape silently dropped them — lossless through every posture is the ticket's contract. Seven codec pins (PostFrontmatterTests) cover the posture round-trip (foreign keys verbatim through import → save → advance → micropub), the lenient unknown-type decode, and the predicates' truth table. Existing pieces are untouched on disk: every posture moved verbatim and the full suite pins the byte shapes (664/664 green; the one under-load flake in the #179 repoint pin is pre-existing on main, proven by a stashed-tree control run). Closes #180
This commit is contained in:
1 parent
5bf09552f6
commit
218e2db397
22 files changed
+479
-221
No files matched your search
@@ -33,7 +33,7 @@ Conventions every suite follows. Per-suite pin inventories are NOT kept here —
|
||||
- `ScriptedEngine.swift` — #178: the scripted in-process `EngineTransport` adapter, shared payloads, and the scripted rig roots every engine suite builds on.
|
||||
- `EngineSmokeTests` — #178: the ONLY child-spawning suite — one real-process end-to-end wire smoke per LLM Surface (chat on `museion`: handshake/stream/verdict/crash/restart/teardown; pass1/pass2: own engine under its profile, teardown reap, chat engine untouched).
|
||||
- `ProfileProvisionerTests`, `ProfileSurfaceTests`, `ProfileSkillMigrationTests`, `ProfileProvisionerFailureTests`, `ProfileBackupTests` — provisioning, symlink migration, failure paths, backup (one fake-`hermes` harness).
|
||||
- `PostLibraryTests`, `PostStoreTests`, `PostStorePassTests` — scan, write side, pass substrate, stats capture, research notes.
|
||||
- `PostLibraryTests`, `PostStoreTests`, `PostStorePassTests`, `PostFrontmatterTests` — scan, write side, pass substrate, stats capture, research notes, the frontmatter codec (#180: posture round-trips, lenient decode, named eligibility predicates).
|
||||
- `DraftStatsTests`, `TrendViewTests` — text metrics and the trend view's pure facts.
|
||||
- `WritingGoalTests`, `WritingLockTests` — Cold Turkey goal engine + lock machine (kiosk calls no-op under hosted tests by construction — never pinned by driving them).
|
||||
- `MicropubTests`, `MicropubModelTests`, `MicropubMediaTests` — publish wire contract, model flow, media/illustration surface.
|
||||
|
||||
@@ -23,7 +23,7 @@ private func makeSessionHome() -> URL {
|
||||
private func seedPost(_ home: URL, id: String, body: String) {
|
||||
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)
|
||||
try? PostFrontmatter.stamped(body: body, passState: .firstDraft, origin: .writer)
|
||||
.write(to: dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ struct LearningDecayTests {
|
||||
try LearningDecay.dismiss(home: home, typeDir: "posts", file: file, now: now)
|
||||
|
||||
let stampedText = try readLearning(home, file)
|
||||
let frontmatter = Post.frontmatter(of: stampedText)
|
||||
let frontmatter = PostFrontmatter.parse(stampedText)
|
||||
#expect(frontmatter["dismissed"] == FixCardStore.formatted(now, "yyyy-MM-dd"))
|
||||
#expect(frontmatter["dismiss_count"] == "1")
|
||||
#expect(frontmatter["dismissed_at_pass"] == "1")
|
||||
@@ -85,7 +85,7 @@ struct LearningDecayTests {
|
||||
#expect(frontmatter["promoted"] == "2026-10-21")
|
||||
#expect(frontmatter["from_card"] == "6D9DB4F2-82F9-4C64-9C2B-1A21B4E9B5D0")
|
||||
// The body is byte-identical.
|
||||
#expect(PostStore.splitFrontmatter(stampedText).body == PostStore.splitFrontmatter(learning).body)
|
||||
#expect(PostFrontmatter.split(stampedText).body == PostFrontmatter.split(learning).body)
|
||||
// Hidden from the pass rail.
|
||||
#expect(try FixCardStore.learnings(piecePath: piece.path).isEmpty)
|
||||
}
|
||||
@@ -125,7 +125,7 @@ struct LearningDecayTests {
|
||||
#expect(try FixCardStore.learnings(piecePath: piece.path).count == 1) // resurfaced
|
||||
|
||||
try LearningDecay.dismiss(home: home, typeDir: "posts", file: file)
|
||||
let frontmatter = try Post.frontmatter(of: readLearning(home, file))
|
||||
let frontmatter = try PostFrontmatter.parse(readLearning(home, file))
|
||||
#expect(frontmatter["dismiss_count"] == "2")
|
||||
#expect(frontmatter["dismissed_at_pass"] == "10") // re-anchored at the current clock
|
||||
|
||||
@@ -221,7 +221,7 @@ struct LearningDecayTests {
|
||||
#expect(throws: FixCardStore.CardError.self) {
|
||||
try LearningDecay.dismiss(home: hugeHome, typeDir: "posts", file: "20261021-huge.md")
|
||||
}
|
||||
#expect(try Post.frontmatter(of: readLearning(hugeHome, "20261021-huge.md"))["dismiss_count"] == String(Int.max))
|
||||
#expect(try PostFrontmatter.parse(readLearning(hugeHome, "20261021-huge.md"))["dismiss_count"] == String(Int.max))
|
||||
}
|
||||
|
||||
/// Scenario: dismiss is a write-path trust boundary — traversal-shaped
|
||||
@@ -251,7 +251,7 @@ struct LearningDecayTests {
|
||||
// Nothing escaped the learnings tree; the real learning is untouched.
|
||||
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("x.md").path))
|
||||
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("learnings/x.md").path))
|
||||
#expect(try Post.frontmatter(of: readLearning(home, file))["dismiss_count"] == nil)
|
||||
#expect(try PostFrontmatter.parse(readLearning(home, file))["dismiss_count"] == nil)
|
||||
}
|
||||
|
||||
/// Scenario: a pre-planted symlinked learnings component must not
|
||||
@@ -326,7 +326,7 @@ struct LearningDecayTests {
|
||||
#expect(throws: FixCardStore.CardError.self) {
|
||||
try LearningDecay.dismiss(home: home, typeDir: "posts", file: file)
|
||||
}
|
||||
#expect(try Post.frontmatter(of: readLearning(home, file))["dismiss_count"] == "1")
|
||||
#expect(try PostFrontmatter.parse(readLearning(home, file))["dismiss_count"] == "1")
|
||||
|
||||
// A negative hand-edited count clamps to 0 at parse → stamp writes 1.
|
||||
let negHome = try toolsHome()
|
||||
@@ -338,7 +338,7 @@ struct LearningDecayTests {
|
||||
---
|
||||
""")
|
||||
try LearningDecay.dismiss(home: negHome, typeDir: "posts", file: file)
|
||||
#expect(try Post.frontmatter(of: readLearning(negHome, file))["dismiss_count"] == "1")
|
||||
#expect(try PostFrontmatter.parse(readLearning(negHome, file))["dismiss_count"] == "1")
|
||||
}
|
||||
|
||||
/// Scenario: decay is defined per pass-clocked content type — a foreign
|
||||
@@ -360,11 +360,11 @@ struct LearningDecayTests {
|
||||
contentsOf: home.appendingPathComponent("learnings/essays/\(file)"),
|
||||
encoding: .utf8,
|
||||
)
|
||||
#expect(Post.frontmatter(of: text)["dismiss_count"] == nil)
|
||||
#expect(PostFrontmatter.parse(text)["dismiss_count"] == nil)
|
||||
}
|
||||
|
||||
/// Scenario: a Learning with NO frontmatter (a hand-made file) still
|
||||
/// dismisses — stampingFrontmatter prepends a fresh block and the body
|
||||
/// dismisses — the update walk prepends a fresh block and the body
|
||||
/// rides verbatim below it (#27 audit: the prepend branch pinned).
|
||||
@Test func `dismiss prepends a fresh block on a frontmatter-less file`() throws {
|
||||
let home = try toolsHome()
|
||||
@@ -375,14 +375,14 @@ struct LearningDecayTests {
|
||||
try LearningDecay.dismiss(home: home, typeDir: "posts", file: file)
|
||||
|
||||
let stampedText = try readLearning(home, file)
|
||||
let frontmatter = Post.frontmatter(of: stampedText)
|
||||
let frontmatter = PostFrontmatter.parse(stampedText)
|
||||
#expect(frontmatter["dismiss_count"] == "1")
|
||||
#expect(frontmatter["dismissed_at_pass"] == "0")
|
||||
#expect(PostStore.splitFrontmatter(stampedText).body == body)
|
||||
#expect(PostFrontmatter.split(stampedText).body == body)
|
||||
}
|
||||
|
||||
/// Scenario: lines ABOVE the opening `---` (a hand-edit blank —
|
||||
/// Post.frontmatter tolerates one) belong to neither block nor body;
|
||||
/// PostFrontmatter.parse tolerates any count) belong to neither block nor body;
|
||||
/// the stamp keeps them — the "every other line verbatim" contract
|
||||
/// (#27 review pin).
|
||||
@Test func `dismiss preserves lines above the frontmatter block`() throws {
|
||||
@@ -394,7 +394,7 @@ struct LearningDecayTests {
|
||||
|
||||
let stampedText = try readLearning(home, file)
|
||||
#expect(stampedText.hasPrefix("\n---\n")) // the leading blank survived
|
||||
#expect(Post.frontmatter(of: stampedText)["dismiss_count"] == "1")
|
||||
#expect(PostFrontmatter.parse(stampedText)["dismiss_count"] == "1")
|
||||
#expect(stampedText.hasSuffix("\n\n# Blank\n")) // body untouched
|
||||
}
|
||||
}
|
||||
@@ -259,7 +259,7 @@ func textRows(of model: MentorSessionModel) -> [String] {
|
||||
contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8,
|
||||
)
|
||||
// Capture fired at the invocation — the five keys ride post.md…
|
||||
let matter = Post.frontmatter(of: onDisk)
|
||||
let matter = PostFrontmatter.parse(onDisk)
|
||||
#expect(matter["stats_captured"] != nil)
|
||||
#expect(matter["stats_burstiness"] != nil)
|
||||
#expect(matter["stats_type_token_ratio"] != nil)
|
||||
@@ -283,7 +283,7 @@ func textRows(of model: MentorSessionModel) -> [String] {
|
||||
contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8,
|
||||
)
|
||||
// Stats fire at INVOCATION, not completion — a cancelled pass has them anyway…
|
||||
let matter = Post.frontmatter(of: onDisk)
|
||||
let matter = PostFrontmatter.parse(onDisk)
|
||||
#expect(matter["stats_captured"] != nil)
|
||||
#expect(matter["stats_burstiness"] != nil)
|
||||
// …while the Pass State never advanced.
|
||||
@@ -308,7 +308,7 @@ func textRows(of model: MentorSessionModel) -> [String] {
|
||||
// The pass ran (origin alone decides, never the outcome)…
|
||||
#expect(onDisk.contains("pass_state: Pass-1 Done"))
|
||||
// …but nothing was stamped — ai-origin is never stats-eligible.
|
||||
let matter = Post.frontmatter(of: onDisk)
|
||||
let matter = PostFrontmatter.parse(onDisk)
|
||||
#expect(matter["stats_captured"] == nil)
|
||||
#expect(matter["stats_burstiness"] == nil)
|
||||
}
|
||||
@@ -339,7 +339,7 @@ struct DeslopLintWiringTests {
|
||||
#expect(cards.contains { $0.pattern == "utilize" && $0.lane == .mechanical })
|
||||
// Stats capture fired at the same invocation — both hooks, one seam.
|
||||
let onDisk = try String(contentsOf: piece.appendingPathComponent("post.md"), encoding: .utf8)
|
||||
let matter = Post.frontmatter(of: onDisk)
|
||||
let matter = PostFrontmatter.parse(onDisk)
|
||||
#expect(matter["stats_captured"] != nil)
|
||||
#expect(onDisk.contains("pass_state: Pass-1 Done"))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
//
|
||||
// PostFrontmatterTests.swift
|
||||
// MuseionTests
|
||||
//
|
||||
// #180: the Post frontmatter codec — one lenient read, three write
|
||||
// postures (fresh / merge-import / update-keys), one known-key table.
|
||||
// Pins: the foreign-keys-verbatim round trip through every posture
|
||||
// (the class of bug that lost captured stats once, un-reintroducible),
|
||||
// the lenient unknown-type decode, and the two named eligibility
|
||||
// predicates the capture gate and the scan read-back gate share.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
@testable import Museion
|
||||
import Testing
|
||||
|
||||
// Scenario: One frontmatter codec behind every post read and write —
|
||||
// from #180. Given a library whose post.md blocks were written by the
|
||||
// old per-site writers, when any posture (import, save, advance,
|
||||
// capture, publish-stamp) runs over a piece carrying foreign frontmatter
|
||||
// keys, then the one PostFrontmatter engine keeps those keys verbatim,
|
||||
// inserts owned keys in the one known order, reads unknown types
|
||||
// leniently as post, and both eligibility gates agree on the same named
|
||||
// predicates.
|
||||
struct PostFrontmatterTests {
|
||||
/// A block with foreign keys above and below our owned keys — every
|
||||
/// posture must keep `foreign_key: keep-me` byte-identical.
|
||||
private let withForeign = "---\nforeign_key: keep-me\ntype: post\npass_state: First Draft\norigin: writer\n---\n# Body"
|
||||
|
||||
@Test func `fresh stamp is the 59 shape in known-key order`() {
|
||||
#expect(PostFrontmatter.stamped(body: "# T", passState: .firstDraft, origin: .writer)
|
||||
== "---\ntype: post\npass_state: First Draft\norigin: writer\n---\n# T")
|
||||
#expect(PostFrontmatter.stamped(body: "- Root", passState: .pass2Done, origin: .ai, type: .outline)
|
||||
== "---\ntype: outline\npass_state: Pass-2 Done\norigin: ai\n---\n- Root")
|
||||
}
|
||||
|
||||
@Test func `merge-import keeps foreign keys verbatim and ours win in place`() {
|
||||
// External block with foreign keys around ours; `origin` present
|
||||
// (overwritten in place), `type`/`pass_state` absent (inserted
|
||||
// before the closing --- in table order).
|
||||
let external = "---\ntitle: Outside\norigin: ai\ndate: 2026-10-11\n---\n# Body"
|
||||
#expect(PostFrontmatter.mergingImport(text: external, origin: .writer)
|
||||
== "---\ntitle: Outside\norigin: writer\ndate: 2026-10-11\ntype: post\npass_state: First Draft\n---\n# Body")
|
||||
// #95: an external `type: outline` still imports as post.
|
||||
#expect(PostFrontmatter.mergingImport(text: "---\ntype: outline\n---\nB", origin: .writer)
|
||||
== "---\ntype: post\npass_state: First Draft\norigin: writer\n---\nB")
|
||||
// Bare file → the fresh block, body verbatim below.
|
||||
#expect(PostFrontmatter.mergingImport(text: "# Bare", origin: .ai)
|
||||
== "---\ntype: post\npass_state: First Draft\norigin: ai\n---\n# Bare")
|
||||
// #180: a hand-edit blank above the `---` survives import (the old
|
||||
// writer dropped it — lossless through every posture is the contract).
|
||||
#expect(PostFrontmatter.mergingImport(text: "\n---\ntitle: Outside\n---\n# B", origin: .writer)
|
||||
== "\n---\ntitle: Outside\ntype: post\npass_state: First Draft\norigin: writer\n---\n# B")
|
||||
}
|
||||
|
||||
@Test func `update-keys stamps in place and keeps every other line byte-identical`() {
|
||||
// An existing key overwrites in place; foreign keys and the body
|
||||
// never move.
|
||||
#expect(PostFrontmatter.updating(text: withForeign, with: [("pass_state", "Pass-1 Done")])
|
||||
== "---\nforeign_key: keep-me\ntype: post\npass_state: Pass-1 Done\norigin: writer\n---\n# Body")
|
||||
// An absent key inserts before the closing --- (pair order).
|
||||
#expect(PostFrontmatter.updating(text: withForeign, with: [("micropub_url", "https://micro.blog/x")])
|
||||
== "---\nforeign_key: keep-me\ntype: post\npass_state: First Draft\norigin: writer\nmicropub_url: https://micro.blog/x\n---\n# Body")
|
||||
// Bare text → a fresh block prepends, body rides verbatim.
|
||||
#expect(PostFrontmatter.updating(text: "# Bare", with: [("pass_state", "Pass-1 Done")])
|
||||
== "---\npass_state: Pass-1 Done\n---\n# Bare")
|
||||
}
|
||||
|
||||
@Test func `update-keys preserves hand-edit lines above the block`() {
|
||||
// The #27 review contract, codec-level: lines above the opening
|
||||
// --- belong to neither block nor body; the stamp keeps them. The
|
||||
// inserted key lands before the closing --- (after existing keys).
|
||||
#expect(PostFrontmatter.updating(text: "\n---\ntype: post\n---\n# B", with: [("dismiss_count", "1")])
|
||||
.hasPrefix("\n---\ntype: post\ndismiss_count: 1\n---\n"))
|
||||
}
|
||||
|
||||
@Test func `foreign keys ride verbatim through every posture chained`() {
|
||||
// The #180 round-trip pin: import → editor save (owned keys) →
|
||||
// pass advance → micropub stamp, foreign keys intact at every hop.
|
||||
var text = PostFrontmatter.mergingImport(text: "---\nforeign: keep\norigin: ai\n---\n# Chain", origin: .writer)
|
||||
for pairs in [
|
||||
[("type", "post"), ("pass_state", "First Draft"), ("origin", "writer")],
|
||||
[("pass_state", "Pass-1 Done")],
|
||||
[("micropub_url", "https://micro.blog/x")],
|
||||
] {
|
||||
text = PostFrontmatter.updating(text: text, with: pairs)
|
||||
#expect(text.contains("\nforeign: keep\n")) // the line never moved
|
||||
}
|
||||
#expect(PostFrontmatter.parse(text)["foreign"] == "keep")
|
||||
#expect(text.hasSuffix("---\n# Chain")) // body untouched throughout
|
||||
}
|
||||
|
||||
@Test func `unknown type reads leniently as post`() {
|
||||
#expect(PostFrontmatter.decoded(PostFrontmatter.parse("---\ntype: alien\norigin: writer\npass_state: First Draft\n---\nB")).type == .post)
|
||||
// The lenient defaults: a bare legacy corpus post is a normal piece.
|
||||
#expect(PostFrontmatter.decoded([:])
|
||||
== PostFrontmatter.Stamp(type: .post, passState: .firstDraft, origin: .writer))
|
||||
}
|
||||
|
||||
@Test func `eligibility predicates name the pair and the capture gate`() {
|
||||
func matter(type: String? = nil, passState: String? = nil, origin: String? = nil) -> [String: String] {
|
||||
["type": type, "pass_state": passState, "origin": origin].compactMapValues { $0 }
|
||||
}
|
||||
// The immutable pair: writer ∧ post, independent of pass_state —
|
||||
// a writer piece that legitimately advanced still carries its arc.
|
||||
#expect(PostFrontmatter.isUnaidedFirstDraftPair(matter(type: "post", passState: "Pass-1 Done", origin: "writer")))
|
||||
#expect(!PostFrontmatter.isCaptureEligible(matter(type: "post", passState: "Pass-1 Done", origin: "writer")))
|
||||
// Capture: the pair ∧ First Draft.
|
||||
#expect(PostFrontmatter.isCaptureEligible(matter(type: "post", passState: "First Draft", origin: "writer")))
|
||||
#expect(PostFrontmatter.isCaptureEligible([:])) // bare legacy post qualifies
|
||||
// Neither gate opens for ai or outline pieces.
|
||||
#expect(!PostFrontmatter.isUnaidedFirstDraftPair(matter(type: "post", origin: "ai")))
|
||||
#expect(!PostFrontmatter.isCaptureEligible(matter(type: "post", origin: "ai")))
|
||||
#expect(!PostFrontmatter.isUnaidedFirstDraftPair(matter(type: "outline", origin: "writer")))
|
||||
#expect(!PostFrontmatter.isCaptureEligible(matter(type: "outline", origin: "writer")))
|
||||
}
|
||||
}
|
||||
@@ -182,7 +182,7 @@ private func writePost(_ home: URL, _ id: String, _ body: String) throws {
|
||||
@Test func `unterminated frontmatter is untitled, never leaked`() {
|
||||
// No closing ---: frontmatter lines must never leak as the Title.
|
||||
#expect(Post.title(of: "---\npass_state: First Draft\norigin: writer\n") == "untitled")
|
||||
#expect(Post.frontmatter(of: "---\npass_state: First Draft\n")["pass_state"] == "First Draft")
|
||||
#expect(PostFrontmatter.parse("---\npass_state: First Draft\n")["pass_state"] == "First Draft")
|
||||
}
|
||||
|
||||
@Test func `leading blank line still opens frontmatter`() throws {
|
||||
|
||||
@@ -309,7 +309,7 @@ private func threeDecimals(_ value: String) -> Bool {
|
||||
let withForeign = try String(contentsOf: url, encoding: .utf8)
|
||||
.replacingOccurrences(of: "---\ntype: post", with: "---\nforeign_key: keep-me\ntype: post")
|
||||
try withForeign.write(to: url, atomically: true, encoding: .utf8)
|
||||
let bodyBefore = PostStore.splitFrontmatter(withForeign).body
|
||||
let bodyBefore = PostFrontmatter.split(withForeign).body
|
||||
|
||||
let captured = PostStore.captureFirstDraftStats(
|
||||
id: post.id, body: "# Draft\n\nOne two three. Four five! Six?", home: home,
|
||||
@@ -317,7 +317,7 @@ private func threeDecimals(_ value: String) -> Bool {
|
||||
|
||||
#expect(captured)
|
||||
let after = try String(contentsOf: url, encoding: .utf8)
|
||||
let matter = Post.frontmatter(of: after) // the trend-view readiness read
|
||||
let matter = PostFrontmatter.parse(after) // the trend-view readiness read
|
||||
#expect(matter["foreign_key"] == "keep-me")
|
||||
#expect(matter["stats_captured"]?.count == 19) // yyyy-MM-dd'T'HH:mm:ss, lexically sortable
|
||||
#expect(matter["stats_burstiness"].map(threeDecimals) == true)
|
||||
@@ -328,7 +328,7 @@ private func threeDecimals(_ value: String) -> Bool {
|
||||
#expect(matter["pass_state"] == "First Draft")
|
||||
#expect(matter["origin"] == "writer")
|
||||
// Body byte-identical — the stamp never touches content (#31 in-place proof).
|
||||
#expect(PostStore.splitFrontmatter(after).body == bodyBefore)
|
||||
#expect(PostFrontmatter.split(after).body == bodyBefore)
|
||||
}
|
||||
|
||||
@Test func `capture refuses an ai origin piece untouched`() throws {
|
||||
@@ -369,7 +369,7 @@ private func threeDecimals(_ value: String) -> Bool {
|
||||
#expect(PostStore.captureFirstDraftStats(id: post.id, body: "One two three. Four five! Six?", home: home))
|
||||
#expect(PostStore.captureFirstDraftStats(id: post.id, body: "a b c. a b c.", home: home))
|
||||
|
||||
let matter = try Post.frontmatter(of: String(
|
||||
let matter = try PostFrontmatter.parse(String(
|
||||
contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8,
|
||||
))
|
||||
#expect(matter["stats_burstiness"] == "-1.000") // σ = 0 over [3, 3] — the second body won
|
||||
@@ -394,7 +394,7 @@ private func threeDecimals(_ value: String) -> Bool {
|
||||
let after = try String(
|
||||
contentsOf: home.appendingPathComponent("projects/posts/bare-legacy/post.md"), encoding: .utf8,
|
||||
)
|
||||
let matter = Post.frontmatter(of: after)
|
||||
let matter = PostFrontmatter.parse(after)
|
||||
#expect(matter["stats_captured"] != nil)
|
||||
#expect(after.contains("# Bare")) // the body survived the fresh-block prepend
|
||||
}
|
||||
@@ -416,14 +416,14 @@ private func threeDecimals(_ value: String) -> Bool {
|
||||
|
||||
#expect(advanced == .pass1Done)
|
||||
let after = try String(contentsOf: url, encoding: .utf8)
|
||||
let matter = Post.frontmatter(of: after)
|
||||
let matter = PostFrontmatter.parse(after)
|
||||
#expect(matter["pass_state"] == "Pass-1 Done")
|
||||
#expect(matter["stats_captured"] != nil) // survived the advance
|
||||
#expect(matter["stats_trigram_repetition"] != nil)
|
||||
#expect(matter["foreign_key"] == "keep-me") // foreign keys too — the latent #24 loss
|
||||
#expect(matter["origin"] == "writer")
|
||||
#expect(matter["type"] == "post")
|
||||
#expect(PostStore.splitFrontmatter(after).body == "# Kept\n\nBody stays.")
|
||||
#expect(PostFrontmatter.split(after).body == "# Kept\n\nBody stays.")
|
||||
}
|
||||
|
||||
@Test func `overwrite preserves stats and foreign keys while swapping the body`() throws {
|
||||
@@ -442,13 +442,13 @@ private func threeDecimals(_ value: String) -> Bool {
|
||||
try PostStore.overwrite(id: post.id, body: "# Draft\n\nEdited mid-pass words.", home: home)
|
||||
|
||||
let after = try String(contentsOf: url, encoding: .utf8)
|
||||
let matter = Post.frontmatter(of: after)
|
||||
let matter = PostFrontmatter.parse(after)
|
||||
#expect(matter["stats_captured"] != nil) // survived the editor save
|
||||
#expect(matter["foreign_key"] == "keep-me")
|
||||
#expect(matter["pass_state"] == "First Draft")
|
||||
#expect(matter["origin"] == "writer")
|
||||
#expect(matter["type"] == "post")
|
||||
#expect(PostStore.splitFrontmatter(after).body == "# Draft\n\nEdited mid-pass words.") // the save still landed
|
||||
#expect(PostFrontmatter.split(after).body == "# Draft\n\nEdited mid-pass words.") // the save still landed
|
||||
}
|
||||
|
||||
@Test func `overwrite normalizes a bare post with the full stamp`() throws {
|
||||
@@ -465,9 +465,9 @@ private func threeDecimals(_ value: String) -> Bool {
|
||||
let after = try String(
|
||||
contentsOf: home.appendingPathComponent("projects/posts/bare-overwrite/post.md"), encoding: .utf8,
|
||||
)
|
||||
let matter = Post.frontmatter(of: after)
|
||||
let matter = PostFrontmatter.parse(after)
|
||||
#expect(matter["type"] == "post")
|
||||
#expect(matter["pass_state"] == "First Draft")
|
||||
#expect(matter["origin"] == "writer")
|
||||
#expect(PostStore.splitFrontmatter(after).body == "Bare body, edited.")
|
||||
#expect(PostFrontmatter.split(after).body == "Bare body, edited.")
|
||||
}
|
||||
@@ -575,7 +575,7 @@ func readPiece(_ home: URL, root: String, project: String, _ id: String) throws
|
||||
#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")
|
||||
#expect(PostFrontmatter.split(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)
|
||||
@@ -584,7 +584,7 @@ func readPiece(_ home: URL, root: String, project: String, _ id: String) throws
|
||||
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")
|
||||
#expect(PostFrontmatter.split(ladder).body == "# Chapter\n## one\n### nested")
|
||||
|
||||
// a vanished piece throws, never resurrects
|
||||
#expect(throws: CocoaError(.fileReadNoSuchFile)) {
|
||||
|
||||
@@ -404,8 +404,10 @@ private func writeMixedOutline(home: URL) throws -> Post {
|
||||
model.stageConversion(post) // staged clean, no warning shown
|
||||
// a foreign write makes the body mixed while the sheet was up
|
||||
let file = home.appendingPathComponent("projects/posts/\(post.id)/post.md", isDirectory: false)
|
||||
try PostStore.stamped(body: "# Clean\n- one\nA paragraph between bullets.\n- two", passState: .firstDraft, origin: .writer, type: .outline)
|
||||
.write(to: file, atomically: true, encoding: .utf8)
|
||||
try PostFrontmatter.stamped(
|
||||
body: "# Clean\n- one\nA paragraph between bullets.\n- two", passState: .firstDraft, origin: .writer, type: .outline,
|
||||
)
|
||||
.write(to: file, atomically: true, encoding: .utf8)
|
||||
|
||||
model.convertToPost(headingsLadder: true)
|
||||
|
||||
|
||||
@@ -78,8 +78,8 @@ struct TrendViewTests {
|
||||
#expect(abs(captured.stats.trigramRepetition - expected.trigramRepetition) < 0.0005)
|
||||
// The parsed date round-trips through the exact stamp format — the
|
||||
// symmetric-read proof, locale-independent by construction.
|
||||
let onDisk = try Post.frontmatter(
|
||||
of: String(
|
||||
let onDisk = try PostFrontmatter.parse(
|
||||
String(
|
||||
contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"),
|
||||
encoding: .utf8,
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user