"""Risk profiles (R-5.3-1, R-5.3-2, R-5.3-3..R-5.3-10).""" import json, os, random, unittest from support import * from sisi import risk class FloorTests(SisiCase): def test_docs_only_is_trivial(self): """R-5.3-4: documentation/text/styling only is trivial.""" p, reasons = risk.assess(["README.md", "docs/guide.txt", "web/site.css"]) self.assertEqual(p, "trivial") self.assertTrue(reasons) def test_single_module_is_isolated(self): """R-5.3-5: one module's behaviour is isolated.""" p, _ = risk.assess(["src/calc.py"]) self.assertEqual(p, "isolated") def test_logic_kinds(self): """R-5.3-6: algorithms, parsing and money are logic.""" for path in ("src/parser/lexer.py", "src/money/rounding.py", "src/algo/sort.py"): with self.subTest(path=path): p, _ = risk.assess([path]) self.assertEqual(p, "logic") def test_two_packages_is_integration(self): """R-5.3-7, R-5.3-11: several top-level packages is integration.""" p, reasons = risk.assess(["src/alpha/a.py", "src/beta/b.py"]) self.assertEqual(p, "integration") self.assertTrue(any("package" in r for r in reasons)) def test_migration_is_data(self): """R-5.3-8: schema/persistence/migration is data.""" p, _ = risk.assess(["db/migrations/0002_add_col.sql"]) self.assertEqual(p, "data") def test_auth_is_critical(self): """R-5.3-9: authorization/secrets/concurrency primitives are critical.""" for path in ("src/auth/login.py", "src/crypto/keys.py", "src/sync/mutex.py"): with self.subTest(path=path): p, _ = risk.assess([path]) self.assertEqual(p, "critical") def test_diff_keyword_raises_floor(self): """R-5.3-1: content keywords count, not only paths.""" p, _ = risk.assess(["src/util.py"], diff_text="+ encrypt the password at rest") self.assertEqual(p, "critical") def test_adding_path_never_lowers_floor(self): """R-5.3-1: property — the floor is monotone in the change set.""" rng = random.Random(7) pool = ["README.md", "src/a/x.py", "src/b/y.py", "db/migrations/m.sql", "src/auth/login.py", "src/parser/p.py", "VERSION", "web/site.css"] order = {name: i for i, name in enumerate(risk.PROFILES)} for _ in range(80): base = rng.sample(pool, rng.randint(0, 4)) extra = rng.choice(pool) before, _ = risk.assess(base) after, _ = risk.assess(base + [extra]) self.assertGreaterEqual(order[after], order[before], (base, extra)) def test_project_override_and_requirements_table(self): """R-5.3-3: a project may override; each profile carries its requirements.""" p, _ = risk.assess(["src/special/thing.py"], extra_patterns=[{"glob": "**/special/**", "profile": "data", "reason": "project override: special holds stored data"}]) self.assertEqual(p, "data") req = risk.requirements_for("trivial") self.assertFalse(req["contract"]) self.assertTrue(risk.requirements_for("critical")["independent_evidence"]) self.assertTrue(risk.requirements_for("release")["contract"]) LABELS = os.path.join(os.path.dirname(os.path.abspath(__file__)), "risk_labels.json") def _labels(): with open(LABELS, encoding="utf-8") as f: return json.load(f) class LabelledSetTests(SisiCase): """The default table against paths a careful senior engineer has already judged.""" def test_labelled_set_is_large_and_covers_every_profile(self): """R-5.3-3: the set is big enough to mean something and spans every profile.""" rows = _labels() self.assertGreaterEqual(len(rows), 120) self.assertEqual({r["floor"] for r in rows}, set(risk.PROFILES)) self.assertEqual(len({r["path"] for r in rows}), len(rows)) for r in rows: self.assertTrue(r["why"].strip(), r) def test_every_labelled_path_gets_its_labelled_floor(self): """R-5.3-1, R-5.3-2: exact accuracy. Too high wastes a contract and a review on a trivial change (SPEC 5.3); too low leaves a dangerous change unexamined. Seen failing first: the old table got 91 of 177 right (lockfiles critical, React hooks critical, AUTHORS.md critical, release-notes.md release, db/README.md data, sparse.py logic).""" wrong = [] for r in _labels(): got, _ = risk.assess([r["path"]]) if got != r["floor"]: wrong.append("%s: labelled %s, got %s (%s)" % (r["path"], r["floor"], got, r["why"])) self.assertEqual(wrong, [], "\n".join(wrong)) def test_no_data_or_critical_path_is_ever_under_classified(self): """R-5.3-8, R-5.3-9: data and critical paths must never get a lower floor, even if someone loosens the exact test above. Seen failing first: the old table gave eight of them a lower floor.""" order = {name: i for i, name in enumerate(risk.PROFILES)} under = [] for r in _labels(): if r["floor"] in ("data", "critical"): got, _ = risk.assess([r["path"]]) if order[got] < order[r["floor"]]: under.append("%s: %s < %s" % (r["path"], got, r["floor"])) self.assertEqual(under, [], "\n".join(under)) def test_named_false_positives(self): """R-5.3-2: the known false positives are not escalated.""" expect = { "package-lock.json": "integration", "Cargo.lock": "integration", "poetry.lock": "integration", "flake.lock": "integration", "src/hooks/useCart.ts": "isolated", "AUTHORS.md": "trivial", "src/components/AuthorCard.tsx": "isolated", "docs/release-notes.md": "trivial", "src/db/README.md": "trivial", "src/sparse.py": "isolated", "docs/installation.md": "trivial"} for path, floor in expect.items(): with self.subTest(path=path): self.assertEqual(risk.assess([path])[0], floor) def test_dependency_change_is_integration_not_trivial_or_critical(self): """R-5.3-7, R-5.3-11: a manifest or lockfile changes code you did not write.""" for path in ("package.json", "yarn.lock", "go.mod", "Gemfile.lock", "requirements.txt"): with self.subTest(path=path): self.assertEqual(risk.assess([path])[0], "integration") def test_test_of_critical_code_is_logic(self): """R-5.3-9: a test of critical code is not critical, and not trivial.""" self.assertEqual(risk.assess(["tests/test_auth.py"])[0], "logic") self.assertEqual(risk.assess(["tests/test_widget.py"])[0], "isolated") def test_code_with_its_test_is_not_a_cross_package_change(self): """R-5.3-2: the commonest change, code plus its test, is not an integration.""" self.assertEqual(risk.assess(["sisi/risk.py", "tests/test_risk.py"])[0], "isolated") self.assertEqual(risk.assess(["src/a/x.py", "docs/a.md", "src/a/x.test.ts"])[0], "isolated") def test_keywords_in_docs_and_lockfiles_do_not_escalate(self): """R-5.3-2: a README that says 'password' is not a credentials change; code that does is.""" doc = "diff --git a/README.md b/README.md\n+ reset your password\n" lock = "diff --git a/package-lock.json b/package-lock.json\n+ \"crypto-js\": \"4.2.0\"\n" self.assertEqual(risk.assess(["README.md"], diff_text=doc)[0], "trivial") self.assertEqual(risk.assess(["package-lock.json"], diff_text=lock)[0], "integration") code = "diff --git a/src/a.py b/src/a.py\n+ reset your password\n" self.assertEqual(risk.assess(["src/a.py"], diff_text=code)[0], "critical") self.assertEqual(risk.assess(["src/a.py", "README.md"], diff_text=doc + code)[0], "critical") class RiskCliTests(SisiCase): def _repo_with(self, files): self.git_init() for rel, text in files.items(): p = os.path.join(self.proj, rel) os.makedirs(os.path.dirname(p), exist_ok=True) with open(p, "w") as f: f.write(text) def test_risk_json_readme_change(self): """R-5.3-2: a README edit does not get the full process.""" self._repo_with({"README.md": "# hi\nchanged\n"}) r = self.sisi("risk", "--json") self.assertEqual(r.returncode, 0, r.stderr) d = json.loads(r.stdout) self.assertEqual(d["floor"], "trivial") self.assertIn("requirements", d) def test_risk_json_migration_change(self): """R-5.3-8: a migrations change floors at data.""" self._repo_with({"db/migrations/0001.sql": "ALTER TABLE t ADD c INT;\n"}) d = json.loads(self.sisi("risk", "--json").stdout) self.assertEqual(d["floor"], "data") def test_risk_declared_profile_from_contract(self): """R-5.3-3: the declared profile in .sisi/contract.json is reported.""" self._repo_with({"src/a.py": "x = 1\n"}) os.makedirs(os.path.join(self.proj, ".sisi"), exist_ok=True) with open(os.path.join(self.proj, ".sisi", "contract.json"), "w") as f: json.dump({"id": "t", "goal": "g", "checks": [], "risk": {"profile": "logic"}}, f) d = json.loads(self.sisi("risk", "--json").stdout) self.assertEqual(d["declared"], "logic") if __name__ == "__main__": unittest.main()