File size: 7,023 Bytes
495529d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
65
66
67
68
69
70
71
72
73
74
75
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# Ticket Comments - Testing Guide

## Manual Testing Checklist

### 1. Create Comment ✅
```bash
curl -X POST "http://localhost:7860/api/v1/tickets/{ticket_id}/comments" \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{
    "comment_text": "Customer prefers morning visits",
    "is_internal": true,
    "comment_type": "note"
  }'
```

**Expected:** 201 Created with comment details

---

### 2. Create Reply (Threading) ✅
```bash
curl -X POST "http://localhost:7860/api/v1/tickets/{ticket_id}/comments" \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{
    "comment_text": "I will handle this",
    "parent_comment_id": "{parent_comment_id}",
    "is_internal": true,
    "comment_type": "note"
  }'
```

**Expected:** 201 Created with parent_comment_id set

---

### 3. List Comments ✅
```bash
curl -X GET "http://localhost:7860/api/v1/tickets/{ticket_id}/comments?page=1&page_size=20" \
  -H "Authorization: Bearer {token}"
```

**Expected:** 200 OK with paginated list

---

### 4. Get Comment Replies ✅
```bash
curl -X GET "http://localhost:7860/api/v1/comments/{comment_id}/replies" \
  -H "Authorization: Bearer {token}"
```

**Expected:** 200 OK with array of replies

---

### 5. Update Comment ✅
```bash
curl -X PUT "http://localhost:7860/api/v1/comments/{comment_id}" \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{
    "comment_text": "Updated: Customer prefers afternoon"
  }'
```

**Expected:** 200 OK with is_edited=true

---

### 6. Delete Comment ✅
```bash
curl -X DELETE "http://localhost:7860/api/v1/comments/{comment_id}" \
  -H "Authorization: Bearer {token}"
```

**Expected:** 200 OK with success message

---

## Error Cases to Test

### 1. Create Comment on Non-Existent Ticket
```bash
curl -X POST "http://localhost:7860/api/v1/tickets/00000000-0000-0000-0000-000000000000/comments" \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{"comment_text": "Test", "is_internal": true, "comment_type": "note"}'
```

**Expected:** 404 Not Found - "Ticket not found"

---

### 2. Update Someone Else's Comment
```bash
# Create comment as User A
# Try to update as User B
curl -X PUT "http://localhost:7860/api/v1/comments/{comment_id}" \
  -H "Authorization: Bearer {user_b_token}" \
  -H "Content-Type: application/json" \
  -d '{"comment_text": "Hacked!"}'
```

**Expected:** 403 Forbidden - "You can only edit your own comments"

---

### 3. Reply to Non-Existent Comment
```bash
curl -X POST "http://localhost:7860/api/v1/tickets/{ticket_id}/comments" \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{
    "comment_text": "Reply",
    "parent_comment_id": "00000000-0000-0000-0000-000000000000",
    "is_internal": true,
    "comment_type": "note"
  }'
```

**Expected:** 404 Not Found - "Parent comment not found"

---

### 4. Mention Non-Existent User
```bash
curl -X POST "http://localhost:7860/api/v1/tickets/{ticket_id}/comments" \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{
    "comment_text": "Test",
    "mentioned_user_ids": ["00000000-0000-0000-0000-000000000000"],
    "is_internal": true,
    "comment_type": "note"
  }'
```

**Expected:** 400 Bad Request - "One or more mentioned users not found"

---

### 5. Empty Comment Text
```bash
curl -X POST "http://localhost:7860/api/v1/tickets/{ticket_id}/comments" \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{
    "comment_text": "",
    "is_internal": true,
    "comment_type": "note"
  }'
```

**Expected:** 422 Validation Error - "comment_text must be at least 1 character"

---

## Integration Testing

### Scenario 1: Threaded Conversation
1. User A creates comment: "Need help with this ticket"
2. User B replies: "I can help"
3. User A replies to B: "Thanks!"
4. Verify reply_count increments
5. Load replies and verify order (oldest first)

### Scenario 2: Edit Tracking
1. User creates comment
2. User edits comment
3. Verify is_edited=true, edited_at set, edited_by_user_id set
4. Edit again
5. Verify edited_at updated

### Scenario 3: Soft Delete
1. User creates comment
2. User deletes comment
3. Verify deleted_at set
4. Try to list comments - deleted comment should not appear
5. Try to get deleted comment by ID - should return 404

### Scenario 4: Pagination
1. Create 100 comments
2. List with page_size=20
3. Verify pages=5
4. Load each page
5. Verify no duplicates, no missing comments

---

## Performance Testing

### Load Test: Create 1000 Comments
```python
import asyncio
import aiohttp

async def create_comment(session, ticket_id, token):
    async with session.post(
        f"http://localhost:7860/api/v1/tickets/{ticket_id}/comments",
        headers={"Authorization": f"Bearer {token}"},
        json={
            "comment_text": "Load test comment",
            "is_internal": True,
            "comment_type": "note"
        }
    ) as response:
        return await response.json()

async def main():
    async with aiohttp.ClientSession() as session:
        tasks = [create_comment(session, ticket_id, token) for _ in range(1000)]
        results = await asyncio.gather(*tasks)
        print(f"Created {len(results)} comments")

asyncio.run(main())
```

**Expected:** All 1000 comments created successfully in < 30 seconds

---

## Database Verification

### Check Comment Count
```sql
SELECT COUNT(*) FROM ticket_comments WHERE deleted_at IS NULL;
```

### Check Threading
```sql
SELECT 
    id,
    comment_text,
    parent_comment_id,
    (SELECT COUNT(*) FROM ticket_comments c2 WHERE c2.parent_comment_id = c1.id) as reply_count
FROM ticket_comments c1
WHERE ticket_id = '{ticket_id}' AND deleted_at IS NULL
ORDER BY created_at DESC;
```

### Check Edit History
```sql
SELECT 
    id,
    comment_text,
    is_edited,
    edited_at,
    edited_by_user_id
FROM ticket_comments
WHERE is_edited = TRUE AND deleted_at IS NULL;
```

---

## Common Issues & Solutions

### Issue: 500 Internal Server Error
**Cause:** Missing relationship loading
**Solution:** Ensure all relationships use `joinedload()` in service layer

### Issue: Comments not appearing
**Cause:** Soft delete not filtered
**Solution:** Always filter `deleted_at IS NULL`

### Issue: Reply count incorrect
**Cause:** Not counting replies properly
**Solution:** Use subquery to count replies in `_to_response()`

### Issue: Mentions not working
**Cause:** User IDs not validated
**Solution:** Query users table to verify all mentioned_user_ids exist

---

## Monitoring

### Key Metrics to Track
- Comment creation rate (comments/hour)
- Average reply depth (threading level)
- Edit frequency (edits/comments ratio)
- Delete frequency (deletes/comments ratio)
- Response time for list endpoint

### Alerts to Set Up
- Comment creation failures > 1%
- List endpoint response time > 2 seconds
- Database connection errors
- Validation errors > 5%