Skip to content

test(desktop): close the client role's test gap, with a multi-host end-to-end test - #956

Merged
huichen merged 6 commits into
mainfrom
feat/remote-complete
Oct 9, 2026
Merged

huichen merged 6 commits into
mainfrom
feat/remote-complete

Conversation

@huichen

@huichen huichen commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

What this is

Follow-up to #955, which shipped the desktop client role with its protocol paths
tested end to end and its own runtime almost untested. This closes that gap,
adds the multi-host end-to-end test, and fixes what writing those tests found.

Scope of the diff: remote_peer/ and commands/remote_peer.rs only. The TS work
merged in #955; nothing here touches the frontend.

The multi-host end-to-end test

One desktop app, three real headless hosts, all connected at once. The hosts
run as separate processes of the same test binary, which is not a convenience:
the host bridge is a process-global singleton and HOME — where the
installation's device identity lives — is process-global too, so one process can
only ever be one host with one identity. A child gets its own HOME, its own device
id and its own bridge, which is the same arrangement as the headless binary on
several machines.

What it proves, all of it things a single-host test cannot see:

  • every host is its own machine (distinct desktop id, its own live session);
  • each host's catalogue is its own, stamped with its own desktop and pair ids,
    and never lists another host's session;
  • each host's event arrives under that host's identity, exactly once — and
    since the traffic keys differ per pairing, a crossed channel would fail to
    authenticate and the event would silently vanish, so three correctly attributed
    events is also evidence the per-host channels are wired to the right sockets;
  • losing one host leaves the others connected and working, not merely reported
    as connected;
  • unpairing one removes only that one.

Checked that it can fail: mutating the "unpairing one leaves the others" count
turns it red, and green again restored.

Three defects the tests found

  • A failed credential refresh was not recorded. It propagates before any socket
    exists, so the guard that records connection failures never saw it: the UI would
    show a host that is simply "not connected", with no reason, while nothing retried
    it.
  • A rotated token that could not be persisted was silent. Renewal now includes
    the write, so a local write fault is reported instead of leaving the next launch
    to renew again from a token this process already replaced.
  • A catalogue reply that is not a JSON object panicked. That field comes from a
    remote peer, so a panic there would hand a broken or hostile host a way to take
    the app down; it is now a reported protocol error.

Two tests that had been passing without testing anything

  • The forged-push test passed vacuously. It injected garbage on the event
    subject right after connecting — but subscribe() returning only means the SUB
    command was sent; the broker had not registered it yet, so the message was never
    delivered and the drop path never ran. It now waits for the subscription first.
  • The multi-host event assertion could not see a duplicate. Waiting for "at
    least N events" and then comparing a sorted list let one host publish three times
    while another had not published at all.

Coverage

Per-line (lcov DA:0) for every line this branch adds: 0 uncovered. Measured
the documented way — cargo llvm-cov clean (no --workspace), isolated HOME,
rustup toolchain on PATH, --lib --test-threads=1, report read from the lcov
DA:0 records rather than the summary.

remote_host/catalog.rs and pairing.rs show one uncovered line each; both files
are already on main and appear in this branch only through #955, so they are not
part of this diff (git diff origin/main --stat is 11 Rust files, all under
remote_peer/ and commands/).

The remaining crate-wide uncovered lines are in modules this change does not touch
(verify_e2e, headless/, tasks.rs, terminal/, …) and are measured here under
a narrower harness than the historical full-suite run, so the two numbers are not
comparable.

Local runs: 1706 passed / 1 failed on the desktop lib suite (the failure is the
pre-existing host-specific desktop_settings_refuse_to_guess_sandbox_availability
recorded in .future/memory/testing-gotchas.md), 8 passed for the headless
integration test, fmt and clippy clean with and without default features.

Notes for review

Two arms needed a seam or a move, and both are worth a look:

  • The race between connecting and addressing the connection (a concurrent transport
    failure, or a Disconnect, removes the entry in that window) is injected with a
    one-shot flag — the same style remote_host::pairing already uses for a
    credential-write failure. It cannot be produced from outside without racing.
  • "The window is gone" after a push is now a named report_emit_failure, because it
    was not reachable where it lived: a mock app's emit returns Ok even with no
    webview and after the window closes, and a real window cannot be made to fail on
    demand. A branch no test can execute is worse than a function that says what it
    handles.

…bugs it found

The client role shipped with its protocol paths tested end to end and its own
runtime almost untested: `remote_peer/runtime.rs` had 313 uncovered lines and
the Tauri command layer 63, because nothing drove the API the UI actually calls.
Per-line coverage is what surfaced that, and writing the tests found three real
defects:

  - **A failed credential refresh was not recorded.** It propagates before any
    socket exists, so the guard that records connection failures never saw it:
    the UI would show a host that is simply "not connected", with no reason,
    while nothing retried it.
  - **A rotated token that could not be persisted was silent.** Renewal now
    includes the write, so a local write fault is reported instead of leaving
    the next launch to renew again from a token this process already replaced.
  - **`stream_count` counted finished tasks.** Production code does not read it;
    the test helper did, which made "the stream ended" unobservable and would
    have hidden exactly the arm it was written to check.

Structurally, two things moved so they could be tested at all rather than only
inspected:

  - The handshake confirmation is now validated by a pure `Confirmed::from_
    confirmation`. Every rejection there is a protocol fault a real host never
    produces, so feeding it what a buggy or hostile peer would send is the only
    way to reach the arms that decide whether a client trusts a claimed
    identity.
  - The reconnect schedule keeps its real values and scales its *unit* under
    test, so the suite exercises the real shape without waiting real minutes.

`Claimed` gains a hand-written `Debug` that redacts both tokens: it lands in
`expect`/`assert` output, and a panic message is one of the easiest places for a
bearer token to leak into a bug report.

