import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
import { init_paths, resolveBundledSkillsDir } from "../../config/paths.js";
import { SkillTestFramework, SkillTestRunner, formatTestResults, formatTestResultsJson, formatTestResultsTap } from "../../agent/skills/test-framework.js";
import "../../agent/skills/index.js";
import { join } from "path";
import { Command } from "commander";
//#region src/cli/commands/skills-test.ts
/**
* Skills Test CLI Command
* 
* Test and validate skills
*/
init_logger();
init_paths();
const log = createLogger("cli:skills-test");
function createSkillsTestCommand(_ctx) {
	const cmd = new Command("test").description("Test and validate skills").argument("[skill-name]", "Specific skill to test (default: all skills)").option("--skills-dir <path>", "Skills directory to test").option("--format <format>", "Output format: text, json, tap", "text").option("--verbose", "Show detailed output").option("--skip-security", "Skip security tests").option("--skip-deps", "Skip dependency tests").option("--skip-examples", "Skip example tests").option("--strict", "Strict mode - fail on warnings").option("--timeout <ms>", "Timeout for example tests", "10000").action(async (skillName, options) => {
		const skillsDir = options.skillsDir ? join(process.cwd(), options.skillsDir) : resolveBundledSkillsDir() || join(process.cwd(), "skills");
		log.info({
			skillsDir,
			skillName
		}, "Running skill tests");
		try {
			if (skillName) {
				const skillDir = join(skillsDir, skillName);
				const report = await new SkillTestFramework({
					skipSecurity: options.skipSecurity,
					skipDeps: options.skipDeps,
					skipExamples: options.skipExamples,
					strict: options.strict,
					exampleTimeout: parseInt(options.timeout, 10)
				}).testSkill(skillDir);
				outputReport(report, options.format, options.verbose);
				process.exit(report.passed ? 0 : 1);
			} else {
				const { reports, passed } = await new SkillTestRunner({
					skillsDir,
					skipSecurity: options.skipSecurity,
					skipDeps: options.skipDeps,
					skipExamples: options.skipExamples,
					strict: options.strict,
					exampleTimeout: parseInt(options.timeout, 10),
					format: options.format,
					verbose: options.verbose
				}).run();
				outputReports(reports, options.format, options.verbose);
				process.exit(passed ? 0 : 1);
			}
		} catch (err) {
			console.error("Error running tests:", err instanceof Error ? err.message : err);
			process.exit(1);
		}
	});
	cmd.command("validate <skill-path>").description("Validate a SKILL.md file").option("--strict", "Strict mode - fail on warnings").action(async (skillPath, options) => {
		const formatResult = new SkillTestFramework({ strict: options.strict }).testSkillMdFormat(skillPath);
		console.log(`\nValidating: ${skillPath}`);
		console.log(`Status: ${formatResult.status === "pass" ? "✅" : "❌"} ${formatResult.message}`);
		if (formatResult.details) for (const detail of formatResult.details) console.log(`  - ${detail}`);
		process.exit(formatResult.status === "pass" ? 0 : 1);
	});
	cmd.command("check-deps [skill-name]").description("Check skill dependencies").action(async (skillName) => {
		const skillsDir = resolveBundledSkillsDir() || join(process.cwd(), "skills");
		const framework = new SkillTestFramework();
		if (skillName) {
			const skillDir = join(skillsDir, skillName);
			const depsResult = (await framework.testSkill(skillDir)).results.find((r) => r.name === "Dependencies");
			if (depsResult) {
				console.log(`\nDependencies for ${skillName}:`);
				console.log(`Status: ${depsResult.status === "pass" ? "✅" : "❌"} ${depsResult.message}`);
				if (depsResult.details) for (const detail of depsResult.details) console.log(`  ${detail}`);
			} else console.log("No dependency tests found");
		} else {
			const { reports } = await new SkillTestRunner({
				skillsDir,
				skipSecurity: true,
				skipExamples: true
			}).run();
			console.log("\nDependency Status:\n");
			for (const report of reports) {
				const depsResult = report.results.find((r) => r.name === "Dependencies");
				const icon = depsResult?.status === "pass" ? "✅" : depsResult?.status === "fail" ? "❌" : "⚠️";
				console.log(`${icon} ${report.skillName}: ${depsResult?.message || "No dependencies"}`);
			}
		}
	});
	cmd.command("security [skill-name]").description("Security audit for skills").option("--deep", "Show detailed findings").action(async (skillName, options) => {
		const skillsDir = resolveBundledSkillsDir() || join(process.cwd(), "skills");
		const framework = new SkillTestFramework();
		if (skillName) {
			const skillDir = join(skillsDir, skillName);
			const securityResult = (await framework.testSkill(skillDir)).results.find((r) => r.name === "Security");
			if (securityResult) {
				console.log(`\nSecurity audit for ${skillName}:`);
				console.log(`Status: ${securityResult.status === "pass" ? "✅" : "❌"} ${securityResult.message}`);
				if (securityResult.details) for (const detail of securityResult.details) console.log(`  ${detail}`);
			}
		} else {
			const { reports } = await new SkillTestRunner({
				skillsDir,
				skipDeps: true,
				skipExamples: true
			}).run();
			console.log("\nSecurity Audit:\n");
			for (const report of reports) {
				const securityResult = report.results.find((r) => r.name === "Security");
				const icon = securityResult?.status === "pass" ? "✅" : securityResult?.status === "fail" ? "❌" : "⚠️";
				console.log(`${icon} ${report.skillName}: ${securityResult?.message || "Not scanned"}`);
				if (options.deep && securityResult?.details) for (const detail of securityResult.details) console.log(`    ${detail}`);
			}
		}
	});
	return cmd;
}
function outputReport(report, format, verbose) {
	switch (format) {
		case "json":
			console.log(formatTestResultsJson([report]));
			break;
		case "tap":
			console.log(formatTestResultsTap([report]));
			break;
		default: console.log(formatTestResults([report], verbose));
	}
}
function outputReports(reports, format, verbose) {
	switch (format) {
		case "json":
			console.log(formatTestResultsJson(reports));
			break;
		case "tap":
			console.log(formatTestResultsTap(reports));
			break;
		default: console.log(formatTestResults(reports, verbose));
	}
}
//#endregion
export { createSkillsTestCommand };

//# sourceMappingURL=skills-test.js.map