defmodule AiDebug.QAGatesTest do use ExUnit.Case, async: false use AiDebugWeb.ConnCase alias AiDebug.QAGates @moduletag :qa_gates describe "run_all_checks/2" do test "returns expected result structure" do target = "http://example.com" {:ok, result} = QAGates.run_all_checks(target) # Test result structure assert Map.has_key?(result, :passed) assert Map.has_key?(result, :checks) assert Map.has_key?(result, :issues) assert Map.has_key?(result, :timestamp) assert Map.has_key?(result, :target) # Test checks structure assert Map.has_key?(result.checks, :accessibility) assert Map.has_key?(result.checks, :console_errors) assert Map.has_key?(result.checks, :performance) assert Map.has_key?(result.checks, :visual_regression) assert Map.has_key?(result.checks, :cross_browser) assert is_boolean(result.passed) assert is_list(result.issues) assert result.target == target end test "handles invalid URLs gracefully" do target = "http://invalid-url-that-does-not-exist.nonexistent" # Should not crash, should return error results {:ok, result} = QAGates.run_all_checks(target) # Some checks may fail, but it should return a valid structure assert Map.has_key?(result, :passed) assert Map.has_key?(result, :checks) assert Map.has_key?(result, :issues) assert result.target == target end end describe "check_accessibility/2" do test "returns expected result structure" do target = "http://example.com" result = QAGates.check_accessibility(target) # Should return a map with expected keys assert is_map(result) assert Map.has_key?(result, :passed) assert is_boolean(result.passed) # Should have either score data or error data assert Map.has_key?(result, :score) or Map.has_key?(result, :error) end test "accessibility audit function uses correct MCP tool" do # Test that accessibility_audit calls the correct MCP tool target = "http://localhost:4000" # This should use run_audit with accessibility category, not run_accessibility_check case AiDebug.accessibility_audit(target) do {:ok, audit} -> # Should return expected structure assert Map.has_key?(audit, :score) assert Map.has_key?(audit, :violations) assert is_number(audit.score) assert is_list(audit.violations) {:error, reason} -> # If it fails, it should be due to network/server issues, not wrong tool name refute String.contains?(to_string(reason), "tool not found") refute String.contains?(to_string(reason), "run_accessibility_check") end end test "handles network errors gracefully" do target = "http://invalid-url.nonexistent" result = QAGates.check_accessibility(target) # Should handle errors gracefully, not crash assert is_map(result) assert Map.has_key?(result, :passed) # Network errors should result in failed check assert result.passed == false end end describe "check_console_errors/2" do test "returns expected result structure" do target = "http://example.com" result = QAGates.check_console_errors(target) assert is_map(result) assert Map.has_key?(result, :passed) assert is_boolean(result.passed) # Should have either errors data or error data assert Map.has_key?(result, :errors) or Map.has_key?(result, :error) end end describe "check_performance/2" do test "returns expected result structure" do target = "http://example.com" result = QAGates.check_performance(target) assert is_map(result) assert Map.has_key?(result, :passed) assert is_boolean(result.passed) # Should have either metrics data or error data assert Map.has_key?(result, :metrics) or Map.has_key?(result, :error) end end describe "check_visual_regression/2" do test "returns expected result structure" do target = "http://example.com" result = QAGates.check_visual_regression(target) assert is_map(result) assert Map.has_key?(result, :passed) # Visual regression can return :skipped for no baseline case result.passed do :skipped -> assert Map.has_key?(result, :message) true -> :ok false -> :ok end end end describe "check_cross_browser/2" do test "returns expected result structure" do target = "http://example.com" result = QAGates.check_cross_browser(target) assert is_map(result) assert Map.has_key?(result, :passed) # Cross-browser check may return :partial currently case result.passed do :partial -> assert Map.has_key?(result, :message) true -> :ok false -> :ok end end end describe "integration with AI Debug MCP tools" do @tag :integration test "QA Gates can be called via MCP tools" do # This tests the integration with our MCP debugging tools # This will help ensure the QA Validator works with the AI Debug platform target = "http://localhost:4000" # Test that QA Gates can be integrated with AI Debug workflow {:ok, result} = QAGates.run_all_checks(target) # Should return proper format for MCP tool consumption assert is_map(result) assert Map.has_key?(result, :passed) assert Map.has_key?(result, :checks) assert Map.has_key?(result, :issues) # Check structure matches what MCP tools expect assert Map.has_key?(result.checks, :accessibility) assert Map.has_key?(result.checks, :console_errors) assert Map.has_key?(result.checks, :performance) assert Map.has_key?(result.checks, :visual_regression) assert Map.has_key?(result.checks, :cross_browser) end end end