RedFlag/.publication/test_commit_voice.py
Fimeg 67e26be2d9 projection: begin the exact-path public epoch
The public tree and its history contain only the listed paths. Earlier projection history remains preserved internally.

Source-Sha: 913fde029b935671833254797f0f20f1eb9fabba

Policy-Sha: 913fde029b935671833254797f0f20f1eb9fabba

Tree-Digest: 180ae530c1058a2a5c89837bdce2d323ae83e669e38590ca72e75b8d92b7262f
2026-09-08 21:59:33 -04:00

84 lines
3.8 KiB
Python

import importlib.util
import json
import os
import subprocess
import tempfile
import unittest
HERE = os.path.dirname(__file__)
SPEC = importlib.util.spec_from_file_location("commit_voice", os.path.join(HERE, "commit_voice.py"))
commit_voice = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(commit_voice)
class CommitVoiceTests(unittest.TestCase):
def test_two_short_public_sentences_pass(self):
self.assertEqual([], commit_voice.violations(
"publication: keep the public edge narrow",
"The exact tested object crosses. Anonymous verification follows.",
))
def test_long_or_three_sentence_prose_blocks(self):
found = dict(commit_voice.violations(
"publication: reject a loose message",
"One sentence. A second sentence. " + "A third sentence is too much. " + "x" * 180,
))
self.assertIn("prose-length", found)
self.assertIn("prose-sentences", found)
def test_projection_trailers_do_not_count_as_prose(self):
body = (
"One public sentence.\n\n"
"Source-Sha: " + "a" * 40 + "\n"
"Policy-Sha: " + "b" * 64 + "\n"
"Tree-Digest: " + "c" * 64
)
self.assertEqual([], commit_voice.violations("publication: carry proof", body))
def test_exception_requires_exact_sha_rule_reason_and_date(self):
sha = "a" * 40
with tempfile.TemporaryDirectory() as work:
path = os.path.join(work, "allowlist.json")
with open(path, "w") as handle:
json.dump({"schema_version": 2, "repository": "Fimeg/RedFlag", "exceptions": [{
"repository": "Fimeg/RedFlag", "sha": sha,
"rule": "prose-length", "reason": "",
"reviewer": "Fimeg", "reviewed": "2026-09-04",
"scope": "test-epoch",
}]}, handle)
with self.assertRaisesRegex(ValueError, "human reason is required"):
commit_voice.load_allowlist(path, "Fimeg/RedFlag")
def test_exception_refuses_another_repository(self):
with tempfile.TemporaryDirectory() as work:
path = os.path.join(work, "allowlist.json")
with open(path, "w") as handle:
json.dump({"schema_version": 2, "repository": "Fimeg/other",
"exceptions": []}, handle)
with self.assertRaisesRegex(ValueError, "repository"):
commit_voice.load_allowlist(path, "Fimeg/RedFlag")
def test_human_exception_approves_only_its_exact_finding(self):
with tempfile.TemporaryDirectory() as repo:
subprocess.run(["git", "init", "-q", repo], check=True)
subprocess.run(["git", "-C", repo, "config", "user.name", "Fimeg"], check=True)
subprocess.run(["git", "-C", repo, "config", "user.email", "test@example.test"], check=True)
proof = os.path.join(repo, "proof")
with open(proof, "w") as handle:
handle.write("proof\n")
subprocess.run(["git", "-C", repo, "add", "proof"], check=True)
subprocess.run(["git", "-C", repo, "commit", "-q", "-m", "voice: test override",
"-m", "x" * 181], check=True)
sha = subprocess.check_output(["git", "-C", repo, "rev-parse", "HEAD"], text=True).strip()
errors, _ = commit_voice.check(repo, "HEAD", {})
self.assertEqual(1, len(errors))
errors, notes = commit_voice.check(repo, "HEAD", {(sha, "prose-length"): {
"reason": "the human reviewed this exact historical message",
"reviewed": "2026-09-04",
}})
self.assertEqual([], errors)
self.assertEqual(1, len(notes))
if __name__ == "__main__":
unittest.main()