#![cfg(feature = "docs")]
use sdforge::core::ApiError;
use sdforge::service_api;
use sdforge::{generate_docs, write_docs, DocFormat};
#[service_api(
name = "docs_test_cmd",
version = "1.0",
description = "Test command for docs generation",
cli = true
)]
async fn docs_test_cmd(name: String) -> Result<String, ApiError> {
Ok(format!("docs:{}", name))
}
#[test]
fn test_generate_docs_openapi_returns_json() {
let json = generate_docs(DocFormat::OpenApi).expect("generate_docs OpenApi");
assert!(
json.starts_with('{'),
"OpenAPI output should be JSON object, got: {}",
&json[..json.len().min(200)]
);
}
#[test]
fn test_generate_docs_cli_markdown_contains_command() {
let md = generate_docs(DocFormat::CliMarkdown).expect("generate_docs CliMarkdown");
assert!(!md.is_empty(), "CLI markdown should not be empty");
assert!(
md.contains("docs_test_cmd"),
"CLI markdown should contain 'docs_test_cmd' command: {}",
&md[..md.len().min(300)]
);
}
#[test]
fn test_generate_docs_all_combines_formats() {
let all = generate_docs(DocFormat::All).expect("generate_docs All");
assert!(
all.contains('{') || all.contains('#'),
"All format should contain JSON '{{' or markdown '#' markers: {}",
&all[..all.len().min(200)]
);
assert!(
all.contains("docs_test_cmd"),
"All format should contain CLI command name: {}",
&all[..all.len().min(400)]
);
}
#[test]
fn test_write_docs_to_file_roundtrip() {
let tmp = tempfile::tempdir().expect("tempdir");
let path = tmp.path().join("docs.json");
write_docs(DocFormat::OpenApi, &path).expect("write_docs should succeed");
assert!(path.exists(), "output file should exist");
let content = std::fs::read_to_string(&path).expect("read file");
assert!(!content.is_empty(), "file content should not be empty");
assert!(
content.starts_with('{'),
"file content should be JSON: {}",
&content[..content.len().min(200)]
);
}
#[test]
fn test_generate_docs_mcp_markdown_returns_nonempty() {
let md = generate_docs(DocFormat::McpMarkdown).expect("generate_docs McpMarkdown");
assert!(!md.is_empty(), "MCP markdown should not be empty");
}