#![allow(deprecated)]
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;
#[test]
fn test_help() {
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("Tools for madskilling"));
}
#[test]
fn test_version() {
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("--version")
.assert()
.success()
.stdout(predicate::str::contains("madskills"));
}
#[test]
fn test_lint_valid_skill() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/test-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\nname: test-skill\ndescription: A test skill for integration testing\n---\n# Test Skill\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("lint").arg(temp.path()).assert().success().code(0);
}
#[test]
fn test_lint_invalid_name() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/TestSkill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\nname: TestSkill\ndescription: Test skill with invalid name\n---\n# Test\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("lint")
.arg(temp.path())
.assert()
.failure()
.code(2)
.stdout(predicate::str::contains("must be lowercase"));
}
#[test]
fn test_lint_name_directory_mismatch() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/wrong-dir");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\nname: test-skill\ndescription: Test skill\n---\n# Test\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("lint")
.arg(temp.path())
.assert()
.failure()
.code(2)
.stdout(predicate::str::contains("does not match"));
}
#[test]
fn test_lint_duplicate_names() {
let temp = TempDir::new().unwrap();
let skill_dir1 = temp.path().join(".github/skills/test-skill");
let skill_dir2 = temp.path().join(".github/skills/test-skill-copy");
fs::create_dir_all(&skill_dir1).unwrap();
fs::create_dir_all(&skill_dir2).unwrap();
let skill_content1 = "---\nname: test-skill\ndescription: Test\n---\n# Test\n";
let skill_content2 = "---\nname: test-skill\ndescription: Test\n---\n# Test\n";
fs::write(skill_dir1.join("SKILL.md"), skill_content1).unwrap();
fs::write(skill_dir2.join("SKILL.md"), skill_content2).unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("lint")
.arg(temp.path())
.assert()
.failure()
.code(2)
.stdout(predicate::str::contains("duplicated"));
}
#[test]
fn test_lint_json_output() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/test-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\nname: test-skill\ndescription: Test\n---\n# Test\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("lint")
.arg("--format")
.arg("json")
.arg(temp.path())
.assert()
.success()
.stdout(predicate::str::contains("\"results\""));
}
#[test]
fn test_list_skills() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/test-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\nname: test-skill\ndescription: Test skill\n---\n# Test\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("list")
.arg(temp.path())
.assert()
.success()
.stdout(predicate::str::contains("test-skill"));
}
#[test]
fn test_init_creates_skill() {
let temp = TempDir::new().unwrap();
fs::create_dir_all(temp.path().join(".github")).unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("init")
.arg("new-skill")
.arg("--root")
.arg(temp.path())
.assert()
.success();
let skill_dir = temp.path().join(".github/skills/new-skill");
assert!(skill_dir.join("SKILL.md").exists());
assert!(skill_dir.join("README.md").exists());
let content = fs::read_to_string(skill_dir.join("SKILL.md")).unwrap();
assert!(content.contains("name: new-skill"));
}
#[test]
fn test_init_auto_detects_claude_location() {
let temp = TempDir::new().unwrap();
fs::create_dir_all(temp.path().join(".claude/skills")).unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("init")
.arg("auto-skill")
.arg("--root")
.arg(temp.path())
.assert()
.success();
let skill_dir = temp.path().join(".claude/skills/auto-skill");
assert!(skill_dir.join("SKILL.md").exists());
}
#[test]
fn test_init_respects_dir_override() {
let temp = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("init")
.arg("custom-skill")
.arg("--root")
.arg(temp.path())
.arg("--dir")
.arg(temp.path().join("custom/location/custom-skill"))
.assert()
.success();
assert!(
temp.path()
.join("custom/location/custom-skill/SKILL.md")
.exists()
);
}
#[test]
fn test_init_invalid_name() {
let temp = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("init")
.arg("Invalid-Name")
.arg("--root")
.arg(temp.path())
.assert()
.failure()
.stderr(predicate::str::contains("must be lowercase"));
}
#[test]
fn test_fmt_normalizes_frontmatter() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/test-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\ndescription: Test\nname: test-skill\n---\n# Test\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("fmt").arg(temp.path()).assert().success();
let content = fs::read_to_string(skill_dir.join("SKILL.md")).unwrap();
let name_pos = content.find("name:").unwrap();
let desc_pos = content.find("description:").unwrap();
assert!(name_pos < desc_pos, "name should come before description");
}
#[test]
fn test_fmt_check_mode() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/test-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\ndescription: Test\nname: test-skill\n---\n# Test\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("fmt")
.arg("--check")
.arg(temp.path())
.assert()
.failure()
.code(2);
let content = fs::read_to_string(skill_dir.join("SKILL.md")).unwrap();
assert!(content.starts_with("---\ndescription:"));
}
#[test]
fn test_fmt_frontmatter_and_markdown() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/test-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\ndescription: Test\nname: test-skill\n---\n# Test\n\nMultiple spaces\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("fmt").arg(temp.path()).assert().success();
let content = fs::read_to_string(skill_dir.join("SKILL.md")).unwrap();
let name_pos = content.find("name:").unwrap();
let desc_pos = content.find("description:").unwrap();
assert!(name_pos < desc_pos, "frontmatter should be normalized");
}
#[test]
fn test_fmt_no_mdlint() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/test-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\ndescription: Test\nname: test-skill\n---\n# Test\n\nMultiple spaces\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("fmt")
.arg("--no-mdlint")
.arg(temp.path())
.assert()
.success();
let content = fs::read_to_string(skill_dir.join("SKILL.md")).unwrap();
let name_pos = content.find("name:").unwrap();
let desc_pos = content.find("description:").unwrap();
assert!(name_pos < desc_pos, "frontmatter should be normalized");
}
#[test]
fn test_fmt_no_frontmatter() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/test-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\nname: test-skill\ndescription: Test\n---\n# Test\n\nMultiple spaces\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("fmt")
.arg("--no-frontmatter")
.arg(temp.path())
.assert()
.success();
let content = fs::read_to_string(skill_dir.join("SKILL.md")).unwrap();
assert!(content.starts_with("---\nname: test-skill\ndescription: Test\n---"));
}
#[test]
fn test_lint_unicode_skill_name() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/café-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\nname: café-skill\ndescription: Unicode test skill\n---\n# Test\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("lint").arg(temp.path()).assert().success();
}
#[test]
fn test_lint_extra_fields() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/test-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\nname: test-skill\ndescription: Test\nunknown_field: bad\n---\n# Test\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("lint")
.arg(temp.path())
.assert()
.failure()
.code(2)
.stdout(predicate::str::contains("Unexpected fields"));
}
#[test]
fn test_lint_best_practices_violations() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/test-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\nname: test-skill\ndescription: I help with testing\n---\n# Test Skill\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("lint")
.arg(temp.path())
.assert()
.success() .code(0)
.stdout(predicate::str::contains("BP-WARN"))
.stdout(predicate::str::contains("AS003"));
}
#[test]
fn test_lint_no_best_practices_flag() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/test-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\nname: test-skill\ndescription: I help with testing\n---\n# Test Skill\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("lint")
.arg("--no-best-practices")
.arg(temp.path())
.assert()
.success() .code(0);
}
#[test]
fn test_lint_best_practices_strict_mode() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/test-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\nname: test-skill\ndescription: I help with testing\n---\n# Test Skill\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("lint")
.arg("--strict")
.arg(temp.path())
.assert()
.failure()
.code(2)
.stdout(predicate::str::contains("BP-ERROR")); }
#[test]
fn test_lint_best_practices_reserved_word() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/claude-helper");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\nname: claude-helper\ndescription: For helping with Claude tasks\n---\n# Helper\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("lint")
.arg(temp.path())
.assert()
.success()
.code(0)
.stdout(predicate::str::contains("AS016"))
.stdout(predicate::str::contains("reserved words"));
}
#[test]
fn test_lint_best_practices_json_output() {
let temp = TempDir::new().unwrap();
let skill_dir = temp.path().join(".github/skills/test-skill");
fs::create_dir_all(&skill_dir).unwrap();
fs::write(
skill_dir.join("SKILL.md"),
"---\nname: test-skill\ndescription: I help\n---\n# Test\n",
)
.unwrap();
let mut cmd = Command::cargo_bin("madskills").unwrap();
cmd.arg("lint")
.arg("--format")
.arg("json")
.arg(temp.path())
.assert()
.success()
.code(0)
.stdout(predicate::str::contains("best_practice_violations"))
.stdout(predicate::str::contains("AS003"));
}