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
474 lines
22 KiB
Swift
474 lines
22 KiB
Swift
//
|
||
// PostStorePassTests.swift
|
||
// MuseionTests
|
||
//
|
||
// #147 split (MicropubTests -> MicropubModelTests precedent): the
|
||
// pass-adjacent write sides out of PostStoreTests — that file crossed
|
||
// its file-length ceiling. #24 pass substrate, #29 research notes,
|
||
// #31 first-draft stats capture; the shared helpers (storeHome/
|
||
// readPost/readPiece) live in PostStoreTests.swift.
|
||
//
|
||
// ## Scenario: candidate rail and the keep write
|
||
// From #29 (S8.1/S8.2)
|
||
// Given a piece in the library and the mentor researching mid-pass
|
||
// When the engine delivers findings as submit_research_notes tool calls
|
||
// Then well-formed candidates are accepted with a received count and
|
||
// nothing is written anywhere; malformed ones answer a tool error
|
||
// And the pass prompt names that rail
|
||
// And when the writer keeps one, one plain markdown note with provenance
|
||
// lands in the piece's research/ — nothing else auto-saves.
|
||
//
|
||
|
||
import Foundation
|
||
@testable import Museion
|
||
import Testing
|
||
|
||
// MARK: Pass substrate (#24)
|
||
|
||
@Test func `snapshotPass writes the pre-pass body verbatim under passes`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Snapshot\n\nPre-pass body.", home: home)
|
||
|
||
try PostStore.snapshotPass(id: post.id, kind: .pass1, body: "# Snapshot\n\nPre-pass body.", home: home)
|
||
|
||
let snapshot = try String(
|
||
contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/passes/pass-1.md"),
|
||
encoding: .utf8,
|
||
)
|
||
#expect(snapshot == "# Snapshot\n\nPre-pass body.") // body only, no frontmatter
|
||
}
|
||
|
||
@Test func `snapshotPass re-run overwrites keeping latest per pass type`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Snap\n\nBefore.", home: home)
|
||
|
||
try PostStore.snapshotPass(id: post.id, kind: .pass1, body: "# Snap\n\nBefore.", home: home)
|
||
try PostStore.snapshotPass(id: post.id, kind: .pass1, body: "# Snap\n\nAfter the first pass.", home: home)
|
||
|
||
let snapshot = try String(
|
||
contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/passes/pass-1.md"),
|
||
encoding: .utf8,
|
||
)
|
||
#expect(snapshot == "# Snap\n\nAfter the first pass.")
|
||
}
|
||
|
||
@Test func `pass snapshots coexist per pass type`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Both\n\nBody.", home: home)
|
||
let passes = home.appendingPathComponent("projects/posts/\(post.id)/passes", isDirectory: true)
|
||
|
||
try PostStore.snapshotPass(id: post.id, kind: .pass1, body: "before pass one", home: home)
|
||
try PostStore.snapshotPass(id: post.id, kind: .pass2, body: "before pass two", home: home)
|
||
|
||
#expect(try String(contentsOf: passes.appendingPathComponent("pass-1.md"), encoding: .utf8) == "before pass one")
|
||
#expect(try String(contentsOf: passes.appendingPathComponent("pass-2.md"), encoding: .utf8) == "before pass two")
|
||
}
|
||
|
||
@Test func `snapshotPass on vanished piece throws`() throws {
|
||
let home = try storeHome()
|
||
#expect(throws: CocoaError(.fileReadNoSuchFile)) {
|
||
try PostStore.snapshotPass(id: "ghost", kind: .pass1, body: "x", home: home)
|
||
}
|
||
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts/ghost").path))
|
||
}
|
||
|
||
@Test func `advancePassState stamps Pass-1 Done preserving origin type and body`() throws {
|
||
let home = try storeHome { home in
|
||
let dir = home.appendingPathComponent("projects/essays/essay", isDirectory: true)
|
||
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
||
try "---\ntype: outline\npass_state: First Draft\norigin: ai\n---\n# Essay\n\nArgued body."
|
||
.write(to: dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
||
}
|
||
|
||
let newState = try PostStore.advancePassState(id: "essay", kind: .pass1, home: home)
|
||
|
||
#expect(newState == .pass1Done)
|
||
// Stamp round-trip: pass_state advanced, origin/type/body byte-preserved.
|
||
#expect(try readPiece(home, root: "projects", project: "essays", "essay")
|
||
== "---\ntype: outline\npass_state: Pass-1 Done\norigin: ai\n---\n# Essay\n\nArgued body.")
|
||
#expect(PostStore.readBody(id: "essay", home: home) == "# Essay\n\nArgued body.")
|
||
let scanned = try #require(Post.scan(home: home).first)
|
||
#expect(scanned.passState == .pass1Done)
|
||
#expect(scanned.origin == .ai)
|
||
#expect(scanned.type == .outline)
|
||
}
|
||
|
||
@Test func `advancePassState reaches Pass-2 Done straight from First Draft`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Direct\n\nBody.", home: home)
|
||
// Passes run in any order — Pass 2 first is legal.
|
||
#expect(try PostStore.advancePassState(id: post.id, kind: .pass2, home: home) == .pass2Done)
|
||
#expect(Post.scan(home: home).first?.passState == .pass2Done)
|
||
}
|
||
|
||
@Test func `advancePassState never regresses a higher state`() throws {
|
||
let home = try storeHome { home in
|
||
let dir = home.appendingPathComponent("projects/posts/done-both", isDirectory: true)
|
||
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
||
try "---\ntype: post\npass_state: Pass-2 Done\norigin: writer\n---\n# Done\n\nBody."
|
||
.write(to: dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
||
}
|
||
|
||
// A Pass 1 completing on a Pass-2 Done piece keeps the higher state.
|
||
let newState = try PostStore.advancePassState(id: "done-both", kind: .pass1, home: home)
|
||
|
||
#expect(newState == .pass2Done)
|
||
#expect(Post.scan(home: home).first?.passState == .pass2Done)
|
||
#expect(try readPost(home, "done-both").contains("pass_state: Pass-2 Done"))
|
||
}
|
||
|
||
@Test func `advancePassState is idempotent on re-run`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Again\n\nBody.", home: home)
|
||
_ = try PostStore.advancePassState(id: post.id, kind: .pass1, home: home)
|
||
let first = try readPost(home, post.id)
|
||
|
||
#expect(try PostStore.advancePassState(id: post.id, kind: .pass1, home: home) == .pass1Done)
|
||
#expect(try readPost(home, post.id) == first)
|
||
}
|
||
|
||
@Test func `advancePassState on vanished piece throws`() throws {
|
||
let home = try storeHome()
|
||
#expect(throws: CocoaError(.fileReadNoSuchFile)) {
|
||
_ = try PostStore.advancePassState(id: "ghost", kind: .pass1, home: home)
|
||
}
|
||
}
|
||
|
||
@Test func `stub carries pass name piece path research rail and fix-card reminder`() {
|
||
let pieceURL = URL(fileURLWithPath: "/vault/projects/essays/essay/post.md")
|
||
let stub = PassContext.stub(kind: .pass1, pieceURL: pieceURL, hasResearch: false)
|
||
|
||
#expect(stub.contains("Pass 1 — paragraph level and up."))
|
||
#expect(stub.contains("Piece: /vault/projects/essays/essay/post.md"))
|
||
#expect(!stub.contains("Research notes:"))
|
||
// #29: the typed research rail is named in the closing block — the
|
||
// engine delivers findings, it never writes candidates itself.
|
||
let closing = "Critique the piece for this pass. Fact-check claims where needed — research findings "
|
||
+ "travel as candidate source-note tool calls (submit_research_notes), never written to disk. "
|
||
+ "Deliver the verdict in your reply — findings travel as fix-card tool calls (submit_fix_cards). "
|
||
+ "Do not write to the piece folder."
|
||
#expect(stub.contains(closing))
|
||
#expect(PassContext.stub(kind: .pass2, pieceURL: pieceURL, hasResearch: false)
|
||
.contains("Pass 2 — sentence level and below."))
|
||
}
|
||
|
||
@Test func `stub adds the research line only when research exists`() {
|
||
let pieceURL = URL(fileURLWithPath: "/vault/projects/essays/essay/post.md")
|
||
let research = "/vault/projects/essays/essay/research"
|
||
#expect(PassContext.stub(kind: .pass1, pieceURL: pieceURL, hasResearch: true)
|
||
.contains("Research notes: \(research)"))
|
||
#expect(!PassContext.stub(kind: .pass1, pieceURL: pieceURL, hasResearch: false).contains(research))
|
||
}
|
||
|
||
@Test func `estimateTokens is stub plus body chars over four rounded up`() {
|
||
let pieceURL = URL(fileURLWithPath: "/vault/essay/post.md")
|
||
let body = "# Title\n\nTwo paragraphs of argued prose."
|
||
for kind in PassKind.allCases {
|
||
for hasResearch in [true, false] {
|
||
let total = PassContext.stub(kind: kind, pieceURL: pieceURL, hasResearch: hasResearch).count + body.count
|
||
let expected = total % 4 == 0 ? total / 4 : total / 4 + 1 // ceil — derived independently of the impl
|
||
#expect(PassContext.estimateTokens(kind: kind, body: body, pieceURL: pieceURL, hasResearch: hasResearch)
|
||
== expected)
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: Research notes (#29)
|
||
|
||
/// The keep writes "today" — computed at test time like promote's day
|
||
/// stamp (a midnight rollover mid-suite is tolerated).
|
||
private func researchDay() -> String {
|
||
FixCardStore.formatted(Date(), "yyyyMMdd")
|
||
}
|
||
|
||
@Test func `writeResearchNote lands the pinned markdown shape in research`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Kept\n", home: home)
|
||
|
||
let name = try PostStore.writeResearchNote(
|
||
piecePath: home.appendingPathComponent("projects/posts/\(post.id)", isDirectory: true).path,
|
||
title: "The Long Now",
|
||
url: "https://example.org/long-now",
|
||
quote: "pace layers",
|
||
note: "Supports the slowness claim.",
|
||
home: home,
|
||
)
|
||
|
||
let day = researchDay()
|
||
#expect(name == "\(day)-the-long-now.md")
|
||
let markdown = try String(
|
||
contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/research/\(name)"),
|
||
encoding: .utf8,
|
||
)
|
||
let expected = "---\nsource: https://example.org/long-now\nkept: \(day)\n---\n\n"
|
||
+ "# The Long Now\n\nSupports the slowness claim.\n\n> pace layers\n"
|
||
#expect(markdown == expected)
|
||
}
|
||
|
||
@Test func `writeResearchNote omits absent note and quote sections`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Bare\n", home: home)
|
||
let pieceDir = home.appendingPathComponent("projects/posts/\(post.id)", isDirectory: true)
|
||
|
||
// Both absent: frontmatter + heading, nothing else.
|
||
let name = try PostStore.writeResearchNote(
|
||
piecePath: pieceDir.path, title: "Only A Link", url: "https://example.org/one",
|
||
quote: "", note: "", home: home,
|
||
)
|
||
let bare = try String(contentsOf: pieceDir.appendingPathComponent("research/\(name)"), encoding: .utf8)
|
||
#expect(bare == "---\nsource: https://example.org/one\nkept: \(researchDay())\n---\n\n# Only A Link\n")
|
||
|
||
// Quote without note: the note section goes, the quote stays.
|
||
let quoted = try PostStore.writeResearchNote(
|
||
piecePath: pieceDir.path, title: "Quoted", url: "https://example.org/two",
|
||
quote: "citation needed", note: "", home: home,
|
||
)
|
||
let text = try String(contentsOf: pieceDir.appendingPathComponent("research/\(quoted)"), encoding: .utf8)
|
||
#expect(text.hasSuffix("\n\n# Quoted\n\n> citation needed\n"))
|
||
}
|
||
|
||
@Test func `writeResearchNote collision takes -2`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Twice\n", home: home)
|
||
let piecePath = home.appendingPathComponent("projects/posts/\(post.id)").path
|
||
|
||
let first = try PostStore.writeResearchNote(
|
||
piecePath: piecePath, title: "Same Source", url: "https://example.org/same",
|
||
quote: "", note: "", home: home,
|
||
)
|
||
let second = try PostStore.writeResearchNote(
|
||
piecePath: piecePath, title: "Same Source", url: "https://example.org/same",
|
||
quote: "", note: "", home: home,
|
||
)
|
||
|
||
let day = researchDay()
|
||
#expect(first == "\(day)-same-source.md")
|
||
#expect(second == "\(day)-same-source-2.md")
|
||
}
|
||
|
||
@Test func `writeResearchNote accepts the post.md file form`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# File Form\n", home: home)
|
||
|
||
let name = try PostStore.writeResearchNote(
|
||
piecePath: home.appendingPathComponent("projects/posts/\(post.id)/post.md").path,
|
||
title: "Via File", url: "https://example.org/file",
|
||
quote: "", note: "", home: home,
|
||
)
|
||
|
||
#expect(FileManager.default.fileExists(
|
||
atPath: home.appendingPathComponent("projects/posts/\(post.id)/research/\(name)").path,
|
||
))
|
||
}
|
||
|
||
@Test func `writeResearchNote on vanished piece throws`() throws {
|
||
let home = try storeHome()
|
||
#expect(throws: FixCardStore.CardError(message: "piece not found: /nowhere/post.md")) {
|
||
try PostStore.writeResearchNote(
|
||
piecePath: "/nowhere/post.md", title: "Ghost", url: "https://example.org",
|
||
quote: "", note: "", home: home,
|
||
)
|
||
}
|
||
}
|
||
|
||
@Test func `writeResearchNote refuses a symlinked research dir`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Guarded\n", home: home)
|
||
let pieceDir = home.appendingPathComponent("projects/posts/\(post.id)", isDirectory: true)
|
||
let target = home.appendingPathComponent("elsewhere", isDirectory: true)
|
||
try FileManager.default.createDirectory(at: target, withIntermediateDirectories: true)
|
||
try FileManager.default.createSymbolicLink(
|
||
at: pieceDir.appendingPathComponent("research"),
|
||
withDestinationURL: target,
|
||
)
|
||
|
||
let planted = pieceDir.appendingPathComponent("research").path
|
||
#expect(throws: FixCardStore.CardError(message: "research path is a symlink: \(planted)")) {
|
||
try PostStore.writeResearchNote(
|
||
piecePath: pieceDir.path, title: "Planted", url: "https://example.org",
|
||
quote: "", note: "", home: home,
|
||
)
|
||
}
|
||
#expect(try FileManager.default.contentsOfDirectory(atPath: target.path).isEmpty)
|
||
}
|
||
|
||
// MARK: First-draft stats capture (#31, S5.1)
|
||
|
||
/// Every `stats_` value is the store-time `%.3f` shape (a period, never a
|
||
/// localized comma) — the trend view (S5.2) parses them back as Doubles.
|
||
private func threeDecimals(_ value: String) -> Bool {
|
||
value.range(of: #"^-?\d+\.\d{3}$"#, options: .regularExpression) != nil
|
||
}
|
||
|
||
@Test func `capture stamps five stats keys in place and preserves foreign frontmatter`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Draft\n\nOne two three. Four five! Six?", home: home)
|
||
let url = home.appendingPathComponent("projects/posts/\(post.id)/post.md", isDirectory: false)
|
||
// Hand-add a foreign key INSIDE the block before capturing — the
|
||
// in-place stamp must keep it byte-identical.
|
||
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 = PostFrontmatter.split(withForeign).body
|
||
|
||
let captured = PostStore.captureFirstDraftStats(
|
||
id: post.id, body: "# Draft\n\nOne two three. Four five! Six?", home: home,
|
||
)
|
||
|
||
#expect(captured)
|
||
let after = try String(contentsOf: url, encoding: .utf8)
|
||
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)
|
||
#expect(matter["stats_type_token_ratio"].map(threeDecimals) == true)
|
||
#expect(matter["stats_sentence_variation"].map(threeDecimals) == true)
|
||
#expect(matter["stats_trigram_repetition"].map(threeDecimals) == true)
|
||
#expect(matter["type"] == "post")
|
||
#expect(matter["pass_state"] == "First Draft")
|
||
#expect(matter["origin"] == "writer")
|
||
// Body byte-identical — the stamp never touches content (#31 in-place proof).
|
||
#expect(PostFrontmatter.split(after).body == bodyBefore)
|
||
}
|
||
|
||
@Test func `capture refuses an ai origin piece untouched`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Machine\n\nAI-born text.", origin: .ai, home: home)
|
||
let url = home.appendingPathComponent("projects/posts/\(post.id)/post.md")
|
||
let before = try String(contentsOf: url, encoding: .utf8)
|
||
|
||
#expect(!PostStore.captureFirstDraftStats(id: post.id, body: "x", home: home))
|
||
#expect(try String(contentsOf: url, encoding: .utf8) == before)
|
||
}
|
||
|
||
@Test func `capture refuses a piece past first draft`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Done Once\n\nAlready critiqued.", home: home)
|
||
try PostStore.advancePassState(id: post.id, kind: .pass1, home: home)
|
||
let url = home.appendingPathComponent("projects/posts/\(post.id)/post.md")
|
||
let before = try String(contentsOf: url, encoding: .utf8)
|
||
|
||
#expect(!PostStore.captureFirstDraftStats(id: post.id, body: "x", home: home))
|
||
#expect(try String(contentsOf: url, encoding: .utf8) == before)
|
||
}
|
||
|
||
@Test func `capture refuses an outline piece`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "- an outline item", type: .outline, home: home)
|
||
let url = home.appendingPathComponent("projects/posts/\(post.id)/post.md")
|
||
let before = try String(contentsOf: url, encoding: .utf8)
|
||
|
||
#expect(!PostStore.captureFirstDraftStats(id: post.id, body: "x", home: home))
|
||
#expect(try String(contentsOf: url, encoding: .utf8) == before)
|
||
}
|
||
|
||
@Test func `recapture while still first draft overwrites latest draft state wins`() throws {
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Evolving\n\nOne two three. Four five! Six?", home: home)
|
||
|
||
#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 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
|
||
#expect(matter["stats_type_token_ratio"] == "0.500")
|
||
#expect(matter["stats_trigram_repetition"] == "0.250")
|
||
}
|
||
|
||
@Test func `capture qualifies a bare legacy post through the lenient defaults`() throws {
|
||
// A bare corpus post (no frontmatter at all) scans writer/First
|
||
// Draft/post — the capture gate must reach the same verdict, not
|
||
// demand stamps that legacy files never carried.
|
||
let home = try storeHome { home in
|
||
let dir = home.appendingPathComponent("projects/posts/bare-legacy", isDirectory: true)
|
||
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
||
try "# Bare\n\nNo frontmatter here.".write(
|
||
to: dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8,
|
||
)
|
||
}
|
||
|
||
#expect(PostStore.captureFirstDraftStats(id: "bare-legacy", body: "No frontmatter here.", home: home))
|
||
|
||
let after = try String(
|
||
contentsOf: home.appendingPathComponent("projects/posts/bare-legacy/post.md"), encoding: .utf8,
|
||
)
|
||
let matter = PostFrontmatter.parse(after)
|
||
#expect(matter["stats_captured"] != nil)
|
||
#expect(after.contains("# Bare")) // the body survived the fresh-block prepend
|
||
}
|
||
|
||
@Test func `advance pass state preserves stats and foreign keys in place`() throws {
|
||
// The #31 regression pin: the advance used to rewrite the block from
|
||
// its three parsed fields, erasing the stats captured at the pass's
|
||
// own invocation (and any foreign frontmatter). The in-place stamp
|
||
// keeps every other line byte-identical.
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Kept\n\nBody stays.", home: home)
|
||
let url = home.appendingPathComponent("projects/posts/\(post.id)/post.md")
|
||
let seeded = try String(contentsOf: url, encoding: .utf8)
|
||
.replacingOccurrences(of: "---\ntype: post", with: "---\nforeign_key: keep-me\ntype: post")
|
||
try seeded.write(to: url, atomically: true, encoding: .utf8)
|
||
#expect(PostStore.captureFirstDraftStats(id: post.id, body: "Body stays.", home: home))
|
||
|
||
let advanced = try PostStore.advancePassState(id: post.id, kind: .pass1, home: home)
|
||
|
||
#expect(advanced == .pass1Done)
|
||
let after = try String(contentsOf: url, encoding: .utf8)
|
||
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(PostFrontmatter.split(after).body == "# Kept\n\nBody stays.")
|
||
}
|
||
|
||
@Test func `overwrite preserves stats and foreign keys while swapping the body`() throws {
|
||
// The #31 finish-audit regression pin: an editor save mid-pass used to
|
||
// route through the stamped() rewrite and erase the stats captured at
|
||
// the invocation (plus foreign frontmatter). The in-place update keeps
|
||
// every non-owned key and swaps only the body.
|
||
let home = try storeHome()
|
||
let post = try PostStore.writeNew(body: "# Draft\n\nFirst words.", home: home)
|
||
let url = home.appendingPathComponent("projects/posts/\(post.id)/post.md")
|
||
let seeded = try String(contentsOf: url, encoding: .utf8)
|
||
.replacingOccurrences(of: "---\ntype: post", with: "---\nforeign_key: keep-me\ntype: post")
|
||
try seeded.write(to: url, atomically: true, encoding: .utf8)
|
||
#expect(PostStore.captureFirstDraftStats(id: post.id, body: "First words.", home: home))
|
||
|
||
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 = 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(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 {
|
||
// The bare-post branch of the #31 audit fix: no block → the #59 stamp
|
||
// shape (type first), exactly as before the fix.
|
||
let home = try storeHome { home in
|
||
let dir = home.appendingPathComponent("projects/posts/bare-overwrite", isDirectory: true)
|
||
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
||
try "Bare body.".write(to: dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
||
}
|
||
|
||
try PostStore.overwrite(id: "bare-overwrite", body: "Bare body, edited.", home: home)
|
||
|
||
let after = try String(
|
||
contentsOf: home.appendingPathComponent("projects/posts/bare-overwrite/post.md"), encoding: .utf8,
|
||
)
|
||
let matter = PostFrontmatter.parse(after)
|
||
#expect(matter["type"] == "post")
|
||
#expect(matter["pass_state"] == "First Draft")
|
||
#expect(matter["origin"] == "writer")
|
||
#expect(PostFrontmatter.split(after).body == "Bare body, edited.")
|
||
}
|