import os import subprocess from pathlib import Path WORK = Path(os.environ["TB_WORK"]) EXPECTED_APP = '''VERSION = 2 DEFAULTS = {"retries": 3, "timeout": 30} def main(): return "v2" def load(path): with open(path, "r", encoding="utf-8") as handle: return handle.read() ''' ORIGINAL_SUBJECTS = [ "Initial import", "Add plan notes", "Add config loader", "Drop legacy module", "Document build", ] def git(*args): return subprocess.run( ["git"] + list(args), cwd=str(WORK), capture_output=True, text=True, encoding="utf-8", ) def subjects(): result = git("log", "--format=%s", "main") assert result.returncode == 0, result.stderr return [line for line in result.stdout.splitlines() if line] def test_app_py_restored_from_config_loader_commit(): path = WORK / "src" / "app.py" assert path.is_file(), "src/app.py was not restored" assert path.read_text(encoding="utf-8") == EXPECTED_APP def test_restore_commit_message(): assert "restore app.py" in subjects() def test_app_py_is_committed_not_just_written(): result = git("ls-files", "--error-unmatch", "src/app.py") assert result.returncode == 0, "src/app.py is not tracked" def test_recovered_branch_points_at_the_wip_commit(): ref = git("rev-parse", "--verify", "refs/heads/recovered") assert ref.returncode == 0, "branch 'recovered' does not exist" message = git("log", "-1", "--format=%s", "recovered") assert message.stdout.strip() == "wip: plan" def test_recovered_is_not_an_ancestor_of_main(): ref = git("rev-parse", "--verify", "refs/heads/recovered") assert ref.returncode == 0, "branch 'recovered' does not exist" result = git("merge-base", "--is-ancestor", "recovered", "main") assert result.returncode == 1, "the rescued commit must stay off main" def test_recovered_commit_keeps_the_work_in_progress_plan(): blob = git("show", "recovered:notes/plan.md") assert blob.returncode == 0, "recovered commit has no notes/plan.md" assert "step two" in blob.stdout def test_tag_v1_points_at_the_config_loader_commit(): tag = git("rev-list", "-n", "1", "v1.0") assert tag.returncode == 0, "tag v1.0 does not exist" subject = git("log", "-1", "--format=%s", tag.stdout.strip()) assert subject.stdout.strip() == "Add config loader" def test_junk_is_ignored_but_still_present(): for rel in ("debug.log", "build/tmp.o"): assert (WORK / rel).is_file(), "%s was deleted" % rel result = git("check-ignore", "-q", rel) assert result.returncode == 0, "%s is not ignored" % rel def test_worktree_is_clean(): status = git("status", "--porcelain") assert status.stdout.strip() == "", "dirty tree:\n%s" % status.stdout def test_original_history_is_preserved(): current = subjects() for subject in ORIGINAL_SUBJECTS: assert subject in current, "commit %r disappeared from main" % subject