// // ToolPayloadsTests.swift // MuseionTests // // #177 (slice 1): the tool-payload decoder's own pins — the strict MCP // trust boundary (piece_path / cards presence, the exact CardError // messages) and the decode facts that used to ride FixCardStore.Input's // Codable directly (occurrence default, unknown lane, provenance.pass). // MCPToolsTests.swift hosts the store-side pins (MCPToolsPromoteTests // split precedent — this file keeps MCPToolsTests under its lint caps). // // ## Scenario: one tool-payload decoder (mirror — source: ToolPayloads.swift) // From #177 // // Given the submit_fix_cards / submit_research_notes wire shapes // When the museion-tools MCP server and the session model's transcript rail both decode a payload // Then both read the same ToolPayloads decoder — the MCP side throws named CardErrors on required-field // violations, the transcript rail falls back to a raw row — and every wire-shape fact (lenient and strict) // is pinned in this one suite: ToolPayloadsTests. // import Foundation @testable import Museion import Testing /// #177: the strict submit entry's arguments as a JSONValue — one /// card wrapped in the {piece_path, cards:[…]} envelope. private func submitArguments(_ card: JSONValue) -> JSONValue { .object(["piece_path": .string("/tmp/piece"), "cards": .array([card])]) } struct ToolPayloadsTests { /// #177: the strict decode rides ToolPayloads (one tool-payload /// decoder) — same facts as the old direct-Codable pins: occurrence /// defaults to 1, absent diagnostic_question decodes nil, an unknown /// lane enum throws a CardError. @Test func `input decode defaults occurrence and rejects unknown enums`() throws { let minimal = submitArguments(.object([ "pattern": .string("p"), "problem": .string("q"), "anchor": .object(["quote": .string("x")]), "lane": .string("mechanical"), "emitter": .string("lint"), ])) let args = try ToolPayloads.submitFixCards(from: minimal) #expect(args.cards.count == 1) #expect(args.cards[0].anchor.occurrence == 1) #expect(args.cards[0].diagnosticQuestion == nil) let badLane = submitArguments(.object([ "pattern": .string("p"), "problem": .string("q"), "anchor": .object(["quote": .string("x")]), "lane": .string("nope"), "emitter": .string("lint"), ])) #expect(throws: FixCardStore.CardError.self) { _ = try ToolPayloads.submitFixCards(from: badLane) } } /// #23 follow-up: pass differentiation — `provenance.pass` rides the /// wire; pre-schema cards (no pass key) decode to nil, never error. @Test func `provenance pass rides the wire and old cards decode without it`() throws { let wire = submitArguments(.object([ "pattern": .string("p"), "problem": .string("q"), "anchor": .object(["quote": .string("x")]), "lane": .string("mechanical"), "emitter": .string("engine"), "provenance": .object(["piece": .string("posts/a"), "pass": .number(2)]), ])) let input = try ToolPayloads.submitFixCards(from: wire) #expect(input.cards[0].provenance?.pass == 2) let old = submitArguments(.object([ "pattern": .string("p"), "problem": .string("q"), "anchor": .object(["quote": .string("x")]), "lane": .string("mechanical"), "emitter": .string("engine"), "provenance": .object(["piece": .string("posts/a")]), ])) let oldInput = try ToolPayloads.submitFixCards(from: old) #expect(oldInput.cards[0].provenance?.pass == nil) } /// #177: the strict entry's own refusals — a missing piece_path and /// an empty cards array are CardErrors, never empty successes (the /// old decode accepted both; an empty submit used to mkdir an empty /// cards/ dir). @Test func `strict submit refuses missing piece path and empty cards`() { let card = JSONValue.object([ "pattern": .string("p"), "problem": .string("q"), "anchor": .object(["quote": .string("x")]), "lane": .string("mechanical"), "emitter": .string("lint"), ]) #expect(throws: FixCardStore.CardError.self) { _ = try ToolPayloads.submitFixCards(from: .object(["cards": .array([card])])) } #expect(throws: FixCardStore.CardError.self) { _ = try ToolPayloads.submitFixCards(from: .object(["piece_path": .string("/tmp/piece"), "cards": .array([])])) } } /// #177: the notes rail's trim rule at the strict entry — a MISSING /// or blank title/url answers the EXACT CardErrors the wire has /// always sent (behavior-pinned messages). @Test func `research notes refuse blank titles and urls with the exact errors`() { func noteArguments(_ note: JSONValue) -> JSONValue { .object(["piece_path": .string("/tmp/piece"), "notes": .array([note])]) } #expect { _ = try ToolPayloads.submitResearchNotes(from: noteArguments(.object(["title": .string("T")]))) } throws: { error in error as? FixCardStore.CardError == FixCardStore.CardError(message: "note url must not be empty") } #expect { _ = try ToolPayloads.submitResearchNotes(from: noteArguments(.object([ "title": .string(" "), "url": .string("https://example.org"), ]))) } throws: { error in error as? FixCardStore.CardError == FixCardStore.CardError(message: "note title must not be empty") } } /// #177: the notes rail's ENVELOPE refusals — a missing piece_path, a /// missing notes array, and an empty notes array are CardErrors (the /// old decode answered `{"received": 0}` to an empty array). @Test func `strict research submit refuses missing piece path and empty notes`() throws { let note = JSONValue.object(["title": .string("T"), "url": .string("https://a")]) #expect(throws: FixCardStore.CardError.self) { _ = try ToolPayloads.submitResearchNotes(from: .object(["notes": .array([note])])) } #expect(throws: FixCardStore.CardError.self) { _ = try ToolPayloads.submitResearchNotes(from: .object(["piece_path": .string("/tmp/piece")])) } #expect(throws: FixCardStore.CardError.self) { _ = try ToolPayloads.submitResearchNotes(from: .object(["piece_path": .string("/tmp/piece"), "notes": .array([])])) } // The happy envelope: the validated count comes back (the rail is // decode-validate only — no fields travel). let count = try ToolPayloads.submitResearchNotes(from: .object([ "piece_path": .string("/tmp/piece"), "notes": .array([note]), ])) #expect(count == 1) } /// #177 audit: the hand-rolled type guards — a present-but-wrong-typed /// value throws a named CardError, never a silent nil/truncation (the /// old DecodingError's job). Also the notes rail's quote/note: a /// non-string quote is an error at the strict boundary, not "". @Test func `strict decode refuses wrong-typed values`() { #expect(throws: FixCardStore.CardError.self) { _ = try ToolPayloads.submitFixCards(from: submitArguments(.object([ "pattern": .string("p"), "problem": .string("q"), "anchor": .object(["quote": .string("x"), "occurrence": .number(1.5)]), "lane": .string("mechanical"), "emitter": .string("lint"), ]))) } #expect(throws: FixCardStore.CardError.self) { _ = try ToolPayloads.submitFixCards(from: submitArguments(.object([ "pattern": .string("p"), "problem": .string("q"), "anchor": .object(["quote": .string("x")]), "lane": .string("mechanical"), "emitter": .string("lint"), "diagnostic_question": .number(3), ]))) } #expect(throws: FixCardStore.CardError.self) { _ = try ToolPayloads.submitFixCards(from: submitArguments(.object([ "pattern": .string("p"), "problem": .string("q"), "anchor": .object(["quote": .string("x")]), "lane": .string("mechanical"), "emitter": .string("lint"), "provenance": .string("nope"), ]))) } #expect(throws: FixCardStore.CardError.self) { _ = try ToolPayloads.submitResearchNotes(from: .object([ "piece_path": .string("/tmp/piece"), "notes": .array([.object(["title": .string("T"), "url": .string("https://a"), "quote": .number(123)])]), ])) } } /// #177 audit: the lenient card rail keeps BOTH shapes — the nested /// card-rail envelope (pinned end-to-end by ACPClientTests' fake) and /// the flat #14-era fake shape (kept per the ticket; pinned here so /// the branch can't silently die). @Test func `lenient fix card accepts the flat shape`() { let card = ToolPayloads.fixCard(from: .object([ "pattern": .string("p"), "problem": .string("q"), "diagnosticQuestion": .string("d"), "fix": .string("f"), "anchor": .string("the anchor line"), ])) #expect(card?.pattern == "p") #expect(card?.problem == "q") #expect(card?.diagnosticQuestion == "d") #expect(card?.fix == "f") #expect(card?.anchor == "the anchor line") #expect(card?.provenance == "engine") #expect(card?.status == "proposed") } /// #29 (S8.2), #177: the LENIENT batch parser the transcript rail /// rides — moved from ResearchCandidateTests so every wire-shape fact /// (lenient and strict) is pinned in this one suite. Two notes → two /// candidates with "" defaults; a missing piece_path, an empty/absent /// notes array, or a note missing a NON-BLANK title/url yields NO /// candidates (the raw row owns it). @Test func `research batch parses notes and refuses garbage`() { let good: JSONValue = .object([ "piece_path": .string("/tmp/piece"), "notes": .array([ .object(["title": .string("A"), "url": .string("https://a"), "quote": .string("q"), "note": .string("n")]), .object(["title": .string("B"), "url": .string("https://b")]), ]), ]) let batch = ToolPayloads.researchCandidates(from: good) #expect(batch.count == 2) #expect(batch[0] == ResearchCandidate(piecePath: "/tmp/piece", title: "A", url: "https://a", quote: "q", note: "n")) #expect(batch[1].quote == "") #expect(batch[1].note == "") #expect(batch[1].disposition == .pending) // Garbage: no piece_path / empty notes / absent notes / note missing url / not an object. #expect(ToolPayloads.researchCandidates(from: .object([ "notes": .array([.object(["title": .string("A"), "url": .string("https://a")])]), ])).isEmpty) #expect(ToolPayloads.researchCandidates(from: .object(["piece_path": .string("/tmp/piece"), "notes": .array([])])).isEmpty) #expect(ToolPayloads.researchCandidates(from: .object(["piece_path": .string("/tmp/piece")])).isEmpty) #expect(ToolPayloads.researchCandidates(from: .object([ "piece_path": .string("/tmp/piece"), "notes": .array([.object(["title": .string("A")])]), ])).isEmpty) // Audit pin: whitespace-only title/url are the wire's refusal too — // both boundaries must agree (an invisible-titled card used to be // keepable as `-untitled.md`). #expect(ToolPayloads.researchCandidates(from: .object([ "piece_path": .string("/tmp/piece"), "notes": .array([.object(["title": .string(" "), "url": .string("https://a")])]), ])).isEmpty) #expect(ToolPayloads.researchCandidates(from: .object([ "piece_path": .string("/tmp/piece"), "notes": .array([.object(["title": .string("A"), "url": .string(" ")])]), ])).isEmpty) #expect(ToolPayloads.researchCandidates(from: nil).isEmpty) } }