-
Notifications
You must be signed in to change notification settings - Fork 0
Add API key configuration, fix chat context persistence, and isolate sessions by IP #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
760cb44
Initial plan
Copilot e7b4ed3
Add ApiKey config, fix chat persistence, add exchange counter
Copilot f7e640e
Add constructor documentation with exception details
Copilot 3ee0b22
Remove unused private variables from constructor
Copilot a9f71a5
Apply all unresolved suggestions from code review
rrusson 96b3609
Isolate chat history by IP address using factory pattern
Copilot 4e3d57b
Remove unnecessary null-forgiving operator with justification
Copilot aeaa00d
Removed sonar-project.properties
rrusson b3d5031
Added some unit tests
rrusson 3c64c3b
Clean-up
rrusson 833e65d
Fix syntax for SONAR_TOKEN in sonarcloud.yml
rrusson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| using Microsoft.Extensions.Caching.Memory; | ||
|
|
||
| using SemanticKernelHelper; | ||
|
|
||
| using SharedInterfaces; | ||
|
|
||
| namespace ClippyWeb.Tests | ||
| { | ||
| [TestClass] | ||
| public class ChatClientFactoryTests | ||
| { | ||
| private const string TestApiUrl = "http://localhost:11434/v1"; | ||
| private const string TestModel = "test-model"; | ||
| private const string TestApiKey = "test-api-key"; | ||
| private IMemoryCache _cache = null!; | ||
|
|
||
| [TestInitialize] | ||
| public void TestInitialize() | ||
| { | ||
| _cache = new MemoryCache(new MemoryCacheOptions()); | ||
| } | ||
|
|
||
| [TestCleanup] | ||
| public void TestCleanup() | ||
| { | ||
| _cache?.Dispose(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void IfSessionKeyIsProvidedThenClientIsReturned() | ||
| { | ||
| var factory = new ChatClientFactory(TestApiUrl, TestModel, TestApiKey, _cache); | ||
|
|
||
| var client = factory.GetOrCreateClient("test-session"); | ||
|
|
||
| Assert.IsNotNull(client); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void IfSameSessionKeyIsUsedThenSameClientIsReturned() | ||
| { | ||
| var factory = new ChatClientFactory(TestApiUrl, TestModel, TestApiKey, _cache); | ||
|
|
||
| var client1 = factory.GetOrCreateClient("test-session"); | ||
| var client2 = factory.GetOrCreateClient("test-session"); | ||
|
|
||
| Assert.AreSame(client1, client2); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void IfDifferentSessionKeysAreUsedThenDifferentClientsAreReturned() | ||
| { | ||
| var factory = new ChatClientFactory(TestApiUrl, TestModel, TestApiKey, _cache); | ||
|
|
||
| var client1 = factory.GetOrCreateClient("session-1"); | ||
| var client2 = factory.GetOrCreateClient("session-2"); | ||
|
|
||
| Assert.AreNotSame(client1, client2); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void IfMultipleSessionsAreConcurrentThenFactoryIsThreadSafe() | ||
| { | ||
| var factory = new ChatClientFactory(TestApiUrl, TestModel, TestApiKey, _cache); | ||
| var clients = new List<IChatClient>(); | ||
| var lockObj = new object(); | ||
|
|
||
| Parallel.For(0, 10, i => | ||
| { | ||
| var client = factory.GetOrCreateClient($"session-{i % 3}"); | ||
| lock (lockObj) | ||
| { | ||
| clients.Add(client); | ||
| } | ||
| }); | ||
|
|
||
| Assert.AreEqual(10, clients.Count); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| global using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| global using Moq; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| using SemanticKernelHelper; | ||
|
|
||
| namespace ClippyWeb.Tests | ||
| { | ||
| [TestClass] | ||
| public class SemanticKernelClientTests | ||
| { | ||
| private const string TestApiUrl = "http://localhost:11434/v1"; | ||
| private const string TestModel = "test-model"; | ||
| private const string TestApiKey = "test-api-key"; | ||
|
|
||
| [TestMethod] | ||
| public void IfApiKeyIsProvidedThenClientIsCreated() | ||
| { | ||
| var client = new SemanticKernelClient(TestApiUrl, TestModel, TestApiKey); | ||
|
|
||
| Assert.IsNotNull(client); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void IfApiKeyIsNullThenClientIsCreatedWithEmptyKey() | ||
| { | ||
| var client = new SemanticKernelClient(TestApiUrl, TestModel, null); | ||
|
|
||
| Assert.IsNotNull(client); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void IfApiKeyIsNotProvidedThenClientIsCreatedWithEmptyKey() | ||
| { | ||
| var client = new SemanticKernelClient(TestApiUrl, TestModel); | ||
|
|
||
| Assert.IsNotNull(client); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task IfEmptyMessageThenReturnsDefaultResponse() | ||
| { | ||
| var client = new SemanticKernelClient(TestApiUrl, TestModel, TestApiKey); | ||
|
|
||
| var response = await client.GetChatResponseAsync(""); | ||
|
|
||
| Assert.AreEqual("You say something?", response); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task IfWhitespaceMessageThenReturnsDefaultResponse() | ||
| { | ||
| var client = new SemanticKernelClient(TestApiUrl, TestModel, TestApiKey); | ||
|
|
||
| var response = await client.GetChatResponseAsync(" "); | ||
|
|
||
| Assert.AreEqual("You say something?", response); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task IfNullMessageThenReturnsDefaultResponse() | ||
| { | ||
| var client = new SemanticKernelClient(TestApiUrl, TestModel, TestApiKey); | ||
|
|
||
| var response = await client.GetChatResponseAsync(null!); | ||
|
|
||
| Assert.AreEqual("You say something?", response); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [ExpectedException(typeof(ArgumentNullException))] | ||
| public void IfApiUrlIsNullThenThrowsArgumentNullException() | ||
| { | ||
| _ = new SemanticKernelClient(null!, TestModel, TestApiKey); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [ExpectedException(typeof(ArgumentNullException))] | ||
| public void IfModelIsNullThenThrowsArgumentNullException() | ||
| { | ||
| _ = new SemanticKernelClient(TestApiUrl, null!, TestApiKey); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [ExpectedException(typeof(UriFormatException))] | ||
| public void IfApiUrlIsInvalidThenThrowsUriFormatException() | ||
| { | ||
| _ = new SemanticKernelClient("not-a-valid-url", TestModel, TestApiKey); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [ExpectedException(typeof(UriFormatException))] | ||
| public void IfApiUrlIsNotHttpOrHttpsThenThrowsUriFormatException() | ||
| { | ||
| _ = new SemanticKernelClient("ftp://localhost:11434", TestModel, TestApiKey); | ||
| } | ||
| } | ||
| } | ||
|
rrusson marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,3 @@ | ||
| using System.Net; | ||
| using System.Net.Sockets; | ||
|
|
||
| using ClippyWeb.Util; | ||
|
|
||
| using Microsoft.Extensions.Configuration; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| namespace ClippyWeb.Util.UnitTests | ||
| { | ||
| /// <summary> | ||
| /// Unit tests for the <see cref="ClippyWeb.Util.TcpClientFactory"/> class. | ||
| /// </summary> | ||
| [TestClass] | ||
| public class TcpClientFactoryTests | ||
| { | ||
| /// <summary> | ||
| /// Tests that the Dispose method calls Dispose(bool) with true parameter. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void CreateWorks() | ||
| { | ||
| // Act | ||
| var result = new TcpClientFactory().Create(); | ||
|
|
||
| // Assert | ||
| Assert.IsNotNull(result); | ||
| Assert.IsInstanceOfType(result, typeof(TcpClientWrapper)); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.