|
| 1 | +import argparse |
| 2 | +import asyncio |
| 3 | +import logging |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from struct_module.commands.generate import GenerateCommand |
| 8 | +from struct_module.mcp_server import StructMCPServer |
| 9 | + |
| 10 | + |
| 11 | +def _ensure_store(tmp_path): |
| 12 | + store_dir = tmp_path / 'store' |
| 13 | + store_dir.mkdir(parents=True, exist_ok=True) |
| 14 | + p = store_dir / 'input.json' |
| 15 | + p.write_text('{}') |
| 16 | + return str(p) |
| 17 | + |
| 18 | + |
| 19 | +def test_generate_summary_counts_created_updated(tmp_path, caplog): |
| 20 | + # capture INFO logs for our modules |
| 21 | + caplog.set_level(logging.INFO) |
| 22 | + caplog.set_level(logging.INFO, logger='struct_module.file_item') |
| 23 | + caplog.set_level(logging.INFO, logger='struct_module.commands.generate') |
| 24 | + |
| 25 | + parser = argparse.ArgumentParser() |
| 26 | + command = GenerateCommand(parser) |
| 27 | + args = parser.parse_args(['struct-x', str(tmp_path / 'base')]) |
| 28 | + |
| 29 | + base_dir = tmp_path / 'base' |
| 30 | + base_dir.mkdir(parents=True, exist_ok=True) |
| 31 | + |
| 32 | + # Prepare one existing file (update) and one new file (create) |
| 33 | + (base_dir / 'update.txt').write_text('old') |
| 34 | + |
| 35 | + config = { |
| 36 | + 'files': [ |
| 37 | + {'create.txt': 'new-content'}, |
| 38 | + {'update.txt': 'newer-content'}, |
| 39 | + ], |
| 40 | + 'folders': [] |
| 41 | + } |
| 42 | + |
| 43 | + args.output = 'file' |
| 44 | + args.input_store = _ensure_store(tmp_path) |
| 45 | + args.dry_run = False |
| 46 | + args.diff = False |
| 47 | + args.vars = None |
| 48 | + args.backup = None |
| 49 | + args.file_strategy = 'overwrite' |
| 50 | + args.global_system_prompt = None |
| 51 | + args.structures_path = None |
| 52 | + args.non_interactive = True |
| 53 | + |
| 54 | + # Execute with mocked config |
| 55 | + with pytest.MonkeyPatch().context() as mp: |
| 56 | + mp.setattr(command, '_load_yaml_config', lambda *_: config) |
| 57 | + command.execute(args) |
| 58 | + |
| 59 | + logs = caplog.text |
| 60 | + assert 'Summary of actions:' in logs |
| 61 | + assert 'Created:' in logs and 'Updated:' in logs |
| 62 | + |
| 63 | + |
| 64 | +def test_fileitem_append_logs_message(tmp_path, caplog): |
| 65 | + caplog.set_level(logging.INFO) |
| 66 | + caplog.set_level(logging.INFO, logger='struct_module.file_item') |
| 67 | + |
| 68 | + parser = argparse.ArgumentParser() |
| 69 | + command = GenerateCommand(parser) |
| 70 | + args = parser.parse_args(['struct-x', str(tmp_path / 'base')]) |
| 71 | + |
| 72 | + base_dir = tmp_path / 'base' |
| 73 | + base_dir.mkdir(parents=True, exist_ok=True) |
| 74 | + |
| 75 | + # Existing file to trigger append |
| 76 | + (base_dir / 'append.txt').write_text('start\n') |
| 77 | + |
| 78 | + config = { |
| 79 | + 'files': [ |
| 80 | + {'append.txt': {'content': 'more', 'config_variables': [], 'input_store': _ensure_store(tmp_path)}}, |
| 81 | + ], |
| 82 | + 'folders': [] |
| 83 | + } |
| 84 | + |
| 85 | + args.output = 'file' |
| 86 | + args.input_store = _ensure_store(tmp_path) |
| 87 | + args.dry_run = False |
| 88 | + args.diff = False |
| 89 | + args.vars = None |
| 90 | + args.backup = None |
| 91 | + args.file_strategy = 'append' |
| 92 | + args.global_system_prompt = None |
| 93 | + args.structures_path = None |
| 94 | + args.non_interactive = True |
| 95 | + |
| 96 | + with pytest.MonkeyPatch().context() as mp: |
| 97 | + mp.setattr(command, '_load_yaml_config', lambda *_: config) |
| 98 | + command.execute(args) |
| 99 | + |
| 100 | + logs = caplog.text |
| 101 | + assert 'Appended:' in logs |
| 102 | + |
| 103 | + |
| 104 | +def test_mcp_get_structure_info_rich_rendering(tmp_path): |
| 105 | + # Create a temp YAML structure with folders/struct/with |
| 106 | + yaml_path = tmp_path / 'my-struct.yaml' |
| 107 | + yaml_path.write_text( |
| 108 | + """ |
| 109 | + description: Example |
| 110 | + files: |
| 111 | + - foo.txt: "bar" |
| 112 | + folders: |
| 113 | + - nested: |
| 114 | + struct: |
| 115 | + - sub/one |
| 116 | + - sub/two |
| 117 | + with: |
| 118 | + team: devops |
| 119 | + env: dev |
| 120 | + """ |
| 121 | + ) |
| 122 | + |
| 123 | + async def run(): |
| 124 | + server = StructMCPServer() |
| 125 | + return await server._handle_get_structure_info({ |
| 126 | + 'structure_name': f'file://{yaml_path}', |
| 127 | + }) |
| 128 | + |
| 129 | + result = asyncio.run(run()) |
| 130 | + text = result.content[0].text |
| 131 | + assert 'Folders:' in text |
| 132 | + assert '• struct' in text or '• struct(s):' in text |
| 133 | + assert '• with:' in text |
0 commit comments