import subprocess from pathlib import Path from app.git_sync import GitSyncManager def test_git_sync_conflict_handling(tmp_path: Path): repo_dir = tmp_path / "fake_repo" repo_dir.mkdir() subprocess.run(["git", "init", "-b", "main", str(repo_dir)], check=True) subprocess.run(["git", "-C", str(repo_dir), "config", "user.email", "test@test.local"], check=True) subprocess.run(["git", "-C", str(repo_dir), "config", "user.name", "Test User"], check=True) test_file = repo_dir / "test.txt" test_file.write_text("initial") subprocess.run(["git", "-C", str(repo_dir), "add", "."], check=True) subprocess.run(["git", "-C", str(repo_dir), "commit", "-m", "initial commit"], check=True) mgr = GitSyncManager(wiki_dir=repo_dir) assert mgr.ensure_repo() is True assert mgr.status == "ok" assert mgr.is_conflict is False def test_git_url_encoding(monkeypatch): monkeypatch.setattr("app.git_sync.GIT_USERNAME", "moritz@graf") monkeypatch.setattr("app.git_sync.GIT_PASSWORD", "p@ss:w/ord%123") monkeypatch.setattr("app.git_sync.GIT_REPO_URL", "https://git.moritzgraf.de/moritz/knowledge-wiki.git") mgr = GitSyncManager() auth_url = mgr.get_auth_repo_url() assert auth_url == "https://moritz%40graf:p%40ss%3Aw%2Ford%25123@git.moritzgraf.de/moritz/knowledge-wiki.git" def test_ensure_repo_handles_non_empty_dir(tmp_path: Path): # Simulate a PVC containing lost+found before repo initialization wiki_pvc = tmp_path / "wiki_pvc" wiki_pvc.mkdir() (wiki_pvc / "lost+found").mkdir() # Create upstream origin to fetch from origin = tmp_path / "origin.git" subprocess.run(["git", "init", "--bare", str(origin)], check=True) # Push initial commit to bare origin work = tmp_path / "work" work.mkdir() subprocess.run(["git", "init", "-b", "main", str(work)], check=True) subprocess.run(["git", "-C", str(work), "config", "user.name", "Admin"], check=True) subprocess.run(["git", "-C", str(work), "config", "user.email", "admin@test.local"], check=True) (work / "README.md").write_text("# Knowledge Wiki") subprocess.run(["git", "-C", str(work), "add", "."], check=True) subprocess.run(["git", "-C", str(work), "commit", "-m", "init"], check=True) subprocess.run(["git", "-C", str(work), "remote", "add", "origin", str(origin)], check=True) subprocess.run(["git", "-C", str(work), "push", "origin", "main"], check=True) # Now verify GitSyncManager initializes cleanly into non-empty directory mgr = GitSyncManager(wiki_dir=wiki_pvc) # Point URL to file repo import app.git_sync app.git_sync.GIT_REPO_URL = str(origin) app.git_sync.GIT_USERNAME = "" app.git_sync.GIT_PASSWORD = "" success = mgr.ensure_repo() assert success is True assert (wiki_pvc / ".git").is_dir() assert (wiki_pvc / "README.md").is_file() assert (wiki_pvc / "lost+found").is_dir()