Enterprise agent platform for building, deploying, and operating AI teammates.
"""Tests for the Meta Ads MCP connector seed migration."""
import importlib.util
from pathlib import Path
from unittest.mock import patch
from alembic.migration import MigrationContext
from alembic.operations import Operations
from sqlalchemy import create_engine, text
def _load_migration_module():
migration_file = (
Path(__file__).parent.parent.parent
/ "src/xagent/migrations/versions/20260909_seed_meta_ads_mcp_app.py"
)
spec = importlib.util.spec_from_file_location(
"seed_meta_ads_migration", migration_file
)
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(module)
return module
def _operations(connection):
return Operations(MigrationContext.configure(connection))
def _create_table(connection):
connection.execute(
text(
"""
CREATE TABLE public_mcp_apps (
id INTEGER PRIMARY KEY,
app_id VARCHAR(100) NOT NULL UNIQUE,
name VARCHAR(200) NOT NULL,
description TEXT,
icon VARCHAR(1000),
transport VARCHAR(50) NOT NULL DEFAULT 'oauth',
provider_name VARCHAR(50),
category VARCHAR(100),
oauth_scopes JSON,
is_visible_in_connector BOOLEAN NOT NULL DEFAULT 1,
launch_config JSON
)
"""
)
)
def _app_ids(connection):
return set(connection.execute(text("SELECT app_id FROM public_mcp_apps")).scalars())
def test_upgrade_inserts_meta_ads(tmp_path):
engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
migration = _load_migration_module()
with engine.begin() as connection:
_create_table(connection)
with patch.object(migration, "op", _operations(connection)):
migration.upgrade()
assert "meta-ads" in _app_ids(connection)
row = connection.execute(
text(
"SELECT transport, provider_name, oauth_scopes, launch_config "
"FROM public_mcp_apps WHERE app_id='meta-ads'"
)
).first()
assert row[0] == "oauth"
assert row[1] == "meta"
assert "ads_read" in str(row[2])
assert "xagent.web.tools.mcp.meta_ads" in str(row[3])
assert "META_ACCESS_TOKEN" in str(row[3])
def test_upgrade_is_idempotent(tmp_path):
engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
migration = _load_migration_module()
with engine.begin() as connection:
_create_table(connection)
with patch.object(migration, "op", _operations(connection)):
migration.upgrade()
migration.upgrade() # second run must not raise or duplicate
rows = connection.execute(
text("SELECT COUNT(*) FROM public_mcp_apps WHERE app_id='meta-ads'")
).scalar()
assert rows == 1
def test_seed_row_matches_registry(tmp_path):
"""The migration snapshot and the runtime registry must define the same
meta-ads row (the migration is a frozen copy; this catches drift)."""
from xagent.web.builtin_mcp_registry import get_builtin_public_mcp_app_rows
migration = _load_migration_module()
registry_row = next(
r for r in get_builtin_public_mcp_app_rows() if r["app_id"] == "meta-ads"
)
assert migration.ROW == registry_row
def test_downgrade_removes_meta_ads(tmp_path):
engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
migration = _load_migration_module()
with engine.begin() as connection:
_create_table(connection)
with patch.object(migration, "op", _operations(connection)):
migration.upgrade()
migration.downgrade()
assert "meta-ads" not in _app_ids(connection)
def test_downgrade_preserves_preexisting_custom_row_with_same_app_id(tmp_path):
"""An administrator could have hand-created a custom public app under the
same free-form app_id before this migration ever ran; upgrade() correctly
no-ops on that collision (see test below), but downgrade() must not then
delete that unrelated row just because the app_id matches."""
engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
migration = _load_migration_module()
with engine.begin() as connection:
_create_table(connection)
connection.execute(
text(
"INSERT INTO public_mcp_apps "
"(app_id, name, description, transport) "
"VALUES ('meta-ads', 'Internal Meta Ads Proxy', "
"'Hand-rolled admin connector', 'stdio')"
)
)
with patch.object(migration, "op", _operations(connection)):
migration.upgrade() # no-ops: app_id already exists
migration.downgrade()
row = connection.execute(
text(
"SELECT name, description, transport FROM public_mcp_apps "
"WHERE app_id='meta-ads'"
)
).first()
assert row is not None, "downgrade must not delete an unowned custom row"
assert row[0] == "Internal Meta Ads Proxy"
assert row[1] == "Hand-rolled admin connector"
assert row[2] == "stdio"