mirror of
https://github.com/open-jarvis/OpenJarvis.git
synced 2026-07-31 03:12:16 +00:00
Adds a new POST /{connector_id}/sync endpoint alongside the existing GET
sync-status endpoint. The new endpoint validates the connector is registered
and connected, then runs SyncEngine.sync() and returns chunks_indexed.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
95 lines
2.9 KiB
Python
95 lines
2.9 KiB
Python
"""Tests for the /v1/connectors API router."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
try:
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
except ImportError:
|
|
pytest.skip("fastapi not installed")
|
|
|
|
from openjarvis.server.connectors_router import create_connectors_router
|
|
|
|
_app = FastAPI()
|
|
router = create_connectors_router()
|
|
_app.include_router(router, prefix="/v1")
|
|
return TestClient(_app)
|
|
|
|
|
|
def test_list_connectors(app):
|
|
"""GET /v1/connectors returns a list that includes the obsidian connector."""
|
|
resp = app.get("/v1/connectors")
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert "connectors" in data
|
|
ids = [c["connector_id"] for c in data["connectors"]]
|
|
assert "obsidian" in ids
|
|
|
|
|
|
def test_connector_detail(app):
|
|
"""GET /v1/connectors/obsidian returns the expected fields."""
|
|
resp = app.get("/v1/connectors/obsidian")
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["connector_id"] == "obsidian"
|
|
assert "display_name" in data
|
|
assert "auth_type" in data
|
|
assert "connected" in data
|
|
assert "mcp_tools" in data
|
|
|
|
|
|
def test_connector_not_found(app):
|
|
"""GET /v1/connectors/nonexistent returns 404."""
|
|
resp = app.get("/v1/connectors/nonexistent")
|
|
assert resp.status_code == 404
|
|
|
|
|
|
def test_connect_obsidian(app, tmp_path):
|
|
"""POST /v1/connectors/obsidian/connect with a valid path marks it connected."""
|
|
# Create a minimal vault directory so is_connected() returns True.
|
|
vault = tmp_path / "vault"
|
|
vault.mkdir()
|
|
|
|
resp = app.post("/v1/connectors/obsidian/connect", json={"path": str(vault)})
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["connector_id"] == "obsidian"
|
|
assert data["connected"] is True
|
|
|
|
|
|
def test_disconnect(app):
|
|
"""POST /v1/connectors/obsidian/disconnect returns 200 with connected=False."""
|
|
resp = app.post("/v1/connectors/obsidian/disconnect")
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["connector_id"] == "obsidian"
|
|
assert data["connected"] is False
|
|
|
|
|
|
def test_sync_status(app):
|
|
"""GET /v1/connectors/obsidian/sync returns a response with a state field."""
|
|
resp = app.get("/v1/connectors/obsidian/sync")
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert "state" in data
|
|
assert data["connector_id"] == "obsidian"
|
|
|
|
|
|
def test_trigger_sync(app, tmp_path: Path) -> None:
|
|
"""POST /v1/connectors/obsidian/sync triggers an incremental sync."""
|
|
vault = tmp_path / "vault"
|
|
vault.mkdir()
|
|
(vault / "note.md").write_text("# Test note\n\nContent here.")
|
|
app.post("/v1/connectors/obsidian/connect", json={"path": str(vault)})
|
|
resp = app.post("/v1/connectors/obsidian/sync")
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["chunks_indexed"] >= 1
|