|
| 1 | +# The MIT License (MIT) |
| 2 | +# Copyright © 2025 Entrius |
| 3 | + |
| 4 | +""" |
| 5 | +Tests for `gitt config set` corrupt-file handling. |
| 6 | +
|
| 7 | +Regression: a single corrupt-config recovery used to wipe every previously-set |
| 8 | +key and replace the file with `{key: value}`. Operator's `network`, |
| 9 | +`contract_address`, `ws_endpoint`, and `hotkey` would silently disappear and |
| 10 | +the next `gitt issues ...` would fall through to finney mainnet. Companion to |
| 11 | +PR #817 / issue #816 (read side) and closes issue #845 (write side). |
| 12 | +""" |
| 13 | + |
| 14 | +import json |
| 15 | +from pathlib import Path |
| 16 | +from unittest.mock import patch |
| 17 | + |
| 18 | +import pytest |
| 19 | +from click.testing import CliRunner |
| 20 | + |
| 21 | +from gittensor.cli.main import config_group |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def temp_config_dir(tmp_path: Path): |
| 26 | + """Redirect CONFIG_FILE/GITTENSOR_DIR to a temp dir for one test.""" |
| 27 | + config_dir = tmp_path / '.gittensor' |
| 28 | + config_file = config_dir / 'config.json' |
| 29 | + with ( |
| 30 | + patch('gittensor.cli.main.GITTENSOR_DIR', config_dir), |
| 31 | + patch('gittensor.cli.main.CONFIG_FILE', config_file), |
| 32 | + ): |
| 33 | + yield config_dir, config_file |
| 34 | + |
| 35 | + |
| 36 | +def _read(config_file: Path) -> dict: |
| 37 | + return json.loads(config_file.read_text()) if config_file.exists() else {} |
| 38 | + |
| 39 | + |
| 40 | +class TestConfigSetCorruption: |
| 41 | + """`gitt config set` must not destroy other keys when the file is corrupt.""" |
| 42 | + |
| 43 | + def test_corrupt_config_aborts_with_nonzero_exit(self, temp_config_dir): |
| 44 | + config_dir, config_file = temp_config_dir |
| 45 | + config_dir.mkdir(parents=True) |
| 46 | + # Truncated JSON — simulates an interrupted write or manual edit. |
| 47 | + config_file.write_text('{"network": "test"') |
| 48 | + |
| 49 | + runner = CliRunner() |
| 50 | + result = runner.invoke(config_group, ['set', 'hotkey', 'default']) |
| 51 | + |
| 52 | + assert result.exit_code != 0 |
| 53 | + assert 'not valid JSON' in result.output |
| 54 | + assert 'Refusing to overwrite' in result.output |
| 55 | + |
| 56 | + def test_corrupt_config_preserves_existing_file(self, temp_config_dir): |
| 57 | + """The whole point: the bad file is left alone, not clobbered.""" |
| 58 | + config_dir, config_file = temp_config_dir |
| 59 | + config_dir.mkdir(parents=True) |
| 60 | + original_bytes = b'{"network": "test"' |
| 61 | + config_file.write_bytes(original_bytes) |
| 62 | + |
| 63 | + runner = CliRunner() |
| 64 | + runner.invoke(config_group, ['set', 'hotkey', 'default']) |
| 65 | + |
| 66 | + # Byte-for-byte identical: the operator can recover the values they |
| 67 | + # had configured before the corruption. |
| 68 | + assert config_file.read_bytes() == original_bytes |
| 69 | + |
| 70 | + def test_valid_config_still_round_trips(self, temp_config_dir): |
| 71 | + """Non-corrupt files must still merge new keys without loss.""" |
| 72 | + config_dir, config_file = temp_config_dir |
| 73 | + config_dir.mkdir(parents=True) |
| 74 | + config_file.write_text(json.dumps({'network': 'test', 'wallet': 'alice'})) |
| 75 | + |
| 76 | + runner = CliRunner() |
| 77 | + result = runner.invoke(config_group, ['set', 'hotkey', 'default']) |
| 78 | + |
| 79 | + assert result.exit_code == 0, result.output |
| 80 | + assert _read(config_file) == {'network': 'test', 'wallet': 'alice', 'hotkey': 'default'} |
| 81 | + |
| 82 | + def test_missing_config_creates_fresh(self, temp_config_dir): |
| 83 | + """First-run case: no existing file is fine — write a fresh one.""" |
| 84 | + _, config_file = temp_config_dir |
| 85 | + assert not config_file.exists() |
| 86 | + |
| 87 | + runner = CliRunner() |
| 88 | + result = runner.invoke(config_group, ['set', 'wallet', 'alice']) |
| 89 | + |
| 90 | + assert result.exit_code == 0, result.output |
| 91 | + assert _read(config_file) == {'wallet': 'alice'} |
0 commit comments