Skip to content

refactor: network seam for protocol tests - #3

Merged
nerdstep merged 9 commits into
mainfrom
network-seam
Nov 25, 2026
Merged

nerdstep merged 9 commits into
mainfrom
network-seam

Conversation

@nerdstep

Copy link
Copy Markdown
Owner

Summary

The engine reaches its peer through a network port instead of using iroh directly. That lets the session protocol run in tests on an in-memory network, with paused time.

 src-tauri/src/
 ├── chat.rs                # the engine: pairing, sessions, catch-up, receipts, files
+│   └── protocol_tests.rs  # two engines on the in-memory network, paused time
+├── net.rs                 # the port: Network, Conn, SendStream, RecvStream; the iroh + mDNS adapter
+│   └── memory.rs          # #[cfg(test)] in-memory adapter: Lan, drop_out
+└── clock.rs               # one wall clock for Core and Db; follows tokio's paused clock in tests
Core::start_with(dir, emit, loopback)        # unchanged for the app
  Core::start_on(dir, emit, clock, network)  # new, pub(crate)
    app:   Network::iroh(secret, loopback)
    tests: lan.join(secret)

Each port type is an enum with one variant per adapter. The memory variant compiles only for tests.

Found and fixed: at shutdown, the dialer's session was dropped partway through. Its file fetcher kept running and held the engine and its log open. The session's tasks now stop however the session ends (fix: commit).

two_peers.rs shrinks to a smoke test of what only the real network can check: mDNS discovery, pairing over QUIC, a message each way, a restart that redials and catches up, and a picture. GLOSSARY.md adds peer, pairing, session and catch-up.

Evidence

  • Before: the only engine test was two_peers: one sequential scenario on real mDNS and QUIC, taking about 10 s.
    After: 20 protocol tests run in about 0.5 s, and two_peers takes about 5 s. The new tests cover:

    • pairing: one code on both screens, declined, timed out, cancelled, trusted key, refused while paired
    • session replacement
    • catch-up
    • sessions dropping and coming back with no duplicates
    • a peer restoring an old backup
    • vanishing messages: an hour unseen, then 5 s
    • builds before 0.6, on 0.6, and newer frames
    • files: arriving, resuming, a hash mismatch, deleted on the way, the sender no longer having it
  • The session replacement test fails when the generation check is removed:

    if st.session.is_some()     # instead of: generation == generation
    → a_new_session_replaces_a_half_dead_one: assertion failed: other.status().connected
    
  • The lost-history test failed about half the time before the fix, depending on which randomly keyed side dialled. It passed 10 out of 10 runs after.

  • Every commit passes cargo fmt --check, clippy -D warnings and cargo test on its own (checked one commit at a time on Windows). bun run check passes.

Merge Danger

Door: two-way

There's no wire or schema change. The app's network code moved behind the port, but its behaviour is unchanged: two_peers passes on real iroh.

Blast Radius: connections

Everything that dials, accepts, discovers or streams now goes through net.rs. CI's macOS job skips the mDNS test, so on macOS only the protocol tests cover this. A quick manual check of pairing and a picture between a Mac and Windows before the next release would be worthwhile.

🤖 Generated with Claude Code

nerdstep and others added 9 commits November 25, 2026 09:14
Clock replaces the two now_ms functions. The app reads the system clock;
tests get one that follows tokio's paused time, so waits and the wall
clock move together. The nudge rate limit uses tokio's Instant for the
same reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The engine reaches its peer through net::Network, Conn and the two
stream types instead of iroh directly. Building the endpoint, the LAN
filter, mDNS and advertising the name move to net.rs. Core::start_with
keeps its signature and calls start_on, which takes the network and the
clock, so tests can run on another network.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
net::Lan is a second adapter for the network port, compiled only for
tests: nodes discover each other by name, connect at once, and stream
through channels, with QUIC's abrupt close. Protocol tests run two
engines on it with tokio's paused clock; the first pairs two peers and
sends a message each way, in milliseconds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Pairing: one code on both screens, declined, timing out after two
minutes, cancelled by the asker, a trusted key paired again without a
prompt, and someone already paired refusing a third person.

Sessions: when the dialer loses its connection and the other side doesn't
notice, the new session replaces the old one, and ending the old one
leaves the new one alone (the generation check; the test fails without
it). The in-memory network gets Lan::drop_out for this, and closes a
connection when the last handle to one side is dropped, as QUIC does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The dialer's session runs inside dial_loop, which shutdown aborts. The
session's writer and file tasks were only stopped at its normal end, so
after a shutdown the file fetcher waited forever, holding the engine and
its log open. They now stop when the session is dropped, too.

Found by the protocol tests: restarting the app within one process left
the old one's database open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Catch-up after one side was off, with receipts surviving the restart;
sessions dropping and coming back three times without a message shown
twice; a peer restoring an old backup getting the rest again, with seen
going back down to what it had; and a vanishing message that waits an
hour unseen, then goes from both sides five seconds after it's seen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A peer played frame by frame stands in for other builds: one from before
0.6 gets neither vanishing messages nor files, one on 0.6 gets vanishing
messages but not files, and a frame from a newer build is skipped without
ending the session.

Files: a picture arrives after its message; a transfer resumes from the
first byte it lacks; a file that doesn't match its hash isn't kept; one
deleted on its way isn't kept either; and one the sender no longer has
ends up gone on both sides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It now checks what only the real network can: mDNS discovery with names,
pairing over QUIC, a message each way, a restart that redials and
catches up, and a picture on a stream of its own. Receipts, reactions,
edits, typing, nudges, away, clearing, do not disturb, vanishing
messages and missing files are covered by the protocol tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
README's layout lists net.rs, clock.rs and the protocol tests. AGENTS.md
asks for a protocol test with each protocol change. GLOSSARY.md adds
peer, pairing, session and catch-up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nerdstep
nerdstep merged commit 7e0df92 into main Nov 25, 2026
3 checks passed
@nerdstep
nerdstep deleted the network-seam branch November 25, 2026 18:15
nerdstep added a commit that referenced this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant