|
| 1 | +using EssentialCSharp.Chat.Common.Models; |
| 2 | +using EssentialCSharp.Chat.Common.Services; |
| 3 | +using Microsoft.Extensions.AI; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using Microsoft.Extensions.VectorData; |
| 6 | +using Moq; |
| 7 | +using Moq.Language.Flow; |
| 8 | +using Npgsql; |
| 9 | + |
| 10 | +namespace EssentialCSharp.Chat.Tests; |
| 11 | + |
| 12 | +public class AISearchServiceTests |
| 13 | +{ |
| 14 | + private static readonly BookContentChunk _TestChunk = new() { Id = "test-1", ChunkText = "test" }; |
| 15 | + |
| 16 | + private static (AISearchService svc, Mock<VectorStoreCollection<string, BookContentChunk>> collectionMock) |
| 17 | + CreateService() |
| 18 | + { |
| 19 | + var collectionMock = new Mock<VectorStoreCollection<string, BookContentChunk>>(); |
| 20 | + |
| 21 | + var vectorStoreMock = new Mock<VectorStore>(); |
| 22 | + vectorStoreMock |
| 23 | + .Setup(vs => vs.GetCollection<string, BookContentChunk>(It.IsAny<string>(), It.IsAny<VectorStoreCollectionDefinition?>())) |
| 24 | + .Returns(collectionMock.Object); |
| 25 | + |
| 26 | + // IEmbeddingGenerator<string, Embedding<float>>.GenerateAsync is the batch interface method |
| 27 | + // called internally by the single-value extension used in EmbeddingService.GenerateEmbeddingAsync. |
| 28 | + var embGenMock = new Mock<IEmbeddingGenerator<string, Embedding<float>>>(); |
| 29 | + embGenMock |
| 30 | + .Setup(g => g.GenerateAsync( |
| 31 | + It.IsAny<IEnumerable<string>>(), |
| 32 | + It.IsAny<EmbeddingGenerationOptions?>(), |
| 33 | + It.IsAny<CancellationToken>())) |
| 34 | + .ReturnsAsync(new GeneratedEmbeddings<Embedding<float>>([new Embedding<float>(new float[1536])])); |
| 35 | + |
| 36 | + var embeddingService = new EmbeddingService(vectorStoreMock.Object, embGenMock.Object); |
| 37 | + var loggerMock = new Mock<ILogger<AISearchService>>(); |
| 38 | + |
| 39 | + return (new AISearchService(vectorStoreMock.Object, embeddingService, loggerMock.Object), collectionMock); |
| 40 | + } |
| 41 | + |
| 42 | + private static async IAsyncEnumerable<VectorSearchResult<BookContentChunk>> OneResultStream() |
| 43 | + { |
| 44 | + yield return new VectorSearchResult<BookContentChunk>(_TestChunk, 0.9f); |
| 45 | + await Task.CompletedTask; |
| 46 | + } |
| 47 | + |
| 48 | + private static ISetup<VectorStoreCollection<string, BookContentChunk>, IAsyncEnumerable<VectorSearchResult<BookContentChunk>>> |
| 49 | + SetupSearch(Mock<VectorStoreCollection<string, BookContentChunk>> mock) => |
| 50 | + mock.Setup(c => c.SearchAsync( |
| 51 | + It.IsAny<ReadOnlyMemory<float>>(), |
| 52 | + It.IsAny<int>(), |
| 53 | + It.IsAny<VectorSearchOptions<BookContentChunk>?>(), |
| 54 | + It.IsAny<CancellationToken>())); |
| 55 | + |
| 56 | + [Test] |
| 57 | + public async Task ExecuteVectorSearch_HappyPath_ReturnsResultsWithoutRetry() |
| 58 | + { |
| 59 | + var (svc, collectionMock) = CreateService(); |
| 60 | + int callCount = 0; |
| 61 | + |
| 62 | + SetupSearch(collectionMock).Returns(() => { callCount++; return OneResultStream(); }); |
| 63 | + |
| 64 | + var results = await svc.ExecuteVectorSearch("test query"); |
| 65 | + |
| 66 | + await Assert.That(results.Count).IsEqualTo(1); |
| 67 | + await Assert.That(callCount).IsEqualTo(1); |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public async Task ExecuteVectorSearch_RetriesOnce_WhenFirstAttemptThrows28000() |
| 72 | + { |
| 73 | + var (svc, collectionMock) = CreateService(); |
| 74 | + int callCount = 0; |
| 75 | + |
| 76 | + SetupSearch(collectionMock).Returns(() => |
| 77 | + { |
| 78 | + callCount++; |
| 79 | + if (callCount == 1) |
| 80 | + throw new PostgresException("auth token expired", "FATAL", "FATAL", "28000"); |
| 81 | + return OneResultStream(); |
| 82 | + }); |
| 83 | + |
| 84 | + var results = await svc.ExecuteVectorSearch("test query"); |
| 85 | + |
| 86 | + await Assert.That(results.Count).IsEqualTo(1); |
| 87 | + await Assert.That(callCount).IsEqualTo(2); |
| 88 | + } |
| 89 | + |
| 90 | + [Test] |
| 91 | + public async Task ExecuteVectorSearch_DoesNotRetry_WhenSqlStateIsNot28000() |
| 92 | + { |
| 93 | + var (svc, collectionMock) = CreateService(); |
| 94 | + |
| 95 | + SetupSearch(collectionMock).Returns(() => throw new PostgresException("table not found", "ERROR", "ERROR", "42P01")); |
| 96 | + |
| 97 | + await Assert.ThrowsAsync<PostgresException>(() => svc.ExecuteVectorSearch("test query")); |
| 98 | + } |
| 99 | + |
| 100 | + [Test] |
| 101 | + public async Task ExecuteVectorSearch_PropagatesException_WhenBothAttemptsFail28000() |
| 102 | + { |
| 103 | + var (svc, collectionMock) = CreateService(); |
| 104 | + |
| 105 | + SetupSearch(collectionMock).Returns(() => throw new PostgresException("auth failed", "FATAL", "FATAL", "28000")); |
| 106 | + |
| 107 | + await Assert.ThrowsAsync<PostgresException>(() => svc.ExecuteVectorSearch("test query")); |
| 108 | + } |
| 109 | +} |
0 commit comments