Files
buzz/mobile/test/features/agents/agents_harness.dart
Tom Brow 64312fa5be fix(mobile): address #8650 second-pass review (#8691)
Follow-up to #8650. This addresses the items from the [second-pass
review](https://github.com/block/buzz/pull/8650#pullrequestreview-5700660448),
which arrived after #8650 merged.

- **Setting menu semantics.** Options in the shared `BuzzSettingMenu`
(model, reasoning effort, and channel TTL) expose their checked state
again. The trigger is announced as a button with enabled and expanded
state, so a disabled "Reasoning effort" no longer reads as plain text.
The `PopupMenuButton` this menu replaced also provided a named route and
a "Dismiss menu" action. `MenuAnchor` does not provide them, so this PR
does not restore them.
- **Remote listing failures.** "Could not load agents" for a failed
Builderlab listing now appears only in communities that support remote
agents. For example, it no longer appears in a non-hosted community
while signed in.
- **Add agent during a community reload.** No change needed here. #8693
already disables Add while the page has no community.

### Validation

- Added tests: setting-menu checked, button, enabled and expanded
semantics; a failed remote listing stays hidden where remote agents are
off. Both fail without these changes.
- Mobile analysis is clean, and the Agents and shared widget tests pass
locally after rebasing onto #8693.
- Not covered: VoiceOver on a device.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@sprout-oss.stage.blox.sqprod.co>
2026-10-29 15:36:21 -07:00

206 lines
6.8 KiB
Dart

import 'package:buzz/features/agents/community_bot_profiles.dart';
import 'package:buzz/shared/me/me_sync.dart';
import '../../shared/me/me_sync_test.dart' show FakeRelay;
import 'dart:async';
import 'package:nostr/nostr.dart' as nostr;
import 'package:buzz/features/agents/agents_page.dart';
import 'package:buzz/features/agents/owned_agent_directory.dart';
import 'package:buzz/shared/agents/remote_agents.dart';
import 'package:buzz/shared/community/community.dart';
import 'package:buzz/shared/community/community_provider.dart';
import 'package:buzz/shared/hosted/account_identity_store.dart';
import 'package:buzz/shared/hosted/hosted_environment.dart';
import 'package:buzz/shared/hosted/hosted_relay_auth.dart';
import 'package:buzz/shared/mentions/agent_identity_provider.dart';
import 'package:buzz/shared/relay/relay.dart';
import 'package:buzz/shared/theme/theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../helpers/remote_agents_fixture.dart';
final existingDesktopKeys = nostr.Keys('0' * 63 + '3');
final otherAgentKeys = nostr.Keys('0' * 63 + '4');
final otherOwnerKeys = nostr.Keys('0' * 63 + '5');
/// Fakes the relay boundary: owner-attested profiles for registry agents.
class _Relay extends FakeRelay {
_Relay(this.harness);
final AgentsHarness harness;
@override
SessionState build() => const SessionState(status: SessionStatus.connected);
@override
Future<List<NostrEvent>> fetchHistory(
NostrFilter filter, {
Duration timeout = const Duration(seconds: 8),
}) async {
if ((filter.authors?.contains(fakeAgent) ?? false) && harness.relayDown) {
throw TimeoutException('relay unavailable');
}
return filter.kinds.contains(30078)
? events
: [
if (filter.authors?.contains(existingDesktopKeys.public) ?? false)
signedAgentProfile(
existingDesktopKeys,
fakeOwner,
content: '{"name":"My desktop agent"}',
),
if (filter.authors?.contains(otherAgentKeys.public) ?? false)
signedAgentProfile(
otherAgentKeys,
otherOwnerKeys,
content: '{"name":"Other owner agent"}',
),
if (filter.authors?.contains(fakeAgent) ?? false)
signedAgentProfile(
fakeAgentKeys,
fakeOwner,
content: '{"name":"Scout"}',
),
];
}
}
/// Holds creation open so a test can observe the in-flight state.
class GatedController extends RelayFakeController {
GatedController(super.account);
Completer<void>? gate;
String? submittedName;
String? submittedInstructions;
@override
Future<bool> create(
Community community,
String name, {
String instructions = '',
String? pictureUrl,
String? modelId,
String? reasoningEffort,
}) {
submittedName = name;
submittedInstructions = instructions;
final gate = this.gate;
if (gate == null) {
return super.create(
community,
name,
instructions: instructions,
pictureUrl: pictureUrl,
modelId: modelId,
reasoningEffort: reasoningEffort,
);
}
return run(() async {
await gate.future;
throw StateError('Creation failed. Retry.');
});
}
}
/// One signed-in owner whose saved key matches the account binding.
///
/// [AccountIdentityStore] serializes through a static queue whose futures
/// belong to the fake-async zone that created them, so each file holds only
/// one widget test that touches the store.
class AgentsHarness {
AgentsHarness._(this.registry, this.controller, this.community);
final FakeRegistry registry;
final GatedController controller;
final Community community;
late final relay = _Relay(this);
/// Fails agent profile fetches until the relay recovers.
bool relayDown = false;
/// [relayUrl] defaults to a hosted community that supports remote agents.
static Future<AgentsHarness> create({String? relayUrl}) async {
FlutterSecureStorage.setMockInitialValues({});
SharedPreferences.setMockInitialValues({});
final registry = FakeRegistry(bound: fakeOwner.public);
await AccountIdentityStore()
.forSubject(registry.account!.subject)
.create(fakeOwner.nsec);
final controller = GatedController(registry);
addTearDown(() {
controller.dispose();
controller.relayAuth.dispose();
registry.dispose();
});
return AgentsHarness._(
registry,
controller,
Community.create(
name: 'Test',
relayUrl: relayUrl ?? 'wss://test.${HostedEnvironment.communitySuffix}',
pubkey: fakeOwner.public,
nsec: fakeOwner.nsec,
),
);
}
/// Opens the Agents sheet over a host page.
Future<void> open(
WidgetTester tester, {
required List<String> directory,
List<String> botDirectory = const [],
Widget? home,
}) async {
final prefs = await SharedPreferences.getInstance();
final sync = MeSync(
relay,
RelayConfig(baseUrl: 'https://test', nsec: fakeOwner.nsec),
prefs: prefs,
);
await tester.pumpWidget(
ProviderScope(
overrides: [
communityBotProfilesProvider.overrideWith(
(ref) async => botDirectory,
),
ownedAgentDirectoryProvider.overrideWith((ref) async => []),
builderlabAccountProvider.overrideWithValue(registry),
remoteAgentControllerProvider.overrideWithValue(controller),
relaySessionProvider.overrideWith(() => relay),
meSyncProvider.overrideWithValue(sync),
activeCommunityProvider.overrideWith((ref) async => community),
agentDirectoryProvider.overrideWith(
(ref) async => [
for (final pubkey in directory)
AgentDirectoryEntry(pubkey: pubkey),
],
),
],
child: MaterialApp(
theme: AppTheme.light(),
builder: (context, child) => MediaQuery(
data: MediaQuery.of(context).copyWith(disableAnimations: true),
child: child!,
),
home:
home ??
Builder(
builder: (context) => Scaffold(
body: TextButton(
onPressed: () => showAgents(context),
child: const Text('Open Agents'),
),
),
),
),
),
);
await tester.pumpAndSettle();
if (home == null) {
await tester.tap(find.text('Open Agents'));
await tester.pumpAndSettle();
}
}
}