|
| 1 | +from posthog.test.base import APIBaseTest |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from rest_framework import status |
| 5 | + |
| 6 | +from ee.models.assistant import AgentArtifact, Conversation |
| 7 | + |
| 8 | + |
| 9 | +class TestAgentArtifactModel(APIBaseTest): |
| 10 | + def test_create_artifact(self): |
| 11 | + conversation = Conversation.objects.create( |
| 12 | + user=self.user, team=self.team, title="Test Conversation", type=Conversation.Type.ASSISTANT |
| 13 | + ) |
| 14 | + |
| 15 | + artifact = AgentArtifact.objects.create( |
| 16 | + name="Test Visualization", |
| 17 | + type=AgentArtifact.Type.VISUALIZATION, |
| 18 | + content={"query": {"kind": "TrendsQuery"}}, |
| 19 | + conversation=conversation, |
| 20 | + team=self.team, |
| 21 | + ) |
| 22 | + |
| 23 | + self.assertIsNotNone(artifact.id) |
| 24 | + self.assertEqual(len(artifact.short_id), 6) |
| 25 | + self.assertEqual(artifact.name, "Test Visualization") |
| 26 | + self.assertEqual(artifact.type, AgentArtifact.Type.VISUALIZATION) |
| 27 | + self.assertEqual(artifact.content, {"query": {"kind": "TrendsQuery"}}) |
| 28 | + self.assertEqual(artifact.conversation, conversation) |
| 29 | + self.assertIsNotNone(artifact.created_at) |
| 30 | + |
| 31 | + def test_short_id_unique(self): |
| 32 | + conversation = Conversation.objects.create( |
| 33 | + user=self.user, team=self.team, title="Test Conversation", type=Conversation.Type.ASSISTANT |
| 34 | + ) |
| 35 | + |
| 36 | + artifact1 = AgentArtifact.objects.create( |
| 37 | + name="Artifact 1", |
| 38 | + type=AgentArtifact.Type.VISUALIZATION, |
| 39 | + content={}, |
| 40 | + conversation=conversation, |
| 41 | + team=self.team, |
| 42 | + ) |
| 43 | + |
| 44 | + artifact2 = AgentArtifact.objects.create( |
| 45 | + name="Artifact 2", |
| 46 | + type=AgentArtifact.Type.DOCUMENT, |
| 47 | + content={}, |
| 48 | + conversation=conversation, |
| 49 | + team=self.team, |
| 50 | + ) |
| 51 | + |
| 52 | + self.assertNotEqual(artifact1.short_id, artifact2.short_id) |
| 53 | + |
| 54 | + def test_cascade_delete_with_conversation(self): |
| 55 | + conversation = Conversation.objects.create( |
| 56 | + user=self.user, team=self.team, title="Test Conversation", type=Conversation.Type.ASSISTANT |
| 57 | + ) |
| 58 | + |
| 59 | + artifact = AgentArtifact.objects.create( |
| 60 | + name="Test Artifact", |
| 61 | + type=AgentArtifact.Type.VISUALIZATION, |
| 62 | + content={}, |
| 63 | + conversation=conversation, |
| 64 | + team=self.team, |
| 65 | + ) |
| 66 | + |
| 67 | + artifact_id = artifact.id |
| 68 | + conversation.delete() |
| 69 | + |
| 70 | + self.assertFalse(AgentArtifact.objects.filter(id=artifact_id).exists()) |
| 71 | + |
| 72 | + def test_short_id_collision_retry(self): |
| 73 | + conversation = Conversation.objects.create( |
| 74 | + user=self.user, team=self.team, title="Test Conversation", type=Conversation.Type.ASSISTANT |
| 75 | + ) |
| 76 | + |
| 77 | + artifact1 = AgentArtifact.objects.create( |
| 78 | + name="Artifact 1", |
| 79 | + type=AgentArtifact.Type.VISUALIZATION, |
| 80 | + content={}, |
| 81 | + conversation=conversation, |
| 82 | + team=self.team, |
| 83 | + ) |
| 84 | + |
| 85 | + original_short_id = artifact1.short_id |
| 86 | + |
| 87 | + with patch("ee.models.assistant.generate_short_id_6", return_value="unique"): |
| 88 | + artifact2 = AgentArtifact( |
| 89 | + name="Artifact 2", |
| 90 | + type=AgentArtifact.Type.DOCUMENT, |
| 91 | + content={}, |
| 92 | + conversation=conversation, |
| 93 | + team=self.team, |
| 94 | + short_id=original_short_id, # Start with duplicate |
| 95 | + ) |
| 96 | + artifact2.save() |
| 97 | + |
| 98 | + self.assertEqual(artifact2.short_id, "unique") |
| 99 | + |
| 100 | + |
| 101 | +class TestAgentArtifactAPI(APIBaseTest): |
| 102 | + def setUp(self): |
| 103 | + super().setUp() |
| 104 | + self.conversation = Conversation.objects.create( |
| 105 | + user=self.user, team=self.team, title="Test Conversation", type=Conversation.Type.ASSISTANT |
| 106 | + ) |
| 107 | + |
| 108 | + def test_list_artifacts(self): |
| 109 | + AgentArtifact.objects.create( |
| 110 | + name="Artifact 1", |
| 111 | + type=AgentArtifact.Type.VISUALIZATION, |
| 112 | + content={"query": {}}, |
| 113 | + conversation=self.conversation, |
| 114 | + team=self.team, |
| 115 | + ) |
| 116 | + |
| 117 | + AgentArtifact.objects.create( |
| 118 | + name="Artifact 2", |
| 119 | + type=AgentArtifact.Type.DOCUMENT, |
| 120 | + content={"blocks": []}, |
| 121 | + conversation=self.conversation, |
| 122 | + team=self.team, |
| 123 | + ) |
| 124 | + |
| 125 | + response = self.client.get(f"/api/environments/{self.team.id}/agent-artifacts/") |
| 126 | + |
| 127 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 128 | + self.assertEqual(len(response.json()["results"]), 2) |
| 129 | + |
| 130 | + # Check that list returns minimal serializer fields |
| 131 | + result = response.json()["results"][0] |
| 132 | + self.assertIn("short_id", result) |
| 133 | + self.assertIn("name", result) |
| 134 | + self.assertIn("type", result) |
| 135 | + self.assertIn("created_at", result) |
| 136 | + self.assertNotIn("content", result) |
| 137 | + self.assertNotIn("id", result) |
| 138 | + |
| 139 | + def test_list_artifacts_filter_by_conversation(self): |
| 140 | + other_conversation = Conversation.objects.create( |
| 141 | + user=self.user, team=self.team, title="Other Conversation", type=Conversation.Type.ASSISTANT |
| 142 | + ) |
| 143 | + |
| 144 | + AgentArtifact.objects.create( |
| 145 | + name="Artifact 1", |
| 146 | + type=AgentArtifact.Type.VISUALIZATION, |
| 147 | + content={}, |
| 148 | + conversation=self.conversation, |
| 149 | + team=self.team, |
| 150 | + ) |
| 151 | + |
| 152 | + AgentArtifact.objects.create( |
| 153 | + name="Artifact 2", |
| 154 | + type=AgentArtifact.Type.DOCUMENT, |
| 155 | + content={}, |
| 156 | + conversation=other_conversation, |
| 157 | + team=self.team, |
| 158 | + ) |
| 159 | + |
| 160 | + response = self.client.get( |
| 161 | + f"/api/environments/{self.team.id}/agent-artifacts/", |
| 162 | + {"conversation": str(self.conversation.id)}, |
| 163 | + ) |
| 164 | + |
| 165 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 166 | + self.assertEqual(len(response.json()["results"]), 1) |
| 167 | + self.assertEqual(response.json()["results"][0]["name"], "Artifact 1") |
| 168 | + |
| 169 | + def test_retrieve_artifact(self): |
| 170 | + artifact = AgentArtifact.objects.create( |
| 171 | + name="Test Artifact", |
| 172 | + type=AgentArtifact.Type.VISUALIZATION, |
| 173 | + content={"query": {"kind": "TrendsQuery"}}, |
| 174 | + conversation=self.conversation, |
| 175 | + team=self.team, |
| 176 | + ) |
| 177 | + |
| 178 | + response = self.client.get(f"/api/environments/{self.team.id}/agent-artifacts/{artifact.short_id}/") |
| 179 | + |
| 180 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 181 | + data = response.json() |
| 182 | + self.assertEqual(data["short_id"], artifact.short_id) |
| 183 | + self.assertEqual(data["name"], "Test Artifact") |
| 184 | + self.assertEqual(data["type"], "visualization") |
| 185 | + self.assertEqual(data["content"], {"query": {"kind": "TrendsQuery"}}) |
| 186 | + self.assertIn("id", data) |
| 187 | + self.assertIn("created_at", data) |
| 188 | + |
| 189 | + def test_create_artifact_not_allowed(self): |
| 190 | + response = self.client.post( |
| 191 | + f"/api/environments/{self.team.id}/agent-artifacts/", |
| 192 | + { |
| 193 | + "name": "New Artifact", |
| 194 | + "type": "visualization", |
| 195 | + "content": {"query": {"kind": "FunnelsQuery"}}, |
| 196 | + "conversation": str(self.conversation.id), |
| 197 | + }, |
| 198 | + format="json", |
| 199 | + ) |
| 200 | + |
| 201 | + self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) |
| 202 | + |
| 203 | + def test_delete_artifact(self): |
| 204 | + artifact = AgentArtifact.objects.create( |
| 205 | + name="To Delete", |
| 206 | + type=AgentArtifact.Type.VISUALIZATION, |
| 207 | + content={}, |
| 208 | + conversation=self.conversation, |
| 209 | + team=self.team, |
| 210 | + ) |
| 211 | + |
| 212 | + response = self.client.delete(f"/api/environments/{self.team.id}/agent-artifacts/{artifact.short_id}/") |
| 213 | + |
| 214 | + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) |
| 215 | + self.assertFalse(AgentArtifact.objects.filter(short_id=artifact.short_id).exists()) |
| 216 | + |
| 217 | + def test_cannot_access_other_user_artifacts(self): |
| 218 | + other_user = self. _create_user( "[email protected]") |
| 219 | + other_conversation = Conversation.objects.create( |
| 220 | + user=other_user, team=self.team, title="Other Conversation", type=Conversation.Type.ASSISTANT |
| 221 | + ) |
| 222 | + |
| 223 | + artifact = AgentArtifact.objects.create( |
| 224 | + name="Other User Artifact", |
| 225 | + type=AgentArtifact.Type.VISUALIZATION, |
| 226 | + content={}, |
| 227 | + conversation=other_conversation, |
| 228 | + team=self.team, |
| 229 | + ) |
| 230 | + |
| 231 | + response = self.client.get(f"/api/environments/{self.team.id}/agent-artifacts/{artifact.short_id}/") |
| 232 | + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) |
| 233 | + |
| 234 | + def test_cannot_access_other_team_artifacts(self): |
| 235 | + other_team = self.organization.teams.create(name="Other Team") |
| 236 | + |
| 237 | + other_team_conversation = Conversation.objects.create( |
| 238 | + user=self.user, team=other_team, title="Other Team Conversation", type=Conversation.Type.ASSISTANT |
| 239 | + ) |
| 240 | + |
| 241 | + artifact = AgentArtifact.objects.create( |
| 242 | + name="Other Team Artifact", |
| 243 | + type=AgentArtifact.Type.VISUALIZATION, |
| 244 | + content={}, |
| 245 | + conversation=other_team_conversation, |
| 246 | + team=other_team, |
| 247 | + ) |
| 248 | + |
| 249 | + response = self.client.get(f"/api/environments/{self.team.id}/agent-artifacts/{artifact.short_id}/") |
| 250 | + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) |
| 251 | + |
| 252 | + def test_list_artifacts_ordered_by_created_at_desc(self): |
| 253 | + AgentArtifact.objects.create( |
| 254 | + name="First", |
| 255 | + type=AgentArtifact.Type.VISUALIZATION, |
| 256 | + content={}, |
| 257 | + conversation=self.conversation, |
| 258 | + team=self.team, |
| 259 | + ) |
| 260 | + |
| 261 | + AgentArtifact.objects.create( |
| 262 | + name="Second", |
| 263 | + type=AgentArtifact.Type.DOCUMENT, |
| 264 | + content={}, |
| 265 | + conversation=self.conversation, |
| 266 | + team=self.team, |
| 267 | + ) |
| 268 | + |
| 269 | + response = self.client.get(f"/api/environments/{self.team.id}/agent-artifacts/") |
| 270 | + |
| 271 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 272 | + results = response.json()["results"] |
| 273 | + self.assertEqual(results[0]["name"], "Second") |
| 274 | + self.assertEqual(results[1]["name"], "First") |
0 commit comments