diff --git a/meshterm/demo/history.py b/meshterm/demo/history.py index 9e315bd..8953fcd 100644 --- a/meshterm/demo/history.py +++ b/meshterm/demo/history.py @@ -49,6 +49,13 @@ TX_POWER = 22 #: The map viewport that the demo opens on: the island of Montréal, with the two shores. MAP_VIEW = (45.5150, -73.5900, 10) +#: The channel slots whose history draws from the one source of chance of the history. A +#: channel in a later slot draws from a source of its own, seeded by its name. Thus a +#: channel added to the demo never changes the rest of it: the shared source gives the same +#: numbers to everything after the conversations (the traces, the neighbours), and the +#: video's trace to Brossard keeps the route that it was made for. +_SHARED_SLOTS = 4 + def seed(repo: Repository, now: datetime) -> None: """Write the full history of the demo into ``repo``. @@ -128,8 +135,13 @@ def _conversations( """ messages: list[ChatMessage] = [] heard: list[traffic.Heard] = _greetings(rng) + shared = rng for channel in world.CHANNELS: identity = channel_identity(channel.name, channel.secret) + if channel.slot < _SHARED_SLOTS: + rng = shared + else: + rng = random.Random(f"meshterm-demo/history/{channel.name}") for line in script.CHANNEL_LINES.get(channel.name, ()): offset = -line.age_min * 60.0 sender = world.node(line.sender) @@ -165,6 +177,7 @@ def _conversations( created_at=when, ) ) + rng = shared for peer_name, lines in script.DIRECT_LINES.items(): peer = world.node(peer_name) for line in lines: @@ -242,26 +255,55 @@ def _greetings(rng: random.Random) -> list[traffic.Heard]: spoke a few minutes ago was also heard a few minutes ago, and the lists show it as recent. The function adds one advert before the first line of each sitting in the last two days (a sitting is a run of lines less than an hour apart). + + The lines of the channels in the shared slots and of the direct messages draw from + ``rng``, as they always did. The lines of a later channel come in a second pass, with a + source of their own (refer to :data:`_SHARED_SLOTS`). Thus a channel added to the demo + does not change the adverts, or anything after them, of the rest of the history. """ - lines = [ - line - for group in (script.CHANNEL_LINES, script.DIRECT_LINES) - for conversation in group.values() - for line in conversation - if line.sender != script.ME and line.age_min < 2 * 1440 - ] + slots = {channel.name: channel.slot for channel in world.CHANNELS} + + def recent(conversations) -> list: # noqa: ANN001 - the values of a dict of lines + return [ + line + for conversation in conversations + for line in conversation + if line.sender != script.ME and line.age_min < 2 * 1440 + ] + + shared = recent( + [ + lines + for name, lines in script.CHANNEL_LINES.items() + if slots.get(name, _SHARED_SLOTS) < _SHARED_SLOTS + ] + + list(script.DIRECT_LINES.values()) + ) + later = recent( + [ + lines + for name, lines in script.CHANNEL_LINES.items() + if slots.get(name, _SHARED_SLOTS) >= _SHARED_SLOTS + ] + ) last_advert: dict[str, float] = {} heard: list[traffic.Heard] = [] - for line in sorted(lines, key=lambda line: -line.age_min): - offset = -line.age_min * 60.0 - if offset - last_advert.get(line.sender, -1e12) < 3600.0: - continue - sender = world.node(line.sender) - moment = offset - rng.uniform(60.0, 900.0) - heard.extend( - traffic.heard_flood(sender, moment, traffic.advert_body(sender), rng, advert=True) - ) - last_advert[line.sender] = offset + for lines, source in ( + (shared, rng), + (later, random.Random("meshterm-demo/history/greetings")), + ): + for line in sorted(lines, key=lambda line: -line.age_min): + offset = -line.age_min * 60.0 + if abs(offset - last_advert.get(line.sender, -1e12)) < 3600.0: + continue + sender = world.node(line.sender) + moment = offset - source.uniform(60.0, 900.0) + heard.extend( + traffic.heard_flood( + sender, moment, traffic.advert_body(sender), source, advert=True + ) + ) + last_advert[line.sender] = offset return heard diff --git a/scripts/video.py b/scripts/video.py index e160431..1ed52ac 100644 --- a/scripts/video.py +++ b/scripts/video.py @@ -356,7 +356,7 @@ def _tour(p: Presenter) -> None: print("the main menu", flush=True) p.camera.roll() - p.hold(3.0) + p.hold(2.5) print("the chat, and the paths of a message", flush=True) p.type("chat") @@ -405,7 +405,7 @@ def _tour(p: Presenter) -> None: # Dave answers a few seconds later (``meshterm.demo.script.REPLIES``). His answer # mentions our node, so it has our star. p.wait_for("be there at 2", timeout=30) - p.hold(3.5) + p.hold(3.0) print("a trace", flush=True) _menu(p, "trace t") @@ -415,14 +415,14 @@ def _tour(p: Presenter) -> None: p.hold(0.4) p.key("enter") p.wait_for("Trace — Brossard") - p.hold(2.0) + p.hold(1.5) p.key("enter", pause=0.0) p.wait_for("Per-hop medians", timeout=30) p.hold(1.0) # The history stores walks of one byte for each hop. Thus this walk of two bytes is new, # and its records go to the trophy case. if p.shows("new record"): - p.hold(3.0) + p.hold(2.5) p.key("enter", pause=0.0) # Close p.hold(1.5) p.key("pagedown", pause=0.0) diff --git a/tests/test_demo.py b/tests/test_demo.py index badd7e4..8f19ad8 100644 --- a/tests/test_demo.py +++ b/tests/test_demo.py @@ -188,3 +188,36 @@ def test_a_mentioned_node_answers(monkeypatch) -> None: # noqa: ANN001 - the py assert [m.text for m in messages] == [f"Dave-Lachine: {script.REPLIES['Dave-Lachine']}"] assert messages[0].is_channel and messages[0].channel == 0 assert f"@[{world.SELF.name}]" in messages[0].text + + +def test_a_later_channel_leaves_the_rest_of_the_history_unchanged(monkeypatch) -> None: # noqa: ANN001 + """A channel after the shared slots draws from its own source of chance. + + The history draws the conversations, then the traces and the neighbours, from one + source. When the ``#mcoimg`` channel took numbers from it, the route that the trace + of the video learns moved off Mont-Royal, and the trace to Brossard got no reply. So + the shared source must end the conversations in the same state with that channel and + without it, and so must it after the greetings, which read the lines of each channel. + """ + import random + + from meshterm.demo import script + + now = utcnow() + + def state_after_conversations() -> tuple: + rng = random.Random("meshterm-demo/history") + history._conversations(now, rng) + return rng.getstate() + + assert any(c.slot >= history._SHARED_SLOTS for c in world.CHANNELS) # else it proves nothing + with_channel = state_after_conversations() + monkeypatch.setattr( + world, "CHANNELS", tuple(c for c in world.CHANNELS if c.slot < history._SHARED_SLOTS) + ) + monkeypatch.setattr( + script, + "CHANNEL_LINES", + {name: lines for name, lines in script.CHANNEL_LINES.items() if name != "#mcoimg"}, + ) + assert state_after_conversations() == with_channel