Skip to content

Commit 364522f

Browse files
committed
tests
1 parent a2edc20 commit 364522f

14 files changed

Lines changed: 494 additions & 3 deletions
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Net;
2+
using Moq;
3+
using Moq.Protected;
4+
using Together.Clients;
5+
using Together.Models.Error;
6+
7+
namespace Together.Tests.Clients;
8+
9+
public class BaseClientTests : TestBase
10+
{
11+
12+
private record TestRequest(string Data);
13+
private record TestResponse(string Result);
14+
15+
[Fact]
16+
public async Task SendRequestAsync_ThrowsException_WhenErrorResponse()
17+
{
18+
// Arrange
19+
var response = new HttpResponseMessage
20+
{
21+
StatusCode = HttpStatusCode.BadRequest,
22+
Content = new StringContent(@"{""error"":{""message"":""Test error""}}")
23+
};
24+
var client = new TestClient(CreateMockHttpClient(response));
25+
26+
// Act & Assert
27+
var exception = await Assert.ThrowsAsync<Exception>(() =>
28+
client.TestSendRequest<TestRequest, TestResponse>("/test", new TestRequest("test")));
29+
Assert.Equal("Test error", exception.Message);
30+
}
31+
32+
[Fact]
33+
public async Task SendRequestAsync_ReturnsResponse_WhenSuccessful()
34+
{
35+
// Arrange
36+
var response = new HttpResponseMessage
37+
{
38+
StatusCode = HttpStatusCode.OK,
39+
Content = new StringContent(@"{""result"":""success""}")
40+
};
41+
var client = new TestClient(CreateMockHttpClient(response));
42+
43+
// Act
44+
var result = await client.TestSendRequest<TestRequest, TestResponse>("/test", new TestRequest("test"));
45+
46+
// Assert
47+
Assert.Equal("success", result.Result);
48+
}
49+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Net;
2+
using Moq;
3+
using Moq.Protected;
4+
using Together.Clients;
5+
using Together.Models.Completions;
6+
7+
namespace Together.Tests.Clients;
8+
9+
public class CompletionClientTests : TestBase
10+
{
11+
[Fact]
12+
public async Task CreateAsync_SuccessfulResponse_ReturnsCompletionResponse()
13+
{
14+
// Arrange
15+
var response = new HttpResponseMessage
16+
{
17+
StatusCode = HttpStatusCode.OK,
18+
Content = new StringContent(@"{
19+
""id"": ""test-id"",
20+
""object"": ""text_completion"",
21+
""created"": 1234567890,
22+
""model"": ""test-model"",
23+
""choices"": [{
24+
""text"": ""Test response"",
25+
""index"": 0,
26+
""logprobs"": null,
27+
""finish_reason"": ""stop""
28+
}]
29+
}")
30+
};
31+
32+
var client = new CompletionClient(CreateMockHttpClient(response));
33+
var request = new CompletionRequest
34+
{
35+
Model = "test-model",
36+
Prompt = "Test prompt"
37+
};
38+
39+
// Act
40+
var result = await client.CreateAsync(request);
41+
42+
// Assert
43+
Assert.NotNull(result);
44+
Assert.Equal("test-id", result.Id);
45+
Assert.Equal("Test response", result.Choices[0].Text);
46+
}
47+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
using System.Net;
3+
using Moq;
4+
using Moq.Protected;
5+
using Together.Clients;
6+
using Together.Models.Embeddings;
7+
8+
namespace Together.Tests.Clients;
9+
10+
public class EmbeddingClientTests : TestBase
11+
{
12+
[Fact]
13+
public async Task CreateAsync_SuccessfulResponse_ReturnsEmbeddingResponse()
14+
{
15+
// Arrange
16+
var response = new HttpResponseMessage
17+
{
18+
StatusCode = HttpStatusCode.OK,
19+
Content = new StringContent(@"{
20+
""object"": ""list"",
21+
""data"": [{
22+
""object"": ""embedding"",
23+
""embedding"": [0.1, 0.2, 0.3],
24+
""index"": 0
25+
}],
26+
""model"": ""test-model"",
27+
""usage"": {
28+
""prompt_tokens"": 10,
29+
""total_tokens"": 10
30+
}
31+
}")
32+
};
33+
34+
var client = new EmbeddingClient(CreateMockHttpClient(response));
35+
var request = new EmbeddingRequest
36+
{
37+
Model = "test-model",
38+
Input = "Test input"
39+
};
40+
41+
// Act
42+
var result = await client.CreateAsync(request);
43+
44+
// Assert
45+
Assert.NotNull(result);
46+
Assert.Single(result.Data);
47+
Assert.Equal(3, result.Data[0].Embedding.Count);
48+
Assert.Equal(0.1f, result.Data[0].Embedding[0]);
49+
}
50+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Net;
2+
using Moq;
3+
using Moq.Protected;
4+
using Together.Clients;
5+
using Together.Models.Files;
6+
7+
namespace Together.Tests.Clients;
8+
9+
public class FileClientTests : TestBase
10+
{
11+
private readonly string _testFilePath = Path.GetTempFileName();
12+
13+
public FileClientTests()
14+
{
15+
File.WriteAllText(_testFilePath, "test content");
16+
}
17+
18+
[Fact]
19+
public async Task UploadAsync_SuccessfulResponse_ReturnsFileResponse()
20+
{
21+
// Arrange
22+
var response = new HttpResponseMessage
23+
{
24+
StatusCode = HttpStatusCode.OK,
25+
Content = new StringContent(@"{
26+
""id"": ""file-123"",
27+
""object"": ""file"",
28+
""bytes"": 1234,
29+
""created_at"": 1234567890,
30+
""filename"": ""test.jsonl"",
31+
""purpose"": ""fine-tune""
32+
}")
33+
};
34+
35+
var client = new FileClient(CreateMockHttpClient(response));
36+
37+
// Act
38+
var result = await client.UploadAsync(_testFilePath);
39+
40+
// Assert
41+
Assert.NotNull(result);
42+
Assert.Equal("file-123", result.Id);
43+
Assert.Equal("test.jsonl", result.Filename);
44+
}
45+
46+
[Fact]
47+
public async Task ListAsync_SuccessfulResponse_ReturnsFileList()
48+
{
49+
// Arrange
50+
var response = new HttpResponseMessage
51+
{
52+
StatusCode = HttpStatusCode.OK,
53+
Content = new StringContent(@"{
54+
""data"": [{
55+
""id"": ""file-123"",
56+
""object"": ""file"",
57+
""bytes"": 1234,
58+
""created_at"": 1234567890,
59+
""filename"": ""test.jsonl"",
60+
""purpose"": ""fine-tune""
61+
}],
62+
""object"": ""list""
63+
}")
64+
};
65+
66+
var client = new FileClient(CreateMockHttpClient(response));
67+
68+
// Act
69+
var result = await client.ListAsync();
70+
71+
// Assert
72+
Assert.NotNull(result);
73+
Assert.Single(result.Data);
74+
Assert.Equal("file-123", result.Data[0].Id);
75+
}
76+
77+
public void Dispose()
78+
{
79+
if (File.Exists(_testFilePath))
80+
{
81+
File.Delete(_testFilePath);
82+
}
83+
}
84+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Net;
2+
using Moq;
3+
using Moq.Protected;
4+
using Together.Clients;
5+
using Together.Models.Images;
6+
7+
namespace Together.Tests.Clients;
8+
9+
public class ImageClientTests : TestBase
10+
{
11+
[Fact]
12+
public async Task GenerateAsync_SuccessfulResponse_ReturnsImageResponse()
13+
{
14+
// Arrange
15+
var response = new HttpResponseMessage
16+
{
17+
StatusCode = HttpStatusCode.OK,
18+
Content = new StringContent(@"{
19+
""created"": 1234567890,
20+
""data"": [{
21+
""url"": ""https://example.com/image.png""
22+
}]
23+
}")
24+
};
25+
26+
var client = new ImageClient(CreateMockHttpClient(response));
27+
var request = new ImageRequest
28+
{
29+
Model = "test-model",
30+
Prompt = "Test prompt"
31+
};
32+
33+
// Act
34+
var result = await client.GenerateAsync(request);
35+
36+
// Assert
37+
Assert.NotNull(result);
38+
Assert.Single(result.Data);
39+
Assert.Equal("https://example.com/image.png", result.Data[0].Url);
40+
}
41+
42+
}

0 commit comments

Comments
 (0)