""" CLI Integration Tests for Issue #40: Associated Files Management. Tests the enhanced CLI commands that work with associated files. """ import json import pytest from pathlib import Path from tempfile import TemporaryDirectory from click.testing import CliRunner from markitect.cli import cli class TestIssue40CLIIntegration: """Test CLI integration for associated files management.""" @pytest.fixture def runner(self): """Create CLI test runner.""" return CliRunner() @pytest.fixture def temp_dir(self): """Create a temporary directory for testing.""" with TemporaryDirectory() as temp_dir: yield Path(temp_dir) def test_schema_generate_with_explicit_associated_path(self, runner, temp_dir): """schema-generate can use explicit associated .json file path.""" md_file = temp_dir / "document.md" md_file.write_text("# Document\n\n## Introduction\n\nContent here.") # Explicitly specify associated file path expected_schema = temp_dir / "document.json" result = runner.invoke(cli, [ 'schema-generate', str(md_file), '--output', str(expected_schema) ]) assert result.exit_code == 0 # Should create associated schema file assert expected_schema.exists() # Verify it's valid JSON schema_content = json.loads(expected_schema.read_text()) assert schema_content['type'] == 'object' def test_schema_generate_interactive_mode_defaults_to_associated_path(self, runner, temp_dir): """schema-generate in interactive mode should default to associated path.""" md_file = temp_dir / "document.md" md_file.write_text("# Document\n\n## Introduction\n\nContent here.") # Run schema-generate without --output in interactive mode result = runner.invoke(cli, [ 'schema-generate', str(md_file) ], env={'MARKITECT_MODE': 'interactive'}) assert result.exit_code == 0 # Should create associated schema file expected_schema = temp_dir / "document.json" assert expected_schema.exists() # Verify it's valid JSON schema_content = json.loads(expected_schema.read_text()) assert schema_content['type'] == 'object' def test_generate_stub_with_explicit_associated_path(self, runner, temp_dir): """generate-stub can use explicit associated .md file path.""" schema_file = temp_dir / "template.json" schema_content = { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "title": "Template Schema", "properties": { "headings": { "type": "object", "properties": { "level_1": {"type": "array", "minItems": 1, "maxItems": 1} } } } } schema_file.write_text(json.dumps(schema_content)) # Explicitly specify associated file path expected_md = temp_dir / "template.md" result = runner.invoke(cli, [ 'generate-stub', str(schema_file), '--output', str(expected_md) ]) assert result.exit_code == 0 # Should create associated markdown file assert expected_md.exists() # Verify it's valid markdown with schema reference md_content = expected_md.read_text() # Content should include schema reference metadata and heading assert '