From db07e9f93ac792f724e93ebea3d2389442ba194c Mon Sep 17 00:00:00 2001 From: Safi Date: Fri, 10 Apr 2026 15:48:54 +0100 Subject: [PATCH] Fix CI: mock lazy anthropic import via sys.modules instead of module attribute Co-Authored-By: Claude Sonnet 4.6 --- tests/test_transcribe.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/test_transcribe.py b/tests/test_transcribe.py index c1a002b2..0157b7ea 100644 --- a/tests/test_transcribe.py +++ b/tests/test_transcribe.py @@ -6,6 +6,8 @@ import os from pathlib import Path from unittest.mock import MagicMock, patch +import sys + import pytest from graphify.transcribe import ( @@ -52,10 +54,12 @@ def test_build_whisper_prompt_llm_success(): fake_response = MagicMock() fake_response.content = [MagicMock(text="Machine learning and deep learning research")] + mock_anthropic = MagicMock() + mock_anthropic.Anthropic.return_value.messages.create.return_value = fake_response + with patch.dict(os.environ, {}, clear=False): os.environ.pop("GRAPHIFY_WHISPER_PROMPT", None) - with patch("anthropic.Anthropic") as MockClient: - MockClient.return_value.messages.create.return_value = fake_response + with patch.dict(sys.modules, {"anthropic": mock_anthropic}): prompt = build_whisper_prompt(god_nodes) assert "Machine learning" in prompt @@ -66,9 +70,12 @@ def test_build_whisper_prompt_llm_failure_fallback(): """If LLM call raises, falls back to topic-based prompt.""" god_nodes = [{"label": "kubernetes"}, {"label": "docker"}, {"label": "helm"}] + mock_anthropic = MagicMock() + mock_anthropic.Anthropic.return_value.messages.create.side_effect = Exception("API error") + with patch.dict(os.environ, {}, clear=False): os.environ.pop("GRAPHIFY_WHISPER_PROMPT", None) - with patch("anthropic.Anthropic", side_effect=Exception("API error")): + with patch.dict(sys.modules, {"anthropic": mock_anthropic}): prompt = build_whisper_prompt(god_nodes) assert "kubernetes" in prompt.lower() or "docker" in prompt.lower()