omi icon indicating copy to clipboard operation
omi copied to clipboard

Stabilize high priority usage tracking tests without LangChain

Open tianmind-studio opened this issue 5 months ago • 1 comments

Summary

  • Stub the minimal langchain_core and pytz surfaces needed by test_high_priority_usage_tracking.py so the source-level usage tracking tests run in lightweight backend test environments.
  • Provide the current database.users.get_user_language_preference and utils.llm.clients.get_llm test 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 importing utils.llm.usage_tracker

Testing

  • python -m pytest tests\unit\test_high_priority_usage_tracking.py -q -> 19 passed
  • python -m black --line-length 120 --skip-string-normalization tests\unit\test_high_priority_usage_tracking.py --check
  • python -m py_compile tests\unit\test_high_priority_usage_tracking.py
  • git diff --check origin/main...HEAD

tianmind-studio avatar Jun 10 '26 09:06 tianmind-studio

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 a pytz.UTC/pytz.timezone stub are inserted into sys.modules before the real usage_tracker import, making the 19 tests runnable without installing LangChain.
  • get_llm stub (_get_llm_stub): Maps known feature names to the appropriate mock LLM, with a silent mock_llm_mini fallback for unrecognised names.
  • _read_source fix: Passes encoding=\"utf-8\" to Path.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

greptile-apps avatar Jun 10 '26 09:06 greptile-apps