Files
live-app-translator/tests/test_ui_smoke.py
EJClaw 8b36b244ff feat: LiveSub 2차 — 자막 on/off, 모니터·위치 선택, GPU 저부하, 게임 용어집
이름을 Hearo → LiveSub 로 변경. 말장난보다 하는 일이 바로 보이는 쪽이 낫다.

자막 on/off
- 전역 단축키 Ctrl+Alt+S (자막) / Ctrl+Alt+D (번역) — Windows RegisterHotKey +
  네이티브 이벤트 필터라 게임 창이 떠 있어도 동작. 다른 OS 에서는 no-op
- 단축키·체크박스·우클릭 메뉴가 모두 같은 경로를 타도록 통합

모니터 선택 + 디스코드식 배치
- placement.py: Qt 비의존 배치 계산 (모니터 목록 → 9분할 좌표)
- AnchorGrid 위젯으로 3x3 위치 선택, 드래그하면 자유 배치로 전환
- 모니터 구성이 바뀌어도 자막이 화면 밖으로 사라지지 않도록 클램프

GPU 저부하 모드 (기본 켜짐)
- 연산 정밀도 int8 강등, VRAM 상한 35%, 추론 후 60ms 양보,
  300초 무음 시 모델 언로드, 중간 결과 비활성
- 기본 티어를 균형(6GB) → 신속(4GB) 으로 하향
- 8GB GPU 기준 자막 2.8GB / 게임 5.2GB

게임 용어집 번들 (304개)
- 롤 74 · 발로란트 59 · 오버워치2 49 · FPS공통 46 · 마크 38 · 방송 38
- 체크박스로 켜고 끄며, 사용자가 직접 등록한 항목이 항상 우선

검증: pytest 91개 통과 (배치 14 + 팩 12 + UI 6 신규), ruff clean

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-09-21 17:49:26 +09:00

181 lines
6.2 KiB
Python

