Skip to content
Open
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
78 changes: 78 additions & 0 deletions tests/unit/vertexai/genai/replays/test_skills_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright 2026 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
#
# http://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.
#
"""Tests the skills.create() method against the Vertex AI endpoint using replays."""

import io
import os
import tempfile
import zipfile

from tests.unit.vertexai.genai.replays import pytest_helper
from vertexai._genai import types

# MANDATORY: Initialize the replay test framework for this module
pytestmark = pytest_helper.setup(
file=__file__,
globals_for_file=globals(),
)


def test_create_skill(client):
"""Tests the creation of a skill using `client.skills.create()`."""
# Target the autopush sandbox endpoint for the Skill Registry API
client._api_client._http_options.base_url = (
"https://us-central1-autopush-aiplatform.sandbox.googleapis.com"
)

with tempfile.TemporaryDirectory() as tmpdir:
# Create a dummy skill structure (SKILL.md is required by the spec)
with open(os.path.join(tmpdir, "SKILL.md"), "w") as f:
f.write("# My Replay Skill\nThis is a test skill for replay tests.")

skill = client.skills.create(
display_name="My Replay Skill",
description="My Replay Skill Description",
local_path=tmpdir,
config=types.CreateSkillConfig(wait_for_completion=True),
)

assert skill.name is not None
assert skill.display_name == 'My Replay Skill'
assert skill.description == 'My Replay Skill Description'


def test_create_skill_with_prezipped_bytes(client):
"""Tests the creation of a skill with pre-zipped bytes."""
# Target the autopush sandbox endpoint for the Skill Registry API
client._api_client._http_options.base_url = (
'https://us-central1-autopush-aiplatform.sandbox.googleapis.com'
)

zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
zip_file.writestr('SKILL.md', '# My Zipped Replay Skill\nThis is a test.')
zipped_bytes = zip_buffer.getvalue()

skill = client.skills.create(
display_name='My Zipped Replay Skill',
description='My Zipped Replay Skill Description',
zipped_filesystem=zipped_bytes,
config=types.CreateSkillConfig(wait_for_completion=True),
)

assert skill.name is not None
assert skill.display_name == 'My Zipped Replay Skill'
assert skill.description == 'My Zipped Replay Skill Description'

