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
Class Method Details
.call(params) ⇒ Object
15 16 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 |
# File 'lib/profiler/mcp/tools/run_tests.rb', line 15 def self.call(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 # 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
68 69 70 71 72 73 74 |
# File 'lib/profiler/mcp/tools/run_tests.rb', line 68 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
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 108 |
# File 'lib/profiler/mcp/tools/run_tests.rb', line 76 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 |