Alialdlfy1 commited on
Commit
b0944c2
·
verified ·
1 Parent(s): b9b99bc

Upload test_preview_cleanliness.py

Browse files
Files changed (1) hide show
  1. tests/test_preview_cleanliness.py +67 -0
tests/test_preview_cleanliness.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ import asyncio
3
+ import os
4
+ import sys
5
+ import unittest
6
+
7
+ _loop = asyncio.new_event_loop()
8
+ asyncio.set_event_loop(_loop)
9
+
10
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
11
+
12
+ from modules.blogger.processor import ArticleProcessor
13
+
14
+
15
+ class TestPreviewCleanliness(unittest.TestCase):
16
+ """تحقق من أن معاينة تيليجرام لا تعرض CSS أو HTML خام."""
17
+
18
+ def _processor(self):
19
+ return ArticleProcessor(db=None, ai_client=None, config={})
20
+
21
+ def _article(self):
22
+ return {
23
+ "title": "عنوان تجريبي",
24
+ "summary": "ملخص المقال",
25
+ "introduction": "مقدمة المقال هنا",
26
+ "body": (
27
+ "<h2>القسم الأول</h2><p>نص تجريبي داخل الفقرة.</p>"
28
+ "<h3>القسم الثاني</h3><p>فقرة ثانية.</p>"
29
+ "<table><tr><th>العمود أ</th><th>العمود ب</th></tr>"
30
+ "<tr><td>قيمة 1</td><td>قيمة 2</td></tr></table>"
31
+ "<ul><li>عنصر أول</li><li>عنصر ثانٍ</li></ul>"
32
+ "<a href='https://example.com'>رابط تجريبي</a>"
33
+ ),
34
+ "labels": ["وظائف", "تجربة"],
35
+ "keywords": "كلمة1، كلمة2",
36
+ "hashtags": ["#تاغ1", "#تاغ2"],
37
+ "reading_time": 3,
38
+ "notes": ["ملاحظة مهمة"],
39
+ "faq": [{"question": "سؤال؟", "answer": "جواب."}],
40
+ "source": {"name": "مصدر تجريبي", "username": "example"},
41
+ }
42
+
43
+ def test_html_to_telegram_contains_no_css(self):
44
+ html = ArticleProcessor.make_article_html(self._processor(), self._article())
45
+ preview = ArticleProcessor.html_to_telegram(html)
46
+ for bad in ("<style", "</style>", "<script", "</script>", "html{", "body{", "<head>", "class=", "style=", "<div", "<span", "<table", "<h2", "scroll-behavior"):
47
+ self.assertNotIn(bad, preview, f"تسرب: {bad}")
48
+ self.assertNotIn("/*", preview)
49
+
50
+ def test_html_to_telegram_keeps_telegram_formatting(self):
51
+ html = ArticleProcessor.make_article_html(self._processor(), self._article())
52
+ preview = ArticleProcessor.html_to_telegram(html)
53
+ self.assertIn("القسم الأول", preview)
54
+ self.assertIn("<a href=", preview)
55
+ self.assertIn("العمود أ", preview)
56
+ self.assertIn("قيمة 1", preview)
57
+
58
+ def test_make_preview_data_clean(self):
59
+ image, text = self._processor().make_preview_data(self._article())
60
+ for bad in ("<style", "html{", "body{", "<head>", "class=", "style="):
61
+ self.assertNotIn(bad, text, f"تسرب: {bad}")
62
+ self.assertIn("<b>عنوان تجريبي</b>", text)
63
+ self.assertIn("مدة القراءة: 3 دقيقة", text)
64
+
65
+
66
+ if __name__ == "__main__":
67
+ unittest.main()