Files
museion/MuseionTests/ResearchCandidateTests.swift
T
AlexKucera d14cb40e73
CI / ci (push) Canceled after 0s
refactor(session): type the transcript rail
SessionRow text rows carry (body, speaker, notice) as data — agent/
user/system plus refused/interrupted/connectionLost/kickoff/failed/
plain — typed at every append site; no stored row holds a speaker
prefix (pinned). The two wire-only classifications are sniffed once at
the append seam: a replayed pass kick-off ('Pass Pass N…') and the
engine's 'Operation interrupted:' notice. The transcript view, the
writing-session filter, and the New Post prefill switch on typed data;
rendering is identical (refusal/connection-lost rows stay agent prose
by pinned render).

Fix Card and Research Note payloads decode through one shared
ToolPayloads decoder: lenient for the transcript rail (both the nested
card-rail shape and the flat #14-era fake kept), strict for the
museion-tools MCP trust boundary with named CardErrors. Three
intentional wire tightenings at the strict entry: empty cards/notes
arrays throw instead of no-op succeeding, the camelCase
diagnosticQuestion alias is rejected (schema is snake_case), and
decode failures answer short named errors instead of DecodingError
dumps. quote/note type parity restored (present-but-wrong-type throws).
Every wire-shape fact is pinned in ToolPayloadsTests.

Full suite 653/653, swiftformat/swiftlint clean. Audit flip-verified.

Closes #177
2026-11-09 16:01:53 +01:00

120 lines
4.5 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// ResearchCandidateTests.swift
// MuseionTests
//
// #29 (S8.1–S8.3): the research-candidate pins, own file —
// SessionLifecycleTests crossed its type-body ceiling (MCPToolsPromoteTests
// precedent). Reuses the shared fake ACP harness. 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 fake = makeFakeACP(at: acpTempDir("research"), mode: "loadReplay")
let mentor = MentorSessionModel(binaryPath: fake, 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 fake = makeFakeACP(at: acpTempDir("disposition"), mode: "loadReplay")
let mentor = MentorSessionModel(binaryPath: fake, 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: "",
)))))
}
}