Files
buzz/mobile/test/shared/relay/relay_config_test.dart
Krishna C 047533c56c fix(mobile): keep TLS on relays joined by invite (#3139)
## Summary

Communities joined via an invite link never connect: the app dials
`ws://` on port 80 instead of `wss://` on 443 and sits on
"Reconnecting…" indefinitely.

`RelayConfig.baseUrl` is documented as an HTTP origin, but the two
onboarding flows disagree on what they persist:

- **Device pairing** validates and stores `https://` —
`pairing_provider.dart:657` throws on anything else.
- **Invite join** stores the relay URL straight off the invite link, and
`deep_link.dart:165` always emits `ws://` or `wss://`.

`wsUrl` only special-cased `https://`, so a `wss://` base fell through
to the plaintext branch:

```dart
final scheme = uri.scheme == 'https' ? 'wss' : 'ws';   // 'wss' is not 'https'
```

The claim request itself succeeds, because `_claimUrlFromRelay`
(`invite_join_provider.dart:242`) maps `wss → https` explicitly. Only
the socket path is missing that conversion — which is why the community
appears, correctly named, and then never loads.

The same `baseUrl` also feeds `/query` (`relay_session.dart:136`), media
upload (`media_upload.dart:765`), Blossom auth (`media_auth.dart:128`)
and `relayClientProvider` (`relay_provider.dart:113`), so those requests
were malformed too. Where port 80 *does* answer, it is additionally a
silent TLS downgrade after `validateInviteRelayUri` insisted on
`wss://`.

This folds the websocket schemes back to their HTTP equivalents in
`baseUrl` itself, so every consumer is correct by construction rather
than needing a second getter remembered at each call site, and
communities **already persisted** with `wss://` are repaired on read
without a migration. `community_icon_provider.dart:46` already performs
this same conversion locally.

One subtlety worth flagging for review: the normalization is derived in
the getter rather than applied in the constructor, so the constructor
stays `const`. The compile-time fallback at `relay_provider.dart:77`
relies on const canonicalization for a stable identity across rebuilds,
and Riverpod's `defaultUpdateShouldNotify` is `previous != next`
(`element.dart:361`), which falls back to identity for this class. A
`factory` constructor here yields a fresh instance per rebuild, which
tears down and resubscribes every listener —
`channels_provider_test.dart` catches it as an unexpected unsubscribe
during reconnect.

### Related issue

Fixes #2662.

### Testing

`flutter test` — **705 passed, 1 skipped, 0 failed**
`flutter analyze` — No issues found
`dart format --set-exit-if-changed .` — 249 files, 0 changed

Run against the Hermit-pinned SDK (Flutter 3.41.7 / Dart 3.11.5),
matching CI.

10 new unit tests in `mobile/test/shared/relay/relay_config_test.dart`
covering both onboarding schemes, `http`/`https` passthrough,
non-default ports, and agreement between the invite and pairing paths
for the same relay.

Verified end-to-end against a self-hosted relay behind `tailscale
serve`, which terminates TLS on 443 and leaves port 80 closed. Relay
logs show the invite claim succeeding over HTTPS at the moment of
joining, while no WebSocket connection ever arrives — no `WebSocket
connection established`, no NIP-42 auth, no `kind:0` profile, no push
registration — across the relay's entire history, even though the member
row is present and correct. Port-80 refusals are not logged by
`tailscaled`'s netstack, which is why the retries leave no trace
server-side. Reproduced on both iOS and Android.

---------

Signed-off-by: Krishna C <github@kumb.uk>
2026-07-29 12:02:42 -07:00

68 lines
2.6 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:buzz/shared/relay/relay_provider.dart';
void main() {
group('RelayConfig.baseUrl normalization', () {
test('folds a wss:// community URL to https://', () {
// Invite joins persist the relay URL straight off the invite link, which
// deep_link.dart always emits as ws:// or wss://.
final config = RelayConfig(baseUrl: 'wss://relay.example.com');
expect(config.baseUrl, 'https://relay.example.com');
});
test('folds a ws:// community URL to http://', () {
final config = RelayConfig(baseUrl: 'ws://relay.example.com:3000');
expect(config.baseUrl, 'http://relay.example.com:3000');
});
test('leaves an https:// community URL untouched', () {
// Device pairing rejects anything but https://, so these already conform.
final config = RelayConfig(baseUrl: 'https://relay.example.com');
expect(config.baseUrl, 'https://relay.example.com');
});
test('leaves an http:// community URL untouched', () {
final config = RelayConfig(baseUrl: 'http://localhost:3000');
expect(config.baseUrl, 'http://localhost:3000');
});
test('preserves a non-default port', () {
final config = RelayConfig(baseUrl: 'wss://relay.example.com:8443');
expect(config.baseUrl, 'https://relay.example.com:8443');
});
});
group('RelayConfig.wsUrl', () {
test('keeps TLS for a relay joined by invite', () {
// Regression: a wss:// base used to fall through to the non-https branch
// and downgrade to ws://, dialing port 80 — which never connects on a
// relay that only serves 443, and drops TLS everywhere else.
final config = RelayConfig(baseUrl: 'wss://relay.example.com');
expect(config.wsUrl, 'wss://relay.example.com');
});
test('keeps TLS for a relay added by pairing', () {
final config = RelayConfig(baseUrl: 'https://relay.example.com');
expect(config.wsUrl, 'wss://relay.example.com');
});
test('both onboarding paths agree on the same relay', () {
final invited = RelayConfig(baseUrl: 'wss://relay.example.com');
final paired = RelayConfig(baseUrl: 'https://relay.example.com');
expect(invited.wsUrl, paired.wsUrl);
expect(invited.baseUrl, paired.baseUrl);
});
test('stays plaintext for local development', () {
final config = RelayConfig(baseUrl: 'http://localhost:3000');
expect(config.wsUrl, 'ws://localhost:3000');
});
test('preserves a non-default port', () {
final config = RelayConfig(baseUrl: 'wss://relay.example.com:8443');
expect(config.wsUrl, 'wss://relay.example.com:8443');
});
});
}