// // DeslopLintTests.swift // MuseionTests // // #6 (S3.4): the native deslop lint — banned-vocab tiers + em-dash // density computed in Swift, findings as real fix cards. Expectations // are derived here from the doctrine data, not copied from the // implementation (DraftStatsTests convention). // // ## Scenario: Native deslop cards at pass invocation // From issue #6 / PRD S3.4 // // Given a saved piece whose draft contains Tier-1 banned words, a Tier-2 // word above its allowance, and more em-dashes than the budget // When the writer invokes a pass (Run Pass 1) // Then the lint computes findings in Swift at invocation — after // First-Draft stats capture, before the engine turn, no engine // round-trip — and each finding lands as a real fix card (emitter // `lint`) under /cards/, rendered by the existing Fix Cards // palette with ask-before Apply on mechanical mapping cards. // import Foundation @testable import Museion import Testing @Suite("DeslopLintTests") struct DeslopLintTests { /// The AC #4 fixture: mixed-case `Utilize` twice, `delve`, `notably` ×2 /// (over the Tier-2 allowance), `furthermore` ×1 (within it), 5 em-dashes /// (over budget 4), plus words that merely CONTAIN tier phrases. private static let sloppy = """ # Sloppy Draft Delve into it. Utilize the tool — and utilize it twice — notably here — notably there — \ a fifth dash — plain words carry the rest. Furthermore stays within allowance. \ Delved, harnessed, showcased stay quiet. """ private func anchor(_ quote: String, _ occurrence: Int) -> FixCardStore.StoredFixCard.Anchor { .init(quote: quote, occurrence: occurrence) } @Test func `known sloppy draft produces the expected findings`() { let findings = DeslopLint.compute(body: Self.sloppy, config: DeslopLint.bundledConfig()) let expected: [DeslopLint.Finding] = [ .init( pattern: "em-dash density", problem: "5 em-dashes in this piece — the budget is 4. Most separation wants a comma " + "or a period; 1-2 earned dashes are plenty. The em-dash habit is the " + "prompt-resistant AI tell.", diagnosticQuestion: "Does this aside need an em-dash, or would a comma or a period carry it?", fix: "", lane: .demonstration, anchor: anchor("—", 1), ), .init( pattern: "delve", problem: "Tier 1 AI vocabulary — dead giveaway, never use. Appears 1× in this piece. " + "Replace with the plain word.", diagnosticQuestion: "What plain word says this instead?", fix: "", lane: .demonstration, anchor: anchor("Delve", 1), ), .init( pattern: "utilize", problem: "Tier 1 AI vocabulary — dead giveaway, never use. Appears 2× in this piece. " + "Replace with the plain word.", diagnosticQuestion: "What plain word says this instead?", fix: "Use", // case-matched: the anchor quotes sentence-initial `Utilize` lane: .mechanical, anchor: anchor("Utilize", 1), ), .init( pattern: "notably", problem: "Tier 2 AI vocabulary — suspicious in density, use sparingly. Appears 2× in " + "this piece. Replace with the plain word.", diagnosticQuestion: "What plain word says this instead?", fix: "", lane: .demonstration, anchor: anchor("notably", 1), ), ] // Exact set AND rank order: em-dash first, then Tier 1 by first-match // position (delve precedes utilize in the text), then Tier 2. The // within-allowance `furthermore` and the containing words // (Delved/harnessed/showcased) contribute nothing. #expect(findings == expected) } @Test func `word boundaries exclude inflected containing words`() { // `delved`/`harnessed`/`showcased` must NOT match `delve`/`harness`/ // `showcase` — whole-word boundaries, not substring. let findings = DeslopLint.compute( body: "Delved deep. Harnessed it. Showcased the work.", config: DeslopLint.bundledConfig(), ) #expect(findings.isEmpty) } @Test func `em dash at budget or zero stays quiet`() { let atBudget = "One — two — three — four." // exactly the budget of 4 #expect(DeslopLint.compute(body: atBudget, config: DeslopLint.bundledConfig()).isEmpty) let none = "No dashes at all here." #expect(DeslopLint.compute(body: none, config: DeslopLint.bundledConfig()).isEmpty) } @Test func `home override wins and a malformed one falls back`() throws { let home = FileManager.default.temporaryDirectory .appendingPathComponent("deslop-config-\(UUID().uuidString)", isDirectory: true) try FileManager.default.createDirectory(at: home, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: home) } // An override that exists AND decodes wins over the bundled default. try """ {"tiers":[{"name":"Home Tier","note":"local","maxAllowed":0,"phrases":[{"phrase":"dig"}]}],"emDashBudget":0} """.write(to: home.appendingPathComponent("deslop-lint.json"), atomically: true, encoding: .utf8) let overridden = DeslopLint.config(home: home) #expect(overridden.tiers.map(\.name) == ["Home Tier"]) #expect(overridden.tiers.first?.phrases.map(\.phrase) == ["dig"]) #expect(overridden.emDashBudget == 0) // Malformed override JSON → the bundled default (lenient, never fatal). try "{not json".write(to: home.appendingPathComponent("deslop-lint.json"), atomically: true, encoding: .utf8) let fallenBack = DeslopLint.config(home: home) #expect(fallenBack.tiers.count == 2) #expect(fallenBack.emDashBudget == 4) // No home at all → the bundled default. let bundled = DeslopLint.config(home: nil) #expect(bundled.tiers.count == 2) #expect(bundled.emDashBudget == 4) } @Test func `bundled config decodes the doctrine`() { let config = DeslopLint.bundledConfig() #expect(config.tiers.map(\.name) == ["Tier 1", "Tier 2"]) #expect(config.emDashBudget == 4) // The mapped phrases carry their replacements (mechanical lanes). let tier1 = config.tiers[0].phrases #expect(tier1.first(where: { $0.phrase == "utilize" })?.replacement == "use") #expect(tier1.first(where: { $0.phrase == "bolsters" })?.replacement == "supports") #expect(tier1.first(where: { $0.phrase == "facilitate" })?.replacement == "help") #expect(tier1.first(where: { $0.phrase == "facilitated" })?.replacement == "helped") #expect(tier1.first(where: { $0.phrase == "delve" })?.replacement == nil) // The tiers mirror the doctrine's allowances. #expect(config.tiers[0].maxAllowed == 0) #expect(config.tiers[1].maxAllowed == 1) #expect(!config.tiers[1].phrases.isEmpty) } @Test func `empty and whitespace phrases in hand-edited data are ignored`() { // Trust boundary: an empty or whitespace-only phrase in a Home // override must not match everywhere and kill the whole submit // (whole-call validation) — they contribute nothing, the rest // still lints. The "— —" in the body is the space-phrase bait // (a bare space flanked by non-letters DOES match without the // trimmed guard — a plain body would pass vacuously); budget 10 // keeps the dashes out of the assertion. let config = DeslopLint.Config(tiers: [ .init(name: "T", note: "n", maxAllowed: 0, phrases: [ .init(phrase: "", replacement: "x"), .init(phrase: " ", replacement: "x"), .init(phrase: "dig", replacement: nil), ]), ], emDashBudget: 10) let findings = DeslopLint.compute( body: "Dig it. Over — the — budget — of — dashes — — end.", config: config, ) #expect(findings.map(\.pattern) == ["dig"]) } @Test func `emit lands lint cards dedups and re-emits after close`() throws { let home = FileManager.default.temporaryDirectory .appendingPathComponent("deslop-emit-\(UUID().uuidString)", isDirectory: true) try FileManager.default.createDirectory(at: home, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: home) } let body = "Utilize this. Delve deep. Utilize it again." let post = try PostStore.writeNew(body: body, home: home) let pieceURL = home.appendingPathComponent("projects/posts/\(post.id)/post.md") DeslopLint.emit(body: body, pieceID: post.id, pieceURL: pieceURL, kind: .pass1, home: home) let first = FixCardStore.scan(pieceURL: pieceURL) #expect(first.count == 2) // delve + utilize — one finding per phrase #expect(first.allSatisfy { $0.emitter == .lint }) #expect(first.allSatisfy { $0.status == .open }) #expect(first.allSatisfy { $0.anchorStatus == .valid }) #expect(first.allSatisfy { $0.provenance?.pass == 1 }) #expect(first.allSatisfy { $0.provenance?.piece == post.id }) // Emit again over unchanged text: every finding already has an OPEN // card with the same pattern + anchor — nothing duplicates. DeslopLint.emit(body: body, pieceID: post.id, pieceURL: pieceURL, kind: .pass1, home: home) #expect(FixCardStore.scan(pieceURL: pieceURL).count == 2) // Close one card: a closed card does NOT block — the still-present // finding re-emits (re-runs make progress). let closed = try FixCardStore.close( pieceURL: pieceURL, id: #require(first.first { $0.pattern == "delve" }?.id), status: .skipped, ) #expect(closed.status == .skipped) DeslopLint.emit(body: body, pieceID: post.id, pieceURL: pieceURL, kind: .pass1, home: home) let after = FixCardStore.scan(pieceURL: pieceURL) #expect(after.count == 3) #expect(after.filter { $0.pattern == "delve" && $0.status == .open }.count == 1) #expect(after.filter { $0.pattern == "utilize" }.count == 1) // still deduped // Pass 2 stamps its own provenance on a fresh finding — the // ternary's other branch, ASSERTED (close → re-emit → pass == 2), // not just deduped silence. try FixCardStore.close( pieceURL: pieceURL, id: #require(after.first { $0.pattern == "delve" && $0.status == .open }?.id), status: .done, ) DeslopLint.emit(body: body, pieceID: post.id, pieceURL: pieceURL, kind: .pass2, home: home) let passTwo = FixCardStore.scan(pieceURL: pieceURL) #expect(passTwo.count == 4) #expect(passTwo.first { $0.pattern == "delve" && $0.status == .open }?.provenance?.pass == 2) } }