arc/hesabixAPI/tests/test_ai_mcp_remaining_coverage.py
Babak Alizadeh 4371de7aa9
Some checks failed
Flutter Windows / verify-windows (push) Has been cancelled
feat(ai): register remaining business-panel JSON as MCP tools
Complete Chat/MCP coverage for tenant JSON APIs with Swagger pointers, catalog completeness, and the in-app MCP connections page.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-06 18:17:35 +03:30

572 lines
20 KiB
Python

"""Coverage for newly registered business MCP tools (tenant pin, schemas, manifest)."""
from __future__ import annotations
from unittest.mock import MagicMock, patch
from app.services.ai.ai_function_extensions_mcp_connectors import get_woocommerce_settings_handler
from app.services.ai.ai_function_extensions_mcp_digipay import UPDATE_SETTINGS_SCHEMA, get_digipay_settings_handler
from app.services.ai.ai_function_extensions_mcp_pay_page import check_pay_page_username_handler
from app.services.ai.ai_function_extensions_mcp_period_fx import preview_period_end_fx_revaluation_handler
from app.services.ai.ai_function_extensions_mcp_zohal import EXECUTE_SCHEMA, list_zohal_services_handler
from app.services.ai.ai_tool_catalog_quality import parse_registered_tool_fields
from app.services.ai.ai_tool_manifest import get_manifest_entry
from app.services.ai.ai_write_guard import is_write_function
NEW_TOOLS = (
"list_zohal_services",
"execute_zohal_inquiry",
"list_zohal_inquiry_logs",
"get_zohal_inquiry_log",
"get_digipay_settings",
"update_digipay_settings",
"test_digipay_connection",
"create_digipay_ticket",
"list_digipay_purchases",
"get_digipay_purchase",
"record_digipay_manual_purchase",
"deliver_digipay_purchase",
"refund_digipay_purchase",
"preview_digipay_settlement",
"register_digipay_settlement",
"get_pay_page_settings",
"check_pay_page_username",
"update_pay_page_settings",
"list_pay_page_payments",
"preview_period_end_fx_revaluation",
"create_period_end_fx_revaluation",
"list_product_variant_dimensions",
"replace_product_variant_dimensions",
"list_product_variants",
"list_product_variant_balances",
"create_product_variant",
"update_product_variant",
"deactivate_product_variant",
"enable_product_inventory_variants",
"resolve_product_barcode",
"list_storage_files",
"get_storage_file",
"rename_storage_file",
"delete_storage_file",
"create_storage_file_share",
"list_storage_file_shares",
"list_storage_shares",
"revoke_storage_file_share",
"get_storage_usage",
"list_storage_plans",
"list_storage_subscriptions",
"subscribe_storage_plan",
"cancel_storage_subscription",
"list_storage_invoices",
"pay_storage_invoice",
"get_crm_web_chat_settings",
"update_crm_web_chat_settings",
"list_crm_chat_widgets",
"create_crm_chat_widget",
"update_crm_chat_widget",
"list_crm_chat_conversations",
"list_crm_chat_messages",
"send_crm_chat_agent_message",
"update_crm_chat_agent_message",
"update_crm_chat_conversation",
"mark_crm_chat_read",
"delete_crm_chat_conversation",
"delete_crm_chat_conversations_bulk",
"get_document_monetization_overview",
"list_document_monetization_plans",
"activate_document_monetization_plan",
"list_document_monetization_charges",
"pay_document_monetization_charge",
"process_document_monetization_document",
"evaluate_document_monetization_amount",
"get_woocommerce_settings",
"update_woocommerce_settings",
"test_woocommerce_bridge",
"get_woocommerce_sync_stats",
"sync_woocommerce_orders",
"sync_woocommerce_products",
"sync_woocommerce_customers",
"sync_woocommerce_product",
"get_woocommerce_stock_conflicts",
"get_basalam_settings",
"update_basalam_settings",
"sync_basalam_orders",
"sync_basalam_products",
"publish_basalam_products",
"pull_basalam_products",
"push_basalam_products_incremental",
"resolve_basalam_product_conflicts",
"create_wallet_top_up",
"invite_business_user",
"list_hscript_reports",
"get_hscript_report",
"create_hscript_report",
"update_hscript_report",
"publish_hscript_report",
"delete_hscript_report",
"create_report_template",
"update_report_template",
"delete_report_template",
"get_fx_auto_sync_settings",
"update_fx_auto_sync_settings",
"preview_fx_auto_sync",
"run_fx_auto_sync",
"update_tax_identity_settings",
"purchase_marketplace_plugin",
"start_marketplace_plugin_trial",
"list_workflow_marketplace_packages",
"install_workflow_marketplace_package",
"publish_workflow_to_marketplace",
"unpublish_workflow_marketplace_package",
"list_business_backups",
"create_business_backup",
"delete_business_backup",
"get_ftp_backup_settings",
"delete_ftp_backup_settings",
"test_ftp_backup_saved",
"scan_ftp_backup_usage",
"list_tax_units",
"list_tax_types",
"search_tax_product_codes",
"get_tax_product_code",
"get_business_user",
"update_business_user_permissions",
"remove_business_user",
"list_telegram_connected_users",
"list_bale_connected_users",
"get_background_job",
"preview_product_lot_dates",
"reserve_document_code",
"cancel_document_code_reservation",
"get_business_print_settings",
"update_business_print_settings",
"get_invoice_share_payment_settings",
"update_invoice_share_payment_settings",
"get_ai_subscription",
"list_ai_plans",
"subscribe_ai_plan",
"set_ai_preferred_model",
"get_ai_usage_stats",
"get_tax_provider",
"select_tax_provider",
"sync_motamed_asa_account",
"get_motamed_asa_invoice_payments",
"register_motamed_asa_invoice_payments",
"sync_fanava_account",
"list_fanava_invoices",
"get_fanava_invoice",
"check_fanava_invoice_status",
"inquiry_fanava_invoice_status",
"test_tax_connection_stored",
"list_ai_skill_marketplace",
"get_ai_skill_package",
"list_installed_ai_skills",
"list_owned_ai_skills",
"list_anthropic_skill_catalog",
"install_ai_skill",
"install_anthropic_ai_skill",
"create_native_ai_skill",
"import_ai_skill_from_git",
"purchase_ai_skill",
"set_ai_skills_enabled",
"publish_ai_skill",
"get_ai_skill_publisher_revenue",
"list_mcp_connections",
"get_mcp_connection_catalog",
"update_mcp_connection",
"revoke_mcp_connection",
"revoke_all_mcp_connections",
"list_crm_process_definitions",
"get_crm_process_definition",
"create_crm_process_definition",
"update_crm_process_definition",
"delete_crm_process_definition",
"cancel_ai_subscription",
"list_ai_invoices",
"pay_ai_invoice",
"get_woocommerce_opening_inventory_status",
"get_woocommerce_opening_inventory_accounts",
"preview_woocommerce_opening_inventory",
"prepare_woocommerce_opening_inventory",
"batch_woocommerce_opening_inventory",
"finalize_woocommerce_opening_inventory",
"cancel_woocommerce_opening_inventory",
"get_basalam_currency_readiness",
"retry_basalam_product_publish",
"clear_basalam_product_conflicts",
"sync_basalam_unverified_payments",
"clear_basalam_dead_letter",
"sync_basalam_inbound_chats",
"reply_basalam_chat",
"get_person_share_link",
"create_person_share_link",
"revoke_person_share_link",
"get_data_table_column_settings",
"save_data_table_column_settings",
"delete_data_table_column_settings",
"get_wallet_settings",
"update_wallet_settings",
"list_wallet_payouts",
"create_wallet_payout",
"approve_wallet_payout",
"cancel_wallet_payout",
"run_wallet_auto_settle",
"get_hscript_plan",
"get_hscript_catalog",
"list_hscript_recipes",
"infer_hscript_param_schema",
"archive_hscript_report",
"list_hscript_report_versions",
"get_hscript_report_version",
"restore_hscript_report_version",
"list_hscript_report_runs",
"run_hscript_report",
"list_hscript_schedules",
"create_hscript_schedule",
"update_hscript_schedule",
"delete_hscript_schedule",
"run_hscript_schedule_now",
"list_marketplace_orders",
"list_marketplace_invoices",
"get_marketplace_plugin_status",
"estimate_sms_notification_cost",
"cancel_background_job",
"get_system_branding",
"recalculate_invoice_profits",
"list_woocommerce_customers",
"get_woocommerce_sales_summary",
"list_woocommerce_control_logs",
"get_woocommerce_connection_status",
"get_woocommerce_plugin_info",
"get_woocommerce_queue_snapshot",
"process_woocommerce_queue_once",
"check_woocommerce_plugin_update",
"run_woocommerce_stock_pull",
"get_woocommerce_stock_status",
"delete_fanava_invoice",
"get_fanava_invoice_fields",
"list_digipay_settlements",
"attach_digipay_purchase_invoice",
"leave_connected_business",
"get_workflow_marketplace_package",
"list_my_workflow_marketplace_packages",
"get_my_workflow_marketplace_package",
"republish_workflow_marketplace_package",
"list_ai_usage_logs",
"get_storage_export_info",
"list_notification_event_types",
"list_available_ai_models",
"get_business_delete_info",
"get_ai_invoice",
"update_oa_secretariat",
"update_oa_external_org",
"list_oa_signatures",
"upsert_oa_signature",
"list_oa_sign_paths",
"save_oa_sign_path",
"reject_oa_letter_sign",
"acknowledge_oa_dispatch",
"cancel_oa_referral",
"unarchive_oa_letter",
"list_oa_letter_versions",
"list_oa_ece_messages",
"list_oa_cartable_rules",
"save_oa_cartable_rule",
"delete_oa_cartable_rule",
"save_oa_archive_folder",
"archive_oa_letter_to_folder",
"get_oa_volume_report",
"get_oa_performance_report",
"notify_oa_due_followups",
"soft_delete_connected_business",
"restore_connected_business",
"get_telephony_my_context",
"list_telephony_assignable_users",
"list_telephony_team_supervisors",
"sync_telephony_extensions",
"get_telephony_screen_pop",
"get_telephony_operators_report",
"get_telephony_person_report",
"get_telephony_extension_report",
"get_telephony_live_snapshot",
"control_telephony_call",
"get_telephony_ops_metrics",
"list_telephony_dead_letters",
"resolve_telephony_dead_letter",
"get_telephony_softphone_health",
"list_distribution_team_members",
"get_distribution_my_preferences",
"unpublish_distribution_daily_plan",
"sync_distribution_offline",
"check_business_currency_usage",
"get_invoice_warehouse_line_quantities",
"create_warehouse_document_from_invoice",
"search_warehouse_invoice_sources",
"update_warehouse_document_line",
"bulk_warehouse_invoice_operations",
"get_person_opening_balance_eligibility",
"get_product_opening_balance_eligibility",
"link_tax_workspace_reference",
"bulk_upsert_persons",
"bulk_delete_persons",
"list_business_fx_global_rates",
"apply_currency_rates_from_global",
"bulk_create_currency_rates",
"sync_product_base_price_from_fx",
"sync_products_base_prices_from_fx",
"finalize_document_monetization_volume",
"renew_storage_subscription",
"pay_storage_over_usage",
"get_storage_file_dependencies",
"revoke_all_storage_file_shares",
"update_storage_file_share",
"list_dashboard_widget_definitions",
"get_dashboard_layout",
"save_dashboard_layout",
"get_default_dashboard_layout",
"save_default_dashboard_layout",
"get_dashboard_widgets_data",
"list_dashboard_quick_link_presets",
"get_dashboard_quick_links",
"save_dashboard_quick_links",
"get_business_menu_preferences",
"save_business_menu_preferences",
"get_persons_summary",
"get_product_commercial_insights",
"list_recent_sales_invoice_products",
"get_quick_sales_customer_balance",
"get_quick_sales_anonymous_customer",
"ensure_quick_sales_anonymous_customer",
"list_quick_sale_jobs",
"resolve_quick_sales_stock_discrepancy",
"list_barcode_label_revisions",
"get_barcode_label_sample_context",
"search_product_instance_by_code",
"get_workflow_error_analytics",
"delete_document_numbering_setting",
"bulk_create_product_instances",
"convert_product_to_unique_instances",
"list_report_template_gallery",
"list_report_template_studio_blocks",
"get_report_template_studio_contract",
"validate_report_template_builder_design",
"validate_report_template_studio_design",
"compile_report_template_studio_design",
"migrate_report_template_v2_to_v3",
"migrate_report_template_v3_to_v4",
"transition_report_template_status",
"list_report_template_status_events",
"list_report_template_status_audit",
"backfill_invoice_profit_ledger",
"recalculate_all_invoice_profits",
"delete_crm_chat_message",
"list_quick_sales_default_shortcuts",
"update_quick_sales_default_shortcuts",
"get_workflow_performance_analytics",
"preview_report_template_html",
"get_workflow_execution_timeline",
"list_workflow_trigger_catalog",
"list_workflow_action_catalog",
"list_kardex_document_lines",
"get_category_tree",
"create_product_category",
"update_product_category",
"move_product_category",
"delete_product_category",
"resolve_or_create_quick_customer",
"get_entity_activity_logs",
"get_accounts_tree",
"list_crm_deal_lines",
"get_repair_shop_plugin_status",
"preview_inventory_lot_dates",
"list_hscript_docs_manifest",
"get_hscript_doc",
"search_hscript_docs",
"get_hscript_assist_context",
"validate_hscript_source",
"list_customer_club_rfm_person_ids",
"get_customer_club_person_balance",
"list_bulk_product_prices_sheet_items",
"apply_bulk_product_prices_sheet",
"list_business_currencies",
"get_project",
"get_receipt_payment",
"get_expense_income",
"get_transfer",
"get_crm_activity",
"get_invoice_delete_info",
"remove_repair_part",
"list_ai_voice_catalog",
"get_business_voice_settings",
"update_business_voice_settings",
"list_warranty_codes",
"get_advanced_production_settings",
"update_advanced_production_settings",
"get_advanced_production_dashboard",
"get_advanced_production_report",
"list_advanced_production_master",
"create_advanced_production_master",
"bulk_delete_warehouse_documents",
"migrate_report_template_builder_to_v2",
)
WRITE_MUST_CONFIRM = (
"execute_zohal_inquiry",
"create_digipay_ticket",
"refund_digipay_purchase",
"update_pay_page_settings",
"create_period_end_fx_revaluation",
"delete_storage_file",
"sync_woocommerce_orders",
"sync_basalam_orders",
"create_wallet_top_up",
"invite_business_user",
"purchase_marketplace_plugin",
"install_workflow_marketplace_package",
"update_tax_identity_settings",
"create_business_backup",
"remove_business_user",
"subscribe_ai_plan",
"select_tax_provider",
"install_ai_skill",
"revoke_mcp_connection",
"pay_ai_invoice",
"finalize_woocommerce_opening_inventory",
"create_wallet_payout",
"create_person_share_link",
"run_hscript_report",
"recalculate_invoice_profits",
"run_woocommerce_stock_pull",
"leave_connected_business",
"delete_fanava_invoice",
"republish_workflow_marketplace_package",
"soft_delete_connected_business",
"restore_connected_business",
"control_telephony_call",
"reject_oa_letter_sign",
"save_oa_cartable_rule",
"delete_oa_cartable_rule",
"unpublish_distribution_daily_plan",
"sync_distribution_offline",
"create_warehouse_document_from_invoice",
"bulk_warehouse_invoice_operations",
"link_tax_workspace_reference",
"bulk_upsert_persons",
"bulk_delete_persons",
"apply_currency_rates_from_global",
"bulk_create_currency_rates",
"sync_products_base_prices_from_fx",
"pay_storage_over_usage",
"revoke_all_storage_file_shares",
"save_default_dashboard_layout",
"ensure_quick_sales_anonymous_customer",
"resolve_quick_sales_stock_discrepancy",
"delete_document_numbering_setting",
"bulk_create_product_instances",
"convert_product_to_unique_instances",
"transition_report_template_status",
"backfill_invoice_profit_ledger",
"recalculate_all_invoice_profits",
"delete_crm_chat_message",
"update_quick_sales_default_shortcuts",
"delete_product_category",
"resolve_or_create_quick_customer",
"apply_bulk_product_prices_sheet",
"remove_repair_part",
"update_business_voice_settings",
"update_advanced_production_settings",
"create_advanced_production_master",
"bulk_delete_warehouse_documents",
)
def _ctx(*, business_id: int = 7, user_id: int = 9, db=None):
user = MagicMock()
user.get_user_id.return_value = user_id
user.can_access_business.return_value = True
return {"db": db if db is not None else MagicMock(), "business_id": business_id, "user_context": user}
def test_remaining_tools_registered_and_manifested():
parsed = parse_registered_tool_fields()
for name in NEW_TOOLS:
assert name in parsed, name
entry = get_manifest_entry(name)
assert entry is not None, name
assert entry.domains
fields = parsed[name]
if fields["is_readonly"]:
assert fields["requires_approval"] is False, name
assert is_write_function(name) is False or get_manifest_entry(name).intent_write is False
else:
assert fields["requires_approval"] is True, name
assert fields["is_readonly"] is False, name
assert get_manifest_entry(name).intent_write is True, name
for name in WRITE_MUST_CONFIRM:
assert parsed[name]["requires_approval"] is True
assert get_manifest_entry(name).always_confirm is True or get_manifest_entry(name).intent_write is True
def test_secret_fields_are_out_of_digipay_mcp_schema():
props = UPDATE_SETTINGS_SCHEMA["properties"]
assert "username" not in props
assert "password" not in props
assert "client_secret" not in props
assert "client_id" not in props
assert UPDATE_SETTINGS_SCHEMA["additionalProperties"] is False
assert EXECUTE_SCHEMA["additionalProperties"] is False
assert "business_id" not in EXECUTE_SCHEMA["properties"]
def test_zohal_list_pins_wallet_to_context_business():
db = MagicMock()
with (
patch("app.services.zohal_service.list_zohal_services", return_value=[{"service_code": "x"}]) as services,
patch("app.services.wallet_service.get_wallet_overview", return_value={"available_balance": 1, "base_currency_code": "IRR"}) as wallet,
patch("app.services.system_settings_service.get_zohal_settings", return_value={"low_balance_threshold": 10}),
):
out = list_zohal_services_handler({}, _ctx(business_id=42, db=db))
wallet.assert_called_once_with(db, 42)
services.assert_called_once()
assert out["services"][0]["service_code"] == "x"
def test_digipay_settings_use_context_business():
db = MagicMock()
with patch("app.services.digipay_service.get_settings", return_value={"environment": "sandbox", "username": "***"}) as getter:
out = get_digipay_settings_handler({}, _ctx(business_id=11, db=db))
getter.assert_called_once()
assert getter.call_args.args[1] == 11
assert out["environment"] == "sandbox"
def test_pay_page_username_check_uses_connected_business():
db = MagicMock()
with patch(
"app.services.business_pay_page_service.check_username_availability",
return_value={"username": "shop", "available": True},
) as chk:
out = check_pay_page_username_handler({"username": "shop"}, _ctx(business_id=5, db=db))
assert chk.call_args.kwargs["business_id"] == 5
assert out["available"] is True
def test_period_end_preview_pins_business():
db = MagicMock()
with patch(
"app.services.period_end_fx_revaluation_service.preview_period_end_fx_revaluation",
return_value={"positions": []},
) as preview:
preview_period_end_fx_revaluation_handler({}, _ctx(business_id=9, db=db))
assert preview.call_args.args[1] == 9
def test_woocommerce_settings_handler_masks_token_and_pins_tenant():
db = MagicMock()
with patch(
"app.services.woocommerce_integration_service.get_settings",
return_value={"store_base_url": "https://x.example", "bridge_token": "secret"},
) as getter:
out = get_woocommerce_settings_handler({}, _ctx(business_id=3, db=db))
getter.assert_called_once_with(db, 3)
assert out["bridge_token"] == "***"
assert out["bridge_token_set"] is True