Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KSemenenko committed Jan 5, 2025
1 parent a2edc20 commit 364522f
Show file tree
Hide file tree
Showing 14 changed files with 494 additions and 3 deletions.
49 changes: 49 additions & 0 deletions Together.Tests/Clients/BaseClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Net;
using Moq;
using Moq.Protected;
using Together.Clients;
using Together.Models.Error;

namespace Together.Tests.Clients;

public class BaseClientTests : TestBase
{

private record TestRequest(string Data);
private record TestResponse(string Result);

[Fact]
public async Task SendRequestAsync_ThrowsException_WhenErrorResponse()
{
// Arrange
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.BadRequest,
Content = new StringContent(@"{""error"":{""message"":""Test error""}}")
};
var client = new TestClient(CreateMockHttpClient(response));

// Act & Assert
var exception = await Assert.ThrowsAsync<Exception>(() =>
client.TestSendRequest<TestRequest, TestResponse>("/test", new TestRequest("test")));
Assert.Equal("Test error", exception.Message);
}

[Fact]
public async Task SendRequestAsync_ReturnsResponse_WhenSuccessful()
{
// Arrange
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(@"{""result"":""success""}")
};
var client = new TestClient(CreateMockHttpClient(response));

// Act
var result = await client.TestSendRequest<TestRequest, TestResponse>("/test", new TestRequest("test"));

// Assert
Assert.Equal("success", result.Result);
}
}
95 changes: 95 additions & 0 deletions Together.Tests/Clients/ChatCompletionClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.Net;
using System.Text;
using Microsoft.Extensions.AI;
using Moq;
using Moq.Protected;
using Together.Clients;
using Together.Models.ChatCompletions;

namespace Together.Tests.Clients;

public class ChatCompletionClientTests : TestBase
{
[Fact]
public async Task CreateAsync_SuccessfulResponse_ReturnsChatCompletionResponse()
{
// Arrange
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(@"{
""id"": ""test-id"",
""object"": ""chat.completion"",
""created"": 1234567890,
""model"": ""test-model"",
""choices"": [{
""index"": 0,
""message"": {
""role"": ""assistant"",
""content"": ""Test response""
},
""finish_reason"": ""stop""
}]
}")
};

var client = new ChatCompletionClient(CreateMockHttpClient(response));
var request = new ChatCompletionRequest
{
Model = "test-model",
Messages = new List<ChatCompletionMessage>
{
new() { Role = ChatRole.User, Content = "Test prompt" }
}
};

// Act
var result = await client.CreateAsync(request);

// Assert
Assert.NotNull(result);
Assert.Equal("test-id", result.Id);
Assert.Equal("Test response", result.Choices[0].Message.Content);
}

[Fact]
public async Task CreateStreamAsync_SuccessfulResponse_YieldsChunks()
{
// Arrange
var streamContent = """
data: {"id":"1","object":"chat.completion.chunk","choices":[{"delta":{"role":"assistant"},"index":0}]}
data: {"id":"1","object":"chat.completion.chunk","choices":[{"delta":{"content":"Hello"},"index":0}]}
data: {"id":"1","object":"chat.completion.chunk","choices":[{"delta":{"content":" world"},"index":0}]}
data: [DONE]
""";

var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(streamContent)
};

var client = new ChatCompletionClient(CreateMockHttpClient(response));
var request = new ChatCompletionRequest
{
Model = "test-model",
Messages = new List<ChatCompletionMessage>
{
new() { Role = ChatRole.User, Content = "Test prompt" }
},
Stream = true
};

// Act
var chunks = new List<ChatCompletionChunk>();
await foreach (var chunk in client.CreateStreamAsync(request))
{
chunks.Add(chunk);
}

// Assert
Assert.Equal(3, chunks.Count);
Assert.Equal("Hello", chunks[1].Choices[0].Delta?.Content);
Assert.Equal(" world", chunks[2].Choices[0].Delta?.Content);
}
}
47 changes: 47 additions & 0 deletions Together.Tests/Clients/CompletionClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Net;
using Moq;
using Moq.Protected;
using Together.Clients;
using Together.Models.Completions;

namespace Together.Tests.Clients;

