open-notebook/tests
orihatav d0bbe4a921 fix: handle tiktoken network errors in offline environments (issue #264)
In air-gapped / offline Docker deployments, tiktoken.get_encoding() tries
to download the encoding file from openaipublic.blob.core.windows.net.
When that request fails it raises a URLError / OSError — not an ImportError
— so the previous except clause silently missed it and the crash surfaced in
the UI.

Widened `except ImportError` to `except Exception` so all failures —
"not installed" and "network unreachable" — fall through to the word-count
fallback (words × 1.3). Added a loguru WARNING so operators can see when
the fallback is active.

TIKTOKEN_CACHE_DIR now reads from the environment with a blank-safe
fallback (`or` guard prevents os.makedirs("") on empty env var). This lets
Docker images redirect the cache to a path outside /app/data/ so user-data
volume mounts cannot shadow the pre-baked encoding.

Both images now pre-download the o200k_base encoding during the builder
stage (internet is available at build time) and copy it into the runtime
image at /app/tiktoken-cache. ENV TIKTOKEN_CACHE_DIR=/app/tiktoken-cache
is set in the runtime stage so no network call is ever needed at runtime.

Added test_token_count_network_error_fallback in tests/test_utils.py:
patches tiktoken.get_encoding with a URLError and asserts token_count()
returns a positive int instead of raising.

Fixes #264

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-10 19:45:14 -05:00
..
conftest.py feat: credential-based API key management (#477) (#540) 2026-02-10 08:30:22 -03:00
README.md Initial commit with all features 2024-10-21 14:56:10 -03:00
test_chunking.py feat: credential-based API key management (#477) (#540) 2026-02-10 08:30:22 -03:00
test_domain.py fix: handle empty/whitespace source content without retry loop (#576) 2026-02-14 18:09:07 -03:00
test_embedding.py fix: embedding batch sizing and 413 error classification (1.7.4) 2026-02-18 11:39:47 -03:00
test_graphs.py Feat/localization tests docker (#371) 2026-01-15 13:51:05 -03:00
test_models_api.py Feat/localization tests docker (#371) 2026-01-15 13:51:05 -03:00
test_notes_api.py feat: expose embed command_id in note API responses (#545) 2026-02-14 18:11:23 -03:00
test_url_validation.py feat: credential-based API key management (#477) (#540) 2026-02-10 08:30:22 -03:00
test_utils.py fix: handle tiktoken network errors in offline environments (issue #264) 2026-03-10 19:45:14 -05:00

Coming Soon