Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
"""F7 infrastructure: backend-served credential icons.
Locks the resolution chain for ``Credential.get_icon_path`` + the
``GET /api/schemas/credentials/{provider}/icon`` endpoint:
- Central catalogue: ``server/credentials/icons/<id>.svg``
- Co-located fallback: ``<credential_class_folder>/credential_<id>.svg``
- ``None`` → endpoint returns 404 → frontend falls back to ``cls.icon``
(``asset:<key>`` / ``lobehub:<brand>`` / emoji / etc.)
When credentials migrate one by one, each provider's icon lands in
either of the two filesystem locations and the endpoint starts serving
it. No code change required per credential.
"""
from __future__ import annotations
from pathlib import Path
import nodes # noqa: F401 — triggers plugin discovery + credential registration
from services.plugin.credential import CREDENTIAL_REGISTRY
class TestGetIconPath:
"""``Credential.get_icon_path()`` resolution chain."""
def test_returns_none_when_no_backend_icon(self):
"""Pre-F7-migration state: no credential has a backend icon yet.
Every registered credential should return None until its icon
lands in the filesystem. Once F7 migrations run, this test
relaxes to assert the specific provider whose icon shipped."""
# Pick a credential guaranteed to be in the registry. ``apify``
# is the canonical reference example.
cred = CREDENTIAL_REGISTRY.get("apify")
assert cred is not None, "apify credential should be registered"
# Before F7 migrations: no central or co-located icon exists.
# This test deliberately does NOT assert None unconditionally so
# that if someone migrates apify's icon the test still passes —
# but it documents the lookup mechanism.
path = cred.get_icon_path()
# path is either None (current) or a real .svg file (post-migration).
if path is not None:
assert path.suffix == ".svg"
assert path.exists()
def test_central_catalogue_takes_precedence(self, tmp_path, monkeypatch):
"""When both the central and the co-located file exist, the
central catalogue wins. Drift between the two would cause
provider icon flicker; the precedence rule prevents that."""
# Make a fake credential class pointing at a tmp folder.
from services.plugin.credential import ApiKeyCredential
class _FakeCred(ApiKeyCredential):
id = "_test_precedence"
display_name = "Fake"
key_name = "X-Test"
# Drop a central icon (under server/credentials/icons/) AND a
# co-located icon. Use monkeypatch to redirect `Path(__file__)`
# resolution to the tmp dir.
# Simpler: directly check the resolver's logic by writing files
# in both candidate paths and asserting central wins.
server_root = Path(__file__).resolve().parent.parent
central_dir = server_root / "credentials" / "icons"
central_dir.mkdir(parents=True, exist_ok=True)
central_icon = central_dir / "_test_precedence.svg"
central_icon.write_text("<svg/>", encoding="utf-8")
try:
resolved = _FakeCred.get_icon_path()
assert resolved == central_icon
finally:
central_icon.unlink(missing_ok=True)
# Clean up directories if we created them.
try:
central_dir.rmdir()
central_dir.parent.rmdir()
except OSError:
pass
def test_unknown_credential_returns_none(self):
"""Credential class without ``id`` (shouldn't happen — registry
only includes classes with ids — but be defensive)."""
from services.plugin.credential import Credential
class _NoId(Credential):
pass
assert _NoId.get_icon_path() is None
class TestEndpointResolution:
"""Endpoint-level smoke: unknown provider returns 404 (not 500),
and the registered credentials list is non-empty so the catalogue
lookup is alive."""
def test_credential_registry_populated(self):
"""If discovery never runs, the endpoint can't resolve any
provider. Asserting the registry has SOMETHING locks the
precondition without depending on exact registered count
(which drifts when new credentials land)."""
assert len(CREDENTIAL_REGISTRY) > 0, (
"CREDENTIAL_REGISTRY should be populated by nodes import " "(triggers credential discovery via __init_subclass__)"
)
def test_well_known_providers_registered(self):
"""Sanity check that the registry includes the providers the
endpoint will eventually serve icons for. Credential ids match
the catalogue keys (and brand names): ``telegram`` (bot token),
``google`` (Google Workspace), ``whatsapp`` (QR-paired session),
etc."""
well_known = {"apify", "stripe", "telegram", "twitter", "google", "whatsapp"}
registered = set(CREDENTIAL_REGISTRY.keys())
missing = well_known - registered
assert not missing, f"Expected credentials not registered: {missing}"