-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversationServiceIntegrationTests.cs
More file actions
497 lines (403 loc) · 22 KB
/
ConversationServiceIntegrationTests.cs
File metadata and controls
497 lines (403 loc) · 22 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
using FluentAssertions;
using ProjectVG.Application.Services.Character;
using ProjectVG.Application.Services.Conversation;
using ProjectVG.Application.Services.Users;
using ProjectVG.Common.Exceptions;
using ProjectVG.Domain.Entities.ConversationHistorys;
using ProjectVG.Tests.Application.Integration.TestBase;
using ProjectVG.Tests.Application.TestUtilities;
using Xunit;
namespace ProjectVG.Tests.Application.Integration
{
[Collection("ApplicationIntegration")]
public class ConversationServiceIntegrationTests
{
private readonly IConversationService _conversationService;
private readonly IUserService _userService;
private readonly ICharacterService _characterService;
private readonly ApplicationIntegrationTestFixture _fixture;
public ConversationServiceIntegrationTests(ApplicationIntegrationTestFixture fixture)
{
_fixture = fixture;
_conversationService = fixture.GetService<IConversationService>();
_userService = fixture.GetService<IUserService>();
_characterService = fixture.GetService<ICharacterService>();
}
#region Add Message Integration Tests
[Fact]
public async Task AddMessageAsync_WithValidParameters_ShouldPersistMessage()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
var content = "Hello, this is a test message!";
var role = ChatRole.User;
// Act
var addedMessage = await _conversationService.AddMessageAsync(userId, characterId, role, content, DateTime.UtcNow);
// Assert
addedMessage.Should().NotBeNull();
addedMessage.UserId.Should().Be(userId);
addedMessage.CharacterId.Should().Be(characterId);
addedMessage.Role.Should().Be(role);
addedMessage.Content.Should().Be(content);
addedMessage.Id.Should().NotBe(Guid.Empty);
addedMessage.CreatedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(10));
}
[Theory]
[InlineData("user")]
[InlineData("assistant")]
[InlineData("system")]
public async Task AddMessageAsync_WithDifferentRoles_ShouldPersistCorrectly(string role)
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
var content = $"Message from {role}";
// Act
var addedMessage = await _conversationService.AddMessageAsync(userId, characterId, role, content, DateTime.UtcNow);
// Assert
addedMessage.Role.Should().Be(role);
addedMessage.Content.Should().Be(content);
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public async Task AddMessageAsync_WithNullOrWhitespaceContent_ShouldThrowValidationException(string content)
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
// Act & Assert
await Assert.ThrowsAsync<ValidationException>(
() => _conversationService.AddMessageAsync(userId, characterId, ChatRole.User, content, DateTime.UtcNow));
}
[Fact]
public async Task AddMessageAsync_WithContentTooLong_ShouldThrowValidationException()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
var longContent = new string('x', 10001); // Exceeds 10000 character limit
// Act & Assert
await Assert.ThrowsAsync<ValidationException>(
() => _conversationService.AddMessageAsync(userId, characterId, ChatRole.User, longContent, DateTime.UtcNow));
}
[Fact]
public async Task AddMessageAsync_WithMaxLengthContent_ShouldSucceed()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
var maxContent = new string('x', 10000); // Exactly 10000 characters
// Act
var addedMessage = await _conversationService.AddMessageAsync(userId, characterId, ChatRole.User, maxContent, DateTime.UtcNow);
// Assert
addedMessage.Should().NotBeNull();
addedMessage.Content.Should().Be(maxContent);
addedMessage.Content.Length.Should().Be(10000);
}
#endregion
#region Get Conversation History Integration Tests
[Fact]
public async Task GetConversationHistoryAsync_WithExistingMessages_ShouldReturnInCorrectOrder()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
// Add messages with slight delays to ensure different timestamps
var message1 = await _conversationService.AddMessageAsync(userId, characterId, ChatRole.User, "First message", DateTime.UtcNow);
await Task.Delay(10);
var message2 = await _conversationService.AddMessageAsync(userId, characterId, ChatRole.Assistant, "Second message", DateTime.UtcNow);
await Task.Delay(10);
var message3 = await _conversationService.AddMessageAsync(userId, characterId, ChatRole.User, "Third message", DateTime.UtcNow);
// Act
var history = await _conversationService.GetConversationHistoryAsync(userId, characterId, 1, 10);
// Assert
var historyList = history.ToList();
historyList.Should().HaveCount(3);
// Should be ordered by creation time (repository determines the order)
historyList.Should().Contain(m => m.Id == message1.Id);
historyList.Should().Contain(m => m.Id == message2.Id);
historyList.Should().Contain(m => m.Id == message3.Id);
}
[Fact]
public async Task GetConversationHistoryAsync_WithCountLimit_ShouldRespectLimit()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
// Add 5 messages
for (int i = 1; i <= 5; i++)
{
await _conversationService.AddMessageAsync(userId, characterId, ChatRole.User, $"Message {i}", DateTime.UtcNow);
await Task.Delay(10); // Ensure different timestamps
}
// Act - Request only 3 messages
var history = await _conversationService.GetConversationHistoryAsync(userId, characterId, 1, 3);
// Assert
var historyList = history.ToList();
historyList.Should().HaveCount(3);
}
[Fact]
public async Task GetConversationHistoryAsync_WithNoMessages_ShouldReturnEmptyCollection()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
// Act
var history = await _conversationService.GetConversationHistoryAsync(userId, characterId);
// Assert
history.Should().NotBeNull();
history.Should().BeEmpty();
}
[Fact]
public async Task GetConversationHistoryAsync_WithDefaultCount_ShouldUse10AsDefault()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
// Add 15 messages
for (int i = 1; i <= 15; i++)
{
await _conversationService.AddMessageAsync(userId, characterId, ChatRole.User, $"Message {i}", DateTime.UtcNow);
}
// Act - Use default count
var history = await _conversationService.GetConversationHistoryAsync(userId, characterId);
// Assert
var historyList = history.ToList();
historyList.Should().HaveCount(10); // Default count should be 10
}
[Theory]
[InlineData(0)]
[InlineData(-1)]
[InlineData(101)]
public async Task GetConversationHistoryAsync_WithInvalidCount_ShouldThrowValidationException(int count)
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
// Act & Assert
await Assert.ThrowsAsync<ValidationException>(
() => _conversationService.GetConversationHistoryAsync(userId, characterId, 1, count));
}
[Fact]
public async Task GetConversationHistoryAsync_WithMultipleUserCharacterPairs_ShouldReturnOnlyRelevantMessages()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var user1 = await CreateUserAsync("user1", "user1@example.com");
var user2 = await CreateUserAsync("user2", "user2@example.com");
var char1 = await CreateCharacterAsync("Character1", user1.Id);
var char2 = await CreateCharacterAsync("Character2", user1.Id);
// Add messages for different user-character combinations
await _conversationService.AddMessageAsync(user1.Id, char1.Id, ChatRole.User, "User1-Char1 Message", DateTime.UtcNow);
await _conversationService.AddMessageAsync(user1.Id, char2.Id, ChatRole.User, "User1-Char2 Message", DateTime.UtcNow);
await _conversationService.AddMessageAsync(user2.Id, char1.Id, ChatRole.User, "User2-Char1 Message", DateTime.UtcNow);
await _conversationService.AddMessageAsync(user2.Id, char2.Id, ChatRole.User, "User2-Char2 Message", DateTime.UtcNow);
// Act
var user1Char1History = await _conversationService.GetConversationHistoryAsync(user1.Id, char1.Id);
// Assert
var historyList = user1Char1History.ToList();
historyList.Should().HaveCount(1);
historyList[0].Content.Should().Be("User1-Char1 Message");
historyList[0].UserId.Should().Be(user1.Id);
historyList[0].CharacterId.Should().Be(char1.Id);
}
#endregion
#region Clear Conversation Integration Tests
[Fact]
public async Task ClearConversationAsync_WithExistingMessages_ShouldRemoveAllMessages()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
// Add multiple messages
await _conversationService.AddMessageAsync(userId, characterId, ChatRole.User, "Message 1", DateTime.UtcNow);
await _conversationService.AddMessageAsync(userId, characterId, ChatRole.Assistant, "Response 1", DateTime.UtcNow);
await _conversationService.AddMessageAsync(userId, characterId, ChatRole.User, "Message 2", DateTime.UtcNow);
// Verify messages exist
var historyBefore = await _conversationService.GetConversationHistoryAsync(userId, characterId);
historyBefore.Should().HaveCount(3);
// Act
await _conversationService.DeleteConversationAsync(userId, characterId);
// Assert
var historyAfter = await _conversationService.GetConversationHistoryAsync(userId, characterId);
historyAfter.Should().BeEmpty();
}
[Fact]
public async Task ClearConversationAsync_WithNoMessages_ShouldNotThrow()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
// Act & Assert - Should not throw
await _conversationService.DeleteConversationAsync(userId, characterId);
}
[Fact]
public async Task ClearConversationAsync_ShouldOnlyAffectSpecificUserCharacterPair()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var user1 = await CreateUserAsync("user1", "user1@example.com");
var user2 = await CreateUserAsync("user2", "user2@example.com");
var char1 = await CreateCharacterAsync("Character1", user1.Id);
var char2 = await CreateCharacterAsync("Character2", user1.Id);
// Add messages for different combinations
await _conversationService.AddMessageAsync(user1.Id, char1.Id, ChatRole.User, "User1-Char1", DateTime.UtcNow);
await _conversationService.AddMessageAsync(user1.Id, char2.Id, ChatRole.User, "User1-Char2", DateTime.UtcNow);
await _conversationService.AddMessageAsync(user2.Id, char1.Id, ChatRole.User, "User2-Char1", DateTime.UtcNow);
// Act - Clear only user1-char1 conversation
await _conversationService.DeleteConversationAsync(user1.Id, char1.Id);
// Assert
var user1Char1History = await _conversationService.GetConversationHistoryAsync(user1.Id, char1.Id);
var user1Char2History = await _conversationService.GetConversationHistoryAsync(user1.Id, char2.Id);
var user2Char1History = await _conversationService.GetConversationHistoryAsync(user2.Id, char1.Id);
user1Char1History.Should().BeEmpty(); // Should be cleared
user1Char2History.Should().HaveCount(1); // Should remain
user2Char1History.Should().HaveCount(1); // Should remain
}
#endregion
#region Get Message Count Integration Tests
[Fact]
public async Task GetMessageCountAsync_WithMultipleMessages_ShouldReturnCorrectCount()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
// Add 5 messages
for (int i = 1; i <= 5; i++)
{
await _conversationService.AddMessageAsync(userId, characterId, ChatRole.User, $"Message {i}", DateTime.UtcNow);
}
// Act
var count = await _conversationService.GetMessageCountAsync(userId, characterId);
// Assert
count.Should().Be(5);
}
[Fact]
public async Task GetMessageCountAsync_WithNoMessages_ShouldReturnZero()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
// Act
var count = await _conversationService.GetMessageCountAsync(userId, characterId);
// Assert
count.Should().Be(0);
}
[Fact]
public async Task GetMessageCountAsync_AfterClearingConversation_ShouldReturnZero()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
// Add messages
await _conversationService.AddMessageAsync(userId, characterId, ChatRole.User, "Message 1", DateTime.UtcNow);
await _conversationService.AddMessageAsync(userId, characterId, ChatRole.Assistant, "Response 1", DateTime.UtcNow);
var countBefore = await _conversationService.GetMessageCountAsync(userId, characterId);
countBefore.Should().Be(2);
// Act
await _conversationService.DeleteConversationAsync(userId, characterId);
var countAfter = await _conversationService.GetMessageCountAsync(userId, characterId);
// Assert
countAfter.Should().Be(0);
}
#endregion
#region Complex Integration Scenarios
[Fact]
public async Task CompleteConversationLifecycle_ShouldWorkCorrectly()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var (userId, characterId) = await CreateUserAndCharacterAsync();
// Initial state - no messages
var initialCount = await _conversationService.GetMessageCountAsync(userId, characterId);
initialCount.Should().Be(0);
var initialHistory = await _conversationService.GetConversationHistoryAsync(userId, characterId);
initialHistory.Should().BeEmpty();
// Add conversation messages
var userMessage = await _conversationService.AddMessageAsync(
userId, characterId, ChatRole.User, "Hello, how are you?", DateTime.UtcNow);
var assistantMessage = await _conversationService.AddMessageAsync(
userId, characterId, ChatRole.Assistant, "I'm doing well, thank you for asking!", DateTime.UtcNow);
var followupMessage = await _conversationService.AddMessageAsync(
userId, characterId, ChatRole.User, "That's great to hear!", DateTime.UtcNow);
// Verify messages were added
var countAfterAdding = await _conversationService.GetMessageCountAsync(userId, characterId);
countAfterAdding.Should().Be(3);
var historyAfterAdding = await _conversationService.GetConversationHistoryAsync(userId, characterId);
historyAfterAdding.Should().HaveCount(3);
// Verify message content and order
var messagesList = historyAfterAdding.ToList();
messagesList.Should().Contain(m => m.Content == "Hello, how are you?" && m.Role == ChatRole.User);
messagesList.Should().Contain(m => m.Content == "I'm doing well, thank you for asking!" && m.Role == ChatRole.Assistant);
messagesList.Should().Contain(m => m.Content == "That's great to hear!" && m.Role == ChatRole.User);
// Clear conversation
await _conversationService.DeleteConversationAsync(userId, characterId);
// Verify conversation is cleared
var countAfterClearing = await _conversationService.GetMessageCountAsync(userId, characterId);
countAfterClearing.Should().Be(0);
var historyAfterClearing = await _conversationService.GetConversationHistoryAsync(userId, characterId);
historyAfterClearing.Should().BeEmpty();
}
[Fact]
public async Task MultipleConversationsSimultaneously_ShouldIsolateCorrectly()
{
// Arrange
await _fixture.ClearDatabaseAsync();
var user1 = await CreateUserAsync("user1", "user1@example.com");
var user2 = await CreateUserAsync("user2", "user2@example.com");
var character1 = await CreateCharacterAsync("Character1", user1.Id);
var character2 = await CreateCharacterAsync("Character2", user1.Id);
// Create conversations for different user-character pairs
// User1 with Character1
await _conversationService.AddMessageAsync(user1.Id, character1.Id, ChatRole.User, "User1 to Char1: Hello", DateTime.UtcNow);
await _conversationService.AddMessageAsync(user1.Id, character1.Id, ChatRole.Assistant, "Char1 to User1: Hi there", DateTime.UtcNow);
// User1 with Character2
await _conversationService.AddMessageAsync(user1.Id, character2.Id, ChatRole.User, "User1 to Char2: Hey", DateTime.UtcNow);
// User2 with Character1
await _conversationService.AddMessageAsync(user2.Id, character1.Id, ChatRole.User, "User2 to Char1: Good morning", DateTime.UtcNow);
await _conversationService.AddMessageAsync(user2.Id, character1.Id, ChatRole.Assistant, "Char1 to User2: Good morning!", DateTime.UtcNow);
await _conversationService.AddMessageAsync(user2.Id, character1.Id, ChatRole.User, "User2 to Char1: How's the weather?", DateTime.UtcNow);
// Verify counts for each conversation
var user1Char1Count = await _conversationService.GetMessageCountAsync(user1.Id, character1.Id);
var user1Char2Count = await _conversationService.GetMessageCountAsync(user1.Id, character2.Id);
var user2Char1Count = await _conversationService.GetMessageCountAsync(user2.Id, character1.Id);
var user2Char2Count = await _conversationService.GetMessageCountAsync(user2.Id, character2.Id);
user1Char1Count.Should().Be(2);
user1Char2Count.Should().Be(1);
user2Char1Count.Should().Be(3);
user2Char2Count.Should().Be(0);
// Verify message isolation
var user1Char1History = await _conversationService.GetConversationHistoryAsync(user1.Id, character1.Id);
var user1Char1Messages = user1Char1History.ToList();
user1Char1Messages.Should().HaveCount(2);
user1Char1Messages.Should().OnlyContain(m => m.UserId == user1.Id && m.CharacterId == character1.Id);
user1Char1Messages.Should().Contain(m => m.Content == "User1 to Char1: Hello");
user1Char1Messages.Should().Contain(m => m.Content == "Char1 to User1: Hi there");
}
#endregion
#region Helper Methods
private async Task<(Guid userId, Guid characterId)> CreateUserAndCharacterAsync()
{
var user = await CreateUserAsync();
var character = await CreateCharacterAsync("TestCharacter", user.Id);
return (user.Id, character.Id);
}
private async Task<ProjectVG.Application.Models.User.UserDto> CreateUserAsync(
string username = "testuser",
string email = "test@example.com")
{
var createCommand = TestDataBuilder.CreateUserCreateCommand(username, email);
return await _userService.CreateUserAsync(createCommand);
}
private async Task<ProjectVG.Application.Models.Character.CharacterDto> CreateCharacterAsync(
string name = "TestCharacter", Guid? userId = null)
{
var createCommand = TestDataBuilder.CreateCreateCharacterWithFieldsCommand(name, userId: userId);
return await _characterService.CreateCharacterWithFieldsAsync(createCommand);
}
#endregion
}
}