public class CompletionClientTests : TestBase
{
[Fact]
public async Task CreateAsync_SuccessfulResponse_ReturnsCompletionResponse()
{
// Arrange
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(@"{
""id"": ""test-id"",
""object"": ""text_completion"",
""created"": 1234567890,
""model"": ""test-model"",
""choices"": [{
""text"": ""Test response"",
""index"": 0,
""logprobs"": null,
""finish_reason"": ""stop""
}]
}")
};

var client = new CompletionClient(CreateMockHttpClient(response));
var request = new CompletionRequest
{
Model = "test-model",
Prompt = "Test prompt"
};

// Act
var result = await client.CreateAsync(request);

// Assert
Assert.NotNull(result);
Assert.Equal("test-id", result.Id);
Assert.Equal("Test response", result.Choices[0].Text);
}
}
50 changes: 50 additions & 0 deletions Together.Tests/Clients/EmbeddingClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

using System.Net;
using Moq;
using Moq.Protected;
using Together.Clients;
using Together.Models.Embeddings;

namespace Together.Tests.Clients;

public class EmbeddingClientTests : TestBase
{
[Fact]
public async Task CreateAsync_SuccessfulResponse_ReturnsEmbeddingResponse()
{
// Arrange
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(@"{
""object"": ""list"",
""data"": [{
""object"": ""embedding"",
""embedding"": [0.1, 0.2, 0.3],
""index"": 0
}],
""model"": ""test-model"",
""usage"": {
""prompt_tokens"": 10,
""total_tokens"": 10
}
}")
};

var client = new EmbeddingClient(CreateMockHttpClient(response));
var request = new EmbeddingRequest
{
Model = "test-model",
Input = "Test input"
};

// Act
var result = await client.CreateAsync(request);

// Assert
Assert.NotNull(result);
Assert.Single(result.Data);
Assert.Equal(3, result.Data[0].Embedding.Count);
Assert.Equal(0.1f, result.Data[0].Embedding[0]);
}
}
84 changes: 84 additions & 0 deletions Together.Tests/Clients/FileClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Net;
using Moq;
using Moq.Protected;
using Together.Clients;
using Together.Models.Files;

namespace Together.Tests.Clients;

public class FileClientTests : TestBase
{
private readonly string _testFilePath = Path.GetTempFileName();

public FileClientTests()
{
File.WriteAllText(_testFilePath, "test content");
}

[Fact]
public async Task UploadAsync_SuccessfulResponse_ReturnsFileResponse()
{
// Arrange
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(@"{
""id"": ""file-123"",
""object"": ""file"",
""bytes"": 1234,
""created_at"": 1234567890,
""filename"": ""test.jsonl"",
""purpose"": ""fine-tune""
}")
};

var client = new FileClient(CreateMockHttpClient(response));

// Act
var result = await client.UploadAsync(_testFilePath);

// Assert
Assert.NotNull(result);
Assert.Equal("file-123", result.Id);
Assert.Equal("test.jsonl", result.Filename);
}

[Fact]
public async Task ListAsync_SuccessfulResponse_ReturnsFileList()
{
// Arrange
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(@"{
""data"": [{
""id"": ""file-123"",
""object"": ""file"",
""bytes"": 1234,
""created_at"": 1234567890,
""filename"": ""test.jsonl"",
""purpose"": ""fine-tune""
}],
""object"": ""list""
}")
};

var client = new FileClient(CreateMockHttpClient(response));

// Act
var result = await client.ListAsync();

// Assert
Assert.NotNull(result);
Assert.Single(result.Data);
Assert.Equal("file-123", result.Data[0].Id);
}

public void Dispose()
{
if (File.Exists(_testFilePath))
{
File.Delete(_testFilePath);
}
}
}
42 changes: 42 additions & 0 deletions Together.Tests/Clients/ImageClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Net;
using Moq;
using Moq.Protected;
using Together.Clients;
using Together.Models.Images;

namespace Together.Tests.Clients;

public class ImageClientTests : TestBase
{
[Fact]
public async Task GenerateAsync_SuccessfulResponse_ReturnsImageResponse()
{
// Arrange
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(@"{
""created"": 1234567890,
""data"": [{
""url"": ""https://example.com/image.png""
}]
}")
};

var client = new ImageClient(CreateMockHttpClient(response));
var request = new ImageRequest
{
Model = "test-model",
Prompt = "Test prompt"
};

// Act
var result = await client.GenerateAsync(request);

// Assert
Assert.NotNull(result);
Assert.Single(result.Data);
Assert.Equal("https://example.com/image.png", result.Data[0].Url);
}

}
Loading

0 comments on commit 364522f

Please sign in to comment.