Files

199 lines
6.1 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/attention/domain/attention_acks.dart';
import 'package:pipelines_app/features/attention/runtime/attention_ack_queue.dart';
import 'package:pipelines_app/features/attention/runtime/attention_ack_sync.dart';
import 'package:pipelines_app/features/attention/runtime/attention_controller.dart';
import 'package:pipelines_app/features/attention/runtime/attention_store.dart';
import 'fake_ack_server.dart';
final _now = DateTime(2026, 10, 22, 12);
Map<String, dynamic> _failed(int id) => {
'id': id,
'name': 'release',
'title': 'Run $id',
'status': 'failed',
'created_at': _now.subtract(const Duration(hours: 1)).toIso8601String(),
'finished_at': _now.subtract(const Duration(minutes: 30)).toIso8601String(),
};
/// One device: its gateway, sync, local stores and Needs-you controller.
class _Device {
_Device(FakeAckServer server)
: client = server.connect(),
store = MemoryAttentionStore(),
queue = MemoryAttentionAckQueue() {
sync = AttentionAckSync(gateway: client, queue: queue);
attention = AttentionController(store: store, sync: sync, now: () => _now);
}
final FakeAckClient client;
final MemoryAttentionStore store;
final MemoryAttentionAckQueue queue;
late final AttentionAckSync sync;
late final AttentionController attention;
Future<void> start(List<Map<String, dynamic>> rows) async {
await sync.load();
await attention.load();
attention.seedRows(rows);
await sync.connected();
}
List<int> get shown => [for (final e in attention.entries) e.runId];
void dispose() {
attention.dispose();
sync.dispose();
}
}
void main() {
late FakeAckServer server;
late _Device web;
late _Device phone;
final rows = [_failed(1), _failed(2), _failed(3)];
setUp(() async {
server = FakeAckServer();
web = _Device(server);
phone = _Device(server);
await web.start(rows);
await phone.start(rows);
});
tearDown(() {
web.dispose();
phone.dispose();
});
test(
'a dismissal on one device disappears on the other via the push',
() async {
expect(phone.shown, [3, 2, 1]);
await web.attention.dismiss(
web.attention.entries.firstWhere((e) => e.runId == 2),
);
await settle();
expect(web.shown, [3, 1]);
expect(phone.shown, [3, 1]);
expect(server.state.dismissed, {'2:Failed'});
// The other device caches the shared identity locally for offline use.
expect(phone.store.stored, contains('2:Failed'));
// The receiving device did not send anything back.
expect(phone.client.count('ack_attention'), 0);
},
);
test('Clear all on the web clears the phone too', () async {
final snapshot = web.attention.entries;
await web.attention.clearAll(snapshot);
await settle();
expect(web.shown, isEmpty);
expect(phone.shown, isEmpty);
expect(server.state.dismissed, {'1:Failed', '2:Failed', '3:Failed'});
});
test('a device that connects later loads the shared set', () async {
await web.attention.dismiss(web.attention.entries.first);
await settle();
final late = _Device(server);
addTearDown(late.dispose);
await late.start(rows);
await settle();
expect(late.shown, [2, 1]);
});
test(
'a dismissal made offline is queued, then synced on reconnect',
() async {
phone.client.online = false;
await phone.attention.dismiss(phone.attention.entries.first);
await settle();
expect(phone.shown, [2, 1]);
expect(web.shown, [3, 2, 1]);
expect(phone.sync.pending.dismissed, {'3:Failed'});
expect(AttentionAcks.decode(phone.queue.stored).dismissed, {'3:Failed'});
// A restarted app reads the persisted queue.
final restarted = AttentionAckSync(
gateway: phone.client,
queue: phone.queue,
);
addTearDown(restarted.dispose);
await restarted.load();
expect(restarted.pending.dismissed, {'3:Failed'});
phone.client.online = true;
await phone.sync.connected();
await settle();
expect(phone.sync.pending.isEmpty, isTrue);
expect(AttentionAcks.decode(phone.queue.stored).isEmpty, isTrue);
expect(web.shown, [2, 1]);
},
);
test('a refused batch is dropped; a legacy server keeps the queue', () async {
server.refuse = 'invalid attention identity';
await web.sync.acknowledge(dismissed: ['9:Failed']);
expect(web.sync.pending.isEmpty, isTrue);
server
..refuse = null
..legacy = true;
await web.sync.connected();
expect(web.sync.supported, isFalse);
await web.attention.dismiss(web.attention.entries.first);
await settle();
expect(web.shown, [2, 1], reason: 'the local dismissal still applies');
expect(web.sync.pending.dismissed, {'3:Failed'});
server.legacy = false;
await web.sync.connected();
await settle();
expect(web.sync.pending.isEmpty, isTrue);
expect(phone.shown, [2, 1]);
});
test('batches stay within the server bound', () {
final big = AttentionAcks(
dismissed: {for (var i = 1; i <= 600; i++) '$i:Failed'},
chatSeen: const {'ses_a': 1},
);
final batch = attentionAckBatch(big);
expect(batch.length, maxAttentionAckItems);
final rest = big.without(batch);
expect(rest.length, big.length - maxAttentionAckItems);
});
test('the send-only path matches replies and refusals by id', () async {
final client = server.connect();
final sync = AttentionAckSync(
gateway: client,
queue: MemoryAttentionAckQueue(),
sendOnly: client.sendOnly,
);
addTearDown(sync.dispose);
await sync.acknowledge(dismissed: ['1:Failed']);
await settle();
expect(sync.pending.isEmpty, isTrue);
expect(phone.shown, [3, 2]);
server.refuse = 'invalid attention identity';
await sync.acknowledge(dismissed: ['2:Failed']);
await settle();
expect(sync.pending.isEmpty, isTrue, reason: 'a refusal is dropped');
server.refuse = null;
await web.attention.dismiss(web.attention.entries.first);
await settle();
await sync.connected();
expect(sync.acks.dismissed, containsAll(['1:Failed', '3:Failed']));
});
}