Stabilize high priority usage tracking tests without LangChain
Summary
- Stub the minimal
langchain_coreandpytzsurfaces needed bytest_high_priority_usage_tracking.pyso the source-level usage tracking tests run in lightweight backend test environments. - Provide the current
database.users.get_user_language_preferenceandutils.llm.clients.get_llmtest stubs expected by imported modules. - Read inspected source files as UTF-8 so Windows non-UTF-8 locales do not fail source checks.
Windows reproduction
Before this change on this Windows test environment:
python -m pytest tests\unit\test_high_priority_usage_tracking.py -q-> collection error- root cause:
ModuleNotFoundError: No module named 'langchain_core'while importingutils.llm.usage_tracker
Testing
python -m pytest tests\unit\test_high_priority_usage_tracking.py -q->19 passedpython -m black --line-length 120 --skip-string-normalization tests\unit\test_high_priority_usage_tracking.py --checkpython -m py_compile tests\unit\test_high_priority_usage_tracking.pygit diff --check origin/main...HEAD
Greptile Summary
This PR fixes test collection failures on Windows (and other environments without LangChain installed) by stubbing langchain_core submodules and pytz before importing utils.llm.usage_tracker, and adds the missing get_user_language_preference and get_llm stubs needed by the imported source modules. A UTF-8 encoding argument is also added to _read_source to prevent failures on non-UTF-8 Windows locales.
- LangChain + pytz stubs: Minimal stub classes (
BaseCallbackHandler,LLMResult,PydanticOutputParser,ChatPromptTemplate) and apytz.UTC/pytz.timezonestub are inserted intosys.modulesbefore the realusage_trackerimport, making the 19 tests runnable without installing LangChain. get_llmstub (_get_llm_stub): Maps known feature names to the appropriate mock LLM, with a silentmock_llm_minifallback for unrecognised names._read_sourcefix: Passesencoding=\"utf-8\"toPath.read_text()so source-level string checks don't fail on Windows systems with a non-UTF-8 default locale.
Confidence Score: 3/5
Safe to merge in environments where LangChain is not installed, but the stub setup can silently replace real LangChain classes in the same pytest session when the package is present.
The stub attribute assignments (lines 72–75) are unconditional — they run whether _stub_module returned a freshly-created empty module or the real installed module. In a full CI environment with LangChain installed, these lines overwrite BaseCallbackHandler, LLMResult, PydanticOutputParser, and ChatPromptTemplate on the live module objects, which can silently corrupt any other test that imports those symbols afterward. The rest of the changes (UTF-8 fix, missing mock attributes) are straightforward and correct.
backend/tests/unit/test_high_priority_usage_tracking.py — the LangChain stub setup block (lines 34–75) needs a guard to skip attribute assignments when the real package is already loaded.
Important Files Changed
| Filename | Overview |
|---|---|
| backend/tests/unit/test_high_priority_usage_tracking.py | Adds LangChain/pytz stubs and missing database/LLM client stubs so tests run without the heavy dependencies; the stub attribute assignments can corrupt real LangChain module objects if the package is installed in the same pytest session. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["pytest collects test file"] --> B{"langchain_core\nin sys.modules?"}
B -- "No (target env)" --> C["_stub_module creates\nfresh ModuleType objects"]
C --> D["Set stub classes on\nnewly-created modules\n(safe)"]
B -- "Yes (LangChain installed)" --> E["_stub_module returns\nreal module objects"]
E --> F["Stub classes assigned\nonto real modules\n⚠️ overwrites real classes"]
D --> G["usage_tracker imports\nBaseCallbackHandler, LLMResult\nfrom stub modules"]
F --> G
G --> H["Test body runs:\ncontext-capture functions\nreplace mock_llm_mini.invoke"]
H --> I["Production function called\n(suggest_goal, get_goal_advice, ...)"]
I --> J["track_usage context manager\nsets _usage_context ContextVar"]
J --> K["LLM mock invoke called;\ntest asserts captured context"]
Reviews (1): Last reviewed commit: "Stabilize high priority usage tracking t..." | Re-trigger Greptile