#![allow(clippy::expect_used)]
use mcp_conformance_core::trace::TraceEvent;
use crate::checks;
use crate::context::TraceContext;
use crate::reader::{Limits, parse_trace};
pub(super) const META: &str = r#""_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}}"#;
pub(super) fn events(trace: &str) -> Vec<TraceEvent> {
parse_trace(trace, &Limits::default()).expect("test trace parses")
}
pub(super) fn findings_for(check: &str, trace: &str) -> Vec<String> {
let events = events(trace);
let context = TraceContext::new(&events);
checks::find(check)
.expect("check is registered")
.run(&context)
.findings
.into_iter()
.map(|finding| finding.detail)
.collect()
}
pub(super) fn client(seq: u64, payload: &str) -> String {
line(seq, "client-to-server", "message", payload)
}
pub(super) fn server(seq: u64, payload: &str) -> String {
line(seq, "server-to-client", "message", payload)
}
fn line(seq: u64, direction: &str, kind: &str, rest: &str) -> String {
format!(
r#"{{"seq":{seq},"direction":"{direction}","transport":"streamable-http","kind":"{kind}","payload":{rest}}}"#
)
}
pub(super) fn post(seq: u64, headers: &str) -> String {
format!(
r#"{{"seq":{seq},"direction":"client-to-server","transport":"streamable-http","kind":"http","headers":{headers}}}"#
)
}
pub(super) fn status(seq: u64, status: u16) -> String {
format!(
r#"{{"seq":{seq},"direction":"server-to-client","transport":"streamable-http","kind":"http","status":{status}}}"#
)
}
pub(super) fn error(seq: u64, id: &str, code: i64) -> String {
server(
seq,
&format!(r#"{{"jsonrpc":"2.0","id":{id},"error":{{"code":{code},"message":"x"}}}}"#),
)
}
pub(super) fn trace(lines: &[String]) -> String {
lines.join("\n")
}