9dfa06ffee
Docker image / Build (linux/amd64) (push) Has been cancelled
Docker image / Build (linux/arm64) (push) Has been cancelled
Docker image / Merge release multi-arch manifest (push) Has been cancelled
Docker image / Merge debug multi-arch manifest (push) Has been cancelled
Docker image / Build public push gateway (linux/amd64) (push) Has been cancelled
Docker image / Build public push gateway (linux/arm64) (push) Has been cancelled
Docker image / Publish public push gateway image (push) Has been cancelled
Sprig image / Build (linux/amd64) (push) Has been cancelled
Sprig image / Build (linux/arm64) (push) Has been cancelled
Sprig image / Merge multi-arch manifest (push) Has been cancelled
Harbor Buzz Orchestra / Python tests and lint (push) Has been cancelled
CI / Detect Changed Paths (push) Has been cancelled
CI / Rust Lint (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Desktop Core (push) Has been cancelled
CI / Desktop Smoke E2E (1) (push) Has been cancelled
CI / Desktop Smoke E2E (2) (push) Has been cancelled
CI / Desktop Smoke E2E (3) (push) Has been cancelled
CI / Desktop Smoke E2E (4) (push) Has been cancelled
CI / Desktop (push) Has been cancelled
CI / Desktop E2E Relay (push) Has been cancelled
CI / Desktop E2E Integration (1/2) (push) Has been cancelled
CI / Desktop E2E Integration (2/2) (push) Has been cancelled
CI / Desktop E2E Integration (push) Has been cancelled
CI / Backend Integration (relay e2e) (push) Has been cancelled
CI / Relay E2E (push) Has been cancelled
CI / Web (push) Has been cancelled
CI / Mobile (push) Has been cancelled
CI / Security (push) Has been cancelled
CI / Dead Token Reference Guard (push) Has been cancelled
CI / Server Cross-Compile (aarch64-unknown-linux-musl) (push) Has been cancelled
CI / Server Cross-Compile (x86_64-unknown-linux-musl) (push) Has been cancelled
CI / Windows Rust (x86_64-pc-windows-msvc) (push) Has been cancelled
CI / Desktop Build (macOS) (push) Has been cancelled
helm chart / lint + unittest + render matrix (push) Has been cancelled
helm chart / install on kind (gated) (push) Has been cancelled
helm chart / publish chart to GHCR (push) Has been cancelled
Mesh Lifecycle / Relay-Driven Mesh Lifecycle Smoke (push) Has been cancelled
Sprig / Build (aarch64-unknown-linux-musl) (push) Has been cancelled
Sprig / Build (x86_64-unknown-linux-musl) (push) Has been cancelled
Sprig / Publish rolling release (push) Has been cancelled
Sprig / Publish tagged release (push) Has been cancelled
Signed-off-by: cls_宁波本机 <908705107@qq.com>
312 lines
10 KiB
Dart
312 lines
10 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:buzz/shared/crypto/nip44.dart';
|
|
import 'package:buzz/shared/relay/relay.dart';
|
|
import 'package:buzz/shared/theme/theme.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
void main() {
|
|
test('delayed absence seeds the intervening local edit', () async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final keys = nostr.Keys.generate();
|
|
final history = Completer<List<NostrEvent>>();
|
|
final session = _ThemeRelaySession(
|
|
keys.nsec,
|
|
keys.public,
|
|
historyFuture: history.future,
|
|
);
|
|
final storage = CommunityThemeStorage(prefs);
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityThemeStorageProvider.overrideWithValue(storage),
|
|
relayConfigProvider.overrideWith(() => _RelayConfig(keys.nsec)),
|
|
relaySessionProvider.overrideWith(() => session),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
container.listen(communityThemeProvider, (_, _) {}, fireImmediately: true);
|
|
|
|
container.read(communityThemeProvider.notifier).setTheme('dracula');
|
|
history.complete([]);
|
|
await _waitUntil(() => session.published != null);
|
|
|
|
expect(container.read(communityThemeProvider).theme, 'dracula');
|
|
expect(
|
|
storage.read(keys.public, 'https://relay.example')?.theme,
|
|
'dracula',
|
|
);
|
|
});
|
|
|
|
test('edit during delayed absence seed wins durable state', () async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final keys = nostr.Keys.generate();
|
|
final history = Completer<List<NostrEvent>>();
|
|
final session = _ThemeRelaySession(
|
|
keys.nsec,
|
|
keys.public,
|
|
historyFuture: history.future,
|
|
);
|
|
final storage = _DelayedThemeStorage(prefs);
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityThemeStorageProvider.overrideWithValue(storage),
|
|
relayConfigProvider.overrideWith(() => _RelayConfig(keys.nsec)),
|
|
relaySessionProvider.overrideWith(() => session),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
container.listen(communityThemeProvider, (_, _) {}, fireImmediately: true);
|
|
|
|
history.complete([]);
|
|
await storage.cacheWriteStarted.future;
|
|
container.read(communityThemeProvider.notifier).setTheme('dracula');
|
|
storage.allowCacheWrite.complete();
|
|
storage.allowOutboxWrite.complete();
|
|
await _waitUntil(() => session.published != null);
|
|
|
|
expect(container.read(communityThemeProvider).theme, 'dracula');
|
|
expect(
|
|
storage.read(keys.public, 'https://relay.example')?.theme,
|
|
'dracula',
|
|
);
|
|
final privateHex = nostr.Nip19.decode(payload: keys.nsec).data;
|
|
final key = getConversationKey(privateHex, keys.public);
|
|
expect(
|
|
jsonDecode(nip44Decrypt(key, session.published!.content))['theme'],
|
|
'dracula',
|
|
);
|
|
});
|
|
|
|
test(
|
|
'local edit stays authoritative before persistence through exact ack',
|
|
() async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final keys = nostr.Keys.generate();
|
|
final session = _ThemeRelaySession(keys.nsec, keys.public);
|
|
final storage = _DelayedThemeStorage(prefs);
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityThemeStorageProvider.overrideWithValue(storage),
|
|
relayConfigProvider.overrideWith(() => _RelayConfig(keys.nsec)),
|
|
relaySessionProvider.overrideWith(() => session),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
|
|
final subscription = container.listen(
|
|
communityThemeProvider,
|
|
(_, _) {},
|
|
fireImmediately: true,
|
|
);
|
|
addTearDown(subscription.close);
|
|
await session.subscribed.future;
|
|
|
|
final notifier = container.read(communityThemeProvider.notifier);
|
|
notifier.setTheme('dracula');
|
|
const local = CommunityThemePreference(
|
|
theme: 'dracula',
|
|
accent: '#3b82f6',
|
|
followSystem: true,
|
|
);
|
|
expect(container.read(communityThemeProvider), local);
|
|
|
|
session.emit(session.remoteEvent(theme: 'houston', id: 'remote-z'));
|
|
expect(container.read(communityThemeProvider), local);
|
|
|
|
storage.allowCacheWrite.complete();
|
|
await storage.outboxWriteStarted.future;
|
|
session.emit(session.remoteEvent(theme: 'solarized', id: 'remote-a'));
|
|
expect(container.read(communityThemeProvider), local);
|
|
|
|
storage.allowOutboxWrite.complete();
|
|
await _waitUntil(() => session.published != null);
|
|
expect(container.read(communityThemeProvider), local);
|
|
|
|
session.emit(session.published!);
|
|
await _pumpEventQueue();
|
|
expect(container.read(communityThemeProvider), local);
|
|
expect(storage.readOutbox(keys.public, 'https://relay.example'), isNull);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'provider rebuild preserves delayed local edit and publishes on replacement manager',
|
|
() async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final keys = nostr.Keys.generate();
|
|
final session = _ThemeRelaySession(keys.nsec, keys.public);
|
|
final storage = _DelayedThemeStorage(prefs);
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityThemeStorageProvider.overrideWithValue(storage),
|
|
relayConfigProvider.overrideWith(() => _RelayConfig(keys.nsec)),
|
|
relaySessionProvider.overrideWith(() => session),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
final subscription = container.listen(
|
|
communityThemeProvider,
|
|
(_, _) {},
|
|
fireImmediately: true,
|
|
);
|
|
addTearDown(subscription.close);
|
|
await session.subscribed.future;
|
|
|
|
container.read(communityThemeProvider.notifier).setTheme('dracula');
|
|
expect(container.read(communityThemeProvider).theme, 'dracula');
|
|
await storage.cacheWriteStarted.future;
|
|
|
|
session.setStatus(SessionStatus.reconnecting);
|
|
await _pumpEventQueue();
|
|
expect(container.read(communityThemeProvider).theme, 'dracula');
|
|
session.setStatus(SessionStatus.connected);
|
|
await _waitUntil(() => session.subscribeCalls == 2);
|
|
expect(container.read(communityThemeProvider).theme, 'dracula');
|
|
|
|
storage.allowCacheWrite.complete();
|
|
storage.allowOutboxWrite.complete();
|
|
await _waitUntil(() => session.published != null);
|
|
|
|
final privateHex = nostr.Nip19.decode(payload: keys.nsec).data;
|
|
final key = getConversationKey(privateHex, keys.public);
|
|
expect(
|
|
jsonDecode(nip44Decrypt(key, session.published!.content))['theme'],
|
|
'dracula',
|
|
);
|
|
expect(container.read(communityThemeProvider).theme, 'dracula');
|
|
},
|
|
);
|
|
}
|
|
|
|
class _DelayedThemeStorage extends CommunityThemeStorage {
|
|
_DelayedThemeStorage(super.prefs);
|
|
|
|
final allowCacheWrite = Completer<void>();
|
|
final allowOutboxWrite = Completer<void>();
|
|
final cacheWriteStarted = Completer<void>();
|
|
final outboxWriteStarted = Completer<void>();
|
|
|
|
@override
|
|
Future<bool> write(
|
|
String pubkey,
|
|
String relayUrl,
|
|
CommunityThemePreference preference,
|
|
) async {
|
|
if (!cacheWriteStarted.isCompleted) cacheWriteStarted.complete();
|
|
await allowCacheWrite.future;
|
|
return super.write(pubkey, relayUrl, preference);
|
|
}
|
|
|
|
@override
|
|
Future<bool> writeOutbox(
|
|
String pubkey,
|
|
String relayUrl,
|
|
CommunityThemePreference preference,
|
|
) async {
|
|
if (!outboxWriteStarted.isCompleted) outboxWriteStarted.complete();
|
|
await allowOutboxWrite.future;
|
|
return super.writeOutbox(pubkey, relayUrl, preference);
|
|
}
|
|
}
|
|
|
|
class _RelayConfig extends RelayConfigNotifier {
|
|
_RelayConfig(this.nsec);
|
|
|
|
final String nsec;
|
|
|
|
@override
|
|
RelayConfig build() =>
|
|
RelayConfig(baseUrl: 'https://relay.example', nsec: nsec);
|
|
}
|
|
|
|
class _ThemeRelaySession extends RelaySessionNotifier {
|
|
_ThemeRelaySession(this.nsec, this.pubkey, {this.historyFuture});
|
|
|
|
final String nsec;
|
|
final String pubkey;
|
|
final Future<List<NostrEvent>>? historyFuture;
|
|
final subscribed = Completer<void>();
|
|
int subscribeCalls = 0;
|
|
void Function(NostrEvent)? _listener;
|
|
NostrEvent? published;
|
|
|
|
@override
|
|
SessionState build() => const SessionState(status: SessionStatus.connected);
|
|
|
|
@override
|
|
Future<List<NostrEvent>> fetchHistory(
|
|
NostrFilter filter, {
|
|
Duration timeout = const Duration(seconds: 8),
|
|
}) async => historyFuture ?? [remoteEvent(theme: 'buzz', id: 'initial')];
|
|
|
|
@override
|
|
Future<void Function()> subscribe(
|
|
NostrFilter filter,
|
|
void Function(NostrEvent) onEvent, {
|
|
void Function(String message)? onClosed,
|
|
}) async {
|
|
subscribeCalls++;
|
|
_listener = onEvent;
|
|
if (!subscribed.isCompleted) subscribed.complete();
|
|
return () => _listener = null;
|
|
}
|
|
|
|
@override
|
|
Future<NostrEvent> publish(
|
|
NostrEvent event, {
|
|
Duration timeout = const Duration(seconds: 8),
|
|
}) async {
|
|
published = event;
|
|
return event;
|
|
}
|
|
|
|
void emit(NostrEvent event) => _listener?.call(event);
|
|
|
|
void setStatus(SessionStatus status) {
|
|
state = SessionState(status: status);
|
|
}
|
|
|
|
NostrEvent remoteEvent({required String theme, required String id}) {
|
|
final privateHex = nostr.Nip19.decode(payload: nsec).data;
|
|
final key = getConversationKey(privateHex, pubkey);
|
|
final preference = CommunityThemePreference(
|
|
theme: theme,
|
|
accent: '#3b82f6',
|
|
followSystem: true,
|
|
);
|
|
return NostrEvent(
|
|
id: id,
|
|
pubkey: pubkey,
|
|
createdAt: 1,
|
|
kind: 30078,
|
|
tags: const [
|
|
['d', communityThemeDTag],
|
|
['t', communityThemeDTag],
|
|
],
|
|
content: nip44Encrypt(key, jsonEncode(preference.toJson())),
|
|
sig: 'sig',
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> _pumpEventQueue() async {
|
|
await Future<void>.delayed(Duration.zero);
|
|
await Future<void>.delayed(Duration.zero);
|
|
}
|
|
|
|
Future<void> _waitUntil(bool Function() condition) async {
|
|
final deadline = DateTime.now().add(const Duration(seconds: 3));
|
|
while (!condition()) {
|
|
if (DateTime.now().isAfter(deadline)) fail('condition not met');
|
|
await Future<void>.delayed(const Duration(milliseconds: 5));
|
|
}
|
|
}
|