Class: Profiler::MCP::Tools::RunTests
- Inherits:
-
Object
- Object
- Profiler::MCP::Tools::RunTests
- Defined in:
- lib/profiler/mcp/tools/run_tests.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
120- DEFAULT_MAX_OUTPUT =
4000- POLL_TIMEOUT =
seconds per wait_for_output call
10
Class Method Summary collapse
- .call(params) ⇒ Object
- .collect_run_profiles(since) ⇒ Object
- .format_result(run, output, timed_out, profile_tokens, files, framework) ⇒ Object
- .run_on_slave(proxy, params) ⇒ Object
Class Method Details
.call(params) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/profiler/mcp/tools/run_tests.rb', line 17 def self.call(params) if (proxy = MCP::SlaveSupport.with_slave_proxy(params)) return run_on_slave(proxy, params) end files = Array(params["files"]) framework = params["framework"]&.to_s timeout_secs = (params["timeout_seconds"] || DEFAULT_TIMEOUT).to_i max_output = (params["max_output"] || DEFAULT_MAX_OUTPUT).to_i # Auto-detect framework if not provided framework ||= begin available = Profiler::TestRunner::Discovery.frameworks.map(&:to_s) available.first || "rspec" end # If no files specified, discover all for the given framework if files.empty? tree = Profiler::TestRunner::Discovery.files(framework: framework.to_sym) files = tree.flat_map { |dir| dir[:files].map { |f| f[:path] } } end if files.empty? return [{ type: "text", text: "No test files found for framework '#{framework}'." }] end run_started_at = Time.now run = Profiler::TestRunner::Runner.start(files: files, framework: framework) output_pos = 0 deadline = Time.now + timeout_secs timed_out = false until Profiler::TestRunner::RunStore::TERMINAL_STATUSES.include?(run.status) remaining = (deadline - Time.now).to_i if remaining <= 0 timed_out = true break end wait = [POLL_TIMEOUT, remaining].min result = Profiler::TestRunner.run_store.wait_for_output(run.id, position: output_pos, timeout: wait) output_pos = result[:position] break if result[:finished] end full_output = run.output_lines.join tail_output = full_output.length > max_output ? "…(truncated)\n" + full_output[-(max_output)..] : full_output # Collect test profiles created during this run profile_tokens = collect_run_profiles(run_started_at) [{ type: "text", text: format_result(run, tail_output, timed_out, profile_tokens, files, framework) }] end |
.collect_run_profiles(since) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/profiler/mcp/tools/run_tests.rb', line 109 def self.collect_run_profiles(since) Profiler.storage.list(limit: 500).select do |p| p.profile_type == "test" && p.started_at && p.started_at >= since end.map(&:token) rescue [] end |
.format_result(run, output, timed_out, profile_tokens, files, framework) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/profiler/mcp/tools/run_tests.rb', line 117 def self.format_result(run, output, timed_out, profile_tokens, files, framework) lines = [] lines << "# Test Run #{timed_out ? "(timed out)" : ""}\n" lines << "## Summary" lines << "| Field | Value |" lines << "|-------|-------|" lines << "| Run ID | `#{run.id}` |" lines << "| Framework | #{framework} |" lines << "| Files | #{files.size} |" lines << "| Status | **#{run.status}** |" lines << "| Exit code | #{run.exit_code.inspect} |" lines << "| Duration | #{run.to_h[:duration]&.round(0)}ms |" if timed_out lines << "" lines << "> ⚠ Timed out — run is still in progress. Use run ID `#{run.id}` to check later." end lines << "\n## Output (last #{output.length} chars)" lines << "```" lines << output.strip lines << "```" if profile_tokens.any? lines << "\n## Test Profiles Created (#{profile_tokens.size})" lines << "Use `get_test_profile` with any of these tokens for detailed SQL/cache/exception data:" profile_tokens.first(10).each { |t| lines << "- `#{t}`" } lines << "- _(#{profile_tokens.size - 10} more…)_" if profile_tokens.size > 10 end lines.join("\n") end |
.run_on_slave(proxy, params) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/profiler/mcp/tools/run_tests.rb', line 74 def self.run_on_slave(proxy, params) files = Array(params["files"]) framework = params["framework"].to_s timeout_secs = (params["timeout_seconds"] || DEFAULT_TIMEOUT).to_i max_output = (params["max_output"] || DEFAULT_MAX_OUTPUT).to_i run_data = proxy.post_json("/_profiler/api/test_runner/runs", { files: files, framework: framework.presence || "rspec" }) if run_data["error"] return [{ type: "text", text: "Error starting tests on slave: #{run_data["error"]}" }] end run_id = run_data["id"] deadline = Time.now + timeout_secs timed_out = false loop do break if Time.now > deadline && (timed_out = true) run_data = proxy.get_json("/_profiler/api/test_runner/runs/#{run_id}") break if %w[completed failed cancelled].include?(run_data["status"]) sleep 2 end output = run_data["output_lines"]&.join.to_s output = "…(truncated)\n" + output[-(max_output)..] if output.length > max_output text = "# Test Run on slave '#{params["slave"]}' #{timed_out ? "(timed out)" : ""}\n\n" text += "| Field | Value |\n|-------|-------|\n" text += "| Run ID | `#{run_id}` |\n" text += "| Status | **#{run_data["status"]}** |\n" text += "| Exit code | #{run_data["exit_code"].inspect} |\n\n" text += "## Output\n```\n#{output.strip}\n```" [{ type: "text", text: text }] end |