99from fastapi .testclient import TestClient
1010from fastapi import FastAPI
1111
12+ # Import the actual dependencies to override them
13+ from src .uckn .api .dependencies import get_knowledge_manager as original_get_knowledge_manager
14+ from src .uckn .api .routers .collaboration import get_collaboration_manager as original_get_collaboration_manager
15+ from src .uckn .api .routers .collaboration import router # Import the router itself
16+
1217from src .uckn .core .molecules .collaboration_manager import (
1318 CollaborationManager ,
1419 ActivityEvent ,
1924
2025
2126@pytest .fixture
22- def app ():
23- """Create FastAPI app for testing."""
27+ def app (mock_knowledge_manager , mock_collaboration_manager ):
28+ """Create FastAPI app for testing with dependency overrides ."""
2429 app = FastAPI ()
2530
26- # Import and include router with dependency override
27- from src .uckn .api .routers .collaboration import router
31+ # Override dependencies
32+ app .dependency_overrides [original_get_knowledge_manager ] = lambda : mock_knowledge_manager
33+ app .dependency_overrides [original_get_collaboration_manager ] = lambda : mock_collaboration_manager
2834
2935 app .include_router (router , prefix = "/api/v1" )
3036 return app
@@ -39,28 +45,39 @@ def client(app):
3945@pytest .fixture
4046def mock_collaboration_manager ():
4147 """Create mock collaboration manager."""
42- return MagicMock (spec = CollaborationManager )
48+ mock_manager = MagicMock (spec = CollaborationManager )
49+ # Explicitly make async methods AsyncMock
50+ mock_manager .add_comment = AsyncMock ()
51+ mock_manager .get_comments = AsyncMock ()
52+ mock_manager .get_activity_feed = AsyncMock ()
53+ mock_manager .set_notification_preference = AsyncMock ()
54+ mock_manager .add_webhook = AsyncMock ()
55+ return mock_manager
4356
4457
4558@pytest .fixture
4659def mock_knowledge_manager ():
4760 """Create mock knowledge manager."""
48- return MagicMock ()
61+ # The knowledge manager is used by CollaborationManager constructor
62+ # and also by the share_pattern endpoint directly.
63+ # It needs to have a get_pattern method if share_pattern is tested.
64+ mock_km = MagicMock ()
65+ # Provide a default return value for get_pattern if it's called by other endpoints
66+ mock_km .get_pattern .return_value = {"id" : "pattern-456" , "name" : "Test Pattern" }
67+ return mock_km
4968
5069
5170class TestCollaborationRouter :
5271 """Test cases for collaboration router endpoints."""
5372
54- @patch ( 'src.uckn.api.routers.collaboration.get_collaboration_manager' )
55- def test_add_comment_success (self , mock_get_collab_manager , client , mock_collaboration_manager ):
73+ # No need for @patch here as dependencies are overridden in the app fixture
74+ def test_add_comment_success (self , client , mock_collaboration_manager ):
5675 """Test successful comment addition."""
57- # Set up mock
58- mock_get_collab_manager .return_value = mock_collaboration_manager
59-
76+ # Set up mock return value for the async method
6077 mock_comment = Comment (
6178 id = "comment-123" ,
6279 pattern_id = "pattern-456" ,
63- user_id = "mock_user_id" ,
80+ user_id = "mock_user_id" , # This is hardcoded in the endpoint
6481 content = "Great pattern!" ,
6582 metadata = {"source" : "web" },
6683 created_at = datetime .now (timezone .utc )
@@ -84,13 +101,17 @@ def test_add_comment_success(self, mock_get_collab_manager, client, mock_collabo
84101 assert data ["pattern_id" ] == "pattern-456"
85102 assert data ["content" ] == "Great pattern!"
86103 assert data ["user_id" ] == "mock_user_id"
104+ # Verify the mock was called correctly
105+ mock_collaboration_manager .add_comment .assert_called_once ()
106+ # Optionally, check the arguments passed to add_comment
107+ called_comment = mock_collaboration_manager .add_comment .call_args [0 ][0 ]
108+ assert called_comment .pattern_id == "pattern-456"
109+ assert called_comment .user_id == "mock_user_id"
110+ assert called_comment .content == "Great pattern!"
87111
88- @patch ('src.uckn.api.routers.collaboration.get_collaboration_manager' )
89- def test_get_comments (self , mock_get_collab_manager , client , mock_collaboration_manager ):
112+ def test_get_comments (self , client , mock_collaboration_manager ):
90113 """Test getting comments for a pattern."""
91- # Set up mock
92- mock_get_collab_manager .return_value = mock_collaboration_manager
93-
114+ # Set up mock return value for the async method
94115 mock_comments = [
95116 Comment (
96117 id = "comment-1" ,
@@ -109,6 +130,7 @@ def test_get_comments(self, mock_get_collab_manager, client, mock_collaboration_
109130 data = response .json ()
110131 assert len (data ) == 1
111132 assert data [0 ]["id" ] == "comment-1"
133+ mock_collaboration_manager .get_comments .assert_called_once_with ("pattern-123" , None )
112134
113135 def test_create_pattern_library (self , client ):
114136 """Test creating a team-scoped pattern library."""
@@ -125,4 +147,6 @@ def test_create_pattern_library(self, client):
125147 assert response .status_code == 201
126148 data = response .json ()
127149 assert data ["team_id" ] == "team-123"
128- assert data ["name" ] == "CI/CD Patterns"
150+ assert data ["name" ] == "CI/CD Patterns"
151+ assert "id" in data # Ensure an ID is generated
152+ assert "created_at" in data # Ensure timestamp is present
0 commit comments