Files
museion/MuseionTests/LearningsPaneTests.swift
T
AlexKucera 78c8844184
CI / ci (push) Canceled after 0s
feat(mentor): dismiss & decay Learnings (#27)
Dismissed patterns fade instead of haunting. Dismissing a Learning
stamps decay state into its own frontmatter (dismissed date,
dismiss_count, dismissed_at_pass — every other line preserved
verbatim, hand-editable, git-diffable) and hides it from every pass:
query_learnings serves only visible learnings. Resurfacing is an
output clock, not a calendar — a per-type counter under
<home>/learnings/pass-counts.json, ticked by every completed pass
(cancels and refusals never advance, so never tick), returns the
Learning after 10 subsequent passes of its content type; each
re-dismiss doubles the interval (10→20→40…), and a per-Learning
resurface_after frontmatter key overrides the base. Hand-edited decay
state reads leniently: garbage means visible, absurd values clamp, a
hand-edit can never trap the pane or the rail. The Learnings pane
rows dismiss (no confirm — low-stakes, self-healing), dim while
hidden with a resurfaces-in-N countdown, and foreign unclocked type
dirs neither offer Dismiss nor show a verdict — their clock can never
advance. Two review waves (two-axis implement review + three-reviewer
finish-audit: security clean) produced five fixes, each pinned or
noted in the session log.

Closes #27
2026-10-21 14:19:43 +02:00

190 lines
7.4 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.
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)
}
// MARK: Decay surface (#27, S4.2)
/// Scenario: Entry carries the decay verdict — visible learnings read
/// resurfacesIn nil; a dismissed one (anchor 2, once-dismissed =
/// interval 10) at pass 5 has 7 passes left; a twice-dismissed one
/// (interval 20) has 17.
@Test func `entry resurfacesIn pins the remaining-pass math`() {
#expect(LearningsIndex.Entry(
file: "20261021-fold-the-ladder.md",
text: "---\npattern: Fold the ladder\n---\n",
).resurfacesIn == nil)
#expect(LearningsIndex.Entry(
file: "20261020-hedging.md",
text: "---\npattern: Hedging\ndismiss_count: 1\ndismissed_at_pass: 2\n---\n",
currentPass: 5,
).resurfacesIn == 7)
#expect(LearningsIndex.Entry(
file: "20261020-hedging.md",
text: "---\npattern: Hedging\ndismiss_count: 2\ndismissed_at_pass: 2\n---\n",
currentPass: 5,
).resurfacesIn == 17)
}
/// Scenario: scan feeds the pass clock from pass-counts.json — the
/// dismissed fixture computes against the Home's counts, not zero.
/// Foreign (unclocked) dirs carry NO verdict even when hand-edited
/// decay state sits in the file — their clock never advances, a
/// countdown would lie (the store refuses to dismiss there; #27 audit).
@Test func `scan carries the pass clock into entries`() throws {
let home = try makeHome { home in
try writeLearning(home, "posts", "20261020-hedging.md", """
---
pattern: Hedging
dismiss_count: 1
dismissed_at_pass: 2
---
# Hedging
""")
try writeLearning(home, "posts", "20261021-fold.md", "---\npattern: Fold\n---\n")
try writeLearning(home, "essays", "20261019-musing.md", """
---
pattern: Musing
dismiss_count: 1
dismissed_at_pass: 2
---
""")
}
try "{\"outlines\": 99, \"posts\": 5}".write(
to: home.appendingPathComponent("learnings/pass-counts.json"),
atomically: true,
encoding: .utf8,
)
let posts = try #require(LearningsIndex.scan(home: home).first { $0.type == "posts" })
#expect(posts.entries.first { $0.id == "20261020-hedging.md" }?.resurfacesIn == 7)
#expect(posts.entries.first { $0.id == "20261021-fold.md" }?.resurfacesIn == nil)
let essays = try #require(LearningsIndex.scan(home: home).first { $0.type == "essays" })
#expect(essays.entries.first?.resurfacesIn == nil) // foreign: no verdict, no lying countdown
}
}