from __future__ import annotations
import importlib.util
import json
import sys
import tempfile
import unittest
from pathlib import Path
TOOLKIT = Path(__file__).resolve().parents[2]
SCRIPTS = TOOLKIT / "scripts"
sys.path.insert(0, str(SCRIPTS))
def load_module(name: str, path: Path):
spec = importlib.util.spec_from_file_location(name, path)
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return module
validate_kit = load_module("agkit_validate_kit", SCRIPTS / "validate_kit.py")
component_registry = sys.modules["component_registry"]
dependency_graph = sys.modules["dependency_graph"]
security_scan = load_module("agkit_security_scan", TOOLKIT / "skills/vulnerability-scanner/scripts/security_scan.py")
dependency_analyzer = load_module("agkit_dependency_analyzer", TOOLKIT / "skills/vulnerability-scanner/scripts/dependency_analyzer.py")
bundle_analyzer = load_module("agkit_bundle_analyzer", TOOLKIT / "skills/performance-profiling/scripts/bundle_analyzer.py")
validation_runner = load_module("agkit_validation_runner", SCRIPTS / "validation_runner.py")
geo_checker = load_module("agkit_geo_checker", TOOLKIT / "skills/geo-fundamentals/scripts/geo_checker.py")
react_performance = load_module(
"agkit_react_performance",
TOOLKIT / "skills/nextjs-react-expert/scripts/react_performance_checker.py",
)
class ToolkitRegressionTests(unittest.TestCase):
def test_toolkit_self_validation_passes(self):
findings = validate_kit.validate(TOOLKIT)
errors = [item for item in findings if item.severity == "error"]
self.assertEqual([], errors)
def test_mcp_config_is_valid_json(self):
data = json.loads((TOOLKIT / "mcp_config.json").read_text("utf-8"))
self.assertIn("mcpServers", data)
def test_security_scanner_ignores_patterns_inside_strings(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "sample.py").write_text(
"""PATTERN = r'eval\\s*\\('
TOKEN = 'YOUR_API_KEY'
""",
"utf-8",
)
report = security_scan.run_full_scan(str(root), "all")
self.assertEqual(0, report["summary"]["critical"])
self.assertEqual(0, report["summary"]["high"])
def test_security_scanner_detects_executable_eval_and_secret(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "bad.py").write_text(
"""api_key = 'sk_live_12345678901234567890'
eval(user_input)
""",
"utf-8",
) # agkit: allow-secret
report = security_scan.run_full_scan(str(root), "all")
self.assertGreaterEqual(report["summary"]["critical"], 1)
self.assertGreaterEqual(report["summary"]["high"], 1)
self.assertTrue(security_scan._should_fail(report, "high"))
def test_security_scanner_detects_nested_nextjs_header_configuration(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "package.json").write_text('{"private":true}', "utf-8")
web = root / "web"
web.mkdir()
(web / "next.config.ts").write_text(
'const headers = [{ key: "Content-Security-Policy", value: "frame-ancestors \'none\'" }];',
"utf-8",
)
report = security_scan.run_full_scan(str(root), "config")
config = report["scans"]["configuration"]
self.assertTrue(config["checks"]["security_headers_config"])
self.assertFalse(any("security-header" in item.get("issue", "") for item in config["findings"]))
def test_dependency_analyzer_flags_missing_lock(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "package.json").write_text('{"dependencies":{"demo":"latest"}}', "utf-8")
report = dependency_analyzer.analyze(root)
issues = {item["issue"] for item in report["findings"]}
self.assertIn("Missing JavaScript lock file", issues)
self.assertIn("Unbounded dependency version", issues)
def test_bundle_analyzer_flags_oversized_asset(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
asset = root / "dist" / "app.js"
asset.parent.mkdir(parents=True)
asset.write_bytes(b"x" * 2048)
report = bundle_analyzer.analyze(root, file_warn_kib=1, file_fail_kib=2, total_fail_kib=100)
self.assertTrue(report["findings"])
self.assertEqual("high", report["findings"][0]["severity"])
def test_geo_checker_follows_localized_mdx_and_skips_layouts(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
route = root / "web" / "src" / "app" / "docs" / "demo"
route.mkdir(parents=True)
(route / "page.tsx").write_text(
'import En from "./content.en.mdx";\n'
'import Vi from "./content.vi.mdx";\n'
'export default function Page(){ return