| 
 | 1 | +using System;  | 
 | 2 | +using System.IO;  | 
 | 3 | +using System.Threading.Tasks;  | 
 | 4 | +using Microsoft.Extensions.Logging;  | 
 | 5 | +using Moq;  | 
 | 6 | +using Xunit;  | 
 | 7 | + | 
 | 8 | +namespace KernelMemory.Extensions.FunctionalTests.Helper;  | 
 | 9 | + | 
 | 10 | +public class LocalFolderPromptStoreTests : IDisposable  | 
 | 11 | +{  | 
 | 12 | +    private readonly string _testDirectory;  | 
 | 13 | +    private readonly Mock<ILogger<LocalFolderPromptStore>> _loggerMock;  | 
 | 14 | +    private readonly LocalFolderPromptStore _store;  | 
 | 15 | + | 
 | 16 | +    public LocalFolderPromptStoreTests()  | 
 | 17 | +    {  | 
 | 18 | +        _testDirectory = Path.Combine(Path.GetTempPath(), $"promptstore_tests_{Guid.NewGuid()}");  | 
 | 19 | +        _loggerMock = new Mock<ILogger<LocalFolderPromptStore>>();  | 
 | 20 | +        _store = new LocalFolderPromptStore(_testDirectory, _loggerMock.Object);  | 
 | 21 | +    }  | 
 | 22 | + | 
 | 23 | +    public void Dispose()  | 
 | 24 | +    {  | 
 | 25 | +        if (Directory.Exists(_testDirectory))  | 
 | 26 | +        {  | 
 | 27 | +            Directory.Delete(_testDirectory, true);  | 
 | 28 | +        }  | 
 | 29 | +    }  | 
 | 30 | + | 
 | 31 | +    [Fact]  | 
 | 32 | +    public async Task GetPromptAsync_NonExistentKey_ReturnsNull()  | 
 | 33 | +    {  | 
 | 34 | +        // Act  | 
 | 35 | +        var result = await _store.GetPromptAsync("nonexistent");  | 
 | 36 | + | 
 | 37 | +        // Assert  | 
 | 38 | +        Assert.Null(result);  | 
 | 39 | +    }  | 
 | 40 | + | 
 | 41 | +    [Fact]  | 
 | 42 | +    public async Task SetAndGetPromptAsync_ValidKey_ReturnsStoredPrompt()  | 
 | 43 | +    {  | 
 | 44 | +        // Arrange  | 
 | 45 | +        const string key = "test-key";  | 
 | 46 | +        const string expectedPrompt = "This is a test prompt";  | 
 | 47 | + | 
 | 48 | +        // Act  | 
 | 49 | +        await _store.SetPromptAsync(key, expectedPrompt);  | 
 | 50 | +        var result = await _store.GetPromptAsync(key);  | 
 | 51 | + | 
 | 52 | +        // Assert  | 
 | 53 | +        Assert.Equal(expectedPrompt, result);  | 
 | 54 | +    }  | 
 | 55 | + | 
 | 56 | +    [Fact]  | 
 | 57 | +    public async Task GetPromptAndSetDefaultAsync_NonExistentKey_SetsAndReturnsDefault()  | 
 | 58 | +    {  | 
 | 59 | +        // Arrange  | 
 | 60 | +        const string key = "default-key";  | 
 | 61 | +        const string defaultPrompt = "Default prompt value";  | 
 | 62 | + | 
 | 63 | +        // Act  | 
 | 64 | +        var result = await _store.GetPromptAndSetDefaultAsync(key, defaultPrompt);  | 
 | 65 | +        var storedPrompt = await _store.GetPromptAsync(key);  | 
 | 66 | + | 
 | 67 | +        // Assert  | 
 | 68 | +        Assert.Equal(defaultPrompt, result);  | 
 | 69 | +        Assert.Equal(defaultPrompt, storedPrompt);  | 
 | 70 | +    }  | 
 | 71 | + | 
 | 72 | +    [Fact]  | 
 | 73 | +    public async Task GetPromptAndSetDefaultAsync_ExistingKey_ReturnsExistingPrompt()  | 
 | 74 | +    {  | 
 | 75 | +        // Arrange  | 
 | 76 | +        const string key = "existing-key";  | 
 | 77 | +        const string existingPrompt = "Existing prompt";  | 
 | 78 | +        const string defaultPrompt = "Default prompt";  | 
 | 79 | +        await _store.SetPromptAsync(key, existingPrompt);  | 
 | 80 | + | 
 | 81 | +        // Act  | 
 | 82 | +        var result = await _store.GetPromptAndSetDefaultAsync(key, defaultPrompt);  | 
 | 83 | + | 
 | 84 | +        // Assert  | 
 | 85 | +        Assert.Equal(existingPrompt, result);  | 
 | 86 | +    }  | 
 | 87 | + | 
 | 88 | +    [Fact]  | 
 | 89 | +    public async Task SetPromptAsync_KeyWithSpecialCharacters_HandlesCorrectly()  | 
 | 90 | +    {  | 
 | 91 | +        // Arrange  | 
 | 92 | +        const string key = "special/\\*:?\"<>|characters";  | 
 | 93 | +        const string expectedPrompt = "Prompt with special characters";  | 
 | 94 | + | 
 | 95 | +        // Act  | 
 | 96 | +        await _store.SetPromptAsync(key, expectedPrompt);  | 
 | 97 | +        var result = await _store.GetPromptAsync(key);  | 
 | 98 | + | 
 | 99 | +        // Assert  | 
 | 100 | +        Assert.Equal(expectedPrompt, result);  | 
 | 101 | +    }  | 
 | 102 | + | 
 | 103 | +    [Fact]  | 
 | 104 | +    public async Task GetPromptAndSetDefaultAsync_MissingPlaceholder_LogsError()  | 
 | 105 | +    {  | 
 | 106 | +        // Arrange  | 
 | 107 | +        const string key = "test-placeholder";  | 
 | 108 | +        const string existingPrompt = "A prompt without placeholder";  | 
 | 109 | +        const string defaultPrompt = "Default prompt with {{$placeholder}}";  | 
 | 110 | +        await _store.SetPromptAsync(key, existingPrompt);  | 
 | 111 | + | 
 | 112 | +        // Act  | 
 | 113 | +        var result = await _store.GetPromptAndSetDefaultAsync(key, defaultPrompt);  | 
 | 114 | + | 
 | 115 | +        // Assert  | 
 | 116 | +        _loggerMock.Verify(  | 
 | 117 | +            x => x.Log(  | 
 | 118 | +                LogLevel.Error,  | 
 | 119 | +                It.IsAny<EventId>(),  | 
 | 120 | +                It.Is<It.IsAnyType>((v, t) => v.ToString().Contains("{{$placeholder}}")),  | 
 | 121 | +                It.IsAny<Exception>(),  | 
 | 122 | +                It.IsAny<Func<It.IsAnyType, Exception, string>>()  | 
 | 123 | +            ),  | 
 | 124 | +            Times.Once);  | 
 | 125 | +    }  | 
 | 126 | + | 
 | 127 | +    [Fact]  | 
 | 128 | +    public async Task GetPromptAndSetDefaultAsync_MultipleMissingPlaceholders_LogsMultipleErrors()  | 
 | 129 | +    {  | 
 | 130 | +        // Arrange  | 
 | 131 | +        const string key = "test-multiple-placeholders";  | 
 | 132 | +        const string existingPrompt = "A prompt without any placeholders";  | 
 | 133 | +        const string defaultPrompt = "Default with {{$first}} and {{$second}}";  | 
 | 134 | +        await _store.SetPromptAsync(key, existingPrompt);  | 
 | 135 | + | 
 | 136 | +        // Act  | 
 | 137 | +        var result = await _store.GetPromptAndSetDefaultAsync(key, defaultPrompt);  | 
 | 138 | + | 
 | 139 | +        // Assert  | 
 | 140 | +        _loggerMock.Verify(  | 
 | 141 | +            x => x.Log(  | 
 | 142 | +                LogLevel.Error,  | 
 | 143 | +                It.IsAny<EventId>(),  | 
 | 144 | +                It.Is<It.IsAnyType>((v, t) => true),  | 
 | 145 | +                It.IsAny<Exception>(),  | 
 | 146 | +                It.IsAny<Func<It.IsAnyType, Exception, string>>()  | 
 | 147 | +            ),  | 
 | 148 | +            Times.Exactly(2));  | 
 | 149 | +    }  | 
 | 150 | + | 
 | 151 | +    [Fact]  | 
 | 152 | +    public async Task GetPromptAndSetDefaultAsync_ValidPlaceholders_NoErrors()  | 
 | 153 | +    {  | 
 | 154 | +        // Arrange  | 
 | 155 | +        const string key = "test-valid-placeholders";  | 
 | 156 | +        const string existingPrompt = "A prompt with {{$placeholder}} correctly set";  | 
 | 157 | +        const string defaultPrompt = "Default with {{$placeholder}}";  | 
 | 158 | +        await _store.SetPromptAsync(key, existingPrompt);  | 
 | 159 | + | 
 | 160 | +        // Act  | 
 | 161 | +        var result = await _store.GetPromptAndSetDefaultAsync(key, defaultPrompt);  | 
 | 162 | + | 
 | 163 | +        // Assert  | 
 | 164 | +        _loggerMock.Verify(  | 
 | 165 | +            x => x.Log(  | 
 | 166 | +                LogLevel.Error,  | 
 | 167 | +                It.IsAny<EventId>(),  | 
 | 168 | +                It.Is<It.IsAnyType>((v, t) => true),  | 
 | 169 | +                It.IsAny<Exception>(),  | 
 | 170 | +                It.IsAny<Func<It.IsAnyType, Exception, string>>()  | 
 | 171 | +            ),  | 
 | 172 | +            Times.Never);  | 
 | 173 | +    }  | 
 | 174 | +}  | 
0 commit comments