Skip to content

Commit 9a921e3

Browse files
committed
test: add missing fetch_tools tests for feature parity
Add 4 critical tests to match Node.js SDK test coverage: 1. test_fetch_tools_account_id_override: - Verify that account_ids parameter overrides set_accounts() - Ensure state is not modified 2. test_fetch_tools_uses_set_accounts_when_no_override: - Verify that set_accounts() is used when no override provided - Test multiple account IDs via set_accounts() 3. test_fetch_tools_multiple_account_ids: - Test fetching tools for 3+ account IDs - Verify correct number of tools returned 4. test_fetch_tools_preserves_account_context: - Verify tools maintain their account_id context - Critical for correct x-account-id header usage Also fix: Change DEFAULT_HYBRID_ALPHA from int to float type annotation. These tests bring Python SDK to feature parity with Node.js SDK's stackone.mcp-fetch.spec.ts test coverage. All 15 toolset tests passing.
1 parent fdcea15 commit 9a921e3

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

stackone_ai/meta_tools.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ class MetaToolSearchResult(BaseModel):
2727
class ToolIndex:
2828
"""Hybrid BM25 + TF-IDF tool search index"""
2929

30-
def __init__(self, tools: list[StackOneTool], hybrid_alpha: float = 0.2) -> None:
30+
DEFAULT_HYBRID_ALPHA: float = 0.2
31+
32+
def __init__(self, tools: list[StackOneTool], hybrid_alpha: float = DEFAULT_HYBRID_ALPHA) -> None:
3133
"""Initialize tool index with hybrid search
3234
3335
Args:

tests/test_toolset.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,82 @@ def test_fetch_tools_with_set_accounts(mock_tools_setup):
397397

398398
tools = toolset.fetch_tools(providers=["hris"])
399399
assert len(tools) == 2
400+
401+
402+
def test_fetch_tools_account_id_override(mock_tools_setup) -> None:
403+
"""Test that fetch_tools account_ids parameter overrides set_accounts"""
404+
toolset = StackOneToolSet(api_key="test_key")
405+
406+
# Set accounts via set_accounts
407+
toolset.set_accounts(["acc1", "acc2"])
408+
409+
# Override with different account IDs in fetch_tools
410+
# This should use acc3, not acc1/acc2
411+
tools = toolset.fetch_tools(account_ids=["acc3"], providers=["hris"])
412+
413+
# Should fetch tools for acc3 only
414+
# With 2 HRIS tools per account
415+
assert len(tools) == 2
416+
417+
# Verify that set_accounts state is not modified
418+
assert toolset._account_ids == ["acc1", "acc2"]
419+
420+
421+
def test_fetch_tools_uses_set_accounts_when_no_override(mock_tools_setup) -> None:
422+
"""Test that fetch_tools uses set_accounts when account_ids not provided"""
423+
toolset = StackOneToolSet(api_key="test_key")
424+
toolset.set_accounts(["acc1", "acc2"])
425+
426+
# Should use accounts from set_accounts
427+
tools = toolset.fetch_tools(providers=["hris"])
428+
429+
# Should fetch tools for both accounts
430+
# 2 HRIS tools × 2 accounts = 4 tools
431+
assert len(tools) == 4
432+
433+
434+
def test_fetch_tools_multiple_account_ids(mock_tools_setup) -> None:
435+
"""Test fetching tools for multiple account IDs"""
436+
toolset = StackOneToolSet(api_key="test_key")
437+
438+
# Fetch tools for multiple accounts
439+
tools = toolset.fetch_tools(account_ids=["acc1", "acc2", "acc3"])
440+
441+
# Should fetch all tools for all 3 accounts
442+
# (4 regular tools + 1 feedback tool) × 3 accounts = 15 tools
443+
assert len(tools) == 15
444+
445+
446+
def test_fetch_tools_preserves_account_context() -> None:
447+
"""Test that tools fetched with account_id maintain their account context"""
448+
with (
449+
patch("stackone_ai.toolset.OAS_DIR") as mock_dir,
450+
patch("stackone_ai.toolset.OpenAPIParser") as mock_parser_class,
451+
):
452+
# Create a simple tool definition
453+
tool_def = ToolDefinition(
454+
description="Test tool",
455+
parameters=ToolParameters(type="object", properties={}),
456+
execute=ExecuteConfig(
457+
method="GET",
458+
url="https://api.stackone.com/test",
459+
name="test_tool",
460+
headers={},
461+
),
462+
)
463+
464+
mock_path = MagicMock()
465+
mock_path.exists.return_value = True
466+
mock_dir.glob.return_value = [mock_path]
467+
468+
mock_parser = MagicMock()
469+
mock_parser.parse_tools.return_value = {"test_tool": tool_def}
470+
mock_parser_class.return_value = mock_parser
471+
472+
toolset = StackOneToolSet(api_key="test_key")
473+
tools = toolset.fetch_tools(account_ids=["specific-account"])
474+
475+
# Get a tool and verify it has the account ID
476+
tool = tools.get_tool("test_tool")
477+
assert tool is not None
478+
assert tool.get_account_id() == "specific-account"

0 commit comments

Comments
 (0)