Files
Artemis-Eapil/tests/unit/tools/test_launch_app_tool.py
T
somew1nd a45daf23f8 feat: add agentic video analysis and clarify action results
Report device actions as dispatched and preserve full failure details. Route
Flash through McpActionExecutor and use ToolFailure across helper tools and
agent loops instead of inferring failure from response text.

Require model-provided descriptions for coordinate targets. Keep observed
element metadata separate from those descriptions in safety checks, visual
summaries, history, and frontend labels.

Add Gemini Interactions API video analysis with configurable processing mode,
chunk size, and timeouts. Centralize Google provider capabilities and token
usage normalization, and update google-genai to 2.22.0.

Preserve system instructions on follow-up interactions, replan pending chunks
when falling back to static processing, and reject unanswered native video
and audio segments instead of caching placeholder results. Keep helper-tool
failure status intact through history tools and committee calls.

Validation: 1,762 Python unit tests passed (3 skipped), 40 frontend formatter
tests passed, and the production UI build completed. Ruff lint, formatting,
the quality ratchet, and git diff checks passed.
2026-09-08 17:05:54 -07:00

196 lines
6.6 KiB
Python

# 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.
from unittest.mock import AsyncMock, MagicMock, patch
from artemis.context import ArtemisContext
from artemis.drivers.base import BaseDeviceDriver
from artemis.graph.state import State
from artemis.tools.base import ArtemisTool
from artemis.tools.mobile.launch_app import (
LaunchApp,
LaunchAppArgs,
LaunchAppTool,
get_launch_app_tool,
launch_app,
launch_app_wrapper,
)
from langchain_core.messages import ToolMessage
import pytest
@pytest.fixture
def mock_ctx():
ctx = MagicMock(spec=ArtemisContext)
ctx.package_cache = {}
return ctx
@pytest.fixture
def mock_driver():
driver = MagicMock(spec=BaseDeviceDriver)
driver.launch_app = AsyncMock(return_value=True)
return driver
@pytest.fixture
def mock_state():
return MagicMock(spec=State)
def test_launch_app_tool_subclass():
"""Verify LaunchAppTool is a subclass of ArtemisTool."""
assert issubclass(LaunchAppTool, ArtemisTool)
assert issubclass(LaunchApp, ArtemisTool)
assert isinstance(launch_app, ArtemisTool)
assert isinstance(launch_app, LaunchAppTool)
assert launch_app.name == "launch_app"
assert launch_app.category == "action"
assert launch_app.args_schema == LaunchAppArgs
# GenAI FunctionDeclaration export
declaration = launch_app.to_genai_declaration()
assert declaration.name == "launch_app"
assert "app_name" in declaration.parameters.properties
# Wrapper check
assert launch_app_wrapper is not None
assert launch_app_wrapper.tool_fn_getter == get_launch_app_tool
@pytest.mark.asyncio
async def test_launch_app_direct_execution_with_driver(mock_driver):
"""Verify direct execution with BaseDeviceDriver."""
result = await launch_app.execute(driver=mock_driver, app_name="com.android.settings")
mock_driver.launch_app.assert_called_once_with("com.android.settings")
assert result == "Launched app 'com.android.settings'."
@pytest.mark.asyncio
async def test_launch_app_direct_execution_with_ctx(mock_ctx):
"""Verify direct execution with ArtemisContext."""
with (
patch(
"artemis.tools.mobile.launch_app.find_package",
new_callable=AsyncMock,
return_value="com.example.myapp",
) as mock_find,
patch(
"artemis.tools.mobile.launch_app.launch_app_with_retries",
new_callable=AsyncMock,
return_value=(True, None),
) as mock_launch,
):
result = await launch_app.execute(ctx=mock_ctx, app_name="My App")
mock_find.assert_called_once_with(ctx=mock_ctx, app_name="My App")
mock_launch.assert_called_once_with(ctx=mock_ctx, app_package="com.example.myapp")
assert result == "Launched app 'My App' (com.example.myapp); foreground confirmed."
@pytest.mark.asyncio
async def test_launch_app_package_not_found(mock_ctx):
"""Verify outcome when package is not found."""
with patch(
"artemis.tools.mobile.launch_app.find_package",
new_callable=AsyncMock,
return_value=None,
):
result = await launch_app.execute(ctx=mock_ctx, app_name="Nonexistent App")
assert "Failed to launch app 'Nonexistent App': Package not found." in result
@pytest.mark.asyncio
async def test_launch_app_execution_failure(mock_ctx):
"""Verify outcome when launch_app_with_retries fails."""
with (
patch(
"artemis.tools.mobile.launch_app.find_package",
new_callable=AsyncMock,
return_value="com.example.failapp",
),
patch(
"artemis.tools.mobile.launch_app.launch_app_with_retries",
new_callable=AsyncMock,
return_value=(False, "App crashed on startup"),
),
):
result = await launch_app.execute(ctx=mock_ctx, app_name="Crash App")
assert "Failed to launch app 'Crash App': App crashed on startup" in result
@pytest.mark.asyncio
async def test_launch_app_with_state_command(mock_ctx, mock_state):
"""Verify LaunchAppTool returns ToolMessage when state is provided."""
with (
patch(
"artemis.tools.mobile.launch_app.find_package",
new_callable=AsyncMock,
return_value="com.example.stateapp",
),
patch(
"artemis.tools.mobile.launch_app.launch_app_with_retries",
new_callable=AsyncMock,
return_value=(True, None),
),
):
cmd = await launch_app.execute(
ctx=mock_ctx,
app_name="State App",
tool_call_id="call_la_1",
state=mock_state,
)
assert isinstance(cmd, ToolMessage)
assert cmd.tool_call_id == "call_la_1"
assert cmd.status == "success"
assert (
cmd.content == "Launched app 'State App' (com.example.stateapp); foreground confirmed."
)
@pytest.mark.asyncio
async def test_launch_app_callable_execution(mock_driver):
"""Verify invoking launch_app directly as a callable."""
result = await launch_app(driver=mock_driver, app_name="com.android.chrome")
assert result == "Launched app 'com.android.chrome'."
@pytest.mark.asyncio
async def test_launch_app_missing_name(mock_ctx):
"""Verify error handling when app_name is empty."""
result = await launch_app.execute(ctx=mock_ctx, app_name="")
assert "Error during launch app: app_name parameter is required." in result
@pytest.mark.asyncio
async def test_get_launch_app_tool_langchain_ainvoke(mock_ctx):
"""Verify get_launch_app_tool exports a LangChain tool that works with ainvoke."""
la_tool = get_launch_app_tool(mock_ctx)
assert la_tool.name == "launch_app"
with (
patch(
"artemis.tools.mobile.launch_app.find_package",
new_callable=AsyncMock,
return_value="com.example.ainvoke",
),
patch(
"artemis.tools.mobile.launch_app.launch_app_with_retries",
new_callable=AsyncMock,
return_value=(True, None),
),
):
result = await la_tool.ainvoke({"app_name": "Invoke App"})
assert result == "Launched app 'Invoke App' (com.example.ainvoke); foreground confirmed."