// // ResearchCandidateTests.swift // MuseionTests // // #29 (S8.1–S8.3): the research-candidate pins, own file — // SessionLifecycleTests crossed its type-body ceiling (MCPToolsPromoteTests // precedent). #178: runs IN-PROCESS over ScriptedEngines rigs at the // EngineTransport seam — no child process. The payload SHAPE pins // live in ToolPayloadsTests (#177); this file pins the curation behavior. // import Foundation @testable import Museion import Testing // #29 suite: candidate parsing, curation, and replay survival. struct ResearchCandidateTests { /// #29 AC (S8.2): candidates live in their session — resume /// re-materializes them from the replayed transcript (engine-owned /// cargo). The planted session's history carries one research tool call /// with two notes; the default open RESUMES it, so the candidates are /// there without any extra turn. @Test @MainActor func `resume re-materializes research candidates from the replay`() async throws { let rosterDir = acpTempDir("research-roster") let entry = SessionEntry(sessionId: "sess-1", name: "Research", pieceTitle: nil, createdAt: Date()) SessionRoster.save([entry], directory: rosterDir) let engines = ScriptedEngines(mode: "loadReplay") let mentor = engines.makeMentor(rosterDirectory: rosterDir) defer { mentor.shutdown() } await mentor.openSession() var candidates: [ResearchCandidate] = [] for _ in 0 ..< 500 { candidates = mentor.transcriptRows(of: entry).compactMap { if case let .researchNote(candidate) = $0.content { candidate } else { nil } } if candidates.count == 2 { break } try await Task.sleep(nanoseconds: 20_000_000) } // #require (not bare #expect + subscript): a missed settle must fail // the test, never trap the runner on an out-of-bounds index. try #require(candidates.count == 2, "expected two candidates from the replay, got \(candidates.count)") #expect(candidates.allSatisfy { $0.piecePath == "/tmp/piece" }) #expect(candidates[0].title == "Hinge Study") #expect(candidates[0].url == "https://example.org/hinge") #expect(candidates[0].quote == "We should stop listening.") #expect(candidates[0].note == "Counts the pivot tense.") // The second note's absent quote/note default to "". #expect(candidates[1].title == "Second Source") #expect(candidates[1].quote == "") #expect(candidates[1].note == "") #expect(candidates.allSatisfy { $0.disposition == .pending }) } /// #29 (S8.3): the curation mutation — kept/rejected replace the row's /// content in the ACTIVE session's transcript; an unknown row id no-ops. @Test @MainActor func `research disposition flips the row and no-ops unknown ids`() async throws { let rosterDir = acpTempDir("disposition-roster") let entry = SessionEntry(sessionId: "sess-1", name: "Research", pieceTitle: nil, createdAt: Date()) SessionRoster.save([entry], directory: rosterDir) let engines = ScriptedEngines(mode: "loadReplay") let mentor = engines.makeMentor(rosterDirectory: rosterDir) defer { mentor.shutdown() } await mentor.openSession() var rows: [SessionRow] = [] for _ in 0 ..< 500 { rows = mentor.transcriptRows(of: entry).filter { if case .researchNote = $0.content { return true } return false } if rows.count == 2 { break } try await Task.sleep(nanoseconds: 20_000_000) } try #require(rows.count == 2, "expected two candidate rows, got \(rows.count)") mentor.setResearchDisposition(rowID: rows[0].id, .kept(fileName: "20261022-hinge-study.md")) mentor.setResearchDisposition(rowID: rows[1].id, .rejected) mentor.setResearchDisposition(rowID: UUID(), .rejected) // unknown id — no-op let after = mentor.transcriptRows(of: entry) #expect(after.contains { row in if case let .researchNote(candidate) = row.content { return candidate.disposition == .kept(fileName: "20261022-hinge-study.md") } return false }) #expect(after.contains { row in if case let .researchNote(candidate) = row.content { return candidate.disposition == .rejected } return false }) // Exactly two research rows still — the unknown id added nothing. #expect(after.filter { if case .researchNote = $0.content { return true } return false }.count == 2) } /// #29 (S8.3): the writing filter keeps candidate cards visible. @Test func `writing filter keeps research candidate rows`() { #expect(SessionTranscriptView.isWritingRow(SessionRow(content: .researchNote(ResearchCandidate( piecePath: "/tmp/piece", title: "A", url: "https://a", quote: "", note: "", ))))) } }