Ghost mode for stories and chats, several photos in one comment, .ltp plugins (rights, defender, rollback, downloads only with consent), custom chat bubbles with a text colour of your choice, the lihont-ltp SDK and the plugin docs, streak flames as widgets with a keeper, avatar shapes, Material 3 surfaces. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
161 lines
8.0 KiB
Python
161 lines
8.0 KiB
Python
"""The LihonT plugin core (.ltp files) on a real JVM, and the files the packer makes.
|
||
|
||
The core of lihont.two.plugin (reading, refusing, conditions, names, the zip) is plain Java, so the tests compile it
|
||
with javac and run each scenario in its own process. The Android parts (registry, engine, screen) are covered by the
|
||
runtime compile test.
|
||
"""
|
||
|
||
import os
|
||
import shutil
|
||
import subprocess
|
||
import sys
|
||
import tempfile
|
||
import unittest
|
||
import zipfile
|
||
from pathlib import Path
|
||
|
||
ROOT = Path(__file__).resolve().parents[1]
|
||
CORE = ["Act", "Cond", "Ctx", "Defender", "DownloadSpec", "Downloader", "Evaluator", "Fields", "Journal", "Json",
|
||
"LtpException", "OverlaySpec", "Permission", "Plugin", "PluginPackage", "Rule"]
|
||
HARNESS = ROOT / "tests" / "java" / "lihont" / "two" / "plugin" / "PluginTest.java"
|
||
|
||
|
||
class PluginCoreTest(unittest.TestCase):
|
||
@classmethod
|
||
def setUpClass(cls):
|
||
cls.javac = shutil.which("javac")
|
||
cls.java = shutil.which("java")
|
||
if not cls.javac or not cls.java:
|
||
raise RuntimeError("Plugin tests require a JDK with java and javac")
|
||
cls.temporary = tempfile.TemporaryDirectory(prefix="lihont-plugin-tests-")
|
||
cls.addClassCleanup(cls.temporary.cleanup)
|
||
cls.classes = Path(cls.temporary.name) / "classes"
|
||
cls.classes.mkdir()
|
||
cls.environment = dict(os.environ)
|
||
cls.environment.pop("JAVA_TOOL_OPTIONS", None)
|
||
sources = [str(ROOT / "src" / "lihont" / "two" / "plugin" / (name + ".java")) for name in CORE]
|
||
cls.run_command([cls.javac, "--release", "17", "-encoding", "UTF-8", "-Xlint:all", "-Werror",
|
||
"-d", str(cls.classes)] + sources + [str(HARNESS)])
|
||
|
||
@classmethod
|
||
def run_command(cls, command, cwd=None):
|
||
result = subprocess.run(command, capture_output=True, text=True, encoding="utf-8", env=cls.environment,
|
||
timeout=120, cwd=cwd)
|
||
if result.returncode:
|
||
raise AssertionError("Command failed with exit %d: %s\n%s%s" % (result.returncode, command[0],
|
||
result.stdout, result.stderr))
|
||
return result
|
||
|
||
def run_scenario(self, name):
|
||
result = self.run_command([self.java, "-Dfile.encoding=UTF-8", "-cp", str(self.classes),
|
||
"lihont.two.plugin.PluginTest", name], cwd=str(ROOT))
|
||
self.assertEqual(result.stdout.strip(), "PASS " + name)
|
||
|
||
def test_a_good_plugin_is_read_and_the_samples_are_valid(self):
|
||
self.run_scenario("parse")
|
||
|
||
def test_bad_plugins_are_refused_with_a_reason(self):
|
||
self.run_scenario("refusals")
|
||
|
||
def test_conditions(self):
|
||
self.run_scenario("conditions")
|
||
|
||
def test_name_rules(self):
|
||
self.run_scenario("names")
|
||
|
||
def test_the_zip_is_read_strictly(self):
|
||
self.run_scenario("package")
|
||
|
||
def test_the_hash_follows_the_bytes(self):
|
||
self.run_scenario("hash")
|
||
|
||
def test_overlay_pictures_need_their_rights_and_files(self):
|
||
self.run_scenario("overlay")
|
||
|
||
def test_files_of_a_plugin_are_checked_by_their_first_bytes_and_hashed(self):
|
||
self.run_scenario("assets")
|
||
|
||
def test_defender_reads_rules_in_plain_words_and_grades_the_risk(self):
|
||
self.run_scenario("defender")
|
||
|
||
def test_downloads_need_a_right_named_sites_and_a_real_file(self):
|
||
self.run_scenario("download")
|
||
|
||
def test_the_licence_and_the_ai_of_a_plugin_are_read_and_shown_as_the_authors_word(self):
|
||
self.run_scenario("origin")
|
||
settings = (ROOT / "src" / "lihont" / "two" / "settings" / "PluginSettings.java").read_text(encoding="utf-8")
|
||
self.assertGreaterEqual(settings.count("origin(plugin)"), 2) # the install dialog and the card
|
||
self.assertIn("(со слов автора)", settings)
|
||
|
||
def test_the_download_is_a_plain_get_that_carries_nothing_about_the_person(self):
|
||
source = (ROOT / "src" / "lihont" / "two" / "plugin" / "Downloader.java").read_text(encoding="utf-8")
|
||
self.assertIn('setRequestMethod("GET")', source)
|
||
self.assertIn("setInstanceFollowRedirects(false)", source)
|
||
self.assertEqual(source.count("setRequestProperty"), 2) # User-Agent and Accept-Encoding, nothing else
|
||
self.assertNotIn("setDoOutput", source)
|
||
net = (ROOT / "src" / "lihont" / "two" / "plugin" / "PluginNet.java").read_text(encoding="utf-8")
|
||
self.assertIn("downloadConfirmed", net) # no file without the person's yes for that address
|
||
engine = (ROOT / "src" / "lihont" / "two" / "plugin" / "PluginEngine.java").read_text(encoding="utf-8")
|
||
self.assertIn("PluginRegistry.netOn(", engine) # and none while the plugin's internet is off
|
||
|
||
def test_long_dialogs_scroll_and_the_review_shows_the_defender(self):
|
||
activity = (ROOT / "src" / "lihont" / "two" / "settings" / "LihonTSettingsActivity.java").read_text(encoding="utf-8")
|
||
self.assertIn("new ScrollView(this)", activity)
|
||
settings = (ROOT / "src" / "lihont" / "two" / "settings" / "PluginSettings.java").read_text(encoding="utf-8")
|
||
self.assertGreaterEqual(settings.count("Defender.check(plugin)"), 2)
|
||
|
||
def test_a_plugin_never_changes_the_handle_that_tiktok_validates(self):
|
||
names = (ROOT / "src" / "lihont" / "two" / "badge" / "AccountNames.java").read_text(encoding="utf-8")
|
||
self.assertIn('"getUniqueId".equals(trace[i].getMethodName())', names)
|
||
self.assertIn("fromHandle()) shown = name", names)
|
||
|
||
def test_boykisser_plugin_is_valid(self):
|
||
self.run_scenario("boykisser")
|
||
|
||
def test_day_cycle_plugin_changes_only_my_name_by_the_time_of_day(self):
|
||
self.run_scenario("daycycle")
|
||
|
||
|
||
class PackerTest(unittest.TestCase):
|
||
def test_packed_samples_hold_only_the_allowed_files(self):
|
||
with tempfile.TemporaryDirectory(prefix="lihont-ltp-") as out:
|
||
result = subprocess.run([sys.executable, "-I", str(ROOT / "scripts" / "make_ltp.py"), "-o", out],
|
||
capture_output=True, text=True, encoding="utf-8", timeout=60, cwd=str(ROOT))
|
||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||
files = sorted(Path(out).glob("*.ltp"))
|
||
self.assertGreaterEqual(len(files), 4)
|
||
for file in files:
|
||
with zipfile.ZipFile(file) as archive:
|
||
for name in archive.namelist():
|
||
self.assertTrue(name in ("plugin.json", "icon.png") or name.startswith("assets/"), file.name)
|
||
self.assertIn("plugin.json", archive.namelist())
|
||
|
||
|
||
class HooksTest(unittest.TestCase):
|
||
"""The hooks that connect the engine to the mod stay in place."""
|
||
|
||
def test_the_feed_filter_asks_the_engine(self):
|
||
text = (ROOT / "src" / "lihont" / "two" / "feed" / "FeedFilter.java").read_text(encoding="utf-8")
|
||
self.assertIn("PluginEngine.feedActive()", text)
|
||
self.assertIn("PluginEngine.hideFeedItem(item)", text)
|
||
|
||
def test_names_pass_through_the_engine(self):
|
||
text = (ROOT / "src" / "lihont" / "two" / "badge" / "AccountNames.java").read_text(encoding="utf-8")
|
||
self.assertIn("PluginEngine.name(uid, name)", text)
|
||
|
||
def test_screens_reach_the_plugin_overlay(self):
|
||
windows = (ROOT / "src" / "lihont" / "two" / "look" / "Windows.java").read_text(encoding="utf-8")
|
||
self.assertIn("PluginScreen.attach(activity)", windows)
|
||
bar = (ROOT / "src" / "lihont" / "two" / "bar" / "BottomBar.java").read_text(encoding="utf-8")
|
||
self.assertIn("PluginScreen.attach(activity)", bar)
|
||
self.assertIn("homeSelected", bar)
|
||
|
||
def test_the_settings_screen_has_the_plugin_section(self):
|
||
text = (ROOT / "src" / "lihont" / "two" / "settings" / "LihonTSettingsActivity.java").read_text(encoding="utf-8")
|
||
self.assertIn('addSection("Плагины"', text)
|
||
self.assertIn("PluginSettings.REQUEST_PICK", text)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|