@@ -156,65 +156,55 @@ def test_language_rejects_invalid(self):
156156 assert r .returncode != 0 , "Invalid language should cause an error"
157157
158158 def test_language_overrides_constant (self ):
159- """Verify that --language actually changes the NOTE_LANGUAGE global."""
160- # Run a small Python snippet that imports note_generation and checks
161- script = textwrap .dedent ("""\
162- import sys, os
163- sys.argv = ['note_generation.py', '--course', '99999', '--language', 'zh']
164- # Patch argparse to not exit on --course 99999
165- import note_generation
166- # At module level, NOTE_LANGUAGE is 'en'
167- assert note_generation.NOTE_LANGUAGE == 'en', (
168- f"Default should be 'en', got {note_generation.NOTE_LANGUAGE!r}")
169- # Simulate main() parsing
170- import argparse
171- parser = argparse.ArgumentParser()
172- parser.add_argument('--course')
173- parser.add_argument('--language', choices=['en', 'zh'], default=None)
174- args, _ = parser.parse_known_args()
175- if args.language:
176- note_generation.NOTE_LANGUAGE = args.language
177- assert note_generation.NOTE_LANGUAGE == 'zh', (
178- f"After --language zh, should be 'zh', got {note_generation.NOTE_LANGUAGE!r}")
179- print('PASS')
180- """ )
181- r = subprocess .run (
182- [PYTHON , "-c" , script ],
183- capture_output = True , text = True , timeout = 15 ,
184- cwd = str (PROJECT_DIR ),
159+ """Verify that --language zh is confirmed in subprocess output."""
160+ r = _run ("note_generation.py" , "--course" , "99999" ,
161+ "--language" , "zh" , "--course-name" , "Test" )
162+ assert "Note language: zh" in r .stdout , (
163+ f"Language confirmation not found in output:\n { r .stdout } \n { r .stderr } "
185164 )
186- assert "PASS" in r .stdout , (
187- f"Language override test failed:\n { r .stdout } \n { r .stderr } "
165+
166+ def test_language_en_not_printed_when_default (self ):
167+ """When --language is not passed, no language line should be printed."""
168+ r = _run ("note_generation.py" , "--course" , "99999" , "--course-name" , "Test" )
169+ assert "Note language:" not in r .stdout , (
170+ f"Language line should not appear without --language flag:\n { r .stdout } "
188171 )
189172
190173 def test_prompt_selection_respects_language (self ):
191174 """Verify _P() returns Chinese prompts when NOTE_LANGUAGE is 'zh'."""
192- script = textwrap .dedent ("""\
193- import sys
194- sys.argv = ['test']
195- import note_generation
196- # Default: English
175+ import note_generation
176+ # Default: English
177+ original = note_generation .NOTE_LANGUAGE
178+ try :
197179 note_generation .NOTE_LANGUAGE = 'en'
198180 en_sys = note_generation ._P ('system' )
199- assert 'diagram' in en_sys.lower() or ' note' in en_sys.lower(), (
181+ assert 'note' in en_sys .lower (), (
200182 'English system prompt should contain English text' )
201- # Switch to Chinese
183+
202184 note_generation .NOTE_LANGUAGE = 'zh'
203185 zh_sys = note_generation ._P ('system' )
204- # Chinese prompt should contain Chinese characters
205- import re
206- cjk = re.compile(r'[\\ u4e00-\\ u9fff]')
207- assert cjk.search(zh_sys), 'Chinese system prompt should contain CJK characters'
186+ assert _CJK_RE .search (zh_sys ), (
187+ 'Chinese system prompt should contain CJK characters' )
208188 assert en_sys != zh_sys , 'English and Chinese prompts should differ'
209- print('PASS')
210- """ )
211- r = subprocess .run (
212- [PYTHON , "-c" , script ],
213- capture_output = True , text = True , timeout = 15 ,
214- cwd = str (PROJECT_DIR ),
189+ finally :
190+ note_generation .NOTE_LANGUAGE = original
191+
192+ def test_global_declaration_at_function_top (self ):
193+ """Ensure 'global NOTE_LANGUAGE' is at the top of main(), not inside if."""
194+ import inspect , ast
195+ import note_generation
196+ src = inspect .getsource (note_generation .main )
197+ tree = ast .parse (textwrap .dedent (src ))
198+ func = tree .body [0 ]
199+ # First statement in function body should be (or contain) the global
200+ first_stmts = func .body [:3 ] # check first few statements
201+ has_global = any (
202+ isinstance (s , ast .Global ) and 'NOTE_LANGUAGE' in s .names
203+ for s in first_stmts
215204 )
216- assert "PASS" in r .stdout , (
217- f"Prompt selection test failed:\n { r .stdout } \n { r .stderr } "
205+ assert has_global , (
206+ "'global NOTE_LANGUAGE' should be at the top of main(), "
207+ "not inside a conditional block"
218208 )
219209
220210
0 commit comments