Files
MeshTerm/tests/test_demo.py
T
Jean-Pierre MartineauandClaude Opus 5.5 37b17d8ba4 The demo's message paths look like a real mesh
Measured against a real Montreal database: most messages arrive twice,
through the two repeaters a companion hears, on paths that share their
start and split near the end, with 3-byte hashes. The demo did five or
six short copies through five neighbours.

Our node now hears only two new rooftop repeaters, Villeray and
Rosemont (the terrain hides the big ones), companions lose 7 dB at street
level, and each sender has its own hash width (mostly 3 bytes). Now 91%
of messages arrive twice and 83% of paths use 3-byte hashes. Noor moves
to Côte-des-Neiges to stay in reach, and the traces leave through
Villeray or Rosemont.

The capture finds the Message paths message by its text (a key press
can be lost while the transcript draws) and selects the second path,
since the first is often a straight line.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-16 04:30:43 -04:00

101 lines
4.0 KiB
Python

# SPDX-License-Identifier: Apache-2.0
"""The demo world: the same history at each run, and a device that agrees with it."""
from __future__ import annotations
import asyncio
from datetime import timedelta
from pathlib import Path
from meshterm.core.channels import channel_identity
from meshterm.core.models import utcnow
from meshterm.demo import history, world
from meshterm.demo.device import DemoDevice
from meshterm.demo.home import prepare
from meshterm.persistence.repository import Repository
def _seed(path: Path, now) -> Repository: # noqa: ANN001 - a datetime
repo = Repository(path)
history.seed(repo, now)
return repo
def test_each_node_has_its_own_hash() -> None:
"""No two nodes of the world share a path hash, so each hop resolves to one node."""
hashes = [node.hash for node in (world.SELF, *world.NODES)]
assert len(hashes) == len(set(hashes))
def test_the_history_is_the_same_at_each_run(tmp_path: Path) -> None:
"""Two runs give the same rows, with the times moved by the gap between the two starts."""
now = utcnow()
first = _seed(tmp_path / "a.db", now)
second = _seed(tmp_path / "b.db", now + timedelta(hours=5))
assert first.table_counts() == second.table_counts()
assert first.kind_counts() == second.kind_counts()
public = world.CHANNELS[0]
identity = channel_identity(public.name, public.secret)
def lines(repo: Repository, start) -> list[tuple[str, int]]: # noqa: ANN001 - a datetime
rows = repo.recent_chat_messages(is_channel=True, channel_id=identity, limit=500)
return [(m.text, round((start - m.created_at).total_seconds())) for m in rows]
assert lines(first, now) == lines(second, now + timedelta(hours=5))
def test_a_channel_line_has_its_heard_copies(tmp_path: Path) -> None:
"""The packets of a channel line decrypt to the line, so its paths can be found."""
from meshterm.services.message_paths import channel_arrivals
repo = _seed(tmp_path / "h.db", utcnow())
public = world.CHANNELS[0]
identity = channel_identity(public.name, public.secret)
last = repo.recent_chat_messages(is_channel=True, channel_id=identity, limit=1)[-1]
arrivals = channel_arrivals(repo, last, channel_name=public.name, secret=public.secret)
assert arrivals
def test_the_demo_device_traces_a_path() -> None:
"""A forced path through linked repeaters comes back with a reading for each link."""
async def run(): # noqa: ANN202
device = DemoDevice()
await device.connect()
spec = ",".join(world.node(n).hash for n in ("Rosemont", "Mont-Royal", "Rosemont"))
for _ in range(10):
result = await device.run_trace("(path)", path=spec)
if result.success:
return result
return result
result = asyncio.run(run())
assert result.success
assert [h.node for h in result.hops] == ["d8", "2a", "d8", None]
def test_prepare_writes_a_home(tmp_path: Path) -> None:
"""The demo home gets a database and the stores of the joined room and the alerts."""
home = tmp_path / "demo"
prepare(home, utcnow())
prepare(home, utcnow()) # a second start writes it again over the first
assert (home / "meshterm.db").exists()
assert (home / "rooms.json").exists()
assert (home / "watchtower.json").exists()
def test_each_line_is_sent_while_its_sender_transmits() -> None:
"""No node speaks before our node first heard it, or after it went quiet."""
from meshterm.demo import script
groups = (script.CHANNEL_LINES, script.DIRECT_LINES, script.ROOM_LINES)
for group in groups:
for lines in group.values():
for line in lines:
node = world.BY_NAME.get(line.sender)
if node is None or node is world.SELF:
continue # an author that no contact names
age_h = line.age_min / 60.0
assert node.quiet_hours is not None, line
assert node.quiet_hours <= age_h <= node.first_heard_days * 24.0, line