"""Rendering-only briefs must preserve existing data on incomplete API snapshots.""" from unittest.mock import Mock import pytest import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) import consistency_check as owner def responses(base, path, params=None, **kwargs): if path.startswith('/repos/'): return {'id': 'repo-1', 'domain_slug': 'infotech'} return [] def test_render_does_not_commit_and_timestamp_only_retry_is_unchanged(tmp_path, monkeypatch): monkeypatch.setattr(owner, '_api_get', responses) commit = Mock(side_effect=AssertionError('Git operations forbidden')) monkeypatch.setattr(owner, '_git_commit_writeback', commit) assert owner._write_custodian_brief('unused', 'demo', str(tmp_path), commit=False, strict=True) before = (tmp_path / '.custodian-brief.md').read_bytes() assert not owner._write_custodian_brief('unused', 'demo', str(tmp_path), commit=False, strict=True) assert (tmp_path / '.custodian-brief.md').read_bytes() == before commit.assert_not_called() @pytest.mark.parametrize('failed_path', ['/repos/demo', '/workplans', '/repo-goals', '/messages', '/tasks']) @pytest.mark.parametrize('failure', [None, {'_error': 'offline'}, 'malformed']) def test_partial_snapshot_preserves_existing_brief(tmp_path, monkeypatch, failed_path, failure): def read(base, path, params=None, **kwargs): if path == failed_path: return failure if path == '/workplans': return [{'id': 'wp-1', 'title': 'Waiting'}] return responses(base, path, params, **kwargs) monkeypatch.setattr(owner, '_api_get', read) path = tmp_path / '.custodian-brief.md'; path.write_text('preserved') with pytest.raises(ValueError, match='unavailable or malformed'): owner._write_custodian_brief('unused', 'demo', str(tmp_path), commit=False, strict=True) assert path.read_text() == 'preserved' def test_check_has_no_write(tmp_path, monkeypatch): monkeypatch.setattr(owner, '_api_get', responses) path = tmp_path / '.custodian-brief.md'; path.write_text('preserved') with pytest.raises(ValueError, match='differs'): owner._write_custodian_brief('unused', 'demo', str(tmp_path), commit=False, strict=True, check=True) assert path.read_text() == 'preserved'