from __future__ import annotations
import json
from unittest.mock import AsyncMock, patch
from django.test import TestCase
from django.urls import reverse
from core.models import ChatSession, Message, Person, PersonIdentifier, User
from core.views.compose import COMPOSE_ASSET_VERSION
class ComposeSendCapabilityTests(TestCase):
def setUp(self):
self.user = User.objects.create_user("compose-send", "send@example.com", "pw")
self.client.force_login(self.user)
@patch("core.views.compose.transport.enqueue_runtime_command")
@patch("core.views.compose.transport.send_message_raw", new_callable=AsyncMock)
def test_unsupported_send_fails_fast_without_dispatch(
self,
mocked_send_message_raw,
mocked_enqueue_runtime_command,
):
response = self.client.post(
reverse("compose_send"),
{
"service": "xmpp",
"identifier": "person@example.com",
"text": "hello",
"failsafe_arm": "1",
"failsafe_confirm": "1",
},
)
self.assertEqual(200, response.status_code)
payload = json.loads(response.headers["HX-Trigger"])["composeSendResult"]
self.assertFalse(payload["ok"])
self.assertEqual("warning", payload["level"])
self.assertIn("Send not supported:", payload["message"])
mocked_send_message_raw.assert_not_awaited()
mocked_enqueue_runtime_command.assert_not_called()
def test_compose_page_displays_send_disabled_reason_for_unsupported_service(self):
response = self.client.get(
reverse("compose_page"),
{
"service": "xmpp",
"identifier": "person@example.com",
},
)
self.assertEqual(200, response.status_code)
content = response.content.decode("utf-8")
self.assertIn("Send disabled:", content)
self.assertIn("compose-send-btn", content)
def test_compose_page_uses_humanized_browser_title(self):
response = self.client.get(
reverse("compose_page"),
{
"service": "signal",
"identifier": "+15551230000",
},
)
self.assertEqual(200, response.status_code)
self.assertContains(response, "
Compose Page ยท GIA", html=False)
def test_compose_page_uses_external_compose_assets(self):
response = self.client.get(
reverse("compose_page"),
{
"service": "signal",
"identifier": "+15551230000",
},
)
self.assertEqual(200, response.status_code)
content = response.content.decode("utf-8")
self.assertIn(
f"/static/css/compose-panel.css?v={COMPOSE_ASSET_VERSION}",
content,
)
self.assertIn(
f"/static/js/compose-panel-core.js?v={COMPOSE_ASSET_VERSION}",
content,
)
self.assertIn(
f"/static/js/compose-panel-thread.js?v={COMPOSE_ASSET_VERSION}",
content,
)
self.assertIn(
f"/static/js/compose-panel-send.js?v={COMPOSE_ASSET_VERSION}",
content,
)
self.assertIn(
f"/static/js/compose-panel.js?v={COMPOSE_ASSET_VERSION}",
content,
)
self.assertNotIn("const initialTyping = JSON.parse(", content)
self.assertNotIn("data-drafts-url=", content)
self.assertNotIn("data-summary-url=", content)
self.assertNotIn("data-quick-insights-url=", content)
self.assertNotIn("data-toggle-command-url=", content)
self.assertNotIn("data-engage-preview-url=", content)
self.assertNotIn("data-engage-send-url=", content)
self.assertNotIn("compose-ticks", content)
self.assertNotIn("compose-receipt-modal", content)
def test_compose_widget_declares_compose_assets_on_widget_shell(self):
response = self.client.get(
reverse("compose_widget"),
{
"service": "signal",
"identifier": "+15551230000",
},
)
self.assertEqual(200, response.status_code)
content = response.content.decode("utf-8")
self.assertIn("data-gia-style-hrefs=", content)
self.assertIn(
f"/static/css/compose-panel.css?v={COMPOSE_ASSET_VERSION}",
content,
)
self.assertIn("data-gia-script-srcs=", content)
self.assertIn(
f"/static/js/compose-panel-core.js?v={COMPOSE_ASSET_VERSION}",
content,
)
self.assertIn(
f"/static/js/compose-panel-thread.js?v={COMPOSE_ASSET_VERSION}",
content,
)
self.assertIn(
f"/static/js/compose-panel-send.js?v={COMPOSE_ASSET_VERSION}",
content,
)
self.assertIn(
f"/static/js/compose-panel.js?v={COMPOSE_ASSET_VERSION}",
content,
)
self.assertIn('gs-id="widget-compose-widget-', content)
self.assertNotIn("