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
8 changes: 4 additions & 4 deletions src/wokwi_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from typing import Any, Callable, Optional, Union, cast

from .__version__ import get_version
from .constants import DEFAULT_WS_URL
from .control import set_control
from .event_queue import EventQueue
from .exceptions import ProtocolError
Expand All @@ -30,7 +29,7 @@
from .serial import monitor_lines, write_serial
from .simulation import pause, restart, resume, start
from .touch import touch_event
from .transport import Transport
from .transport import TRANSPORT_DEFAULT_WS_URL, Transport
from .vcd import VCDData, read_vcd, save_vcd


Expand All @@ -52,10 +51,11 @@ def __init__(self, token: str, server: Optional[str] = None):

Args:
token: API token for authentication (get from https://wokwi.com/dashboard/ci).
server: Optional custom server URL. Defaults to the public Wokwi server.
server: Optional custom server URL. Defaults to the WOKWI_CLI_SERVER environment
variable, or the public Wokwi server if it is not set.
"""
self.version = get_version()
self._transport = Transport(token, server or DEFAULT_WS_URL)
self._transport = Transport(token, server or TRANSPORT_DEFAULT_WS_URL)
self.last_pause_nanos = 0
self._transport.add_event_listener("sim:pause", self._on_pause)
# Lazily create in an active event loop (important for py3.9 and sync client)
Expand Down
40 changes: 40 additions & 0 deletions tests/test_server_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# SPDX-FileCopyrightText: 2025-present CodeMagic LTD
#
# SPDX-License-Identifier: MIT

import os
import subprocess
import sys
from typing import Optional

from wokwi_client.constants import DEFAULT_WS_URL


def _client_url(env_server: Optional[str], server: Optional[str] = None) -> str:
"""Return the URL picked by a WokwiClient created in a fresh interpreter.

WOKWI_CLI_SERVER is read at import time, so each case needs its own process.
"""
env = {k: v for k, v in os.environ.items() if k != "WOKWI_CLI_SERVER"}
if env_server is not None:
env["WOKWI_CLI_SERVER"] = env_server
code = (
"from wokwi_client import WokwiClient; "
f"print(WokwiClient('token', {server!r})._transport._url)"
)
result = subprocess.run(
[sys.executable, "-c", code], env=env, capture_output=True, text=True, check=True
)
return result.stdout.strip()


def test_default_server_url() -> None:
assert _client_url(None) == DEFAULT_WS_URL


def test_server_url_from_env() -> None:
assert _client_url("ws://localhost:9177") == "ws://localhost:9177"


def test_explicit_server_url_overrides_env() -> None:
assert _client_url("ws://localhost:9177", "ws://example.com:3000") == "ws://example.com:3000"
Loading