Files
pipelines/app/test/features/attention/notify/notification_test_support.dart
T
alan 7718604268 feat(app): add the notification capability behind a platform seam
An abstract NotificationBridge with a dart:js_interop web implementation and
an honest unsupported stub; a NotificationController that reports only the
platform's permission word, asks only from an explicit gesture, never
re-prompts a denied answer, and notifies once per run+kind+finished marker
under its own persisted ledger key. One reusable native
NotificationPermissionTile for Settings, an isolated browser probe, and
focused tests. No shell/settings wiring.
2026-10-22 01:23:24 +01:00

113 lines
3.2 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_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),
'finished_at': at(age - const Duration(minutes: 5)),
'export_pending_sequence': ?exportPending,
...extra,
};
/// One qualifying pushed frame for a run, as `run_event` carries it. A blank
/// [finishedAt] leaves the marker to fall back to the creation time.
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 ?? 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),
)!,
);
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;
}
}