Skip to content

Commit 9ea9e74

Browse files
committed
test(galgame): cover option language priority
1 parent e865e00 commit 9ea9e74

1 file changed

Lines changed: 50 additions & 1 deletion

File tree

tests/unit/test_react_chat_window_static.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import json
22
import re
3+
import shutil
34

45
import pytest
56
from pathlib import Path
7+
from tests.node_harness import run_node_script
68
from tests.static_app_parts import read_js_parts
79

810

@@ -1612,20 +1614,67 @@ def test_galgame_history_excludes_tutorial_guide_messages():
16121614
def test_galgame_option_template_follows_interface_language():
16131615
react_host = APP_REACT_CHAT_WINDOW_PATH.read_text(encoding="utf-8")
16141616

1615-
language_block = react_host.split("function pickAcceptLanguage()", 1)[1].split(
1617+
language_tail = react_host.split("function pickAcceptLanguage()", 1)[1].split(
16161618
"var GALGAME_FETCH_TIMEOUT_MS",
16171619
1,
16181620
)[0]
1621+
language_block = "function pickAcceptLanguage()" + language_tail
16191622
assert "getConversationLanguagePreference" not in language_block
16201623
assert "window.getCurrentLocale" in language_block
16211624
assert "window.i18next.language" in language_block
1625+
assert "navigator.language" in language_block
1626+
assert language_block.index("window.getCurrentLocale") < language_block.index(
1627+
"window.i18next.language"
1628+
)
1629+
assert language_block.index("window.i18next.language") < language_block.index(
1630+
"navigator.language"
1631+
)
16221632

16231633
fetch_block = react_host.split("function fetchGalgameOptionsForLatestTurn()", 1)[1].split(
16241634
"I.handleGalgameModeToggle",
16251635
1,
16261636
)[0]
16271637
assert "language: pickAcceptLanguage()" in fetch_block
16281638

1639+
node_path = shutil.which("node")
1640+
assert node_path, "node is required to verify GalGame language selection"
1641+
script = f"""
1642+
const assert = require('node:assert/strict');
1643+
const vm = require('node:vm');
1644+
const functionSource = {json.dumps(language_block)};
1645+
1646+
function resolve(windowValue, navigatorLanguage) {{
1647+
const context = {{ window: windowValue }};
1648+
if (navigatorLanguage !== undefined) {{
1649+
context.navigator = {{ language: navigatorLanguage }};
1650+
}}
1651+
vm.createContext(context);
1652+
vm.runInContext(functionSource, context);
1653+
return vm.runInContext('pickAcceptLanguage()', context);
1654+
}}
1655+
1656+
assert.equal(resolve({{
1657+
getConversationLanguagePreference: () => 'pt',
1658+
getCurrentLocale: () => 'ja',
1659+
i18next: {{ language: 'en' }}
1660+
}}, 'ko'), 'ja');
1661+
assert.equal(resolve({{
1662+
getCurrentLocale: () => '',
1663+
i18next: {{ language: 'en' }}
1664+
}}, 'ko'), 'en');
1665+
assert.equal(resolve({{ i18next: {{}} }}, 'ko'), 'ko');
1666+
assert.equal(resolve({{ i18next: {{}} }}, undefined), '');
1667+
"""
1668+
result = run_node_script(
1669+
node_path,
1670+
script,
1671+
cwd=Path(__file__).resolve().parents[2],
1672+
capture_output=True,
1673+
text=True,
1674+
check=False,
1675+
)
1676+
assert result.returncode == 0, result.stderr
1677+
16291678

16301679
def test_icebreaker_reset_clears_prompt_by_source_without_session_match():
16311680
react_host = APP_REACT_CHAT_WINDOW_PATH.read_text(encoding="utf-8")

0 commit comments

Comments
 (0)