Files
vikingowl 73ce3ad0df feat(reset-watcher): add config and CronJob entrypoint
Env-driven Config fails fast on missing required values; LOCAL_MODE relaxes
the K8s-specific knobs so the bot can run from a plain docker container
against a bind-mounted oauth-tokens dir. Entrypoint wires together token
refresh, usage poll, alert computation, optional armed cold-start message,
E2EE Matrix send, and atomic state persistence. Adds a pytest -m live
smoke test that hits the real /api/oauth/usage endpoint, skipped by
default so CI stays offline.
2026-05-18 17:23:12 +02:00

75 lines
2.4 KiB
Python

import pytest
from claude_matrix_bot.config import load_config
def _write_matrix_secret(dir_path) -> None:
(dir_path / "homeserver_url").write_text("https://matrix.example.org")
(dir_path / "user_id").write_text("@bot:example.org")
(dir_path / "access_token").write_text("syt_xxx")
(dir_path / "device_id").write_text("DEVICE")
def _base_env(monkeypatch, matrix_dir) -> None:
monkeypatch.setenv("MATRIX_SECRET_DIR", str(matrix_dir))
monkeypatch.setenv("MATRIX_ROOM_ID", "!room:example.org")
monkeypatch.setenv("OAUTH_TOKENS_DIR", "/tmp/whatever-not-read-by-config")
def test_local_mode_relaxes_k8s_requirements(tmp_path, monkeypatch) -> None:
matrix_dir = tmp_path / "matrix"
matrix_dir.mkdir()
_write_matrix_secret(matrix_dir)
_base_env(monkeypatch, matrix_dir)
monkeypatch.setenv("LOCAL_MODE", "1")
monkeypatch.delenv("K8S_OAUTH_SECRET_NAME", raising=False)
monkeypatch.delenv("K8S_NAMESPACE", raising=False)
cfg = load_config()
assert cfg.local_mode is True
assert cfg.k8s_oauth_secret_name == ""
# Namespace may be empty (SA file absent locally) — that's the point of local mode.
def test_local_mode_namespace_env_override(tmp_path, monkeypatch) -> None:
matrix_dir = tmp_path / "matrix"
matrix_dir.mkdir()
_write_matrix_secret(matrix_dir)
_base_env(monkeypatch, matrix_dir)
monkeypatch.setenv("LOCAL_MODE", "1")
monkeypatch.setenv("K8S_NAMESPACE", "tenant-2")
cfg = load_config()
assert cfg.k8s_namespace == "tenant-2"
def test_non_local_mode_requires_secret_name(tmp_path, monkeypatch) -> None:
matrix_dir = tmp_path / "matrix"
matrix_dir.mkdir()
_write_matrix_secret(matrix_dir)
_base_env(monkeypatch, matrix_dir)
monkeypatch.delenv("LOCAL_MODE", raising=False)
monkeypatch.delenv("K8S_OAUTH_SECRET_NAME", raising=False)
monkeypatch.setenv("K8S_NAMESPACE", "tenant-2") # avoid SA file read
with pytest.raises(RuntimeError, match="K8S_OAUTH_SECRET_NAME"):
load_config()
def test_room_id_always_required(tmp_path, monkeypatch) -> None:
matrix_dir = tmp_path / "matrix"
matrix_dir.mkdir()
_write_matrix_secret(matrix_dir)
monkeypatch.setenv("MATRIX_SECRET_DIR", str(matrix_dir))
monkeypatch.delenv("MATRIX_ROOM_ID", raising=False)
monkeypatch.setenv("LOCAL_MODE", "1")
with pytest.raises(RuntimeError, match="MATRIX_ROOM_ID"):
load_config()