"""Behavioral checks beyond the fixed 60 requirement scenarios.""" import copy import hashlib import json import math import re import shutil import subprocess import sys import tempfile import unittest from contextlib import contextmanager from pathlib import Path import yaml from cad_knowledge import KnowledgeBase from cad_knowledge.library import read_document from cad_knowledge.render import generated_outputs ROOT = Path(__file__).resolve().parents[1] class KnowledgeTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.kb = KnowledgeBase(ROOT) def params(self, fid): eid = self.kb.families[fid]["meta"]["example_id"] return copy.deepcopy(self.kb.documents[eid]["meta"]["params"]) @contextmanager def generation_fixture(self): """Keep loader regressions independent of the live admission policy.""" with tempfile.TemporaryDirectory() as directory: root = Path(directory) for name in ("references", "schemas"): shutil.copytree(ROOT / name, root / name) (root / "generation/candidates").mkdir(parents=True) catalog = {"schema_version": 1, "types": [ {"id": "gear-transmission", "parent": None, "kind": "group"}, {"id": "involute-spur-gear", "parent": "gear-transmission", "kind": "part"}, {"id": "internal-spur-gear", "parent": "gear-transmission", "kind": "part"}, ]} (root / "generation/catalog.json").write_text(json.dumps(catalog), encoding="utf-8") policy = {"schema_version": 1, "documents": []} for type_id, text in [("involute-spur-gear", "External tooth construction fixture"), ("internal-spur-gear", "Internal ring construction fixture")]: relative = f"generation/candidates/{type_id}.md" content = (text + "\n").encode("utf-8") (root / relative).write_bytes(content) policy["documents"].append({"doc_id": f"fixture-{type_id}", "version": "0.1.0", "family_ids": [type_id], "status": "experimental", "engine_build": "1" * 24, "compatible_builds": ["2" * 24], "path": relative, "sha256": hashlib.sha256(content).hexdigest()}) (root / "generation/policy.json").write_text(json.dumps(policy), encoding="utf-8") yield KnowledgeBase(root), root, policy def test_catalog_and_examples_are_consistent(self): check = self.kb.check() self.assertTrue(check["passed"], check["errors"]) self.assertFalse(check["geometry_validated"]) def test_all_registered_family_names_and_aliases_are_retrievable(self): for fid, doc in self.kb.families.items(): meta = doc["meta"] for term in [meta["name_zh"], meta["name_en"], *meta["aliases"]]: with self.subTest(family=fid, term=term): result = self.kb.search("制作 " + term + " please") self.assertIn(fid, [c["family_id"] for c in result["candidates"]]) self.assertIsNone(result["selected_family_id"]) def test_english_word_boundaries_and_unicode_normalization(self): self.assertFalse(self.kb.search("xstepped shafting")["candidates"]) result = self.kb.search("MOUNTING PLATE") self.assertEqual(result["candidates"][0]["family_id"], "mounting-plate") def test_retrieval_preserves_negation_and_ambiguity(self): result = self.kb.search("不要台阶轴,安装板或者六角头螺栓") self.assertTrue({"negation", "multi_family", "ambiguous"}.issubset(result["flags"])) self.assertIsNone(result["selected_family_id"]) self.assertTrue(result["needs_semantic_review"]) def test_context_keeps_parameters_and_only_relevant_families(self): context = self.kb.build_context(["mounting-plate", "mounting-plate"], ["feature-holes"]) ids = [d["doc_id"] for d in context["manifest"]] self.assertEqual(ids, ["common-conventions", "mounting-plate", "feature-holes"]) self.assertIn("parameter_schema:", context["text"]) self.assertIn("x-datum:", context["text"]) self.assertNotIn("ribbed-bearing-housing@", context["text"]) self.assertNotIn("example-mounting-plate@", context["text"]) def test_multi_family_context_deduplicates_shared_features(self): context = self.kb.build_context(["involute-spur-gear", "ribbed-bearing-housing"]) ids = [d["doc_id"] for d in context["manifest"]] self.assertEqual(ids.count("feature-holes"), 1) self.assertEqual(len(ids), len(set(ids))) def test_examples_are_opt_in_and_include_actual_input(self): context = self.kb.build_context(["covered-thin-wall-enclosure"], include_examples=True) self.assertEqual(context["manifest"][-1]["doc_id"], "example-covered-thin-wall-enclosure") self.assertIn("outer_length: 120", context["text"]) self.assertIn("expected_derived:", context["text"]) def test_generation_context_does_not_promote_retrieval_evidence(self): engine = "5707e4ae25621c4a32c40108" result = self.kb.build_generation_context(["involute-spur-gear"], engine) self.assertEqual(result["text"], "") self.assertEqual(result["skipped"][0]["reason"], "not_approved") trial = self.kb.build_generation_context(["involute-spur-gear"], engine, experimental=True) self.assertEqual(len(trial["manifest"]), 1) self.assertNotIn("gear-browser-workflow", [d["doc_id"] for d in trial["manifest"]]) self.assertNotIn("parameter_schema:", trial["text"]) self.assertNotIn("common-conventions@", trial["text"]) unrelated = self.kb.build_generation_context(["mounting-plate"], engine, experimental=True) self.assertTrue({entry['doc_id'] for entry in unrelated['manifest']} <= {'plate-through-holes'}) self.assertNotIn('gear-browser-equation-workflow', [entry['doc_id'] for entry in unrelated['manifest']]) mismatch = self.kb.build_generation_context(["involute-spur-gear"], "another-engine", experimental=True) self.assertEqual(mismatch['text'], trial['text']) def test_generation_admission_hash_evidence_and_deduplication(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) shutil.copytree(ROOT / "references", root / "references") shutil.copytree(ROOT / "schemas", root / "schemas") shutil.copytree(ROOT / "generation", root / "generation") policy_path = root / "generation/policy.json" policy = json.loads(policy_path.read_text()) entry = policy["documents"][0] entry["status"] = "approved" policy_path.write_text(json.dumps(policy)) kb = KnowledgeBase(root) result = kb.build_generation_context(["involute-spur-gear"], entry["engine_build"]) self.assertEqual(result["skipped"][0]["reason"], "benefit_evidence_missing") entry["evidence"] = {"verdict": "benefit_observed", "engine_replay": "fixture replay", "ab_comparison": "fixture comparison", "parameter_transfer": "fixture transfer"} policy_path.write_text(json.dumps(policy)) result = kb.build_generation_context(["involute-spur-gear"] * 2, entry["engine_build"]) self.assertEqual(len(result["manifest"]), 1) candidate = root / entry["path"] self.assertEqual(result["text"], candidate.read_text().strip()) candidate.write_text(candidate.read_text() + "changed") result = kb.build_generation_context(["involute-spur-gear"], entry["engine_build"]) self.assertEqual(result["text"], "") self.assertEqual(result["skipped"][0]["reason"], "content_changed") entry["path"] = "../outside.md" policy_path.write_text(json.dumps(policy)) with self.assertRaisesRegex(ValueError, "outside registry"): kb.build_generation_context(["involute-spur-gear"], entry["engine_build"]) def test_generation_reports_build_only_as_diagnostic_metadata(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) for name in ("references", "schemas", "generation"): shutil.copytree(ROOT / name, root / name) policy_path = root / "generation/policy.json" policy = json.loads(policy_path.read_text()) entry = next(item for item in policy["documents"] if item["status"] == "experimental") historical_build, new_build = "1" * 24, "2" * 24 entry["engine_build"] = historical_build entry["compatible_builds"] = [new_build] policy["documents"] = [entry] policy_path.write_text(json.dumps(policy)) kb = KnowledgeBase(root) result = kb.build_generation_context(["involute-spur-gear"], new_build, experimental=True) self.assertTrue(result["text"]) self.assertEqual(result["manifest"][0]["engine_build"], new_build) for independent_build in (historical_build, "3" * 24, 'frontend-only-update', '', None): with self.subTest(build=independent_build): loaded = kb.build_generation_context(["involute-spur-gear"], independent_build, experimental=True) self.assertEqual(loaded["text"], result["text"]) self.assertEqual(loaded["manifest"][0]["engine_build"], independent_build) self.assertEqual(kb.build_generation_context(["involute-spur-gear"], experimental=True)["text"], result["text"]) def test_generation_legacy_build_metadata_still_replays_historical_records(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) for name in ("references", "schemas", "generation"): shutil.copytree(ROOT / name, root / name) policy_path = root / "generation/policy.json" policy = json.loads(policy_path.read_text()) entry = next(item for item in policy["documents"] if item["status"] == "experimental") entry.pop("compatible_builds", None) policy["documents"] = [entry] policy_path.write_text(json.dumps(policy)) kb = KnowledgeBase(root) result = kb.build_generation_context(["involute-spur-gear"], entry["engine_build"], experimental=True) self.assertTrue(result["text"]) self.assertEqual(kb.build_generation_context(["involute-spur-gear"], "4" * 24, experimental=True)["text"], result["text"]) def test_generation_ignores_historical_build_lists_for_loading(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) for name in ("references", "schemas", "generation"): shutil.copytree(ROOT / name, root / name) policy_path = root / "generation/policy.json" policy = json.loads(policy_path.read_text()) entry = next(item for item in policy["documents"] if item["status"] == "experimental") policy["documents"] = [entry] kb = KnowledgeBase(root) for invalid in (None, [], "1" * 24, ["any"], ["*"], [24], ["F" * 24], ["1" * 23], ["1" * 24, "1" * 24]): with self.subTest(compatible_builds=invalid): entry["compatible_builds"] = invalid policy_path.write_text(json.dumps(policy)) result = kb.build_generation_context(["involute-spur-gear"], experimental=True) self.assertTrue(result["text"]) self.assertEqual(result["manifest"][0]["engine_build"], None) def test_generation_cli_build_label_is_optional_and_does_not_change_loading(self): command = [sys.executable, "-m", "cad_knowledge", "generation-context", "involute-spur-gear"] missing = subprocess.run(command, cwd=ROOT, capture_output=True, text=True) self.assertEqual(missing.returncode, 0, missing.stderr) self.assertEqual(json.loads(missing.stdout)["text"], "") result = subprocess.run(command + ["--engine-build", "5707e4ae25621c4a32c40108"], cwd=ROOT, capture_output=True, text=True) self.assertEqual(result.returncode, 0, result.stderr) self.assertEqual(json.loads(result.stdout)["text"], "") experimental = subprocess.run(command + ['--experimental'], cwd=ROOT, capture_output=True, text=True) self.assertEqual(experimental.returncode, 0, experimental.stderr) self.assertTrue(json.loads(experimental.stdout)["text"]) def test_generation_registered_type_selects_only_its_own_recipe(self): with self.generation_fixture() as (kb, _, _): internal = kb.build_generation_context(["internal-spur-gear"] * 2, "2" * 24, experimental=True) self.assertEqual([entry["doc_id"] for entry in internal["manifest"]], ["fixture-internal-spur-gear"]) self.assertEqual(internal["text"], "Internal ring construction fixture") external = kb.build_generation_context(["involute-spur-gear"], "2" * 24, experimental=True) self.assertEqual([entry["doc_id"] for entry in external["manifest"]], ["fixture-involute-spur-gear"]) self.assertNotIn("Internal", external["text"]) self.assertEqual(kb.build_generation_context(["mounting-plate"], "2" * 24, experimental=True)["text"], "") def test_generation_new_type_keeps_status_hash_and_evidence_guards(self): with self.generation_fixture() as (kb, root, original): policy_path = root / "generation/policy.json" trials = [("default", {}, "2" * 24, False, "not_approved"), ("retired", {"status": "retired"}, "2" * 24, True, "not_approved"), ("wrong-hash", {"sha256": "0" * 64}, "2" * 24, True, "content_changed"), ("unproven-approval", {"status": "approved"}, "2" * 24, False, "benefit_evidence_missing")] for label, change, engine, experimental, reason in trials: with self.subTest(case=label): policy = copy.deepcopy(original) policy["documents"][1].update(change) policy_path.write_text(json.dumps(policy), encoding="utf-8") result = kb.build_generation_context(["internal-spur-gear"], engine, experimental=experimental) self.assertEqual(result["text"], "") self.assertEqual(result["skipped"], [{"doc_id": "fixture-internal-spur-gear", "reason": reason}]) approved = copy.deepcopy(original) approved["documents"][1].update(status="approved", evidence={"verdict": "benefit_observed", "engine_replay": "fixture replay", "ab_comparison": "fixture comparison", "parameter_transfer": "fixture transfer"}) policy_path.write_text(json.dumps(approved), encoding="utf-8") result = kb.build_generation_context(["internal-spur-gear"], "2" * 24) self.assertTrue(result["text"]) approved["documents"][1]["compatible_builds"] = ["*"] policy_path.write_text(json.dumps(approved), encoding="utf-8") self.assertEqual(kb.build_generation_context(["internal-spur-gear"], "1" * 24)["text"], result["text"]) def test_generation_rejects_unknown_ids_and_paths_for_catalog_types(self): with self.generation_fixture() as (kb, root, policy): for type_id in ("not-registered", "../../outside", "/etc/passwd", "internal_spur_gear", "feature-holes", None, {"path": "outside"}): with self.subTest(type_id=type_id), self.assertRaises(ValueError): kb.build_generation_context([type_id], "2" * 24, experimental=True) policy["documents"][1]["path"] = "../outside.md" (root / "generation/policy.json").write_text(json.dumps(policy), encoding="utf-8") with self.assertRaisesRegex(ValueError, "outside registry"): kb.build_generation_context(["internal-spur-gear"], "2" * 24, experimental=True) policy["documents"][1]["family_ids"] = ["unknown-policy-type"] (root / "generation/policy.json").write_text(json.dumps(policy), encoding="utf-8") with self.assertRaises(ValueError): kb.build_generation_context(["involute-spur-gear"], "2" * 24, experimental=True) def test_generation_catalog_cannot_register_paths_or_duplicate_ids(self): with self.generation_fixture() as (kb, root, _): catalog_path = root / "generation/catalog.json" original = json.loads(catalog_path.read_text(encoding="utf-8")) for invalid in ("../outside", "/etc/passwd", "not a stable id", "involute-spur-gear"): with self.subTest(catalog_id=invalid): catalog = copy.deepcopy(original) catalog["types"][2]["id"] = invalid catalog_path.write_text(json.dumps(catalog), encoding="utf-8") with self.assertRaisesRegex(ValueError, "generation catalog"): kb.build_generation_context(["mounting-plate"], "2" * 24, experimental=True) def test_generation_types_do_not_expand_legacy_family_interfaces(self): with self.generation_fixture() as (kb, _, _): self.assertNotIn("internal-spur-gear", kb.families) with self.assertRaises(ValueError): kb.build_context(["internal-spur-gear"]) with self.assertRaises(ValueError): kb.validate_params("internal-spur-gear", {}) def test_generation_cli_can_select_a_registered_generation_type(self): with self.generation_fixture() as (_, root, _): result = subprocess.run([sys.executable, "-m", "cad_knowledge", "--root", str(root), "generation-context", "internal-spur-gear", "--engine-build", "2" * 24, "--experimental"], cwd=ROOT, capture_output=True, text=True) self.assertEqual(result.returncode, 0, result.stderr) self.assertEqual(json.loads(result.stdout)["text"], "Internal ring construction fixture") def test_registry_rejects_arbitrary_paths_and_unrelated_features(self): for family_ids in (["../../tmp/file"], ["/etc/passwd"], "mounting-plate"): with self.subTest(ids=family_ids), self.assertRaises(ValueError): self.kb.build_context(family_ids) with self.assertRaises(ValueError): self.kb.build_context(["stepped-shaft"], ["feature-involute"]) def test_array_and_matrix_plate_layouts_are_equivalent(self): params = self.params("mounting-plate") first = self.kb.validate_params("mounting-plate", params) del params["holes"] params["hole_pattern"] = dict(rows=2, columns=2, pitch_x=70, pitch_y=30, origin_x=-35, origin_y=-15, diameter=6) second = self.kb.validate_params("mounting-plate", params) self.assertTrue(second["valid"]) self.assertEqual(first["derived"], second["derived"]) def test_tangent_and_duplicate_holes_are_rejected(self): params = self.params("mounting-plate") params["holes"][0]["x"] = -47 # r=3 touches X=-50. self.assertFalse(self.kb.validate_params("mounting-plate", params)["valid"]) params = self.params("mounting-plate") params["holes"][1] = copy.deepcopy(params["holes"][0]) self.assertFalse(self.kb.validate_params("mounting-plate", params)["valid"]) def test_uniform_scaling_has_cubic_volume(self): original = self.params("mounting-plate") scaled = copy.deepcopy(original) for key in ("length", "width", "thickness"): scaled[key] *= 3 for hole in scaled["holes"]: for key in ("x", "y", "diameter"): hole[key] *= 3 a = self.kb.validate_params("mounting-plate", original) b = self.kb.validate_params("mounting-plate", scaled) self.assertTrue(b["valid"]) self.assertAlmostEqual(b["derived"]["volume"] / a["derived"]["volume"], 27) def test_bolt_head_volume_uses_across_flats(self): params = self.params("hex-head-bolt") first = self.kb.validate_params("hex-head-bolt", params) params["head_height"] += 1 second = self.kb.validate_params("hex-head-bolt", params) self.assertAlmostEqual(second["derived"]["volume"] - first["derived"]["volume"], math.sqrt(3) * 14 ** 2 / 2) self.assertEqual(second["derived"]["total_length"] - first["derived"]["total_length"], 1) def test_gear_scaling_preserves_angles_and_involute_parameter(self): params = self.params("involute-spur-gear") first = self.kb.validate_params("involute-spur-gear", params) for key in ("module", "face_width", "hub_diameter", "hub_length", "bore_diameter", "keyway_width", "keyway_depth", "min_wall"): params[key] *= 2 second = self.kb.validate_params("involute-spur-gear", params) self.assertTrue(second["valid"]) self.assertAlmostEqual(second["derived"]["base_diameter"], first["derived"]["base_diameter"] * 2) for key in ("angular_pitch_deg", "involute_tip_parameter"): self.assertAlmostEqual(second["derived"][key], first["derived"][key]) def test_cover_gap_changes_assembly_position_not_interface(self): params = self.params("covered-thin-wall-enclosure") first = self.kb.validate_params("covered-thin-wall-enclosure", params) params["assembly_gap"] += 2 second = self.kb.validate_params("covered-thin-wall-enclosure", params) self.assertTrue(second["valid"]) self.assertEqual(first["derived"]["fastener_centers"], second["derived"]["fastener_centers"]) self.assertEqual(first["derived"]["inner_height"], second["derived"]["inner_height"]) self.assertAlmostEqual(second["derived"]["cover_bottom_z"] - first["derived"]["cover_bottom_z"], 2) def test_validation_does_not_modify_caller_parameters(self): params = self.params("ribbed-bearing-housing") before = copy.deepcopy(params) self.kb.validate_params("ribbed-bearing-housing", params) self.assertEqual(params, before) def test_bearing_hole_diagnostic_points_to_real_input_field(self): params = self.params("ribbed-bearing-housing") params["mount_holes"][0]["x"] = 59 result = self.kb.validate_params("ribbed-bearing-housing", params) self.assertTrue(any(e["path"] == "$.mount_holes[0]" for e in result["conflicts"])) def test_nonfinite_and_boolean_dimensions_are_rejected(self): for value in (float("nan"), float("inf"), float("-inf"), True, "8"): with self.subTest(value=value): params = self.params("mounting-plate") params["thickness"] = value result = self.kb.validate_params("mounting-plate", params) self.assertFalse(result["valid"]) self.assertTrue(result["conflicts"]) def test_missing_and_unsupported_values_are_not_silently_filled(self): params = self.params("hex-head-bolt") del params["under_head_length"] self.assertTrue(self.kb.validate_params("hex-head-bolt", params)["missing"]) params = self.params("hex-head-bolt") params["representation"] = "real_thread" result = self.kb.validate_params("hex-head-bolt", params) self.assertTrue(result["unsupported"]) self.assertFalse(result["derived"]) def test_malformed_structure_returns_diagnostics(self): for value in (None, [], 3, {"segments": [None, {}]}): with self.subTest(value=value): self.assertFalse(self.kb.validate_params("stepped-shaft", value)["valid"]) def test_duplicate_yaml_keys_are_rejected(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "duplicate.md" path.write_text("---\na: 1\na: 2\n---\nbody", encoding="utf-8") with self.assertRaisesRegex(ValueError, "Duplicate YAML key"): read_document(path) def test_missing_and_circular_document_references_are_rejected(self): for reference, error in (("unknown-feature", "Unknown document reference"), ("feature-holes", "Circular reference"), ("mounting-plate", "cannot load a family")): with self.subTest(reference=reference), tempfile.TemporaryDirectory() as directory: root = Path(directory) shutil.copytree(ROOT / "references", root / "references") shutil.copytree(ROOT / "schemas", root / "schemas") path = root / "references/features/holes.md" meta, body = read_document(path) meta["requires"] = [reference] path.write_text("---\n" + yaml.safe_dump(meta, allow_unicode=True) + "---\n" + body, encoding="utf-8") with self.assertRaisesRegex(ValueError, error): KnowledgeBase(root) def test_generated_views_are_current_and_mermaid_covers_all_nodes(self): for relative, text in generated_outputs(self.kb).items(): self.assertEqual((ROOT / relative).read_text(encoding="utf-8"), text) graph = (ROOT / "references/classification/mcb-taxonomy.zh-CN.mmd").read_text() self.assertEqual(len(re.findall(r'^ (?:root|n\d+)\[', graph, re.M)), 204) self.assertEqual(len(re.findall(r' --> ', graph)), 203) def test_all_local_markdown_links_resolve(self): paths = [ROOT / "README.md", ROOT / "SKILL.md", *(ROOT / "references").rglob("*.md"), *(ROOT / "reports").rglob("*.md")] for path in paths: for target in re.findall(r'\[[^\]]+\]\(([^)]+)\)', path.read_text(encoding="utf-8")): if target.startswith("<") and target.endswith(">"): target = target[1:-1] if re.match(r"^[a-z]+://", target) or target.startswith("#"): continue target = target.split("#", 1)[0] self.assertTrue((path.parent / target).exists(), f"{path}: {target}") def test_cli_reports_valid_input_and_nonfinite_json(self): command = [sys.executable, "-m", "cad_knowledge", "validate", "mounting-plate"] valid = subprocess.run(command + ["--example"], cwd=ROOT, capture_output=True, text=True) self.assertEqual(valid.returncode, 0, valid.stderr) self.assertTrue(json.loads(valid.stdout)["valid"]) with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "bad.json" path.write_text('{"thickness": NaN}') bad = subprocess.run(command + ["--params", str(path)], cwd=ROOT, capture_output=True, text=True) self.assertEqual(bad.returncode, 2) self.assertIn("non-finite", json.loads(bad.stderr)["error"]) if __name__ == "__main__": unittest.main()