Files
pipelines/app/test/features/attention/notify/notification_test_support.dart
T

137 lines
4.3 KiB
Dart

import 'package:pipelines_app/features/attention/domain/attention_models.dart';
import 'package:pipelines_app/features/attention/domain/attention_transitions.dart';
import 'package:pipelines_app/features/attention/notify/notification_bridge.dart';
import 'package:pipelines_app/features/attention/notify/notification_controller.dart';
import 'package:pipelines_app/features/attention/notify/notification_ledger.dart';
import 'package:pipelines_app/features/attention/notify/notification_models.dart';
import 'package:pipelines_app/features/attention/runtime/attention_controller.dart';
import 'package:pipelines_app/features/attention/runtime/attention_store.dart';
/// The fixed local time the attention tests order and window by.
final attentionNow = DateTime(2026, 10, 22, 12);
String at(Duration before) => attentionNow.subtract(before).toIso8601String();
/// One run summary, built exactly as the runs list holds it.
Map<String, dynamic> runSummary(
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),
// The server leaves finished_at empty for a waiting run (and for one still in
// flight); only a settled run carries a finish time.
'finished_at': status == 'waiting' || status == 'running'
? ''
: at(age - const Duration(minutes: 5)),
'export_pending_sequence': ?exportPending,
...extra,
};
/// One qualifying pushed frame for a run, as `run_event` carries it.
///
/// A waiting or running frame has no `finished_at`, matching the server; pass
/// [finishedAt] only to describe a settled run.
Map<String, dynamic> runFrame(
int id,
String status, {
String name = 'release',
String? title,
String diagnostic = '',
String? finishedAt,
}) => {
'type': 'run_event',
'run_id': id,
'name': name,
'title': title ?? 'Run $id',
'status': status,
'diagnostic': diagnostic,
'created_at': at(const Duration(hours: 1)),
'finished_at':
finishedAt ??
(status == 'waiting' || status == 'running'
? ''
: at(const Duration(minutes: 1))),
};
/// One transition for a run that just entered [status], for batch tests.
AttentionTransition transitionFor(
int id, {
String status = 'failed',
String name = 'release',
String? title,
String? finishedAt,
}) => AttentionTransition(
attentionEntryFor(
runFrame(id, status, name: name, title: title, finishedAt: finishedAt),
)!,
);
/// Let the controller's fire-and-forget ledger write land.
Future<void> settle() => Future<void>.delayed(Duration.zero);
/// Publish exactly the way the manager wires it: the accumulated transitions,
/// the current rows and the dismissed set from the same attention snapshot.
int publish(NotificationController notify, AttentionController attention) =>
notify.publishTransitions(
attention.transitions,
qualifying: notificationQualifyingKeys(attention.rows),
dismissed: attention.dismissed,
);
AttentionController attentionController() =>
AttentionController(store: MemoryAttentionStore(), now: () => attentionNow);
/// A bridge whose answers are set explicitly, so every permission path is
/// exercised without a browser. It records what the controller asked for.
class FakeNotificationBridge implements NotificationBridge {
FakeNotificationBridge({
this.supported = true,
this.secureContext = true,
this.permission = 'default',
this.answer = 'granted',
this.accepts = true,
});
bool supported;
bool secureContext;
String permission;
String answer;
bool accepts;
int requests = 0;
final List<NotificationMessage> shown = [];
final List<void Function()?> taps = [];
@override
NotificationEnvironment environment() => NotificationEnvironment(
supported: supported,
secureContext: secureContext,
permission: permission,
);
@override
Future<String> requestPermission() async {
requests++;
permission = answer;
return answer;
}
@override
bool show(NotificationMessage message, {void Function()? onTap}) {
shown.add(message);
taps.add(onTap);
return accepts;
}
}