Files
buzz/mobile/test/shared/hosted/hosted_backup_service_test.dart
klopez4212 0cde1fe2f4 Make mobile Agents a full-page owned-agent directory (#8693)
Replace the mobile Agents sheet with a full-page directory of verified
owned agents. Add dedicated creation and avatar pages with
catalog-backed model selection, reuse the profile emoji/photo/camera
controls, and expose Builderbot model settings while labeling desktop
agents “Manage on desktop.” Share one account-scoped model catalog
across editable cards and retries. Choose Bestie from the agent profile
using the shared encrypted main-agent record (text-only action, filled
star once selected), and move confirmed deletion to a top-left trash
control with native iOS glass. Include uploaded avatars in owner
registrations so desktop can display them.

Validation: mobile analysis and all 3,958 hosted-enabled app tests
passed; default-build tests run by the push hook. Earlier iPhone build
installed and iterated with the user; the current iOS build succeeds,
but final changes still need a device pass. Independent review found no
new blockers. Actual camera capture and live Bestie sync remain
unconfirmed; the existing shared `main-agent` wire format and Bestie
terminology are retained.

### Snapshots

Widget-harness captures with test data (not native iOS chrome).

| Agents directory | Image avatar editor |
| --- | --- |
| ![Agents
directory](https://github.com/user-attachments/assets/c68a0505-90a5-41db-980f-e3197cac63ac)
| ![Image avatar
editor](https://github.com/user-attachments/assets/9b126186-1e7c-4169-9a74-8f7f252cfa93)
|

---------

Signed-off-by: kenny lopez <klopez4212@gmail.com>
Co-authored-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz>
2026-10-29 18:30:17 +00:00

180 lines
5.7 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:buzz/shared/hosted/account_identity_store.dart';
import 'package:buzz/shared/hosted/builderlab_account.dart';
import 'package:buzz/shared/hosted/hosted_backup_service.dart';
import 'package:buzz/shared/hosted/hpke_backup.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:nostr/nostr.dart' as nostr;
class FakeAccount extends BuilderlabAccountClient {
FakeAccount(this.envelope);
final Map<String, dynamic> envelope;
int enrollments = 0;
final uploads = <String>[];
bool fail = true;
bool reject = false;
bool rejectEnrollment = false;
String subject = 'subject';
@override
BuilderlabAccount get account =>
BuilderlabAccount(subject: subject, expiresAt: DateTime(2100));
@override
Future<Map<String, dynamic>> post(
String path,
Map<String, dynamic> body, {
String? expectedSubject,
}) async {
if (path.endsWith('/enrollment')) {
enrollments++;
if (rejectEnrollment) throw AccountFailure.rejected;
return envelope;
}
uploads.add(jsonEncode(body));
expect(
await const FlutterSecureStorage().read(
key: HostedBackupService.pendingKey(
subject,
envelope['nostr_pubkey'] as String,
),
),
isNotNull,
);
if (reject) throw AccountFailure.rejected;
if (fail) throw AccountFailure.unavailable;
return {'stored': true, 'backup_id': envelope['backup_id']};
}
}
class FakeHpke extends HpkeBackup {
FakeHpke(this.envelope);
final Map<String, dynamic> envelope;
int calls = 0;
@override
Future<Map<String, dynamic>> seal({
required Map<String, dynamic> enrollment,
required String secretHex,
}) async {
calls++;
return envelope;
}
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late Map<String, dynamic> envelope;
setUp(() {
FlutterSecureStorage.setMockInitialValues({});
final fixture =
jsonDecode(
File(
'../desktop/src-tauri/src/testdata/hpke_nsec_backup_v1.json',
).readAsStringSync(),
)
as Map<String, dynamic>;
envelope = Map<String, dynamic>.from(fixture['envelope'] as Map);
});
test(
'uncertain upload replays persisted envelope after restart and receipt prevents further upload',
() async {
final identity = AccountIdentityStore();
await identity.create(nostr.Keys('42' * 32).nsec);
final account = FakeAccount(envelope);
final hpke = FakeHpke(envelope);
await expectLater(
HostedBackupService(account, identity, hpke: hpke).backup(),
throwsA(AccountFailure.unavailable),
);
expect((await identity.read())!.backup, isNull);
account.fail = false;
await HostedBackupService(
account,
AccountIdentityStore(),
hpke: hpke,
).backup();
expect(account.enrollments, 1);
expect(hpke.calls, 1);
expect(account.uploads, hasLength(2));
expect(account.uploads.first, account.uploads.last);
expect((await identity.read())!.backup!.backupId, envelope['backup_id']);
await HostedBackupService(account, identity, hpke: hpke).backup();
expect(account.uploads, hasLength(2));
},
);
test('unreadable pending ciphertext blocks enrollment and upload', () async {
final identity = AccountIdentityStore();
await identity.create(nostr.Keys('42' * 32).nsec);
await const FlutterSecureStorage().write(
key: HostedBackupService.pendingKey(
'subject',
envelope['nostr_pubkey'] as String,
),
value: 'invalid',
);
final account = FakeAccount(envelope);
await expectLater(
HostedBackupService(account, identity).backup(),
throwsA(HostedBackupFailure.storage),
);
expect(account.enrollments, 0);
expect(account.uploads, isEmpty);
});
test(
'definitive rejection permits only one re-enrollment per attempt',
() async {
final identity = AccountIdentityStore();
await identity.create(nostr.Keys('42' * 32).nsec);
final account = FakeAccount(envelope)..reject = true;
await expectLater(
HostedBackupService(
account,
identity,
hpke: FakeHpke(envelope),
).backup(),
throwsA(HostedBackupFailure.rejected),
);
expect(account.enrollments, 2);
expect(account.uploads, hasLength(2));
expect((await identity.read())!.backup, isNull);
},
);
test('enrollment rejection is terminal without upload', () async {
final identity = AccountIdentityStore();
await identity.create(nostr.Keys('42' * 32).nsec);
final account = FakeAccount(envelope)..rejectEnrollment = true;
await expectLater(
HostedBackupService(account, identity).backup(),
throwsA(HostedBackupFailure.unavailable),
);
expect(account.enrollments, 1);
expect(account.uploads, isEmpty);
});
test(
'another subject pending envelope neither blocks nor gets replaced',
() async {
final identity = AccountIdentityStore();
await identity.create(nostr.Keys('42' * 32).nsec);
final oldKey = HostedBackupService.pendingKey(
'other',
envelope['nostr_pubkey'] as String,
);
await const FlutterSecureStorage().write(
key: oldKey,
value: 'old pending',
);
final account = FakeAccount(envelope)..fail = false;
await HostedBackupService(
account,
identity,
hpke: FakeHpke(envelope),
).backup();
expect(
await const FlutterSecureStorage().read(key: oldKey),
'old pending',
);
expect(account.enrollments, 1);
},
);
}