#!/usr/bin/env python3
import json
import os
import shutil
import unittest
import xml.etree.ElementTree as ET

from selfdrive.ui.update_translations import TRANSLATIONS_DIR, LANGUAGES_FILE, update_translations

TMP_TRANSLATIONS_DIR = os.path.join(TRANSLATIONS_DIR, "tmp")


class TestTranslations(unittest.TestCase):
  @classmethod
  def setUpClass(cls):
    with open(LANGUAGES_FILE, "r") as f:
      cls.translation_files = json.load(f)

    # Set up temp directory
    shutil.copytree(TRANSLATIONS_DIR, TMP_TRANSLATIONS_DIR, dirs_exist_ok=True)

  @classmethod
  def tearDownClass(cls):
    shutil.rmtree(TMP_TRANSLATIONS_DIR, ignore_errors=True)

  @staticmethod
  def _read_translation_file(path, file):
    tr_file = os.path.join(path, f"{file}.ts")
    with open(tr_file, "rb") as f:
      # fix relative path depth
      return f.read().replace(b"filename=\"../../", b"filename=\"../")

  def test_missing_translation_files(self):
    for name, file in self.translation_files.items():
      with self.subTest(name=name, file=file):
        self.assertTrue(os.path.exists(os.path.join(TRANSLATIONS_DIR, f"{file}.ts")),
                        f"{name} has no XML translation file, run selfdrive/ui/update_translations.py")

  def test_translations_updated(self):
    update_translations(plural_only=["main_en"], translations_dir=TMP_TRANSLATIONS_DIR)

    for name, file in self.translation_files.items():
      with self.subTest(name=name, file=file):
        # caught by test_missing_translation_files
        if not os.path.exists(os.path.join(TRANSLATIONS_DIR, f"{file}.ts")):
          self.skipTest(f"{name} missing translation file")

        cur_translations = self._read_translation_file(TRANSLATIONS_DIR, file)
        new_translations = self._read_translation_file(TMP_TRANSLATIONS_DIR, file)
        self.assertEqual(cur_translations, new_translations,
                         f"{file} ({name}) XML translation file out of date. Run selfdrive/ui/update_translations.py to update the translation files")

  @unittest.skip("Only test unfinished translations before going to release")
  def test_unfinished_translations(self):
    for name, file in self.translation_files.items():
      with self.subTest(name=name, file=file):
        cur_translations = self._read_translation_file(TRANSLATIONS_DIR, file)
        self.assertTrue(b"<translation type=\"unfinished\">" not in cur_translations,
                        f"{file} ({name}) translation file has unfinished translations. Finish translations or mark them as completed in Qt Linguist")

  def test_vanished_translations(self):
    for name, file in self.translation_files.items():
      with self.subTest(name=name, file=file):
        cur_translations = self._read_translation_file(TRANSLATIONS_DIR, file)
        self.assertTrue(b"<translation type=\"vanished\">" not in cur_translations,
                        f"{file} ({name}) translation file has obsolete translations. Run selfdrive/ui/update_translations.py --vanish to remove them")

  def test_plural_translations(self):
    for name, file in self.translation_files.items():
      with self.subTest(name=name, file=file):
        tr_xml = ET.parse(os.path.join(TRANSLATIONS_DIR, f"{file}.ts"))

        for context in tr_xml.getroot():
          for message in context.iterfind("message"):
            if message.get("numerus") == "yes":
              translation = message.find("translation")
              numerusform = translation.findall("numerusform")

              # Do not assert finished translations
              if translation.get("type") == "unfinished":
                continue

              self.assertNotIn(None, [x.text for x in numerusform], "Ensure all plural translation forms are completed.")


if __name__ == "__main__":
  unittest.main()

@@ -38,16 +38,26 @@
 port os%0A
+import re%0A
 import s
@@ -3052,32 +3052,213 @@
 slations(self):%0A
+    %22%22%22%0A      Tests:%0A      - that any numerus (plural) translations marked %22finished%22 have all plural forms non-empty%0A      - that the correct format specifier is used (%25n)%0A    %22%22%22%0A
     for name, fi
@@ -3287,32 +3287,32 @@
 _files.items():%0A
-
       with self.
@@ -3641,16 +3641,33 @@
 usform =
+ %5Bt.text for t in
  transla
@@ -3693,16 +3693,17 @@
 usform%22)
+%5D
 %0A%0A      
@@ -3872,25 +3872,8 @@
 one,
- %5Bx.text for x in
  num
@@ -3880,17 +3880,16 @@
 erusform
-%5D
 , %22Ensur
@@ -3936,16 +3936,214 @@
 eted.%22)%0A
+              self.assertTrue(all(%5Bre.search(%22%25%5B0-9%5D+%22, t) is None for t in numerusform%5D),%0A                              %22Plural translations must use %25n, not %251, %252, etc.: %7B%7D%22.format(numerusform))%0A
 %0A%0Aif __n

add plural translation test (#25491)