import os import sys import unittest from pathlib import Path # Add the project root to sys.path PROJECT_ROOT = Path(__file__).resolve().parent.parent sys.path.append(str(PROJECT_ROOT)) from infra.prusaslicer_profiles.cache import LocalCache from infra.prusaslicer_profiles.fields import search_in_db class TestPrusaSlicerProfiles(unittest.TestCase): @classmethod def setUpClass(cls): cls.cache = LocalCache() cls.test_dir = PROJECT_ROOT / "profiles" / "prusaslicer" cls.ini_file = cls.test_dir / "2.4.5.ini" if not cls.ini_file.exists(): raise FileNotFoundError(f"Test baseline {cls.ini_file} not found.") cls.cache.load([cls.test_dir]) def test_profile_counts(self): """Verify that a large number of profiles are loaded from the baseline INI.""" self.assertGreater(len(self.cache.profiles), 6000) self.assertGreater(len(self.cache.printer_profiles), 200) self.assertGreater(len(self.cache.print_profiles), 500) self.assertGreater(len(self.cache.filament_profiles), 5000) def test_inheritance_and_vendor(self): """Verify that inheritance is resolved and vendor information is extracted.""" # Find a known filament profile filament_keys = [k for k in self.cache.display_profiles.keys() if k.startswith("filament:")] self.assertGreater(len(filament_keys), 0) profile = self.cache.profiles[filament_keys[0]] self.assertTrue(len(profile.all_conf_dict) > 0) self.assertTrue(profile.vendor or profile.all_conf_dict.get('filament_vendor') is None) def test_fields_database(self): """Verify that the fields database is accessible and searchable.""" res = search_in_db("bed shape") self.assertIn("bed_shape", res) self.assertEqual(res["bed_shape"].get("label"), "Bed shape") def test_compatibility(self): """Verify that compatibility evaluation runs without errors and finds matches.""" printer_keys = {k.removeprefix("printer:") for k in self.cache.printer_profiles.keys()} vendors = set(self.cache.vendors) self.cache.evaluate_compatibility(printer_keys, vendors) has_compatible = any(p.compatible_profiles for p in self.cache.printer_profiles.values()) self.assertTrue(has_compatible, "No compatible profiles found for any printer.") def test_all_conditions_in_baseline(self): """Exhaustively parse and evaluate all unique conditions in 2.4.5.ini across all printer profiles.""" from infra.prusaslicer_profiles.parser import Parser conditions = [p.all_conf_dict.get('compatible_printers_condition') for p in self.cache.profiles.values()] unique_conditions = sorted(list(set(c for c in conditions if c))) self.assertGreater(len(unique_conditions), 300) printers = [p for p in self.cache.profiles.values() if p.category == 'printer'] self.assertGreater(len(printers), 200) total_evals = 0 positive_evals = 0 for cond_str in unique_conditions: expr = Parser(cond_str).parse() self.assertIsNotNone(expr) for p in printers: res = bool(expr.eval(p.all_conf_dict)) if res: positive_evals += 1 total_evals += 1 self.assertEqual(total_evals, len(unique_conditions) * len(printers)) self.assertEqual(positive_evals, 3722) def test_parser_operators(self): """Test individual parser operator semantics.""" from infra.prusaslicer_profiles.parser import Parser ctx = { "printer_model": "MK3S", "nozzle_diameter": "0.4,0.6", "single_extruder_multi_material": "0", "layer_height": "0.2" } self.assertTrue(Parser('printer_model == "MK3S"').parse().eval(ctx)) self.assertFalse(Parser('printer_model == "MK2"').parse().eval(ctx)) self.assertTrue(Parser('printer_model =~ /MK(2|3).*/').parse().eval(ctx)) self.assertFalse(Parser('printer_model =~ /MINI/').parse().eval(ctx)) self.assertTrue(Parser('printer_model !~ /MINI/').parse().eval(ctx)) self.assertTrue(Parser('nozzle_diameter[0] == 0.4').parse().eval(ctx)) self.assertTrue(Parser('nozzle_diameter[1] == 0.6').parse().eval(ctx)) self.assertTrue(Parser('nozzle_diameter[0] < 0.5 and nozzle_diameter[1] > 0.5').parse().eval(ctx)) self.assertTrue(Parser('! single_extruder_multi_material').parse().eval(ctx)) self.assertTrue(Parser('! (printer_model == "MK2" and nozzle_diameter[0] == 0.25)').parse().eval(ctx)) if __name__ == "__main__": unittest.main()