Files
museion/MuseionTests/LearningsPaneTests.swift
AlexKucera d7fd11cf0d
CI / ci (push) Canceled after 0s
chore(lint): repo-wide green becomes the contract
Close the lint arc #194-#197: deliberate-style rule disables with
rationales (#194), filter-safe test renames + --test-case-name-format
preserve (#195), every surviving warning fixed through the sanctioned
lint-stable shapes (#196), and the docs now state the true contract
(#197): bare swiftlint . reports zero violations and swiftformat .
reformats nothing, while push gates stay scoped to the push's own
files (settled #190 design) — the pre-push hook's stale 'pre-existing
violations' rationale blocks refreshed, gate logic untouched.

Also in the sweep: quiesce the FSEvent-burst watcher race in
LibrarySearchTests (two rescans double-count one shared PostScanCache
miss; benign revision over-bump), pin the learnings scan's symlink-skip
contract (flip-verified — defended in depth by the lstat guard AND
contentsOfDirectory's own symlink refusal), reorder that scan's guard
so cheap rejections precede the directory listing, and park the roster
chmod window with a ceiling comment.

Finish-audit: 3 reviewers, 0 blocking; the one code finding was
disproved by probe (&& out-binds ?? — the guard was correct). Suite
693/693 via scripts/full-test.sh; swiftlint 0 violations; swiftformat
0/141; hook rig 23/23.

Closes #197
2026-11-17 16:29:46 +01:00

209 lines
8.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)
}
/// Scenario: the skip posture Post.scan pins, on this scan — a symlinked
/// type dir must not be followed. Defended in depth: the lstat guard
/// (a symlink reports isDirectory=false) AND contentsOfDirectory's own
/// refusal of a symlinked entry — flip-verified: the pin fails only when
/// BOTH go traversal-shaped (fileExists guard + resolved-URL listing).
@Test func `a symlinked type dir never surfaces`() throws {
let home = try makeHome { home in
try writeLearning(home, "posts", "20261021-one-real-entry.md", "# real\n")
}
let learnings = home.appendingPathComponent("learnings", isDirectory: true)
try FileManager.default.createSymbolicLink(
at: learnings.appendingPathComponent("linked", isDirectory: true),
withDestinationURL: learnings.appendingPathComponent("posts", isDirectory: true),
)
let groups = LearningsIndex.scan(home: home)
#expect(groups.map(\.type) == ["posts"]) // the link's alias never groups
#expect(groups.flatMap(\.entries).count == 1) // its view of posts never doubles
}
// 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
}
}