|
| 1 | +using System.Net; |
| 2 | +using System.Text; |
| 3 | +using Microsoft.Extensions.AI; |
| 4 | +using Moq; |
| 5 | +using Moq.Protected; |
| 6 | +using Together.Clients; |
| 7 | +using Together.Models.ChatCompletions; |
| 8 | + |
| 9 | +namespace Together.Tests.Clients; |
| 10 | + |
| 11 | +public class ChatCompletionClientTests : TestBase |
| 12 | +{ |
| 13 | + [Fact] |
| 14 | + public async Task CreateAsync_SuccessfulResponse_ReturnsChatCompletionResponse() |
| 15 | + { |
| 16 | + // Arrange |
| 17 | + var response = new HttpResponseMessage |
| 18 | + { |
| 19 | + StatusCode = HttpStatusCode.OK, |
| 20 | + Content = new StringContent(@"{ |
| 21 | + ""id"": ""test-id"", |
| 22 | + ""object"": ""chat.completion"", |
| 23 | + ""created"": 1234567890, |
| 24 | + ""model"": ""test-model"", |
| 25 | + ""choices"": [{ |
| 26 | + ""index"": 0, |
| 27 | + ""message"": { |
| 28 | + ""role"": ""assistant"", |
| 29 | + ""content"": ""Test response"" |
| 30 | + }, |
| 31 | + ""finish_reason"": ""stop"" |
| 32 | + }] |
| 33 | + }") |
| 34 | + }; |
| 35 | + |
| 36 | + var client = new ChatCompletionClient(CreateMockHttpClient(response)); |
| 37 | + var request = new ChatCompletionRequest |
| 38 | + { |
| 39 | + Model = "test-model", |
| 40 | + Messages = new List<ChatCompletionMessage> |
| 41 | + { |
| 42 | + new() { Role = ChatRole.User, Content = "Test prompt" } |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + // Act |
| 47 | + var result = await client.CreateAsync(request); |
| 48 | + |
| 49 | + // Assert |
| 50 | + Assert.NotNull(result); |
| 51 | + Assert.Equal("test-id", result.Id); |
| 52 | + Assert.Equal("Test response", result.Choices[0].Message.Content); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public async Task CreateStreamAsync_SuccessfulResponse_YieldsChunks() |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + var streamContent = """ |
| 60 | + data: {"id":"1","object":"chat.completion.chunk","choices":[{"delta":{"role":"assistant"},"index":0}]} |
| 61 | + data: {"id":"1","object":"chat.completion.chunk","choices":[{"delta":{"content":"Hello"},"index":0}]} |
| 62 | + data: {"id":"1","object":"chat.completion.chunk","choices":[{"delta":{"content":" world"},"index":0}]} |
| 63 | + data: [DONE] |
| 64 | + """; |
| 65 | + |
| 66 | + var response = new HttpResponseMessage |
| 67 | + { |
| 68 | + StatusCode = HttpStatusCode.OK, |
| 69 | + Content = new StringContent(streamContent) |
| 70 | + }; |
| 71 | + |
| 72 | + var client = new ChatCompletionClient(CreateMockHttpClient(response)); |
| 73 | + var request = new ChatCompletionRequest |
| 74 | + { |
| 75 | + Model = "test-model", |
| 76 | + Messages = new List<ChatCompletionMessage> |
| 77 | + { |
| 78 | + new() { Role = ChatRole.User, Content = "Test prompt" } |
| 79 | + }, |
| 80 | + Stream = true |
| 81 | + }; |
| 82 | + |
| 83 | + // Act |
| 84 | + var chunks = new List<ChatCompletionChunk>(); |
| 85 | + await foreach (var chunk in client.CreateStreamAsync(request)) |
| 86 | + { |
| 87 | + chunks.Add(chunk); |
| 88 | + } |
| 89 | + |
| 90 | + // Assert |
| 91 | + Assert.Equal(3, chunks.Count); |
| 92 | + Assert.Equal("Hello", chunks[1].Choices[0].Delta?.Content); |
| 93 | + Assert.Equal(" world", chunks[2].Choices[0].Delta?.Content); |
| 94 | + } |
| 95 | +} |
0 commit comments