Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ def ensure_dirs() -> None:


def local_url() -> str:
return f"http://{HOST}:{PORT}{MCP_PATH}"
# An IPv6 host needs brackets before the port, or the URL cannot be parsed.
host = f"[{HOST}]" if ":" in HOST else HOST
return f"http://{host}:{PORT}{MCP_PATH}"
35 changes: 34 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os
import tempfile
import unittest
import urllib.parse
from pathlib import Path
from unittest import mock

Expand All @@ -19,7 +20,12 @@

import config # noqa: E402

_VARIABLES = ("ACAD_MCP_PORT", "ACAD_MCP_SAVE_DIR", "ACAD_MCP_SECRET_PATH")
_VARIABLES = (
"ACAD_MCP_HOST",
"ACAD_MCP_PORT",
"ACAD_MCP_SAVE_DIR",
"ACAD_MCP_SECRET_PATH",
)


def load(**env):
Expand All @@ -43,6 +49,33 @@ def test_empty_variable_counts_as_unset(self):
self.assertEqual(load(ACAD_MCP_PORT="").PORT, 8770)


class LocalUrl(unittest.TestCase):
"""The advertised connector URL must parse for every host shape."""

def test_ipv6_host_is_bracketed_and_parses(self):
url = load(
ACAD_MCP_HOST="::1", ACAD_MCP_SECRET_PATH="offline-fixture"
).local_url()
self.assertEqual(url, "http://[::1]:8770/offline-fixture/mcp")
parts = urllib.parse.urlsplit(url)
self.assertEqual(parts.hostname, "::1")
self.assertEqual(parts.port, 8770)
self.assertEqual(parts.path, "/offline-fixture/mcp")

def test_ipv4_and_hostname_hosts_keep_their_shape(self):
cases = [
("127.0.0.1", "http://127.0.0.1:8770/offline-fixture/mcp"),
("localhost", "http://localhost:8770/offline-fixture/mcp"),
]
for host, expected in cases:
with self.subTest(host=host):
url = load(
ACAD_MCP_HOST=host, ACAD_MCP_SECRET_PATH="offline-fixture"
).local_url()
self.assertEqual(url, expected)
self.assertEqual(urllib.parse.urlsplit(url).hostname, host)


class SaveDirDefault(unittest.TestCase):
def test_default_lives_in_the_current_users_profile(self):
home = Path(tempfile.gettempdir()) / "someone-else"
Expand Down
Loading