File size: 7,183 Bytes
f1e06e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import importlib.util
import sys
import types
import unittest
from pathlib import Path
from unittest.mock import Mock, patch


class FakeRequestException(Exception):
    def __init__(self, message="", response=None):
        super().__init__(message)
        self.response = response


class FakeTimeout(FakeRequestException):
    pass


def install_dependency_stubs():
    requests_module = types.ModuleType("requests")
    exceptions = types.SimpleNamespace(
        RequestException=FakeRequestException,
        Timeout=FakeTimeout,
    )
    requests_module.exceptions = exceptions
    requests_module.get = Mock()
    requests_module.post = Mock()
    sys.modules["requests"] = requests_module

    torch_module = types.ModuleType("torch")
    torch_module.float32 = "float32"
    torch_module.Tensor = type("Tensor", (), {})
    torch_module.zeros = Mock(return_value="placeholder-image")
    sys.modules["torch"] = torch_module

    tiktoken_module = types.ModuleType("tiktoken")
    tiktoken_module.get_encoding = Mock()
    sys.modules["tiktoken"] = tiktoken_module

    pil_module = types.ModuleType("PIL")
    image_module = types.ModuleType("PIL.Image")
    image_module.open = Mock()
    image_module.fromarray = Mock()
    pil_module.Image = image_module
    sys.modules["PIL"] = pil_module
    sys.modules["PIL.Image"] = image_module

    sys.modules.setdefault("numpy", types.ModuleType("numpy"))


def load_node_module():
    install_dependency_stubs()
    root = Path(__file__).resolve().parents[1]
    package_name = "comfy_openrouter_node_test"

    package = types.ModuleType(package_name)
    package.__path__ = [str(root)]
    sys.modules[package_name] = package

    spec = importlib.util.spec_from_file_location(
        f"{package_name}.node",
        root / "node.py",
        submodule_search_locations=[str(root)],
    )
    module = importlib.util.module_from_spec(spec)
    sys.modules[spec.name] = module
    spec.loader.exec_module(module)
    return module


class RequestTimeoutTests(unittest.TestCase):
    def setUp(self):
        self.node_module = load_node_module()
        self.node = self.node_module.OpenRouterNode()
        self.node.fetch_credits = Mock(return_value="Remaining: $1.000")
        self.node.count_tokens = Mock(return_value=1)

    def call_generate_response(self, request_timeout=120, reasoning_effort="auto"):
        return self.node.generate_response(
            api_key="test-key",
            system_prompt="system",
            user_message_box="hello",
            model="openai/gpt-4o",
            web_search=False,
            cheapest=False,
            fastest=False,
            temperature=1.0,
            pdf_engine="auto",
            chat_mode=False,
            request_timeout=request_timeout,
            reasoning_effort=reasoning_effort,
        )

    def test_main_openrouter_request_uses_configured_timeout(self):
        response = Mock()
        response.raise_for_status = Mock()
        response.json.return_value = {
            "choices": [{"message": {"content": "done"}}],
            "usage": {"prompt_tokens": 2, "completion_tokens": 3},
        }
        self.node_module.requests.post.return_value = response

        result = self.call_generate_response(request_timeout=45)

        self.assertEqual(result[0], "done")
        self.node_module.requests.post.assert_called_once()
        self.assertEqual(self.node_module.requests.post.call_args.kwargs["timeout"], 45)

    def test_default_reasoning_effort_omits_reasoning_override(self):
        response = Mock()
        response.raise_for_status = Mock()
        response.json.return_value = {
            "choices": [{"message": {"content": "done"}}],
            "usage": {"prompt_tokens": 2, "completion_tokens": 3},
        }
        self.node_module.requests.post.return_value = response

        result = self.call_generate_response()

        self.assertEqual(result[0], "done")
        payload = self.node_module.requests.post.call_args.kwargs["json"]
        self.assertNotIn("reasoning", payload)

    def test_explicit_reasoning_effort_is_sent(self):
        response = Mock()
        response.raise_for_status = Mock()
        response.json.return_value = {
            "choices": [{"message": {"content": "done"}}],
            "usage": {"prompt_tokens": 2, "completion_tokens": 3},
        }
        self.node_module.requests.post.return_value = response

        result = self.call_generate_response(reasoning_effort="high")

        self.assertEqual(result[0], "done")
        payload = self.node_module.requests.post.call_args.kwargs["json"]
        self.assertEqual(payload["reasoning"], {"effort": "high"})

    def test_invalid_reasoning_effort_falls_back_to_auto(self):
        response = Mock()
        response.raise_for_status = Mock()
        response.json.return_value = {
            "choices": [{"message": {"content": "done"}}],
            "usage": {"prompt_tokens": 2, "completion_tokens": 3},
        }
        self.node_module.requests.post.return_value = response

        result = self.call_generate_response(reasoning_effort="unsupported")

        self.assertEqual(result[0], "done")
        payload = self.node_module.requests.post.call_args.kwargs["json"]
        self.assertNotIn("reasoning", payload)

    def test_is_changed_includes_reasoning_effort(self):
        base_args = dict(
            api_key="test-key",
            system_prompt="system",
            user_message_box="hello",
            model="openai/gpt-4o",
            web_search=False,
            cheapest=False,
            fastest=False,
            temperature=1.0,
            pdf_engine="auto",
            chat_mode=False,
            request_timeout=120,
        )

        low_key = self.node_module.OpenRouterNode.IS_CHANGED(**base_args, reasoning_effort="low")
        high_key = self.node_module.OpenRouterNode.IS_CHANGED(**base_args, reasoning_effort="high")

        self.assertNotEqual(low_key, high_key)

    def test_timeout_exception_returns_clear_error(self):
        self.node_module.requests.post.side_effect = FakeTimeout("request timed out")

        result = self.call_generate_response(request_timeout=2)

        self.assertIn("API Request Error", result[0])
        self.assertIn("request timed out", result[0])
        self.assertEqual(result[2], "Stats N/A due to error")
        self.assertEqual(result[3], "Credits N/A due to error")

    def test_fetch_credits_uses_configured_timeout(self):
        self.node.fetch_credits = self.node_module.OpenRouterNode().fetch_credits
        response = Mock()
        response.raise_for_status = Mock()
        response.json.return_value = {
            "data": {
                "total_credits": 10.0,
                "total_usage": 3.25,
            }
        }
        self.node_module.requests.get.return_value = response

        credits = self.node.fetch_credits("test-key", timeout=45)

        self.assertEqual(credits, "Remaining: $6.750")
        self.node_module.requests.get.assert_called_once()
        self.assertEqual(self.node_module.requests.get.call_args.kwargs["timeout"], 45)


if __name__ == "__main__":
    unittest.main()