"""The sets as committed: their shape, that nothing of the answer or of where they were made is in what the model reads, and that every kind is asked.""" import json import os import re import sys import unittest HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.dirname(HERE)) # scan/ import prompts # noqa: E402 SETS = os.path.join(os.path.dirname(HERE), "sets") USERS = {"linwei", "meilin", "haoran", "yuki", "tomas", "anika", "soren", "dalia", "ruben", "ines"} COUNT = {"posture": 80, "process": 100} def load(name): with open(os.path.join(SETS, name + ".json"), encoding="utf-8") as f: return json.load(f) class Sets(unittest.TestCase): def each(self): for name in COUNT: yield name, load(name) def test_how_many_and_where_from(self): for name, s in self.each(): with self.subTest(name): self.assertEqual(len(s["items"]), COUNT[name]) self.assertEqual(len({i["id"] for i in s["items"]}), COUNT[name]) self.assertRegex(s["scan"]["commit"], r"^[0-9a-f]{7,40}$") self.assertIn("rules", s["scan"]["version"]) self.assertIsInstance(s["seed"], int) self.assertEqual(s["clock"], "2026-10-20T12:00:00Z") def test_the_kinds_are_the_ones_the_prompt_names(self): for name, s in self.each(): with self.subTest(name): self.assertEqual(s["kinds"], list(prompts.KINDS[name])) for k in s["kinds"]: self.assertIn(k + "(", prompts.RULES[name]) # the criteria say every kind's severity def test_answers_are_what_a_rule_can_say(self): for name, s in self.each(): for i in s["items"]: for a in i["answer"]: with self.subTest(i["id"], rule=a["rule"]): self.assertIn(a["kind"], s["kinds"]) self.assertIn(a["severity"], prompts.SEVERITIES) self.assertIn(a["verdict"], prompts.VERDICTS) self.assertTrue(a["rule"].endswith("." + a["kind"])) def test_every_kind_is_asked_and_some_questions_have_nothing_to_report(self): for name, s in self.each(): with self.subTest(name): asked = {a["kind"] for i in s["items"] for a in i["answer"]} self.assertEqual(asked, set(s["kinds"])) self.assertGreaterEqual(sum(1 for i in s["items"] if not i["answer"]), 15) self.assertGreaterEqual(sum(1 for i in s["items"] if len(i["answer"]) >= 2), 10) def test_the_model_reads_nothing_of_the_answer_or_of_where_the_set_was_made(self): for name, s in self.each(): for i in s["items"]: with self.subTest(i["id"]): facts = i["facts"] for a in i["answer"]: self.assertNotIn(a["rule"], facts) if a["title"]: self.assertNotIn(a["title"], facts) for word in ("luoke", "fakes.json", "bench-scan", "FIXTURE", "not a real library"): self.assertNotIn(word, facts) for user in re.findall(r"/Users/([^/\s]+)", facts): self.assertIn(user, USERS) # made-up hosts only for host in re.findall(r"[a-z0-9-]+(?:\.[a-z0-9-]+)*\.(?:com|net|org|cn)\b", facts): self.fail(f"a host that is not .example: {host}") def test_process_tables_have_30_to_80_rows_and_answers_name_rows_of_them(self): for i in load("process")["items"]: with self.subTest(i["id"]): pids = re.findall(r"(?m)^PID (\d+) ", i["facts"]) self.assertEqual(len(pids), len(set(pids))) self.assertTrue(30 <= len(pids) <= 80, len(pids)) for a in i["answer"]: if a["kind"] == "partial": self.assertEqual(a["targets"], []) continue self.assertTrue(a["targets"]) self.assertLessEqual(set(a["targets"]), set(pids)) def test_settings_have_no_targets(self): for i in load("posture")["items"]: for a in i["answer"]: self.assertEqual(a["targets"], [], i["id"]) def test_questions_where_the_rules_said_something_else_than_meant_are_listed(self): for name, s in self.each(): ids = {i["id"] for i in s["items"]} for x in s["surprises"]: self.assertIn(x["id"], ids) self.assertNotEqual(x["meant"], x["rules"]) if __name__ == "__main__": unittest.main()