Files
DemonGatanjieu-Anomalous_Mo…/tests/test_network_check.py
T
DemonGatanjieuandClaude Opus 5.5 103ee6a25d First-run setup on Home: check Civitai, optional key, scan everything, then the tour
New users did not know to scan first. Until anything is scanned, Home shows a card with four
steps and "Set up for me": GET /anomalous/civitai_check (api/network_check.py, local requests
only) says whether Civitai answers and whether a set key is refused; when it does not, the card
offers Retry (with a proxy) or an offline scan. The scan never renames files or refetches,
whatever the scan page was set to; the card follows its progress. Never a popup; "Later" hides it
per browser and Home's "New here?" line brings it back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 22:26:13 +08:00

54 lines
2.0 KiB
Python

"""The first-run setup's question: does this computer reach Civitai, and is a set key refused?"""
import io
import sys
import unittest
import urllib.error
from pathlib import Path
from unittest import mock
PLUGIN = Path(__file__).resolve().parents[1]
sys.path[:0] = [str(PLUGIN), str(PLUGIN.parents[1])]
from api import local_guard, network_check
class _Answer(io.BytesIO):
def __enter__(self):
return self
def __exit__(self, *exc):
return False
def _http_error(code):
return urllib.error.HTTPError(network_check.PROBE_URL, code, "x", {}, io.BytesIO(b""))
class CivitaiCheckTests(unittest.TestCase):
def check(self, answer, key=None):
headers = {"User-Agent": "t", **({"Authorization": f"Bearer {key}"} if key else {})}
with mock.patch.object(network_check, "civitai_headers", return_value=headers), \
mock.patch.object(network_check.urllib.request, "urlopen", side_effect=[answer]) as opened:
result = network_check.check_civitai()
self.assertEqual(opened.call_args.kwargs["timeout"], network_check.TIMEOUT)
return result
def test_reachable_with_and_without_a_key(self):
self.assertEqual((lambda r: (r["reachable"], r["key"]))(self.check(_Answer(b"{}"))), (True, "none"))
self.assertEqual((lambda r: (r["reachable"], r["key"]))(self.check(_Answer(b"{}"), key="k")), (True, "set"))
def test_a_refused_key_is_told_apart_from_no_network(self):
refused = self.check(_http_error(401), key="bad")
self.assertEqual((refused["reachable"], refused["key"]), (True, "refused"))
down = self.check(urllib.error.URLError("timed out"))
self.assertEqual((down["reachable"], down["key"]), (False, "none"))
self.assertIn("timed out", down["error"])
self.assertFalse(self.check(_http_error(503))["reachable"])
def test_it_answers_this_computer_only(self):
self.assertIn("/anomalous/civitai_check", local_guard.LOCAL_GETS)
if __name__ == "__main__":
unittest.main()