use crate::snippets::types::{Language, Snippet, ValidationLevel};
pub(crate) const NO_SESSION_CONFIGURED_PHRASE: &str = "no snippet validation session is configured for";
pub(crate) fn unresolved_dependency_message(
no_session_configured: bool,
language: Language,
session_target: &str,
effective_level: ValidationLevel,
raw_output: &str,
) -> String {
if no_session_configured {
format!(
"could not validate at {effective_level}: {NO_SESSION_CONFIGURED_PHRASE} {language} snippets, so \
this one ran in an isolated scratch directory with no access to the crate's built package. \
`alef build` does not fix this; add a `[workspace.docs.snippets.sessions.{session_target}]` \
entry: {raw_output}"
)
} else {
format!(
"could not validate at {effective_level}: {language} toolchain ran but reported a missing \
dependency or build artifact -- run `alef build` first if this crate validates snippets against \
built artifacts: {raw_output}"
)
}
}
pub(super) fn session_target_hint(snippet: &Snippet) -> String {
snippet
.metadata
.target
.as_deref()
.map(Language::normalize_session_target)
.unwrap_or_else(|| snippet.language.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_session_configured_message_never_points_at_alef_build() {
let message = unresolved_dependency_message(
true,
Language::Go,
"go",
ValidationLevel::Compile,
"cannot find package",
);
assert!(message.contains(NO_SESSION_CONFIGURED_PHRASE), "{message}");
assert!(
!message.contains("run `alef build`"),
"no-session message must not tell the reader to rebuild: {message}"
);
assert!(
message.contains("cannot find package"),
"raw validator output must survive: {message}"
);
}
#[test]
fn no_session_configured_message_names_the_target_not_the_language() {
let message = unresolved_dependency_message(
true,
Language::TypeScript,
"wasm",
ValidationLevel::Compile,
"cannot find module",
);
assert!(
message.contains("sessions.wasm]"),
"the actionable hint must name the target `wasm`, not the language: {message}"
);
assert!(
!message.contains("sessions.typescript]"),
"the actionable hint must not name the language as if it were the target: {message}"
);
}
#[test]
fn ordering_message_still_points_at_alef_build() {
let message = unresolved_dependency_message(
false,
Language::Go,
"go",
ValidationLevel::Compile,
"cannot find package",
);
assert!(message.contains("run `alef build` first"), "{message}");
assert!(!message.contains(NO_SESSION_CONFIGURED_PHRASE), "{message}");
assert!(
message.contains("cannot find package"),
"raw validator output must survive: {message}"
);
}
}