Files
museion/MuseionTests/LearningsPaneTests.swift
T
AlexKucera 0e5c28ad95
CI / ci (push) Canceled after 0s
feat(mentor): promote & consult Learnings (#26)
Only the writer's explicit confirmation promotes a Fix Card into a
Learning — one confirm, one entry, never a repetition counter. The
Learning is a plain markdown file under <home>/learnings/<type>s/
carrying the card's teaching fields (pattern, problem, diagnostic
question, the demonstration example) plus promotion provenance; the
instance-bound anchor and mechanical replacement stay on the card.
Every pass consults these files via query_learnings; the Learnings
pane replaces its placeholder. Promote consults the on-disk card as
the truth and rejects symlinked learnings paths before any write
(finish-audit round). Dismiss/decay follows in #27.

Closes #26
2026-10-21 12:13:24 +02:00

127 lines
5.1 KiB
Swift

//
// LearningsPaneTests.swift
// MuseionTests
//
// #26 (S4.4, ADR-0005): the Learnings pane's disk reader — one plain
// markdown file per Learning under <home>/learnings/<type-plural>/,
// frontmatter read leniently (a stranger's hand-edit never surfaces
// wrong or crashes the pane).
//
import Foundation
@testable import Museion
import Testing
/// Throwaway Homes with hand-written .md Learning fixtures — the format
/// contract the promote rail writes: frontmatter pattern/problem/promoted
/// (all optional, quoted or bare), file named YYYYMMDD-<slug>.md,
/// dir = the content-type plural.
private func makeHome(_ write: (URL) throws -> Void) throws -> URL {
let home = FileManager.default.temporaryDirectory
.appendingPathComponent("learnings-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(at: home, withIntermediateDirectories: true)
try write(home)
return home
}
/// Seeds <home>/learnings/<type>/<file> with raw markdown.
private func writeLearning(_ home: URL, _ type: String, _ file: String, _ markdown: String) throws {
let dir = home.appendingPathComponent("learnings/\(type)", isDirectory: true)
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
try markdown.write(to: dir.appendingPathComponent(file), atomically: true, encoding: .utf8)
}
struct LearningsPaneTests {
@Test func `scan groups by type dir and orders entries newest first`() throws {
// Scenario: two content-type dirs; within posts three files — the
// YYYYMMDD prefix orders newest-first and an undated file sorts last.
let home = try makeHome { home in
try writeLearning(home, "posts", "20261020-late-scene-reveal.md", """
---
pattern: "Open on the reveal"
problem: "Scenes opened after the reveal"
promoted: 2026-10-20
---
""")
try writeLearning(home, "posts", "20261021-fold-the-ladder.md", """
---
pattern: Fold the ladder
problem: Ladders left standing
promoted: "2026-10-21"
---
""")
try writeLearning(home, "posts", "undated-note.md", "# just a hand edit\n")
try writeLearning(home, "outlines", "20261019-parallel-arcs.md", """
---
pattern: Parallel arcs
promoted: 2026-10-19
---
""")
}
let groups = LearningsIndex.scan(home: home)
#expect(groups.map(\.type) == ["outlines", "posts"])
let posts = try #require(groups.first { $0.type == "posts" })
#expect(posts.entries.map(\.id) == [
"20261021-fold-the-ladder.md",
"20261020-late-scene-reveal.md",
"undated-note.md",
])
let newest = try #require(posts.entries.first)
#expect(newest.pattern == "Fold the ladder") // bare value, not just the quoted form
#expect(newest.problem == "Ladders left standing")
#expect(newest.promoted == "2026-10-21")
let outlines = try #require(groups.first { $0.type == "outlines" })
#expect(outlines.entries.count == 1)
#expect(outlines.entries.first?.problem == nil) // absent key omits the line
}
@Test func `scan reads leniently - noise never surfaces or crashes`() throws {
// Scenario: a stranger hand-edits the vault — a dated file with no
// frontmatter (pattern derives from the filename), non-md and hidden
// files, a hidden dir, a loose file directly under learnings/.
let home = try makeHome { home in
try writeLearning(home, "posts", "20261018-dated-without-frontmatter.md", "# note\n")
try writeLearning(home, "posts", ".DS_Store", "junk")
try writeLearning(home, "posts", "draft.txt", "not markdown")
try writeLearning(home, ".git", "20260101-x.md", "---\npattern: hidden\n---\n")
// A symlinked type-dir with FULL payload (a real .md behind the
// link) never surfaces — Post.scan posture, pinned.
let linkedTo = home.appendingPathComponent("real-drafts", isDirectory: true)
try FileManager.default.createDirectory(at: linkedTo, withIntermediateDirectories: true)
try "---\npattern: behind the link\n---\n".write(
to: linkedTo.appendingPathComponent("20261016-linked.md"),
atomically: true,
encoding: .utf8,
)
try FileManager.default.createSymbolicLink(
at: home.appendingPathComponent("learnings/linked-posts"),
withDestinationURL: linkedTo,
)
try writeLearning(home, "outlines", "20261017-outline-edge.md", """
---
promoted: 2026-10-17
---
""")
try "stray".write(
to: home.appendingPathComponent("learnings/loose.md"),
atomically: true,
encoding: .utf8,
)
}
let groups = LearningsIndex.scan(home: home)
#expect(groups.map(\.type) == ["outlines", "posts"]) // hidden dir + loose file: no group
let posts = try #require(groups.first { $0.type == "posts" })
#expect(posts.entries.map(\.pattern) == ["dated-without-frontmatter"]) // date+hyphen stripped, ext gone
#expect(posts.entries.first?.promoted == nil)
let outlines = try #require(groups.first { $0.type == "outlines" })
#expect(outlines.entries.first?.pattern == "outline-edge") // no pattern key → filename, date+hyphen stripped
}
@Test func `missing learnings dir scans empty`() throws {
// Scenario: a fresh Home before the first promotion — nothing to
// read, no crash.
let home = try makeHome { _ in }
#expect(LearningsIndex.scan(home: home).isEmpty)
}
}