Remote peer per-line uncovered: 425 -> 42, with the rest enumerated next.
…ess hosts

The client role was only ever proven against a single host, which is the one
case where "which host is this row from?" cannot go wrong. This asserts the
multi-host properties end to end, with three real host bridges running as
**separate processes**:

  - every host is its own machine (distinct desktop id, its own live session);
  - each host's catalogue is its own, stamped with its own desktop and pair ids,
    and never lists another host's session;
  - each host's event arrives under that host's identity, exactly once — and
    since the traffic keys differ per pairing, a crossed channel would fail to
    authenticate and the event would simply vanish, so three correctly
    attributed events is also evidence the per-host channels are wired to the
    right sockets;
  - losing one host leaves the others connected *and working*, not merely
    reported as connected;
  - unpairing one removes only that one.

Separate processes are not a convenience here: the host bridge is a
process-global singleton and `HOME` (where the installation's device identity
lives) is process-global too, so one process can only ever be one host with one
identity. A child of this test binary gets its own HOME, its own device id and
its own bridge — the same arrangement as the headless binary on several
machines. It is started with the machine's own agent endpoint variables removed,
so a sibling test's mock agent cannot leak in.

The child signals through files rather than stdout: a test binary's stdout
belongs to libtest, and parsing it would be a second thing to keep in sync. It
consumes each request rather than acting on it every tick — the first version
republished on every loop iteration, and the parent caught it as duplicate
events.

Checked that it can fail: mutating the "unpairing one leaves the others" count
turns it red, and it is green again restored.
…lse tests

Every line this branch adds under `remote_peer/` and `commands/remote_peer.rs` is
now executed by a test (`remote_host/catalog.rs` and `pairing.rs` are already on
main, so this branch's whole diff is covered). Writing those tests found several
real problems, and two of the tests that "passed" turned out not to test
anything:

  - **The forged-push test passed vacuously.** It injected garbage on the event
    subject right after connecting — but `subscribe()` returning only means the
    SUB command was *sent*; the broker had not registered it yet, so the message
    was never delivered and the drop path never ran. It now waits for the
    subscription (`wait_for_sub`) before injecting, which is what makes it a
    test of anything.
  - **The multi-host event assertion could not see a duplicate.** Waiting for
    "at least N events" and then comparing a sorted list let a host publish
    three times while another had not published at all. It now waits for every
    host to be heard from and then requires *exactly* one event per host.
  - **A catalogue reply that is not a JSON object used to panic.** That field
    comes from a remote peer, so a panic there would hand a broken or hostile
    host a way to take the app down; it is now a reported protocol error.
  - **The two identical `subscribe` `map_err`s are one helper.** The second copy
    could only ever be reached by whichever call happened to fail first, so it
    was an arm no test could execute.

One arm needed a seam and one needed extracting:

  - The race between connecting and addressing the connection (a concurrent
    transport failure or a Disconnect removes the entry in that window) is
    injected with a one-shot flag, the same style `remote_host::pairing` uses for
    a credential-write failure — it cannot be produced from outside without
    racing.
  - "The window is gone" after a push is now a named `report_emit_failure`,
    because it was not reachable where it lived: a mock app's `emit` returns `Ok`
    even with no webview and after the window closes, and a real window cannot be
    made to fail on demand. A branch no test can execute is worse than a function
    that says what it handles.
@huichen
huichen enabled auto-merge (squash) October 9, 2026 13:50
… is wrong

The Linux CI run saw four peers where three were paired. It does not reproduce
locally (this machine cannot run the full suite in parallel — it deadlocks, a
known local hazard), so the assertion now carries the peers, the HOME in effect
and the identities this test created, which is what the next CI run needs to say
what the extra entry is.
…racked runtime

The Linux CI run of the multi-host test saw a fourth live connection where three
hosts were paired. It is not a stale credential: the book was right, `live_count`
was not — the runtime's `live` map held an entry left by a previous test.

The cause is the spawn. `spawn_event_stream` and `spawn_supervisor` used
`tokio::spawn` directly, so their tasks outlived the `#[tokio::test]` runtime
that started them and were invisible to `runtime::cancel_test_tasks` — the crate's
registry of process-lifetime tasks, which every `HomeGuard` calls precisely so
that no task from an earlier test can still resolve the process-global HOME and
the runtime. A leaked stream also kept its socket open, which is what the next
test then found in `live`.

Both now go through `crate::runtime::spawn`. That returns a different handle type
per feature (`tauri::async_runtime::JoinHandle` for gui, `tokio`'s for headless),
so the module exports a `TaskHandle` alias and a `task_finished` helper instead of
making every spawn site repeat the feature split; `agent_bridge::observer` takes
the alias too.

Also: the multi-host assertion now reports the peers it saw, the HOME in effect,
and the identities the test created, rather than a bare count — which is what
made this diagnosable from CI instead of guesswork.
…efore it

The Linux job still saw a fourth live connection after the spawn fix, and the
reason is the order of the two lines:

    reset_for_test().await;          // clears the runtime
    let (home, fx) = fixture(label); // takes the home guard

The home guard is what serializes these tests — but the clear happened before
taking it, so a test that already held the guard could connect and populate the
runtime in that window, and the next test would then see its own three hosts plus
whatever the other test had left. Moving the clear inside the guard closes the
window: while the guard is held, nothing else can be writing.

It cannot reproduce with `--test-threads=1` (no interleaving) and this machine
deadlocks on the full parallel suite before reaching the test, which is why two
rounds were spent on the spawn before the ordering. That is on me; the ordering
is the reason the failure was consistently `4 == 3` rather than an arbitrary
number, and the diagnostic added in the previous commit is what made the two
causes distinguishable at all.
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