Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 221 additions & 0 deletions Google.GenAI.E2E.Tests/CountTokens/CountTokensTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Google.GenAI;
using Google.GenAI.Types;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using TestServerSdk;

[TestClass]
public class CountTokensTest {
private static TestServerProcess? _server;
private Client vertexClient;
private Client geminiClient;
private string modelName;
public TestContext TestContext { get; set; }

[ClassInitialize]
public static void ClassInit(TestContext _) {
_server = TestServer.StartTestServer();
}

[ClassCleanup]
public static void ClassCleanup() {
TestServer.StopTestServer(_server);
}

[TestInitialize]
public void TestInit() {
// Test server specific setup.
if (_server == null) {
throw new InvalidOperationException("Test server is not initialized.");
}
var geminiClientHttpOptions = new HttpOptions {
Headers = new Dictionary<string, string> { { "Test-Name",
$"{GetType().Name}.{TestContext.TestName}" } },
BaseUrl = "http://localhost:1453"
};
var vertexClientHttpOptions = new HttpOptions {
Headers = new Dictionary<string, string> { { "Test-Name",
$"{GetType().Name}.{TestContext.TestName}" } },
BaseUrl = "http://localhost:1454"
};

// Common setup for both clients.
string project = System.Environment.GetEnvironmentVariable("GOOGLE_CLOUD_PROJECT");

Check warning on line 65 in Google.GenAI.E2E.Tests/CountTokens/CountTokensTest.cs

View workflow job for this annotation

GitHub Actions / unit-e2e-tests

Converting null literal or possible null value to non-nullable type.

Check warning on line 65 in Google.GenAI.E2E.Tests/CountTokens/CountTokensTest.cs

View workflow job for this annotation

GitHub Actions / unit-e2e-tests

Converting null literal or possible null value to non-nullable type.
string location =
System.Environment.GetEnvironmentVariable("GOOGLE_CLOUD_LOCATION") ?? "us-central1";
string apiKey = System.Environment.GetEnvironmentVariable("GOOGLE_API_KEY");

Check warning on line 68 in Google.GenAI.E2E.Tests/CountTokens/CountTokensTest.cs

View workflow job for this annotation

GitHub Actions / unit-e2e-tests

Converting null literal or possible null value to non-nullable type.

Check warning on line 68 in Google.GenAI.E2E.Tests/CountTokens/CountTokensTest.cs

View workflow job for this annotation

GitHub Actions / unit-e2e-tests

Converting null literal or possible null value to non-nullable type.
vertexClient = new Client(project: project, location: location, vertexAI: true,
credential: TestServer.GetCredentialForTestMode(),
httpOptions: vertexClientHttpOptions);
geminiClient =
new Client(apiKey: apiKey, vertexAI: false, httpOptions: geminiClientHttpOptions);

// Specific setup for this test class
modelName = "gemini-2.5-flash";
}

[TestMethod]
public async Task CountTokensVertexTest() {
var contents = new List<Content>
{
new Content
{
Role = "user",
Parts = new List<Part>
{
new Part
{
Text = "What is the capital of France?"
}
}
}
};
var vertexResponse = await vertexClient.Models.CountTokensAsync(
model: modelName, contents: contents, config: null);

Check warning on line 96 in Google.GenAI.E2E.Tests/CountTokens/CountTokensTest.cs

View workflow job for this annotation

GitHub Actions / unit-e2e-tests

Cannot convert null literal to non-nullable reference type.

Check warning on line 96 in Google.GenAI.E2E.Tests/CountTokens/CountTokensTest.cs

View workflow job for this annotation

GitHub Actions / unit-e2e-tests

Cannot convert null literal to non-nullable reference type.

Assert.IsNotNull(vertexResponse.TotalTokens);
}

[TestMethod]
public async Task CountTokensGeminiTest() {
var contents = new List<Content>
{
new Content
{
Role = "user",
Parts = new List<Part>
{
new Part
{
Text = "What is the capital of France?"
}
}
}
};
var geminiResponse = await geminiClient.Models.CountTokensAsync(
model: modelName, contents: contents, config: null);

Assert.IsNotNull(geminiResponse.TotalTokens);
}

[TestMethod]
public async Task CountTokensContentVertexTest() {
var contents = new Content
{
Role = "user",
Parts = new List<Part>
{
new Part
{
Text = "What is the capital of France?"
}
}
};
var vertexResponse = await vertexClient.Models.CountTokensAsync(
model: modelName, contents: contents);

Assert.IsNotNull(vertexResponse.TotalTokens);
}

[TestMethod]
public async Task CountTokensContentGeminiTest() {
var contents = new Content
{
Role = "user",
Parts = new List<Part>
{
new Part
{
Text = "What is the capital of France?"
}
}
};
var geminiResponse = await geminiClient.Models.CountTokensAsync(
model: modelName, contents: contents);

Assert.IsNotNull(geminiResponse.TotalTokens);
}

[TestMethod]
public async Task CountTokensStringVertexTest() {
var vertexResponse = await vertexClient.Models.CountTokensAsync(
model: modelName, contents: "What is the capital of France?");

Assert.IsNotNull(vertexResponse.TotalTokens);
}

[TestMethod]
public async Task CountTokensStringGeminiTest() {
var geminiResponse = await geminiClient.Models.CountTokensAsync(
model: modelName, contents: "What is the capital of France?");

Assert.IsNotNull(geminiResponse.TotalTokens);
}

[TestMethod]
public async Task CountTokensConfigVertexTest() {
var config = new CountTokensConfig
{
SystemInstruction = new Content
{
Parts = new List<Part>
{
new Part
{
Text = "You are a helpful assistant."
}
}
}
};
var vertexResponse = await vertexClient.Models.CountTokensAsync(
model: modelName, contents: "What is the capital of France?", config: config);

Assert.IsNotNull(vertexResponse.TotalTokens);
}

[TestMethod]
public async Task CountTokensConfigGeminiTest() {
var config = new CountTokensConfig
{
SystemInstruction = new Content
{
Parts = new List<Part>
{
new Part
{
Text = "You are a helpful assistant."
}
}
}
};
var ex = await Assert.ThrowsExceptionAsync<NotSupportedException>(async () => {
await geminiClient.Models.CountTokensAsync(
model: modelName, contents: "What is the capital of France?", config: config);
});

StringAssert.Contains(ex.Message, "systemInstruction");
StringAssert.Contains(ex.Message, "not supported in Gemini API");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"recordID": "CountTokensTest.CountTokensConfigVertexTest",
"interactions": [
{
"request": {
"method": "POST",
"url": "/v1beta1/projects/REDACTED/locations/us-central1/publishers/google/models/gemini-2.5-flash:countTokens",
"request": "POST /v1beta1/projects/REDACTED/locations/us-central1/publishers/google/models/gemini-2.5-flash:countTokens HTTP/1.1",
"headers": {
"Content-Length": "154",
"Content-Type": "application/json; charset=utf-8",
"Test-Name": "CountTokensTest.CountTokensConfigVertexTest"
},
"bodySegments": [
{
"contents": [
{
"parts": [
{
"text": "What is the capital of France?"
}
],
"role": "user"
}
],
"systemInstruction": {
"parts": [
{
"text": "You are a helpful assistant."
}
]
}
}
],
"previousRequest": "b4d6e60a9b97e7b98c63df9308728c5c88c0b40c398046772c63447b94608b4d",
"serverAddress": "us-central1-aiplatform.googleapis.com",
"port": 443,
"protocol": "https"
},
"shaSum": "fd721119f6ac02e8f92a3bef3156934ed0f425917c6ea5e8af48cd847269c354",
"response": {
"statusCode": 200,
"headers": {
"Content-Type": "application/json; charset=UTF-8",
"Date": "Tue, 18 Nov 2025 04:52:19 GMT",
"Server": "scaffolding on HTTPServer2",
"Vary": "Origin, X-Origin, Referer",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "SAMEORIGIN",
"X-Xss-Protection": "0"
},
"bodySegments": [
{
"promptTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 13
}
],
"totalBillableCharacters": 49,
"totalTokens": 13
}
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"recordID": "CountTokensTest.CountTokensContentGeminiTest",
"interactions": [
{
"request": {
"method": "POST",
"url": "/v1beta/models/gemini-2.5-flash:countTokens",
"request": "POST /v1beta/models/gemini-2.5-flash:countTokens HTTP/1.1",
"headers": {
"Content-Length": "82",
"Content-Type": "application/json; charset=utf-8",
"Test-Name": "CountTokensTest.CountTokensContentGeminiTest"
},
"bodySegments": [
{
"contents": [
{
"parts": [
{
"text": "What is the capital of France?"
}
],
"role": "user"
}
]
}
],
"previousRequest": "b4d6e60a9b97e7b98c63df9308728c5c88c0b40c398046772c63447b94608b4d",
"serverAddress": "generativelanguage.googleapis.com",
"port": 443,
"protocol": "https"
},
"shaSum": "6f82a5a817d114c72184e8c84277173b4ba2ed15d23acfe6f3f2f58278f81f6f",
"response": {
"statusCode": 200,
"headers": {
"Content-Type": "application/json; charset=UTF-8",
"Date": "Tue, 18 Nov 2025 04:27:12 GMT",
"Server": "scaffolding on HTTPServer2",
"Server-Timing": "gfet4t7; dur=53",
"Vary": "Origin, X-Origin, Referer",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "SAMEORIGIN",
"X-Xss-Protection": "0"
},
"bodySegments": [
{
"promptTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 8
}
],
"totalTokens": 8
}
]
}
}
]
}
Loading
Loading