use std::path::PathBuf;
use heddle_cli_args::INIT_VERB;
use schemars::JsonSchema;
use serde::Serialize;
use super::verification_health::RepositoryVerificationState;
#[derive(Serialize, JsonSchema)]
#[schemars(rename = "InitSchema")]
pub struct InitOutput {
pub output_kind: &'static str,
pub status: String,
pub action: String,
#[schemars(with = "String")]
pub path: PathBuf,
pub repository_mode: String,
pub git_detected: bool,
pub heddle_initialized: bool,
pub installed_heddleignore: bool,
pub principal_configured: bool,
pub principal_status: String,
pub principal_source: Option<String>,
pub principal: Option<InitPrincipalOutput>,
pub principal_recommended_action: Option<String>,
#[serde(skip)]
#[schemars(skip)]
pub placeholder_principal_warning: Option<String>,
pub side_effects: Vec<String>,
pub message: String,
pub next_action: Option<String>,
pub recommended_action: Option<String>,
#[allow(dead_code)]
#[serde(skip_serializing)]
#[serde(rename = "verification")]
#[schemars(skip)]
pub trust: RepositoryVerificationState,
}
impl InitOutput {
pub const VERB: &'static str = INIT_VERB;
}
#[derive(Serialize, JsonSchema)]
#[schemars(rename = "InitPrincipalSchema")]
pub struct InitPrincipalOutput {
pub name: String,
pub email: String,
}
#[cfg(test)]
mod tests {
use clap::CommandFactory;
use schemars::schema_for;
use serde_json::Value;
use super::super::{command_catalog, schemas};
use super::*;
use crate::cli::Cli;
fn property_keys(schema: &Value) -> Vec<String> {
let Some(properties) = schema.get("properties").and_then(Value::as_object) else {
return Vec::new();
};
properties.keys().cloned().collect()
}
#[test]
fn init_verb_resolves_across_clap_catalog_and_schema() {
assert!(
Cli::command().find_subcommand(INIT_VERB).is_some(),
"clap must resolve INIT_VERB"
);
assert!(
command_catalog::schema_verbs().contains(&INIT_VERB),
"catalog schema_verbs must include INIT_VERB"
);
assert!(
schemas::schema_for_verb(INIT_VERB).is_some(),
"schema_for_verb must resolve INIT_VERB"
);
}
#[test]
fn init_schema_is_the_real_output_type() {
let from_type = serde_json::to_value(schema_for!(InitOutput))
.expect("InitOutput schema should serialize");
let type_keys = property_keys(&from_type);
assert!(
!type_keys.iter().any(|key| key == "verification"),
"skip-serialized verification must not appear on the derived schema"
);
assert!(
!type_keys
.iter()
.any(|key| key == "placeholder_principal_warning"),
"text-only placeholder warning must not appear on the derived schema"
);
assert_eq!(
from_type.get("title").and_then(Value::as_str),
Some("InitSchema"),
"published schema title stays InitSchema"
);
}
}