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
118 lines
6.7 KiB
Swift
118 lines
6.7 KiB
Swift
//
|
|
// 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")))
|
|
}
|
|
}
|