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
2 changes: 1 addition & 1 deletion mqterm/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def format_topic(*parts):
"""Create a slash-delimited MQTT topic from a list of strings."""
return "/" + "/".join(part.strip("/") for part in parts if part)
return "/".join(part.strip("/") for part in parts if part)


def format_properties(client_id, seq):
Expand Down
14 changes: 7 additions & 7 deletions tests/e2e/e2e_file_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
control_client = MQTTClient(control_config, logger=control_logger)

# Set up the terminal
term = MqttTerminal(device_client, topic_prefix="/test")
term = MqttTerminal(device_client, topic_prefix="test")


async def send_file(buffer: BytesIO):
Expand All @@ -48,7 +48,7 @@ async def send_file(buffer: BytesIO):
seq = 0
props = format_properties("tty0", seq)
await control_client.publish(
"/test/tty/in", "cp test.txt".encode("utf-8"), properties=props
"test/tty/in", "cp test.txt".encode("utf-8"), properties=props
)

# Send the file in 4-byte chunks; close when done
Expand All @@ -62,7 +62,7 @@ async def send_file(buffer: BytesIO):
seq = -1
props = format_properties("tty0", seq)
logger.debug(f"Sending chunk {seq} of size {len(chunk)}: {chunk!r}")
await control_client.publish("/test/tty/in", chunk, properties=props)
await control_client.publish("test/tty/in", chunk, properties=props)
if seq == -1:
break

Expand All @@ -84,7 +84,7 @@ async def get_file(buffer: BytesIO):
seq = 0
props = format_properties("tty0", seq)
await control_client.publish(
"/test/tty/in", "cat test.txt".encode(), properties=props
"test/tty/in", "cat test.txt".encode(), properties=props
)

# Wait until the received buffer gets populated with the response
Expand All @@ -107,7 +107,7 @@ async def device_handler():
# Handler for control messages that logs and stores them
async def control_handler(buffer):
async for topic, payload, _retained, properties in control_client.queue:
if topic == "/test/tty/err":
if topic == "test/tty/err":
logger.error(f"Control received error: {payload.decode('utf-8')}")
else:
buffer.write(payload) # Don't decode yet
Expand All @@ -118,8 +118,8 @@ async def control_handler(buffer):
async def main():
# Connect all clients and the terminal
await control_client.connect(True)
await control_client.subscribe("/test/tty/out")
await control_client.subscribe("/test/tty/err")
await control_client.subscribe("test/tty/out")
await control_client.subscribe("test/tty/err")
await device_client.connect(True)
await term.connect()

Expand Down
15 changes: 12 additions & 3 deletions tests/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest import TestCase, skip

from mqterm.jobs import Job, SequentialJob
from mqterm.terminal import MqttTerminal, format_properties
from mqterm.terminal import MqttTerminal, format_properties, format_topic
from tests.utils import AsyncMock, Mock, call


Expand All @@ -28,10 +28,19 @@ async def update(self, payload, seq):
def output(self):
return BytesIO(self.contents.encode("utf-8"))

class TestFormatTopic(TestCase):
def test_format_topic(self):
"""Test the format_topic function."""
self.assertEqual(format_topic("a", "b", "c"), "a/b/c")
self.assertEqual(format_topic("a", None, "c"), "a/c")
self.assertEqual(format_topic("", "b", None), "b")
self.assertEqual(format_topic("a/", "b/"), "a/b")
self.assertEqual(format_topic(), "")


class TestMqttTerminal(TestCase):
def setUp(self):
self.in_topic = b"/tty/in"
self.in_topic = b"tty/in"
self.mqtt_client = Mock()
self.mqtt_client.publish = Mock()
self.term = MqttTerminal(self.mqtt_client)
Expand Down Expand Up @@ -67,7 +76,7 @@ def test_handle_msg_bad_seq(self):
def test_handle_msg_wrong_topic(self):
"""MqttTerminal should ignore messages on wrong topic"""
payload = "get_file file.txt"
topic = b"/wrong/topic"
topic = b"wrong/topic"
self.term.update_job = AsyncMock()
asyncio.run(
self.term.handle_msg(topic, payload.encode("utf-8"), format_properties("localhost", -1))
Expand Down