"""HTTP compatibility tests against a temporary database and mocked realtime.""" import sys import tempfile import unittest from pathlib import Path from unittest.mock import AsyncMock, patch sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'services')) from fastapi import FastAPI from fastapi.testclient import TestClient from social_svc import store from social_svc.routers.api import router class SocialApiTests(unittest.TestCase): def setUp(self): directory = tempfile.TemporaryDirectory() self.addCleanup(directory.cleanup) override = patch.object(store, 'SOCIAL_DB_PATH', Path(directory.name) / 'social.db') override.start() self.addCleanup(override.stop) publish = patch('social_svc.routers.api.publish_chat_message', new_callable=AsyncMock) publish.start() self.addCleanup(publish.stop) app = FastAPI() app.include_router(router, prefix='/api/social/v1') self.client = TestClient(app) self.addCleanup(self.client.close) self.headers = {'X-Social-Guest-Id': 'isolated-test'} def test_message_retry_conflict_and_legacy_payload(self): url = '/api/social/v1/channels/general/messages' body = {'author': 'Name', 'text': 'Hello', 'clientId': 'same'} first = self.client.post(url, json=body, headers=self.headers) self.assertEqual(first.status_code, 200) self.assertEqual(self.client.post(url, json=body, headers=self.headers).json(), first.json()) self.assertEqual(self.client.post(url, json={**body, 'text': 'Changed'}, headers=self.headers).status_code, 409) self.assertEqual(self.client.post(url, json={'text': 'Legacy'}, headers=self.headers).status_code, 200) self.assertEqual(len(self.client.get(url).json()['items']), 2) def test_whitespace_and_missing_identity_do_not_write_messages(self): url = '/api/social/v1/channels/general/messages' self.assertEqual(self.client.post(url, json={'text': ' '}, headers=self.headers).status_code, 400) self.assertEqual(self.client.post(url, json={'text': 'Hello'}).status_code, 400) def test_application_retry_and_whitespace_validation(self): url = '/api/social/v1/gigs/project/apply' body = {'name': 'Name', 'contact': 'Contact', 'requestId': 'same'} first = self.client.post(url, json=body, headers=self.headers) self.assertEqual(first.status_code, 200) self.assertEqual(self.client.post(url, json=body, headers=self.headers).json(), first.json()) self.assertEqual(self.client.post(url, json={**body, 'contact': 'Changed'}, headers=self.headers).status_code, 409) self.assertEqual(self.client.post(url, json={**body, 'name': ' '}, headers=self.headers).status_code, 400) def test_roster_round_trip_keeps_private_fields_and_guest_isolation(self): url = '/api/social/v1/roster/me' body = {'name': 'Name', 'phone': 'private-contact', 'linktree': 'https://example.invalid'} self.assertEqual(self.client.put(url, json=body, headers=self.headers).status_code, 200) self.assertEqual(self.client.get(url, headers=self.headers).json()['item']['phone'], 'private-contact') self.assertIsNone(self.client.get(url, headers={'X-Social-Guest-Id': 'another-test'}).json()['item']) if __name__ == '__main__': unittest.main()