39 changes: 39 additions & 0 deletions tests/unit/vertexai/genai/replays/test_skills_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Tests the skills.get() method against the autopush endpoint."""

from google.api_core import exceptions
from tests.unit.vertexai.genai.replays import pytest_helper
import pytest

PROJECT_ID = "srbai-testing"
REGION = "us-central1"
# SKILL_ID = "5578834038405201920"
SKILL_ID = "7184367305562783744"
ENDPOINT = f"{REGION}-autopush-aiplatform.sandbox.googleapis.com"

# # Configure HTTP options to target the autopush endpoint
# my_http_options = genai_types.HttpOptions(
# api_version="v1beta1",
# base_url=f"https://{ENDPOINT}/v1beta1/" # <---APPENDED /v1beta1/ here
# )

pytestmark = pytest_helper.setup(
file=__file__,
globals_for_file=globals(),
# http_options=my_http_options,
)


def test_get_skill(client): # client fixture is injected by pytest_helper.setup
"""Tests the skills.get() method against the autopush endpoint."""

client._api_client._http_options.base_url = (
"https://us-central1-autopush-aiplatform.sandbox.googleapis.com"
)
skill_name = f"projects/{PROJECT_ID}/locations/{REGION}/skills/{SKILL_ID}"

try:
skill = client.skills.get(name=skill_name)
assert skill.name == skill_name

except exceptions.GoogleAPIError as e:
pytest.fail(f"Error calling client.skills.get(): {e}")
267 changes: 267 additions & 0 deletions tests/unit/vertexai/genai/test_genai_skills.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
# Copyright 2026 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
#
# http://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.
#
# //third_party/py/google/cloud/aiplatform/tests/unit/vertexai/genai/test_genai_skills.py
import json
from unittest import mock

from vertexai import _genai as genai
from vertexai._genai import client as vertexai_client
from google.genai import types as genai_types
import pytest


@pytest.fixture
def skills_client():
creds = mock.MagicMock()
creds.token = "test_token"
client = vertexai_client.Client(
project="test-project", location="test-location", credentials=creds
)
return client.skills


@pytest.fixture
def async_skills_client():
creds = mock.MagicMock()
creds.token = "test_token"
client = vertexai_client.Client(
project="test-project", location="test-location", credentials=creds
)
return client.aio.skills


class TestGenaiSkills:
mock_get_skill_response = {
"name": "projects/test-project/locations/test-location/skills/test-skill",
"displayName": "My Test Skill",
}

def test_get_skill(self, skills_client):
"""Tests the get_skill method."""
with mock.patch.object(skills_client._api_client, "request") as request_mock:
request_mock.return_value = genai_types.HttpResponse(
body=json.dumps(self.mock_get_skill_response)
)
skill_name = (
"projects/test-project/locations/test-location/skills/test-skill"
)
skill = skills_client.get(name=skill_name)
request_mock.assert_called_with(
"get",
skill_name,
{"_url": {"name": skill_name}},
None,
)
assert isinstance(skill, genai.types.Skill)
assert skill.name == skill_name
assert skill.display_name == "My Test Skill"

def test_create_skill(self, skills_client):
"""Tests the create_skill method with wait_for_completion=True."""
import tempfile
import os

with tempfile.TemporaryDirectory() as tmpdir:
# Create a dummy file in tmpdir
with open(os.path.join(tmpdir, "SKILL.md"), "w") as f:
f.write("# Test Skill")

# Prepare mock responses
pending_op = {
"name": "projects/test-project/locations/test-location/skills/test-skill/operations/op-123",
"done": False,
}
finished_op = {
"name": "projects/test-project/locations/test-location/skills/test-skill/operations/op-123",
"done": True,
"response": {
"name": "projects/test-project/locations/test-location/skills/test-skill",
"displayName": "My Test Skill",
"description": "My Test Skill Description",
},
}

# Final Skill response returned by get call
skill_response = {
"name": "projects/test-project/locations/test-location/skills/test-skill",
"displayName": "My Test Skill",
"description": "My Test Skill Description",
}

with mock.patch.object(
skills_client._api_client, "request"
) as request_mock:
request_mock.side_effect = [
genai_types.HttpResponse(body=json.dumps(pending_op)),
genai_types.HttpResponse(body=json.dumps(finished_op)),
genai_types.HttpResponse(body=json.dumps(skill_response)),
]

# We mock time.sleep to speed up the test
with mock.patch("time.sleep", return_value=None):
skill = skills_client.create(
display_name="My Test Skill",
description="My Test Skill Description",
local_path=tmpdir,
config={"wait_for_completion": True},
)

# Assertions
assert request_mock.call_count == 3

# Verify POST request
post_call = request_mock.call_args_list[0]
assert post_call[0][0] == "post"
assert post_call[0][1] == "skills"

post_body = post_call[0][2]
assert post_body["displayName"] == "My Test Skill"
assert post_body["description"] == "My Test Skill Description"
assert isinstance(post_body["zippedFilesystem"], str)

# Verify GET request (polling)
get_call = request_mock.call_args_list[1]
assert get_call[0][0] == "get"
assert (
get_call[0][1]
== "projects/test-project/locations/test-location/skills/test-skill/operations/op-123"
)

# Verify final GET request to fetch the skill
get_skill_call = request_mock.call_args_list[2]
assert get_skill_call[0][0] == "get"
assert (
get_skill_call[0][1]
== "projects/test-project/locations/test-location/skills/test-skill"
)

# Verify returned skill
assert isinstance(skill, genai.types.Skill)
assert (
skill.name
== "projects/test-project/locations/test-location/skills/test-skill"
)
assert skill.display_name == "My Test Skill"
assert skill.description == "My Test Skill Description"

def test_create_skill_no_wait(self, skills_client):
"""Tests the create_skill method with wait_for_completion=False."""
import tempfile
import os

with tempfile.TemporaryDirectory() as tmpdir:
with open(os.path.join(tmpdir, "SKILL.md"), "w") as f:
f.write("# Test Skill")

pending_op = {
"name": "projects/test-project/locations/test-location/skills/test-skill/operations/op-123",
"done": False,
}

with mock.patch.object(
skills_client._api_client, "request"
) as request_mock:
request_mock.return_value = genai_types.HttpResponse(
body=json.dumps(pending_op)
)

operation = skills_client.create(
display_name="My Test Skill",
description="My Test Skill Description",
local_path=tmpdir,
config={"wait_for_completion": False},
)

# Assertions
assert request_mock.call_count == 1
assert isinstance(operation, genai.types.SkillOperation)
assert (
operation.name
== "projects/test-project/locations/test-location/skills/test-skill/operations/op-123"
)
assert not operation.done

@pytest.mark.asyncio
async def test_create_skill_async(self, async_skills_client):
"""Tests the create_skill method asynchronously with wait_for_completion=True."""
import tempfile
import os

with tempfile.TemporaryDirectory() as tmpdir:
with open(os.path.join(tmpdir, "SKILL.md"), "w") as f:
f.write("# Test Skill")

pending_op = {
"name": "projects/test-project/locations/test-location/skills/test-skill/operations/op-123",
"done": False,
}
finished_op = {
"name": "projects/test-project/locations/test-location/skills/test-skill/operations/op-123",
"done": True,
"response": {
"name": "projects/test-project/locations/test-location/skills/test-skill",
"displayName": "My Test Skill",
"description": "My Test Skill Description",
},
}

# Final Skill response returned by async get call
skill_response = {
"name": "projects/test-project/locations/test-location/skills/test-skill",
"displayName": "My Test Skill",
"description": "My Test Skill Description",
}

with mock.patch.object(
async_skills_client._api_client, "async_request"
) as request_mock:
request_mock.side_effect = [
genai_types.HttpResponse(body=json.dumps(pending_op)),
genai_types.HttpResponse(body=json.dumps(finished_op)),
genai_types.HttpResponse(body=json.dumps(skill_response)),
]

with mock.patch("asyncio.sleep", new_callable=mock.AsyncMock):
skill = await async_skills_client.create(
display_name="My Test Skill",
description="My Test Skill Description",
local_path=tmpdir,
config={"wait_for_completion": True},
)

# Assertions
assert request_mock.call_count == 3

# Verify POST request
post_call = request_mock.call_args_list[0]
assert post_call[0][0] == "post"
assert post_call[0][1] == "skills"

# Verify final GET request to fetch the skill
get_skill_call = request_mock.call_args_list[2]
assert get_skill_call[0][0] == "get"
assert (
get_skill_call[0][1]
== "projects/test-project/locations/test-location/skills/test-skill"
)

# Verify returned skill
assert isinstance(skill, genai.types.Skill)
assert (
skill.name
== "projects/test-project/locations/test-location/skills/test-skill"
)
assert skill.display_name == "My Test Skill"
assert skill.description == "My Test Skill Description"
Loading
Loading