Make database browser selection optional
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
import io
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from zipfile import ZipFile
|
||||
@@ -9,6 +11,57 @@ from zipfile import ZipFile
|
||||
from tools import c4hr, epub
|
||||
|
||||
|
||||
class DatabaseBrowserTests(unittest.TestCase):
|
||||
def open_database(self, config, run_bsk):
|
||||
args = ["c4hr.py", "open-db", "archives"]
|
||||
entry = {"name": "Archives", "purpose": "Find records",
|
||||
"url": "https://archive.example/", "enabled": True, **config}
|
||||
with patch.object(sys, "argv", args), patch.object(c4hr, "run_bsk", run_bsk):
|
||||
with patch.object(c4hr, "read_config", return_value={"databases": {"archives": entry}}):
|
||||
return c4hr.main()
|
||||
|
||||
def test_enabled_database_without_browser_uses_unique_connected_instance(self):
|
||||
calls = []
|
||||
|
||||
def run_bsk(args, *, capture_stdout=False):
|
||||
calls.append(args)
|
||||
if args[0] == "browsers":
|
||||
self.assertTrue(capture_stdout)
|
||||
return subprocess.CompletedProcess(args, 0, stdout='[{"instance_id":"chrome-profile"}]')
|
||||
return subprocess.CompletedProcess(args, 0)
|
||||
|
||||
self.assertEqual(self.open_database({}, run_bsk), 0)
|
||||
self.assertEqual(calls[0], ["browsers", "--json"])
|
||||
self.assertEqual(calls[1][-4:], ["--browser", "chrome-profile", "--json", "https://archive.example/"])
|
||||
|
||||
def test_explicit_browser_does_not_probe_other_instances(self):
|
||||
calls = []
|
||||
|
||||
def run_bsk(args, *, capture_stdout=False):
|
||||
calls.append(args)
|
||||
self.assertFalse(capture_stdout)
|
||||
return subprocess.CompletedProcess(args, 0)
|
||||
|
||||
self.assertEqual(self.open_database({"browser": "My research profile"}, run_bsk), 0)
|
||||
self.assertEqual(len(calls), 1)
|
||||
self.assertEqual(calls[0][-4:], ["--browser", "My research profile", "--json", "https://archive.example/"])
|
||||
|
||||
def test_no_or_multiple_connected_browsers_never_open_database(self):
|
||||
for browsers, message in [
|
||||
([], "尚未连接浏览器"),
|
||||
([{"instance_id": "chrome"}, {"instance_id": "edge"}], "已连接多个浏览器"),
|
||||
]:
|
||||
with self.subTest(browsers=browsers), patch("sys.stderr", new_callable=io.StringIO) as error:
|
||||
def run_bsk(args, *, capture_stdout=False):
|
||||
self.assertEqual(args, ["browsers", "--json"])
|
||||
return subprocess.CompletedProcess(args, 0, stdout=json.dumps(browsers))
|
||||
|
||||
with self.assertRaises(SystemExit) as exit_state:
|
||||
self.open_database({}, run_bsk)
|
||||
self.assertEqual(exit_state.exception.code, 1)
|
||||
self.assertIn(message, error.getvalue())
|
||||
|
||||
|
||||
class ProxyTests(unittest.TestCase):
|
||||
def test_redirect_rechecks_proxy_and_none_clears_environment_routing(self):
|
||||
config = (
|
||||
|
||||
Reference in New Issue
Block a user