test_wxgf.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. """Unit tests for WXGF decoding with ffmpeg."""
  3. import os
  4. import sys
  5. import shutil
  6. import pytest
  7. from io import BytesIO
  8. from PIL import Image
  9. # Add parent directory to path for imports
  10. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  11. from wechat.wxgf import (
  12. decode_wxgf_with_ffmpeg,
  13. is_wxgf_buffer,
  14. )
  15. # Path to the test WXGF file
  16. TEST_WXGF_FILE = os.path.join(
  17. os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
  18. "WXGFDecoder", "app", "src", "main", "res", "raw", "test_wxgf.jpg"
  19. )
  20. # Fixtures
  21. @pytest.fixture(scope="module")
  22. def has_ffmpeg():
  23. """Check if ffmpeg and ffprobe are available."""
  24. return shutil.which("ffmpeg") is not None and shutil.which("ffprobe") is not None
  25. @pytest.fixture(scope="module")
  26. def has_test_file():
  27. """Check if test WXGF file exists."""
  28. return os.path.exists(TEST_WXGF_FILE)
  29. @pytest.fixture(scope="module")
  30. def test_wxgf_data(has_test_file):
  31. """Load test WXGF file data."""
  32. if not has_test_file:
  33. pytest.skip("Test WXGF file not available")
  34. with open(TEST_WXGF_FILE, 'rb') as f:
  35. return f.read()
  36. def test_is_wxgf_buffer():
  37. """Test is_wxgf_buffer function."""
  38. assert is_wxgf_buffer(b'wxgf\x00\x00\x00') is True
  39. assert is_wxgf_buffer(b'\x89PNG\r\n\x1a\n') is False
  40. assert is_wxgf_buffer(b'') is False
  41. assert is_wxgf_buffer(b'abc') is False
  42. @pytest.mark.skipif(
  43. not shutil.which("ffmpeg") or not shutil.which("ffprobe"),
  44. reason="ffmpeg/ffprobe not available"
  45. )
  46. def test_decode_wxgf_with_ffmpeg_success(test_wxgf_data):
  47. """Test successful WXGF decoding with ffmpeg."""
  48. data = test_wxgf_data
  49. # Decode the WXGF file
  50. result = decode_wxgf_with_ffmpeg(data)
  51. # Should return PNG bytes (starts with PNG signature)
  52. assert result is not None, "Decoding should succeed"
  53. assert isinstance(result, bytes)
  54. assert result.startswith(b'\x89PNG'), "Decoded output should be a PNG file"
  55. # PNG should have reasonable size
  56. assert len(result) > 100, "PNG should have reasonable size"
  57. img = Image.open(BytesIO(result))
  58. assert img.size == (1920, 1080), f"Expected image size (1920, 1080), got {img.size}"
  59. @pytest.mark.skipif(
  60. not shutil.which("ffmpeg") or not shutil.which("ffprobe"),
  61. reason="ffmpeg/ffprobe not available"
  62. )
  63. def test_decode_wxgf_with_ffmpeg_invalid_data():
  64. """Test ffmpeg decoding with invalid data."""
  65. # Not a WXGF file
  66. result = decode_wxgf_with_ffmpeg(b'\x89PNG\r\n\x1a\n')
  67. assert result is None
  68. # WXGF header but no valid HEVC data
  69. result = decode_wxgf_with_ffmpeg(b'wxgf\x00\x00\x00\x01\x00\x00')
  70. assert result is None
  71. def test_decode_wxgf_without_ffmpeg(test_wxgf_data):
  72. """Test decoding when ffmpeg is not available."""
  73. data = test_wxgf_data
  74. # Use non-existent ffmpeg paths
  75. result = decode_wxgf_with_ffmpeg(data, ffmpeg="nonexistent_ffmpeg", ffprobe="nonexistent_ffprobe")
  76. assert result is None