"""UI가 실제로 만들어지고 자막이 그려지는지 확인한다 (offscreen 렌더링)."""
from __future__ import annotations
import os
import pytest
pytest.importorskip("PySide6")
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
from PySide6.QtGui import QImage, QPainter # noqa: E402
from PySide6.QtWidgets import QApplication # noqa: E402
from livesub.config import AppConfig # noqa: E402
from livesub.core.events import EngineState, EngineStatus, TranslationLine # noqa: E402
from livesub.models import ModelManager # noqa: E402
from livesub.ui.main_window import MainWindow # noqa: E402
from livesub.ui.overlay import paint_subtitle # noqa: E402
from livesub.ui.placement import ANCHOR_CUSTOM, list_screens, resolve_rect # noqa: E402
@pytest.fixture(scope="session")
def qapp():
app = QApplication.instance() or QApplication([])
yield app
@pytest.fixture
def window(qapp, tmp_path, monkeypatch):
monkeypatch.setattr("livesub.constants.user_data_dir", lambda: tmp_path)
config = AppConfig()
config.models.preload_on_start = False
config.glossary.path = str(tmp_path / "glossary.json")
win = MainWindow(config)
yield win
win.engine.shutdown()
win.overlay.close()
win.close()
def test_main_window_builds_all_pages(window):
assert window.stack.count() == 5
def test_navigation_switches_pages(window):
for index in range(5):
window._goto_page(index)
assert window.stack.currentIndex() == index
def test_status_updates_toggle_button(window):
window._on_status(EngineStatus(EngineState.RUNNING, "듣는 중"))
assert window.home_page.toggle_button.text() == "번역 정지"
window._on_status(EngineStatus(EngineState.IDLE, "대기 중"))
assert window.home_page.toggle_button.text() == "번역 시작"
def test_final_line_reaches_overlay_and_log(window):
window.config.log_transcripts = False
window._on_line(
TranslationLine("hello", "안녕하세요", "en", "ko", is_final=True, latency_s=1.2)
)
assert window.overlay._final_text == "안녕하세요"
assert window.home_page.log_list.count() == 1
def test_partial_line_is_not_logged(window):
window.config.log_transcripts = False
window._on_line(TranslationLine("hel", "", "en", "ko", is_final=False))
assert window.home_page.log_list.count() == 0
def test_tier_selection_updates_config(window):
window.models_page._on_tier_selected(window.models_page._cards[3].tier)
assert window.config.models.tier == "precision"
def test_subtitle_style_edit_propagates(window):
window.subtitle_page.size_spin.setValue(60)
assert window.config.subtitle.font_size == 60
def test_subtitle_toggle_turns_overlay_on_and_off(window):
"""자막 on/off — 단축키와 체크박스가 같은 경로를 탄다."""
window.config.overlay.visible = True
window.overlay.set_subtitle_visible(True)
window.toggle_subtitle()
assert window.config.overlay.visible is False
assert window.overlay.isVisible() is False
# 체크박스가 따라와야 한다 (시그널 연결 확인)
assert window.subtitle_page.overlay_check.isChecked() is False
window.toggle_subtitle()
assert window.config.overlay.visible is True
assert window.subtitle_page.overlay_check.isChecked() is True
def test_turning_subtitle_off_clears_leftover_text(window):
window.config.log_transcripts = False
window.overlay.set_subtitle_visible(True)
window._on_line(TranslationLine("hello", "안녕", "en", "ko", is_final=True))
assert window.overlay._final_text
window.toggle_subtitle()
assert window.overlay._final_text == ""
def test_checkbox_and_hotkey_do_not_fight_each_other(window):
"""체크박스 → 오버레이 → 체크박스 되먹임이 무한루프가 되면 안 된다."""
window.config.overlay.visible = True
window.overlay.set_subtitle_visible(True)
window.subtitle_page.overlay_check.setChecked(False)
assert window.config.overlay.visible is False
assert window.overlay.isVisible() is False
def test_anchor_selection_moves_the_overlay(window):
window.subtitle_page._on_anchor_selected("top-left")
assert window.config.overlay.anchor == "top-left"
screens = list_screens()
if screens: # offscreen 플랫폼에도 가상 화면이 하나 있다
expected = resolve_rect(
screens[0], "top-left",
window.config.overlay.width, window.config.overlay.height,
window.config.overlay.margin,
)
assert (window.config.overlay.x, window.config.overlay.y) == expected[:2]
def test_dragging_switches_to_custom_placement(window):
window.subtitle_page._on_anchor_selected("bottom-center")
assert window.config.overlay.anchor == "bottom-center"
window.overlay.move(123, 456)
window.overlay._drag_from = window.overlay.pos()
window.overlay.mouseReleaseEvent(None)
assert window.config.overlay.anchor == ANCHOR_CUSTOM
def test_glossary_packs_reach_the_engine(window):
window.config.glossary.enabled_packs = ["lol"]
window._on_glossary_changed()
_, repl = window.engine.glossary.protect("the Nexus", "ko")
assert repl == ["넥서스"]
def test_game_mode_lowers_precision(window):
window.config.performance.game_mode = True
manager = ModelManager(window.config.models, low_power=True)
assert "int8" in manager.effective_asr_spec().compute_type
assert manager.effective_asr_spec().beam_size == 1
normal = ModelManager(window.config.models, low_power=False)
assert normal.effective_asr_spec() == normal.tier.asr
def test_paint_subtitle_actually_draws_pixels(qapp):
"""자막 글씨가 투명 이미지 위에 실제로 그려지는지 (빈 화면이면 실패).
배경 상자가 픽셀을 채워버리면 글씨 유무를 알 수 없으므로 배경은 끄고 잰다.
"""
st = AppConfig().subtitle
st.background_opacity = 0
image = QImage(900, 200, QImage.Format.Format_ARGB32)
image.fill(0)
painter = QPainter(image)
paint_subtitle(painter, image.rect(), st, "안녕하세요 반갑습니다", "Hello there")
painter.end()
non_transparent = sum(
1
for y in range(0, image.height(), 4)
for x in range(0, image.width(), 4)
if image.pixelColor(x, y).alpha() > 0
)
assert non_transparent > 100