"""The Logbook store and its automatic commit log.""" import json import shutil import subprocess import unittest from helpers import HarborCase from harbor import logbook, logbook_auto from harbor.util import HarborError class LogbookStore(HarborCase): def setUp(self): super().setUp() self.h.project.logbook = {"dir": str(self.dir / "logbook")} def test_entries_stats_and_milestones(self): e = logbook.add_entry(self.h.project, "First", "Something happened.", kind="tooling", at="2026-10-05T10:00:00Z") self.assertEqual(e["id"], "20261005-1000-1") with self.assertRaises(HarborError): logbook.add_entry(self.h.project, "x", "y", kind="gossip") logbook.set_stats(self.h.project, {"functions": 10, "classified_percent": 50.0, "weighted_percent": 5.0}) logbook.upsert_milestone(self.h.project, {"id": "m1", "order": 1, "code": "M1", "title": "T", "items": []}) data = logbook.read_all(self.h.project) self.assertEqual((data["log"][0]["title"], data["stats"]["functions"], data["history"][0]["classified"], data["milestones"][0]["code"]), ("First", 10, 50.0, "M1")) class LogbookAuto(HarborCase): def setUp(self): super().setUp() self.repo = self.dir / "repo" self.repo.mkdir() git = lambda *a: subprocess.run(["git", "-C", str(self.repo), *a], check=True, capture_output=True) git("init", "-q") git("config", "user.name", "T") git("config", "user.email", "t@example.com") (self.repo / "a.txt").write_text("1", encoding="utf-8") git("add", "-A") git("commit", "-q", "-m", "first") self.first = subprocess.run(["git", "-C", str(self.repo), "rev-parse", "HEAD"], capture_output=True, text=True).stdout.strip() (self.repo / "annotations").mkdir() (self.repo / "annotations" / "names.csv").write_text("x", encoding="utf-8") git("add", "-A") git("commit", "-q", "-m", "Name things", "-m", "Body line one.\nCo-Authored-By: someone") self.h.project.logbook = {"dir": str(self.dir / "logbook"), "decomp": str(self.repo)} def test_new_commits_become_entries_once(self): logbook_auto._save_state(self.h.project, {"last_head": self.first}) out = logbook_auto.refresh(self.h.project) self.assertEqual(out["entries"], 1) entry = logbook.read_all(self.h.project)["log"][0] self.assertEqual((entry["title"], entry["kind"]), ("Name things", "naming")) self.assertIn("Body line one.", entry["detail"]) self.assertNotIn("Co-Authored-By", entry["detail"]) self.assertTrue(entry["at"].endswith("Z")) self.assertFalse(logbook_auto.refresh(self.h.project)["changed"]) if __name__ == "__main__": unittest.main()