Skip to main content
Glama

Conduit

by mcpnow-io
test_differential.py13.9 kB
import time from unittest import TestCase from src.client.base import PhabricatorAPIError from src.client.differential import DifferentialClient from src.client.diffusion import DiffusionClient from src.conduit import get_config class TestDifferentialClient(TestCase): def setUp(self): super().setUp() config = get_config() self.cli = DifferentialClient(config.url, config.token) self.diffusion_cli = DiffusionClient(config.url, config.token) # Create test data self.test_diff_id = None self.test_revision_id = None self._setup_test_data() def _setup_test_data(self): """Create test diff and revision for testing""" try: # Create a test diff diff_content = """diff --git a/test_file.py b/test_file.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/test_file.py @@ -0,0 +1 @@ +# Test file for differential testing """ diff_result = self.cli.create_raw_diff(diff=diff_content) if "id" in diff_result: self.test_diff_id = diff_result["id"] diff_phid = diff_result.get("phid") # Create a test revision using PHID revision_result = self.cli.edit_revision( transactions=[ {"type": "title", "value": f"Test Revision {int(time.time())}"}, {"type": "summary", "value": "Test revision for unit testing"}, {"type": "testPlan", "value": "Automated unit tests"}, {"type": "update", "value": diff_phid}, ] ) if "object" in revision_result: self.test_revision_id = revision_result["object"]["id"] except Exception as e: print(f"Failed to create test data: {e}") def test_search_revisions(self): """Test revision searching""" with self.subTest("Search all revisions"): result = self.cli.search_revisions(limit=10) self.assertIn("data", result) self.assertIsInstance(result["data"], list) with self.subTest("Search with constraints"): if self.test_revision_id: constraints = {"ids": [self.test_revision_id]} result = self.cli.search_revisions(constraints=constraints) self.assertIn("data", result) self.assertEqual(len(result["data"]), 1) def test_edit_revision(self): """Test revision editing""" if not self.test_revision_id: self.skipTest("No test revision available") # Update title new_title = f"Updated Test Revision {int(time.time())}" result = self.cli.edit_revision( transactions=[{"type": "title", "value": new_title}], object_identifier=str(self.test_revision_id), ) self.assertIn("object", result) def test_search_diffs(self): """Test diff searching""" with self.subTest("Search all diffs"): result = self.cli.search_diffs(limit=10) self.assertIn("data", result) self.assertIsInstance(result["data"], list) with self.subTest("Search with constraints"): if self.test_diff_id: constraints = {"ids": [self.test_diff_id]} result = self.cli.search_diffs(constraints=constraints) self.assertIn("data", result) def test_search_changesets(self): """Test changeset searching""" result = self.cli.search_changesets(limit=5) self.assertIn("data", result) self.assertIsInstance(result["data"], list) def test_create_diff(self): """Test diff creation with changes""" # Use raw diff instead of changes structure as it's simpler and more reliable diff_content = """diff --git a/new_test_file.py b/new_test_file.py new file mode 100644 index 0000000..7c7e421 --- /dev/null +++ b/new_test_file.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python3 + +print('Hello from new file') """ result = self.cli.create_raw_diff(diff=diff_content) self.assertTrue("id" in result or "diffid" in result) def test_create_raw_diff(self): """Test raw diff creation""" diff_content = """diff --git a/raw_test.txt b/raw_test.txt new file mode 100644 index 0000000..557db03 --- /dev/null +++ b/raw_test.txt @@ -0,0 +1 @@ +Hello World """ result = self.cli.create_raw_diff(diff=diff_content) self.assertTrue("id" in result or "diffid" in result) def test_get_raw_diff(self): """Test raw diff retrieval""" if not self.test_diff_id: self.skipTest("No test diff available") try: result = self.cli.get_raw_diff(diff_id=self.test_diff_id) self.assertIsInstance(result, str) except PhabricatorAPIError: # Some diffs might not have raw content pass def test_get_commit_message(self): """Test commit message retrieval""" if not self.test_revision_id: self.skipTest("No test revision available") result = self.cli.get_commit_message(revision_id=self.test_revision_id) # The API returns a string commit message, not a dict self.assertIsInstance(result, str) self.assertIn("Differential Revision:", result) def test_get_commit_paths(self): """Test commit paths retrieval""" if not self.test_revision_id: self.skipTest("No test revision available") try: result = self.cli.get_commit_paths(revision_id=self.test_revision_id) self.assertIsInstance(result, list) except PhabricatorAPIError: # Some revisions might not have paths pass def test_parse_commit_message(self): """Test commit message parsing""" commit_message = """Test Commit Summary: This is a test commit message for parsing. Test Plan: Run unit tests Reviewers: #test-group """ result = self.cli.parse_commit_message(corpus=commit_message) self.assertIsInstance(result, dict) self.assertIn("fields", result) def test_set_diff_property(self): """Test setting diff properties""" if not self.test_diff_id: self.skipTest("No test diff available") result = self.cli.set_diff_property( diff_id=self.test_diff_id, name="test-property", data={"test": "value"} ) # Some APIs return None on success, others return empty dict self.assertTrue(result is None or isinstance(result, dict)) def test_add_comment_legacy(self): """Test adding comments using legacy method""" if not self.test_revision_id: self.skipTest("No test revision available") try: result = self.cli.add_comment( revision_id=self.test_revision_id, message="This is a test comment", action="comment", ) self.assertIsInstance(result, dict) except PhabricatorAPIError: # Legacy method might not work on all installations pass def test_add_inline_comment(self): """Test adding inline comments""" if not self.test_revision_id: self.skipTest("No test revision available") try: result = self.cli.add_inline_comment( revision_id=self.test_revision_id, file_path="test_file.py", line_number=1, content="This line needs improvement", is_new=True, ) self.assertIsInstance(result, dict) except PhabricatorAPIError: # Inline comments might fail if file doesn't exist pass def test_close_revision_legacy(self): """Test closing revisions using legacy method""" if not self.test_revision_id: self.skipTest("No test revision available") try: result = self.cli.close_revision(revision_id=self.test_revision_id) self.assertIsInstance(result, dict) except PhabricatorAPIError: # Legacy method might not work or revision might not be closeable pass def test_query_revisions_legacy(self): """Test legacy revision querying""" result = self.cli.query_revisions(limit=5) self.assertIsInstance(result, list) def test_query_diffs_legacy(self): """Test legacy diff querying""" if self.test_diff_id: try: result = self.cli.query_diffs(ids=[self.test_diff_id]) self.assertIsInstance(result, dict) except PhabricatorAPIError as e: if "500" in str(e) or "method" in str(e).lower(): # Skip if the legacy API is not available self.skipTest("Legacy differential.querydiffs API not available") else: raise def test_create_revision_legacy(self): """Test legacy revision creation""" # Create a new diff for this test diff_content = """diff --git a/legacy_test.txt b/legacy_test.txt new file mode 100644 index 0000000..ce01362 --- /dev/null +++ b/legacy_test.txt @@ -0,0 +1 @@ +legacy test """ try: diff_result = self.cli.create_raw_diff(diff=diff_content) if "id" in diff_result or "diffid" in diff_result: diff_id = diff_result.get("id") or diff_result.get("diffid") _ = diff_result.get("phid") # PHID result = self.cli.create_revision( diff_id=diff_id, title=f"Legacy Test Revision {int(time.time())}", summary="Test revision created with legacy method", ) self.assertIsInstance(result, dict) except PhabricatorAPIError: # Legacy method might not work on all installations pass def test_update_revision_legacy(self): """Test legacy revision updating""" if not self.test_revision_id: self.skipTest("No test revision available") try: result = self.cli.update_revision( revision_id=self.test_revision_id, message="Updated via legacy method" ) self.assertIsInstance(result, dict) except PhabricatorAPIError: # Legacy method might not work pass class TestDifferentialWorkflows(TestCase): """Test complete differential workflows""" def setUp(self): super().setUp() config = get_config() self.cli = DifferentialClient(config.url, config.token) def test_complete_review_workflow(self): """Test a complete code review workflow""" # 1. Create initial diff initial_diff = """diff --git a/feature.py b/feature.py new file mode 100644 index 0000000..9daeafb --- /dev/null +++ b/feature.py @@ -0,0 +1,5 @@ +def new_feature(): + # TODO: Implement this + pass + +print("Feature added") """ try: diff_result = self.cli.create_raw_diff(diff=initial_diff) _ = diff_result.get("id") or diff_result.get("diffid") # diff ID diff_phid = diff_result.get("phid") # 2. Create revision using PHID revision_result = self.cli.edit_revision( transactions=[ {"type": "title", "value": f"Add new feature {int(time.time())}"}, { "type": "summary", "value": "This adds a new feature to the codebase", }, {"type": "testPlan", "value": "Unit tests and manual testing"}, {"type": "update", "value": diff_phid}, ] ) _ = revision_result["object"]["id"] # revision ID revision_phid = revision_result["object"]["phid"] # 3. Add review comment self.cli.edit_revision( transactions=[ {"type": "comment", "value": "Please implement the TODO"} ], object_identifier=revision_phid, ) # 4. Create updated diff updated_diff = """diff --git a/feature.py b/feature.py new file mode 100644 index 0000000..1234567 --- /dev/null +++ b/feature.py @@ -0,0 +1,6 @@ +def new_feature(): + return "Feature implemented!" + +if __name__ == "__main__": + print(new_feature()) """ updated_diff_result = self.cli.create_raw_diff(diff=updated_diff) _ = updated_diff_result.get("id") or updated_diff_result.get( "diffid" ) # updated_diff_id updated_diff_phid = updated_diff_result.get("phid") # 5. Update revision with new diff using PHID self.cli.edit_revision( transactions=[ {"type": "update", "value": updated_diff_phid}, { "type": "comment", "value": "Implemented the feature as requested", }, ], object_identifier=revision_phid, ) # 6. Accept the revision self.cli.edit_revision( transactions=[ {"type": "accept", "value": True}, {"type": "comment", "value": "Looks good to me!"}, ], object_identifier=revision_phid, ) self.assertTrue(True) # If we get here, the workflow succeeded except PhabricatorAPIError as e: print(f"Workflow test failed: {e}") # Some operations might fail due to permissions or configuration

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mcpnow-io/conduit'

If you have feedback or need assistance with the MCP directory API, please join our Discord server