| Server IP : 3.147.158.171 / Your IP : 216.73.216.88 Web Server : Apache/2.4.67 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-2-178.us-east-2.compute.internal 6.1.172-216.329.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 20 06:31:34 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.21 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /tsai/repo/api/tests/unit/ |
Upload File : |
"""Tests for the site index endpoint and service."""
import sqlite3
import pytest
from pathlib import Path
from unittest.mock import patch
from app.appTypes import IndexedSite
from app.services.site_index_service import SiteIndexService
@pytest.fixture
def tmp_db(tmp_path):
"""Create a temporary SQLite database with test data."""
db_path = tmp_path / "site_index.db"
conn = sqlite3.connect(str(db_path))
conn.execute('''
CREATE TABLE site_index (
slug TEXT PRIMARY KEY,
wp_host TEXT NOT NULL,
name TEXT DEFAULT '',
tagline TEXT DEFAULT '',
link TEXT DEFAULT '',
chat_url TEXT DEFAULT '',
logo_full TEXT DEFAULT '',
logo_72 TEXT DEFAULT '',
logo_150 TEXT DEFAULT '',
logo_300 TEXT DEFAULT '',
indexed_at TEXT NOT NULL,
promoted INTEGER DEFAULT 1
)
''')
conn.execute('''
INSERT INTO site_index VALUES
('tsai', 'http://turmansolutions.test', 'Turman Solutions', 'AI Chatbot Platform',
'http://turmansolutions.test', 'http://chat.turmansolutions.test',
'http://turmansolutions.test/logo.png',
'http://turmansolutions.test/logo-72.png',
'http://turmansolutions.test/logo-150.png',
'http://turmansolutions.test/logo-300.png',
'2026-03-08T00:00:00+00:00', 1)
''')
conn.execute('''
INSERT INTO site_index VALUES
('jpt', 'http://johnturman.test', 'John''s Blog', 'Personal blog',
'http://johnturman.test', 'http://chat.johnturman.test',
'http://johnturman.test/logo.png',
'http://johnturman.test/logo-72.png',
'http://johnturman.test/logo-150.png',
'http://johnturman.test/logo-300.png',
'2026-03-08T00:00:00+00:00', 0)
''')
conn.commit()
conn.close()
return db_path
class TestSiteIndexService:
def test_get_all_returns_sites(self, tmp_db):
with patch.object(
SiteIndexService, '__init_subclass__', lambda: None
):
with patch('app.services.site_index_service.DB_PATH', tmp_db):
service = SiteIndexService()
sites = service.get_all()
assert len(sites) == 2
slugs = [s.slug for s in sites]
assert 'tsai' in slugs
assert 'jpt' in slugs
# Check promoted field
tsai = next(s for s in sites if s.slug == 'tsai')
jpt = next(s for s in sites if s.slug == 'jpt')
assert tsai.promoted is True
assert jpt.promoted is False
def test_get_all_empty_when_no_db(self, tmp_path):
missing_db = tmp_path / "nonexistent.db"
with patch('app.services.site_index_service.DB_PATH', missing_db):
service = SiteIndexService()
sites = service.get_all()
assert sites == []
def test_get_by_slug_found(self, tmp_db):
with patch('app.services.site_index_service.DB_PATH', tmp_db):
service = SiteIndexService()
site = service.get_by_slug('tsai')
assert site is not None
assert site.slug == 'tsai'
assert site.name == 'Turman Solutions'
assert site.tagline == 'AI Chatbot Platform'
assert site.chat_url == 'http://chat.turmansolutions.test'
assert site.promoted is True
def test_get_by_slug_not_found(self, tmp_db):
with patch('app.services.site_index_service.DB_PATH', tmp_db):
service = SiteIndexService()
site = service.get_by_slug('nonexistent')
assert site is None
def test_get_by_slug_no_db(self, tmp_path):
missing_db = tmp_path / "nonexistent.db"
with patch('app.services.site_index_service.DB_PATH', missing_db):
service = SiteIndexService()
site = service.get_by_slug('tsai')
assert site is None
class TestSiteIndexEndpoints:
def test_get_site_index(self, test_client, tmp_db):
with patch('app.services.site_index_service.DB_PATH', tmp_db):
response = test_client.get("/api/v1/site-index")
assert response.status_code == 200
data = response.json()
assert len(data) == 2
# Check camelCase serialization
site = next(s for s in data if s['slug'] == 'tsai')
assert site['name'] == 'Turman Solutions'
assert site['chatUrl'] == 'http://chat.turmansolutions.test'
assert site['wpHost'] == 'http://turmansolutions.test'
assert site['logoFull'] == 'http://turmansolutions.test/logo.png'
assert site['indexedAt'] == '2026-03-08T00:00:00+00:00'
assert site['promoted'] is True
# Check jpt is not promoted
jpt = next(s for s in data if s['slug'] == 'jpt')
assert jpt['promoted'] is False
def test_get_site_index_empty(self, test_client, tmp_path):
missing_db = tmp_path / "nonexistent.db"
with patch('app.services.site_index_service.DB_PATH', missing_db):
response = test_client.get("/api/v1/site-index")
assert response.status_code == 200
assert response.json() == []
def test_get_site_index_by_slug(self, test_client, tmp_db):
with patch('app.services.site_index_service.DB_PATH', tmp_db):
response = test_client.get("/api/v1/site-index/tsai")
assert response.status_code == 200
data = response.json()
assert data['slug'] == 'tsai'
assert data['name'] == 'Turman Solutions'
assert data['tagline'] == 'AI Chatbot Platform'
assert data['promoted'] is True
def test_get_site_index_by_slug_not_found(self, test_client, tmp_db):
with patch('app.services.site_index_service.DB_PATH', tmp_db):
response = test_client.get("/api/v1/site-index/nonexistent")
assert response.status_code == 404
class TestIndexScript:
def test_compute_chat_url(self):
"""Test the chat URL computation logic."""
# Import from script
import sys
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent / 'scripts'))
from index_sites import compute_chat_url
# jpt and tsai use "chat." prefix
assert compute_chat_url('tsai', 'http://turmansolutions.test') == 'http://chat.turmansolutions.test'
assert compute_chat_url('jpt', 'http://johnturman.test') == 'http://chat.johnturman.test'
# All others use "chat-" prefix
assert compute_chat_url('sandbox', 'http://sandbox.turmansolutions.test') == 'http://chat-sandbox.turmansolutions.test'
assert compute_chat_url('demo', 'https://demo.turmansolutions.ai') == 'https://chat-demo.turmansolutions.ai'
def test_compute_chat_url_no_protocol(self):
import sys
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent / 'scripts'))
from index_sites import compute_chat_url
assert compute_chat_url('sandbox', 'sandbox.test') == 'chat-sandbox.test'