import json from pathlib import Path import tempfile import unittest from unittest.mock import patch import business_acceptance as acceptance import prepare_module_implementation_packet as packet class DesktopAdapterScopeTests(unittest.TestCase): def test_packet_includes_existing_module_adapter_before_shared_call_sites(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) sources = root / "web/src" sources.mkdir(parents=True) (sources / "part-desktop-adapter.ts").write_text("export function validatePartDesktopResponse() { return null; }\n") (sources / "techdraw-desktop-adapter.ts").write_text("export function prepareTechDrawCommand() { return null; }\n") (sources / "desktop-command-dispatcher.ts").write_text("async execute() { return this.router.request(); }\n") with patch.object(packet, "ROOT", root): paths = [region["path"] for region in packet.application_integration_excerpts("Part")] self.assertEqual(paths[0], "web/src/part-desktop-adapter.ts") self.assertIn("web/src/desktop-command-dispatcher.ts", paths) self.assertNotIn("web/src/techdraw-desktop-adapter.ts", paths) def test_module_adapter_changes_invalidate_owner_and_web_but_not_unrelated_modules(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) sources = root / "web/src" sources.mkdir(parents=True) manifest = root / "web/public/modules/manifest.json" manifest.parent.mkdir(parents=True) manifest.write_text(json.dumps({"modules": {"Part": {}, "TechDraw": {}, "Web": {}}})) adapter = sources / "techdraw-desktop-adapter.ts" adapter.write_text("export const value = 1;\n") dispatcher = sources / "desktop-command-dispatcher.ts" dispatcher.write_text('import { value } from "./techdraw-desktop-adapter.js";\n') with patch.object(acceptance, "ROOT", root): def inputs(): acceptance.digest.cache_clear() acceptance.module_inputs.cache_clear() return {module: acceptance.module_inputs(module) for module in ("Part", "TechDraw", "Web")} before = inputs() adapter.write_text("export const value = 2;\n") after = inputs() self.assertEqual(before["Part"], after["Part"]) self.assertNotEqual(before["TechDraw"], after["TechDraw"]) self.assertNotEqual(before["Web"], after["Web"]) (sources / "part-desktop-adapter.ts").write_text("export const partSelection = true;\n") part_changed = inputs() self.assertNotEqual(after["Part"], part_changed["Part"]) self.assertEqual(after["TechDraw"], part_changed["TechDraw"]) self.assertNotEqual(after["Web"], part_changed["Web"]) dispatcher.write_text("export const changedSharedBehavior = true;\n") changed_shared = inputs() for module in after: self.assertNotEqual(after[module], changed_shared[module]) acceptance.digest.cache_clear() acceptance.module_inputs.cache_clear() if __name__ == "__main__": unittest.main()