mirror of
https://github.com/zapstore/zapstore.git
synced 2026-10-06 04:58:23 +00:00
141 lines
4.6 KiB
Dart
141 lines
4.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:models/models.dart';
|
|
import 'package:zapstore/constants/app_constants.dart';
|
|
import 'package:zapstore/services/unmanaged_apps_service.dart';
|
|
|
|
void main() {
|
|
setUpAll(() {
|
|
Model.register(
|
|
kind: 30267,
|
|
constructor: AppStack.fromMap,
|
|
partialConstructor: PartialAppStack.fromMap,
|
|
);
|
|
});
|
|
|
|
group('UnmanagedAppsNotifier', () {
|
|
test('second unmanage includes the first app', () async {
|
|
final writes = <Set<String>>[];
|
|
final notifier = UnmanagedAppsNotifier((appIds, _) async {
|
|
writes.add(appIds);
|
|
});
|
|
addTearDown(notifier.dispose);
|
|
|
|
await notifier.toggle('app.one', unmanage: true);
|
|
await notifier.toggle('app.two', unmanage: true);
|
|
|
|
expect(notifier.state, {'app.one', 'app.two'});
|
|
expect(writes, [
|
|
{'app.one'},
|
|
{'app.one', 'app.two'},
|
|
]);
|
|
});
|
|
|
|
test('serializes overlapping writes and keeps optimistic state', () async {
|
|
final firstWrite = Completer<void>();
|
|
final writes = <Set<String>>[];
|
|
final notifier = UnmanagedAppsNotifier((appIds, _) async {
|
|
writes.add(appIds);
|
|
if (writes.length == 1) await firstWrite.future;
|
|
});
|
|
addTearDown(notifier.dispose);
|
|
|
|
final first = notifier.toggle('app.one', unmanage: true);
|
|
final second = notifier.toggle('app.two', unmanage: true);
|
|
|
|
expect(notifier.state, {'app.one', 'app.two'});
|
|
firstWrite.complete();
|
|
await Future.wait([first, second]);
|
|
|
|
expect(writes, [
|
|
{'app.one'},
|
|
{'app.one', 'app.two'},
|
|
]);
|
|
});
|
|
|
|
test('uses increasing event seconds for rapid replacements', () async {
|
|
final timestamps = <DateTime>[];
|
|
final now = DateTime.utc(2026, 7, 10, 20, 0, 0, 900);
|
|
final notifier = UnmanagedAppsNotifier(
|
|
(_, createdAt) async => timestamps.add(createdAt),
|
|
clock: () => now,
|
|
);
|
|
addTearDown(notifier.dispose);
|
|
|
|
await notifier.toggle('app.one', unmanage: true);
|
|
await notifier.toggle('app.two', unmanage: true);
|
|
|
|
expect(timestamps, [
|
|
DateTime.utc(2026, 7, 10, 20),
|
|
DateTime.utc(2026, 7, 10, 20, 0, 1),
|
|
]);
|
|
});
|
|
|
|
test('writes after a newer persisted replacement', () async {
|
|
final timestamps = <DateTime>[];
|
|
final now = DateTime.utc(2026, 7, 10, 20);
|
|
final notifier = UnmanagedAppsNotifier(
|
|
(_, createdAt) async => timestamps.add(createdAt),
|
|
clock: () => now,
|
|
)..acceptPersisted({'app.remote'}, now.add(const Duration(seconds: 5)));
|
|
addTearDown(notifier.dispose);
|
|
|
|
await notifier.toggle('app.local', unmanage: true);
|
|
|
|
expect(timestamps, [now.add(const Duration(seconds: 6))]);
|
|
expect(notifier.state, {'app.remote', 'app.local'});
|
|
});
|
|
|
|
test('rolls back optimistic state when local save fails', () async {
|
|
final persistedAt = DateTime.utc(2026, 7, 10, 19);
|
|
final notifier = UnmanagedAppsNotifier(
|
|
(_, _) async => throw const UnmanagedAppsSaveException('save failed'),
|
|
)..acceptPersisted({'app.one'}, persistedAt);
|
|
addTearDown(notifier.dispose);
|
|
|
|
final write = notifier.toggle('app.two', unmanage: true);
|
|
expect(notifier.state, {'app.one', 'app.two'});
|
|
|
|
await expectLater(write, throwsA(isA<UnmanagedAppsSaveException>()));
|
|
expect(notifier.state, {'app.one'});
|
|
});
|
|
});
|
|
|
|
test('unmanaged stack includes relay-required coordinate tags', () {
|
|
final createdAt = DateTime.utc(2026, 7, 10, 20);
|
|
final stack = createUnmanagedAppsStack(
|
|
appIds: {'app.one', 'app.two'},
|
|
platform: 'android-arm64-v8a',
|
|
createdAt: createdAt,
|
|
);
|
|
|
|
expect(stack.identifier, kUnmanagedAppsIdentifier);
|
|
expect(stack.platform, 'android-arm64-v8a');
|
|
expect(stack.privateAppIds.toSet(), {'app.one', 'app.two'});
|
|
expect(stack.event.createdAt, createdAt);
|
|
expect(stack.event.containsTag('h'), isFalse);
|
|
});
|
|
|
|
group('wasUnmanagedStackAccepted', () {
|
|
test('requires explicit acceptance for the stack event', () {
|
|
final accepted = PublishResponse()
|
|
..addEvent(
|
|
'stack-id',
|
|
relayUrl: 'wss://relay.zapstore.dev',
|
|
accepted: true,
|
|
);
|
|
final rejected = PublishResponse()
|
|
..addEvent(
|
|
'stack-id',
|
|
relayUrl: 'wss://relay.zapstore.dev',
|
|
accepted: false,
|
|
);
|
|
|
|
expect(wasUnmanagedStackAccepted(accepted, 'stack-id'), isTrue);
|
|
expect(wasUnmanagedStackAccepted(rejected, 'stack-id'), isFalse);
|
|
expect(wasUnmanagedStackAccepted(PublishResponse(), 'stack-id'), isFalse);
|
|
});
|
|
});
|
|
}
|