File size: 8,713 Bytes
87dd0fa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""آزمونهای گواهی استفادهٔ موفق: دفتر زنجیرههشدار، شمارش، حریم خصوصی و گواهی دواِمضا.
هیچ آزمونی دفتر واقعی (data/usage-ledger.json) را دست نمیزند؛ همه در پوشهٔ موقتی کار میکنند.
"""
import json
import pathlib
import subprocess
import sys
import tempfile
import unittest
from unittest import mock
BASE = pathlib.Path(__file__).resolve().parents[1]
if str(BASE) not in sys.path:
sys.path.insert(0, str(BASE))
import usage_meter as um # noqa: E402
class LedgerChain(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.log = pathlib.Path(self.tmp.name) / "usage.json"
def tearDown(self):
self.tmp.cleanup()
def test_record_appends_and_chains(self):
a = um.record("judge", "kind=box", path=self.log)
b = um.record("ritual", "روز ۱", path=self.log)
self.assertNotEqual(a["chain"], b["chain"])
self.assertEqual(len(um.rows(self.log)), 2)
self.assertTrue(um.verify(self.log)["ok"])
def test_unknown_kind_is_refused(self):
with self.assertRaises(KeyError):
um.record("چیزعجیب", path=self.log)
self.assertEqual(um.rows(self.log), [])
def test_tamper_breaks_the_chain(self):
um.record("judge", "الف", path=self.log)
um.record("judge", "ب", path=self.log)
data = json.loads(self.log.read_text(encoding="utf-8"))
data[0]["label"] = "دستکاریشده"
self.log.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
v = um.verify(self.log)
self.assertFalse(v["ok"])
self.assertEqual(v["broken_at"], 1)
def test_label_is_cleaned_and_bounded(self):
rec = um.record("run", " چند فاصله " + "x" * 300, path=self.log)
self.assertTrue(rec["label"].startswith("چند فاصله x"), rec["label"][:20])
self.assertLessEqual(len(rec["label"]), 80)
self.assertNotIn(" ", rec["label"])
class SummaryAndPrivacy(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.log = pathlib.Path(self.tmp.name) / "usage.json"
for kind in ("judge", "judge", "ritual", "bundle", "blind"):
um.record(kind, "نمونه", path=self.log)
def tearDown(self):
self.tmp.cleanup()
def test_summary_counts_and_days(self):
s = um.summary(self.log)
self.assertEqual(s["total"], 5)
self.assertEqual(s["by_kind"]["judge"], 2)
self.assertEqual(s["active_days"], 1)
self.assertEqual(s["today"], 5)
self.assertTrue(s["verify"]["ok"])
self.assertTrue(s["kept_local"])
def test_privacy_guard_flags_financial_keys(self):
self.assertTrue(um.privacy_check(self.log)["clean"])
data = json.loads(self.log.read_text(encoding="utf-8"))
data[0]["profit"] = 1234
self.log.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
res = um.privacy_check(self.log)
self.assertFalse(res["clean"])
self.assertIn("profit", res["offending_keys"])
def test_ledger_never_contains_financial_field_names(self):
raw = self.log.read_text(encoding="utf-8")
for word in ("price", "spot", "strike", "premium", "balance", "account"):
self.assertNotIn(f'"{word}"', raw)
class Statement(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.dir = pathlib.Path(self.tmp.name)
self.log = self.dir / "usage.json"
for kind in ("judge", "decision", "ritual", "exam", "judge"):
um.record(kind, "نمونه", path=self.log)
def tearDown(self):
self.tmp.cleanup()
def test_statement_files_and_content(self):
s = um.summary(self.log, trial_days=3, real_days=2)
hp, mp = um.build_statement(s, client="شرکت نمونه", period="مهر ۱۴۰۵",
html_path=self.dir / "st.html", md_path=self.dir / "st.md")
md = mp.read_text(encoding="utf-8")
self.assertTrue(hp.exists() and mp.exists())
self.assertIn("شرکت نمونه", md)
self.assertIn("مهر ۱۴۰۵", md)
self.assertIn("امضای ارائهدهنده", md)
self.assertIn("امضای کاربر/شرکت", md)
self.assertIn("اثبات نمیکند", md)
self.assertIn("اثر انگشت زنجیرهٔ دفتر", md)
self.assertIn("سالم ✅", md)
self.assertIn("| داوری سیگنال (میماند یا میافتد) | `judge` | 2 |", md)
html = hp.read_text(encoding="utf-8")
self.assertIn("این گواهی چه میگوید و چه نمیگوید", html)
self.assertIn("Masoud Mojarabian", html)
def test_empty_ledger_still_builds_honest_statement(self):
empty = self.dir / "empty.json"
s = um.summary(empty)
hp, mp = um.build_statement(s, html_path=self.dir / "e.html", md_path=self.dir / "e.md")
md = mp.read_text(encoding="utf-8")
self.assertIn("مجموع کارهای ثبتشده: **0**", md)
self.assertIn("اثبات نمیکند", md)
self.assertEqual(s["active_days"], 0)
def test_broken_chain_is_shown_on_the_statement(self):
data = json.loads(self.log.read_text(encoding="utf-8"))
data[0]["kind"] = "bundle"
self.log.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
s = um.summary(self.log)
_, mp = um.build_statement(s, html_path=self.dir / "b.html", md_path=self.dir / "b.md")
self.assertIn("شکسته ⚠️", mp.read_text(encoding="utf-8"))
class Cli(unittest.TestCase):
"""فقط دستورهای خواندنی را از خط فرمان میآزماییم تا دفتر واقعی دست نخورد."""
def _run(self, *args):
return subprocess.run([sys.executable, "usage_meter.py", *args], cwd=str(BASE),
capture_output=True, text=True, timeout=120)
def test_summary_cli(self):
r = self._run("--json")
self.assertEqual(r.returncode, 0, r.stderr)
data = json.loads(r.stdout)
self.assertIn("total", data)
self.assertIn("kept_local", data)
def test_verify_cli(self):
r = self._run("--verify")
self.assertIn(r.returncode, (0, 1))
self.assertIn("دفتر", r.stdout)
def test_unknown_kind_cli_fails_cleanly(self):
r = self._run("--record", "چیزعجیب")
self.assertEqual(r.returncode, 1)
self.assertIn("خطا", r.stdout)
class CompatWithTheApp(unittest.TestCase):
"""کلیدها و نامهایی که کارساز/رابط گرافیکی انتظار دارند نباید بشکنند."""
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.log = pathlib.Path(self.tmp.name) / "u.json"
for kind in ("run", "judge", "blind", "ladder", "pledge", "exam", "dossier",
"bundle", "snapshot", "import"):
um.record_event(kind, label=um.KIND_LABELS[kind], path=self.log)
def tearDown(self):
self.tmp.cleanup()
def test_every_kind_the_server_uses_is_known(self):
for kind in ("run", "judge", "blind", "ladder", "pledge", "exam", "dossier",
"bundle", "snapshot", "import"):
self.assertIn(kind, um.KIND_LABELS)
def test_summary_exposes_app_keys(self):
s = um.summary(self.log)
for key in ("success_total", "rows", "chain_ok", "broken_at", "by_kind", "active_days"):
self.assertIn(key, s)
self.assertEqual(s["success_total"], 10)
self.assertEqual(s["rows"], 10)
self.assertTrue(s["chain_ok"])
def test_verify_chain_matches_verify(self):
self.assertEqual(um.verify_chain(self.log), um.verify(self.log))
def test_build_statement_returns_two_paths(self):
hp, mp = um.build_statement(um.summary(self.log),
html_path=pathlib.Path(self.tmp.name) / "a.html",
md_path=pathlib.Path(self.tmp.name) / "a.md")
self.assertTrue(pathlib.Path(hp).exists() and pathlib.Path(mp).exists())
def test_record_event_never_raises_on_weird_label(self):
rec = um.record_event("run", label=" ", note=None, path=self.log)
self.assertEqual(rec["label"], "")
if __name__ == "__main__":
unittest.main(verbosity=2)
|