251 lines
8.1 KiB
Dart
251 lines
8.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/features/attention/domain/attention_models.dart';
|
|
import 'package:pipelines_app/features/attention/runtime/attention_controller.dart';
|
|
import 'package:pipelines_app/features/attention/runtime/attention_store.dart';
|
|
|
|
final _now = DateTime(2026, 10, 22, 12);
|
|
|
|
String _at(Duration before) => _now.subtract(before).toIso8601String();
|
|
|
|
Map<String, dynamic> _run(
|
|
int id,
|
|
String status, {
|
|
String name = 'release',
|
|
String? title,
|
|
Duration age = const Duration(hours: 1),
|
|
String diagnostic = '',
|
|
Object? exportPending,
|
|
Map<String, dynamic> extra = const {},
|
|
}) => {
|
|
'id': id,
|
|
'name': name,
|
|
'title': title ?? 'Run $id',
|
|
'status': status,
|
|
'diagnostic': diagnostic,
|
|
'created_at': _at(age),
|
|
'finished_at': _at(age - const Duration(minutes: 5)),
|
|
'export_pending_sequence': ?exportPending,
|
|
...extra,
|
|
};
|
|
|
|
AttentionController _controller(AttentionStore store) =>
|
|
AttentionController(store: store, now: () => _now);
|
|
|
|
class _SlowStore extends MemoryAttentionStore {
|
|
final gate = Completer<bool>();
|
|
|
|
@override
|
|
Future<bool> write(String value) {
|
|
writes++;
|
|
stored = value;
|
|
return gate.future;
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
group('entries from the rows the Runs destination already holds', () {
|
|
test('only failed and human waiting rows become entries', () async {
|
|
final controller = _controller(MemoryAttentionStore());
|
|
addTearDown(controller.dispose);
|
|
await controller.load();
|
|
controller.seedRows([
|
|
_run(1, 'running'),
|
|
_run(2, 'succeeded'),
|
|
_run(3, 'queued'),
|
|
_run(4, 'canceled'),
|
|
_run(5, 'failed', diagnostic: 'Stage 2 failed'),
|
|
_run(6, 'waiting', title: 'Approve the release'),
|
|
]);
|
|
|
|
expect(controller.entries.map((entry) => entry.key), [
|
|
'6:Decision',
|
|
'5:Failed',
|
|
]);
|
|
expect(controller.entries.last.detail, 'Stage 2 failed');
|
|
});
|
|
|
|
test('export handoffs and yesterday do not ask the operator', () async {
|
|
final controller = _controller(MemoryAttentionStore());
|
|
addTearDown(controller.dispose);
|
|
await controller.load();
|
|
controller.seedRows([
|
|
_run(1, 'waiting', exportPending: 4),
|
|
_run(2, 'waiting', exportPending: ' 2 '),
|
|
_run(3, 'waiting', exportPending: 0),
|
|
_run(4, 'failed', age: const Duration(days: 1, hours: 2)),
|
|
_run(5, 'waiting', age: const Duration(days: 1)),
|
|
]);
|
|
|
|
expect(controller.entries.map((entry) => entry.runId), [3]);
|
|
});
|
|
});
|
|
|
|
group('dismissal persistence', () {
|
|
test('a dismissed identity survives a rebuilt controller', () async {
|
|
final store = MemoryAttentionStore();
|
|
final first = _controller(store);
|
|
await first.load();
|
|
first.seedRows([_run(7, 'failed')]);
|
|
final entry = first.entries.single;
|
|
|
|
expect(await first.dismiss(entry), isTrue);
|
|
expect(first.entries, isEmpty);
|
|
expect(store.stored, encodeAttentionDismissals({'7:Failed'}));
|
|
first.dispose();
|
|
|
|
final rebuilt = _controller(store);
|
|
addTearDown(rebuilt.dispose);
|
|
await rebuilt.load();
|
|
rebuilt.seedRows([_run(7, 'failed')]);
|
|
expect(rebuilt.dismissed, {'7:Failed'});
|
|
expect(rebuilt.entries, isEmpty);
|
|
});
|
|
|
|
test('Clear all applies only the snapshot, leaving later arrivals', () async {
|
|
final controller = _controller(MemoryAttentionStore());
|
|
addTearDown(controller.dispose);
|
|
await controller.load();
|
|
controller.seedRows([_run(1, 'failed'), _run(2, 'waiting')]);
|
|
final snapshot = attentionDismissalSnapshot(controller.entries);
|
|
|
|
// A third run arrives while the sheet is open. Clear all must not know it.
|
|
controller.seedRows([
|
|
_run(1, 'failed'),
|
|
_run(2, 'waiting'),
|
|
_run(3, 'failed'),
|
|
]);
|
|
expect(await controller.clearAll(snapshot), isTrue);
|
|
|
|
expect(controller.dismissed, {'1:Failed', '2:Decision'});
|
|
expect(controller.entries.map((entry) => entry.key), ['3:Failed']);
|
|
});
|
|
|
|
test('a slow write never holds the bell or entries back', () async {
|
|
final store = _SlowStore();
|
|
final controller = _controller(store);
|
|
addTearDown(controller.dispose);
|
|
await controller.load();
|
|
controller.seedRows([_run(4, 'failed')]);
|
|
final entry = controller.entries.single;
|
|
|
|
final pending = controller.dismiss(entry);
|
|
expect(controller.entries, isEmpty, reason: 'memory applies first');
|
|
expect(store.stored, encodeAttentionDismissals({'4:Failed'}));
|
|
store.gate.complete(true);
|
|
expect(await pending, isTrue);
|
|
});
|
|
|
|
test('failed reads and writes are total and keep this session', () async {
|
|
final store = MemoryAttentionStore()
|
|
..failReads = true
|
|
..failWrites = true;
|
|
final controller = _controller(store);
|
|
addTearDown(controller.dispose);
|
|
|
|
await expectLater(controller.load(), completes);
|
|
expect(controller.loaded, isTrue);
|
|
expect(controller.dismissed, isEmpty);
|
|
|
|
controller.seedRows([_run(8, 'failed')]);
|
|
final entry = controller.entries.single;
|
|
expect(await controller.dismiss(entry), isFalse);
|
|
expect(controller.dismissed, {'8:Failed'});
|
|
expect(controller.entries, isEmpty);
|
|
});
|
|
});
|
|
|
|
group('pushed frames and reconnect', () {
|
|
test('a live frame that becomes failed appears without a reload', () async {
|
|
final controller = _controller(MemoryAttentionStore());
|
|
addTearDown(controller.dispose);
|
|
await controller.load();
|
|
controller.seedRows([_run(1, 'running')]);
|
|
expect(controller.entries, isEmpty);
|
|
|
|
controller.observeFrame({
|
|
'type': 'run_event',
|
|
'run_id': 1,
|
|
'status': 'failed',
|
|
'diagnostic': 'Stage 4 failed',
|
|
'finished_at': _at(const Duration(minutes: 1)),
|
|
});
|
|
|
|
expect(controller.entries.map((entry) => entry.key), ['1:Failed']);
|
|
expect(controller.entries.single.detail, 'Stage 4 failed');
|
|
expect(controller.transitions.map((item) => item.key), ['1:Failed']);
|
|
|
|
// The same state again is not a new transition.
|
|
controller.observeFrame({
|
|
'type': 'run_event',
|
|
'run_id': 1,
|
|
'status': 'failed',
|
|
'diagnostic': 'Stage 4 failed',
|
|
});
|
|
expect(controller.entries, hasLength(1));
|
|
expect(controller.transitions, hasLength(1));
|
|
});
|
|
|
|
test('a live frame for a new run can create its own summary', () async {
|
|
final controller = _controller(MemoryAttentionStore());
|
|
addTearDown(controller.dispose);
|
|
await controller.load();
|
|
controller.seedRows(const []);
|
|
|
|
controller.observeFrame({
|
|
'type': 'run_event',
|
|
'run_id': 9,
|
|
'pipeline_id': 10,
|
|
'name': 'nightly',
|
|
'title': 'Nightly audit',
|
|
'status': 'waiting',
|
|
'created_at': _at(const Duration(minutes: 3)),
|
|
});
|
|
|
|
expect(controller.entries.single.runId, 9);
|
|
expect(controller.entries.single.kind, AttentionKind.decision);
|
|
});
|
|
|
|
test(
|
|
'a reconnect reseed keeps dismissals and does not re-notify',
|
|
() async {
|
|
final controller = _controller(MemoryAttentionStore());
|
|
addTearDown(controller.dispose);
|
|
await controller.load();
|
|
controller.seedRows([_run(1, 'running'), _run(2, 'failed')]);
|
|
final failed = controller.entries.single;
|
|
expect(await controller.dismiss(failed), isTrue);
|
|
|
|
controller.observeFrame({
|
|
'type': 'run_event',
|
|
'run_id': 1,
|
|
'status': 'failed',
|
|
});
|
|
expect(controller.transitions, hasLength(1));
|
|
|
|
controller.resetForReconnect();
|
|
controller.seedRows([
|
|
_run(1, 'failed'),
|
|
_run(2, 'failed'),
|
|
], reconnect: true);
|
|
|
|
expect(controller.dismissed, {'2:Failed'});
|
|
expect(controller.entries.map((entry) => entry.key), ['1:Failed']);
|
|
expect(
|
|
controller.transitions,
|
|
hasLength(1),
|
|
reason: 'the reconnect baseline is already failed for run 1',
|
|
);
|
|
|
|
controller.observeFrame({
|
|
'type': 'run_event',
|
|
'run_id': 1,
|
|
'status': 'failed',
|
|
});
|
|
expect(controller.transitions, hasLength(1));
|
|
},
|
|
);
|
|
});
|
|
}
|