test_porting_workspace.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. from __future__ import annotations
  2. import subprocess
  3. import sys
  4. import unittest
  5. from pathlib import Path
  6. from src.commands import PORTED_COMMANDS
  7. from src.parity_audit import run_parity_audit
  8. from src.port_manifest import build_port_manifest
  9. from src.query_engine import QueryEnginePort
  10. from src.tools import PORTED_TOOLS
  11. class PortingWorkspaceTests(unittest.TestCase):
  12. def test_manifest_counts_python_files(self) -> None:
  13. manifest = build_port_manifest()
  14. self.assertGreaterEqual(manifest.total_python_files, 20)
  15. self.assertTrue(manifest.top_level_modules)
  16. def test_query_engine_summary_mentions_workspace(self) -> None:
  17. summary = QueryEnginePort.from_workspace().render_summary()
  18. self.assertIn('Python Porting Workspace Summary', summary)
  19. self.assertIn('Command surface:', summary)
  20. self.assertIn('Tool surface:', summary)
  21. def test_cli_summary_runs(self) -> None:
  22. result = subprocess.run(
  23. [sys.executable, '-m', 'src.main', 'summary'],
  24. check=True,
  25. capture_output=True,
  26. text=True,
  27. )
  28. self.assertIn('Python Porting Workspace Summary', result.stdout)
  29. def test_parity_audit_runs(self) -> None:
  30. result = subprocess.run(
  31. [sys.executable, '-m', 'src.main', 'parity-audit'],
  32. check=True,
  33. capture_output=True,
  34. text=True,
  35. )
  36. self.assertIn('Parity Audit', result.stdout)
  37. def test_root_file_coverage_is_complete_when_local_archive_exists(self) -> None:
  38. audit = run_parity_audit()
  39. if audit.archive_present:
  40. self.assertEqual(audit.root_file_coverage[0], audit.root_file_coverage[1])
  41. self.assertGreaterEqual(audit.directory_coverage[0], 28)
  42. self.assertGreaterEqual(audit.command_entry_ratio[0], 150)
  43. self.assertGreaterEqual(audit.tool_entry_ratio[0], 100)
  44. def test_command_and_tool_snapshots_are_nontrivial(self) -> None:
  45. self.assertGreaterEqual(len(PORTED_COMMANDS), 150)
  46. self.assertGreaterEqual(len(PORTED_TOOLS), 100)
  47. def test_commands_and_tools_cli_run(self) -> None:
  48. commands_result = subprocess.run(
  49. [sys.executable, '-m', 'src.main', 'commands', '--limit', '5', '--query', 'review'],
  50. check=True,
  51. capture_output=True,
  52. text=True,
  53. )
  54. tools_result = subprocess.run(
  55. [sys.executable, '-m', 'src.main', 'tools', '--limit', '5', '--query', 'MCP'],
  56. check=True,
  57. capture_output=True,
  58. text=True,
  59. )
  60. self.assertIn('Command entries:', commands_result.stdout)
  61. self.assertIn('Tool entries:', tools_result.stdout)
  62. def test_subsystem_packages_expose_archive_metadata(self) -> None:
  63. from src import assistant, bridge, utils
  64. self.assertGreater(assistant.MODULE_COUNT, 0)
  65. self.assertGreater(bridge.MODULE_COUNT, 0)
  66. self.assertGreater(utils.MODULE_COUNT, 100)
  67. self.assertTrue(utils.SAMPLE_FILES)
  68. def test_route_and_show_entry_cli_run(self) -> None:
  69. route_result = subprocess.run(
  70. [sys.executable, '-m', 'src.main', 'route', 'review MCP tool', '--limit', '5'],
  71. check=True,
  72. capture_output=True,
  73. text=True,
  74. )
  75. show_command = subprocess.run(
  76. [sys.executable, '-m', 'src.main', 'show-command', 'review'],
  77. check=True,
  78. capture_output=True,
  79. text=True,
  80. )
  81. show_tool = subprocess.run(
  82. [sys.executable, '-m', 'src.main', 'show-tool', 'MCPTool'],
  83. check=True,
  84. capture_output=True,
  85. text=True,
  86. )
  87. self.assertIn('review', route_result.stdout.lower())
  88. self.assertIn('review', show_command.stdout.lower())
  89. self.assertIn('mcptool', show_tool.stdout.lower())
  90. def test_bootstrap_cli_runs(self) -> None:
  91. result = subprocess.run(
  92. [sys.executable, '-m', 'src.main', 'bootstrap', 'review MCP tool', '--limit', '5'],
  93. check=True,
  94. capture_output=True,
  95. text=True,
  96. )
  97. self.assertIn('Runtime Session', result.stdout)
  98. self.assertIn('Startup Steps', result.stdout)
  99. self.assertIn('Routed Matches', result.stdout)
  100. def test_bootstrap_session_tracks_turn_state(self) -> None:
  101. from src.runtime import PortRuntime
  102. session = PortRuntime().bootstrap_session('review MCP tool', limit=5)
  103. self.assertGreaterEqual(len(session.turn_result.matched_tools), 1)
  104. self.assertIn('Prompt:', session.turn_result.output)
  105. self.assertGreaterEqual(session.turn_result.usage.input_tokens, 1)
  106. def test_exec_command_and_tool_cli_run(self) -> None:
  107. command_result = subprocess.run(
  108. [sys.executable, '-m', 'src.main', 'exec-command', 'review', 'inspect security review'],
  109. check=True,
  110. capture_output=True,
  111. text=True,
  112. )
  113. tool_result = subprocess.run(
  114. [sys.executable, '-m', 'src.main', 'exec-tool', 'MCPTool', 'fetch resource list'],
  115. check=True,
  116. capture_output=True,
  117. text=True,
  118. )
  119. self.assertIn("Mirrored command 'review'", command_result.stdout)
  120. self.assertIn("Mirrored tool 'MCPTool'", tool_result.stdout)
  121. def test_setup_report_and_registry_filters_run(self) -> None:
  122. setup_result = subprocess.run(
  123. [sys.executable, '-m', 'src.main', 'setup-report'],
  124. check=True,
  125. capture_output=True,
  126. text=True,
  127. )
  128. command_result = subprocess.run(
  129. [sys.executable, '-m', 'src.main', 'commands', '--limit', '5', '--no-plugin-commands'],
  130. check=True,
  131. capture_output=True,
  132. text=True,
  133. )
  134. tool_result = subprocess.run(
  135. [sys.executable, '-m', 'src.main', 'tools', '--limit', '5', '--simple-mode', '--no-mcp'],
  136. check=True,
  137. capture_output=True,
  138. text=True,
  139. )
  140. self.assertIn('Setup Report', setup_result.stdout)
  141. self.assertIn('Command entries:', command_result.stdout)
  142. self.assertIn('Tool entries:', tool_result.stdout)
  143. def test_load_session_cli_runs(self) -> None:
  144. from src.runtime import PortRuntime
  145. session = PortRuntime().bootstrap_session('review MCP tool', limit=5)
  146. session_id = Path(session.persisted_session_path).stem
  147. result = subprocess.run(
  148. [sys.executable, '-m', 'src.main', 'load-session', session_id],
  149. check=True,
  150. capture_output=True,
  151. text=True,
  152. )
  153. self.assertIn(session_id, result.stdout)
  154. self.assertIn('messages', result.stdout)
  155. def test_tool_permission_filtering_cli_runs(self) -> None:
  156. result = subprocess.run(
  157. [sys.executable, '-m', 'src.main', 'tools', '--limit', '10', '--deny-prefix', 'mcp'],
  158. check=True,
  159. capture_output=True,
  160. text=True,
  161. )
  162. self.assertIn('Tool entries:', result.stdout)
  163. self.assertNotIn('MCPTool', result.stdout)
  164. def test_turn_loop_cli_runs(self) -> None:
  165. result = subprocess.run(
  166. [sys.executable, '-m', 'src.main', 'turn-loop', 'review MCP tool', '--max-turns', '2', '--structured-output'],
  167. check=True,
  168. capture_output=True,
  169. text=True,
  170. )
  171. self.assertIn('## Turn 1', result.stdout)
  172. self.assertIn('stop_reason=', result.stdout)
  173. def test_remote_mode_clis_run(self) -> None:
  174. remote_result = subprocess.run([sys.executable, '-m', 'src.main', 'remote-mode', 'workspace'], check=True, capture_output=True, text=True)
  175. ssh_result = subprocess.run([sys.executable, '-m', 'src.main', 'ssh-mode', 'workspace'], check=True, capture_output=True, text=True)
  176. teleport_result = subprocess.run([sys.executable, '-m', 'src.main', 'teleport-mode', 'workspace'], check=True, capture_output=True, text=True)
  177. self.assertIn('mode=remote', remote_result.stdout)
  178. self.assertIn('mode=ssh', ssh_result.stdout)
  179. self.assertIn('mode=teleport', teleport_result.stdout)
  180. def test_flush_transcript_cli_runs(self) -> None:
  181. result = subprocess.run(
  182. [sys.executable, '-m', 'src.main', 'flush-transcript', 'review MCP tool'],
  183. check=True,
  184. capture_output=True,
  185. text=True,
  186. )
  187. self.assertIn('flushed=True', result.stdout)
  188. def test_command_graph_and_tool_pool_cli_run(self) -> None:
  189. command_graph = subprocess.run([sys.executable, '-m', 'src.main', 'command-graph'], check=True, capture_output=True, text=True)
  190. tool_pool = subprocess.run([sys.executable, '-m', 'src.main', 'tool-pool'], check=True, capture_output=True, text=True)
  191. self.assertIn('Command Graph', command_graph.stdout)
  192. self.assertIn('Tool Pool', tool_pool.stdout)
  193. def test_setup_report_mentions_deferred_init(self) -> None:
  194. result = subprocess.run(
  195. [sys.executable, '-m', 'src.main', 'setup-report'],
  196. check=True,
  197. capture_output=True,
  198. text=True,
  199. )
  200. self.assertIn('Deferred init:', result.stdout)
  201. self.assertIn('plugin_init=True', result.stdout)
  202. def test_execution_registry_runs(self) -> None:
  203. from src.execution_registry import build_execution_registry
  204. registry = build_execution_registry()
  205. self.assertGreaterEqual(len(registry.commands), 150)
  206. self.assertGreaterEqual(len(registry.tools), 100)
  207. self.assertIn('Mirrored command', registry.command('review').execute('review security'))
  208. self.assertIn('Mirrored tool', registry.tool('MCPTool').execute('fetch mcp resources'))
  209. def test_bootstrap_graph_and_direct_modes_run(self) -> None:
  210. graph_result = subprocess.run([sys.executable, '-m', 'src.main', 'bootstrap-graph'], check=True, capture_output=True, text=True)
  211. direct_result = subprocess.run([sys.executable, '-m', 'src.main', 'direct-connect-mode', 'workspace'], check=True, capture_output=True, text=True)
  212. deep_link_result = subprocess.run([sys.executable, '-m', 'src.main', 'deep-link-mode', 'workspace'], check=True, capture_output=True, text=True)
  213. self.assertIn('Bootstrap Graph', graph_result.stdout)
  214. self.assertIn('mode=direct-connect', direct_result.stdout)
  215. self.assertIn('mode=deep-link', deep_link_result.stdout)
  216. if __name__ == '__main__':
  217. unittest.main()