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.
This commit is contained in:
@@ -0,0 +1,403 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/attention/domain/attention_models.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/notify/notification_store.dart';
|
||||
|
||||
import 'notification_test_support.dart';
|
||||
|
||||
/// Let the controller's fire-and-forget ledger write land.
|
||||
Future<void> settle() => Future<void>.delayed(Duration.zero);
|
||||
|
||||
void main() {
|
||||
group('the seed never notifies', () {
|
||||
test('an already failed and waiting list is silent', () async {
|
||||
final attention = attentionController();
|
||||
final bridge = FakeNotificationBridge();
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(attention.dispose);
|
||||
addTearDown(notify.dispose);
|
||||
await attention.load();
|
||||
await notify.load();
|
||||
|
||||
attention.seedRows([
|
||||
runSummary(1, 'failed', diagnostic: 'Stage 3 stopped'),
|
||||
runSummary(2, 'waiting'),
|
||||
]);
|
||||
|
||||
expect(attention.transitions, isEmpty);
|
||||
expect(notify.publishTransitions(attention.transitions), 0);
|
||||
expect(bridge.shown, isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group('a live transition notifies once', () {
|
||||
test('the frame\'s own words and identity are used', () async {
|
||||
final attention = attentionController();
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(attention.dispose);
|
||||
addTearDown(notify.dispose);
|
||||
await attention.load();
|
||||
await notify.load();
|
||||
expect(notify.canNotify, isTrue);
|
||||
|
||||
attention.seedRows([runSummary(1, 'running', name: 'nightly')]);
|
||||
attention.observeFrame(
|
||||
runFrame(
|
||||
1,
|
||||
'failed',
|
||||
name: 'nightly',
|
||||
title: 'Nightly audit',
|
||||
diagnostic: 'Stage 3 stopped',
|
||||
finishedAt: '2026-10-22T11:00:00Z',
|
||||
),
|
||||
);
|
||||
|
||||
expect(notify.publishTransitions(attention.transitions), 1);
|
||||
final message = bridge.shown.single;
|
||||
expect(message.title, 'A run failed');
|
||||
expect(message.body, 'Nightly audit \u00b7 nightly');
|
||||
expect(message.tag, '1:Failed@2026-10-22T11:00:00Z');
|
||||
expect(message.runId, 1);
|
||||
});
|
||||
|
||||
test('a duplicate frame and a republished list stay silent', () async {
|
||||
final attention = attentionController();
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(attention.dispose);
|
||||
addTearDown(notify.dispose);
|
||||
await attention.load();
|
||||
await notify.load();
|
||||
|
||||
attention.seedRows([runSummary(1, 'running')]);
|
||||
attention.observeFrame(runFrame(1, 'failed'));
|
||||
expect(notify.publishTransitions(attention.transitions), 1);
|
||||
|
||||
attention.observeFrame(runFrame(1, 'failed'));
|
||||
expect(attention.transitions, hasLength(1));
|
||||
expect(notify.publishTransitions(attention.transitions), 0);
|
||||
expect(bridge.shown, hasLength(1));
|
||||
});
|
||||
|
||||
test('a reconnect reseed cannot replay the notification', () async {
|
||||
final attention = attentionController();
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(attention.dispose);
|
||||
addTearDown(notify.dispose);
|
||||
await attention.load();
|
||||
await notify.load();
|
||||
|
||||
attention.seedRows([runSummary(1, 'running')]);
|
||||
attention.observeFrame(runFrame(1, 'failed'));
|
||||
expect(notify.publishTransitions(attention.transitions), 1);
|
||||
|
||||
attention.resetForReconnect();
|
||||
attention.seedRows([runSummary(1, 'failed')], reconnect: true);
|
||||
expect(notify.publishTransitions(attention.transitions), 0);
|
||||
attention.observeFrame(runFrame(1, 'failed'));
|
||||
expect(notify.publishTransitions(attention.transitions), 0);
|
||||
expect(bridge.shown, hasLength(1));
|
||||
});
|
||||
|
||||
test('a rebuilt controller with the same store stays silent', () async {
|
||||
final store = MemoryNotificationStore();
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final attention = attentionController();
|
||||
final notify = NotificationController(store: store, bridge: bridge);
|
||||
addTearDown(attention.dispose);
|
||||
addTearDown(notify.dispose);
|
||||
await attention.load();
|
||||
await notify.load();
|
||||
|
||||
attention.seedRows([runSummary(1, 'running')]);
|
||||
attention.observeFrame(runFrame(1, 'failed'));
|
||||
expect(notify.publishTransitions(attention.transitions), 1);
|
||||
await settle();
|
||||
|
||||
final restarted = NotificationController(store: store, bridge: bridge);
|
||||
addTearDown(restarted.dispose);
|
||||
await restarted.load();
|
||||
expect(restarted.notified, contains('1:Failed@${at(const Duration(minutes: 1))}'));
|
||||
expect(restarted.publishTransitions(attention.transitions), 0);
|
||||
expect(bridge.shown, hasLength(1));
|
||||
});
|
||||
|
||||
test('a genuinely new failure of the same run notifies again', () async {
|
||||
final attention = attentionController();
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(attention.dispose);
|
||||
addTearDown(notify.dispose);
|
||||
await attention.load();
|
||||
await notify.load();
|
||||
|
||||
attention.seedRows([runSummary(1, 'running')]);
|
||||
attention.observeFrame(
|
||||
runFrame(1, 'failed', finishedAt: '2026-10-22T09:10:00Z'),
|
||||
);
|
||||
expect(notify.publishTransitions(attention.transitions), 1);
|
||||
|
||||
attention.observeFrame(runFrame(1, 'running'));
|
||||
attention.observeFrame(
|
||||
runFrame(1, 'failed', finishedAt: '2026-10-22T11:30:00Z'),
|
||||
);
|
||||
expect(notify.publishTransitions(attention.transitions), 1);
|
||||
expect(bridge.shown, hasLength(2));
|
||||
expect(bridge.shown.map((m) => m.tag).toSet(), hasLength(2));
|
||||
});
|
||||
});
|
||||
|
||||
group('a dismissed entry never notifies', () {
|
||||
test('the detector suppresses it and nothing is posted', () async {
|
||||
final attention = attentionController();
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(attention.dispose);
|
||||
addTearDown(notify.dispose);
|
||||
await attention.load();
|
||||
await notify.load();
|
||||
|
||||
attention.seedRows([runSummary(1, 'running')]);
|
||||
await attention.dismiss(attentionEntryFor(runSummary(1, 'failed'))!);
|
||||
attention.observeFrame(runFrame(1, 'failed'));
|
||||
|
||||
expect(attention.transitions, isEmpty);
|
||||
expect(notify.publishTransitions(attention.transitions), 0);
|
||||
expect(bridge.shown, isEmpty);
|
||||
});
|
||||
|
||||
test('the publish guard records but does not show a dismissed key', () async {
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(notify.dispose);
|
||||
await notify.load();
|
||||
|
||||
final transition = transitionFor(4);
|
||||
expect(
|
||||
notify.publishTransitions([transition], dismissed: {transition.key}),
|
||||
0,
|
||||
);
|
||||
expect(notify.notified, contains(notificationIdentity(transition)));
|
||||
expect(bridge.shown, isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group('permission is only ever the platform\'s answer', () {
|
||||
test('an unsupported platform is never asked and never posts', () async {
|
||||
final bridge = FakeNotificationBridge(supported: false);
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(notify.dispose);
|
||||
await notify.load();
|
||||
expect(notify.permission, NotificationPermissionState.unsupported);
|
||||
|
||||
expect(
|
||||
await notify.requestPermission(),
|
||||
NotificationPermissionState.unsupported,
|
||||
);
|
||||
expect(bridge.requests, 0);
|
||||
expect(notify.publishTransitions([transitionFor(1)]), 0);
|
||||
expect(bridge.shown, isEmpty);
|
||||
});
|
||||
|
||||
test('an insecure page reports needsSecureContext', () async {
|
||||
final bridge = FakeNotificationBridge(secureContext: false);
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(notify.dispose);
|
||||
await notify.load();
|
||||
expect(notify.permission, NotificationPermissionState.needsSecureContext);
|
||||
await notify.requestPermission();
|
||||
expect(bridge.requests, 0);
|
||||
});
|
||||
|
||||
test('a default page is asked once and follows the real answer', () async {
|
||||
final store = MemoryNotificationStore();
|
||||
final bridge = FakeNotificationBridge(answer: 'granted');
|
||||
final notify = NotificationController(store: store, bridge: bridge);
|
||||
addTearDown(notify.dispose);
|
||||
await notify.load();
|
||||
expect(notify.permission, NotificationPermissionState.notRequested);
|
||||
expect(notify.canNotify, isFalse);
|
||||
|
||||
final state = await notify.requestPermission();
|
||||
expect(bridge.requests, 1);
|
||||
expect(state, NotificationPermissionState.granted);
|
||||
expect(notify.canNotify, isTrue);
|
||||
expect(notify.asked, isTrue);
|
||||
await settle();
|
||||
expect(store.stored, contains('"asked":true'));
|
||||
});
|
||||
|
||||
test('a denied answer is never re-prompted', () async {
|
||||
final bridge = FakeNotificationBridge(permission: 'denied');
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(notify.dispose);
|
||||
await notify.load();
|
||||
expect(notify.permission, NotificationPermissionState.denied);
|
||||
|
||||
expect(
|
||||
await notify.requestPermission(),
|
||||
NotificationPermissionState.denied,
|
||||
);
|
||||
expect(bridge.requests, 0);
|
||||
expect(notify.publishTransitions([transitionFor(1)]), 0);
|
||||
expect(bridge.shown, isEmpty);
|
||||
});
|
||||
|
||||
test('a prompt that grants nothing keeps the honest default wording', () async {
|
||||
final bridge = FakeNotificationBridge(answer: 'default');
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(notify.dispose);
|
||||
await notify.load();
|
||||
|
||||
final state = await notify.requestPermission();
|
||||
expect(state, NotificationPermissionState.notRequested);
|
||||
expect(notify.asked, isTrue);
|
||||
expect(notify.canNotify, isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('batching and the tap seam', () {
|
||||
test('a burst posts a bounded batch in order, then the rest', () async {
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(notify.dispose);
|
||||
await notify.load();
|
||||
|
||||
final transitions = [for (var id = 1; id <= 7; id++) transitionFor(id)];
|
||||
expect(notify.publishTransitions(transitions), 5);
|
||||
expect(bridge.shown.map((m) => m.runId), [1, 2, 3, 4, 5]);
|
||||
expect(notify.publishTransitions(transitions), 2);
|
||||
expect(bridge.shown.map((m) => m.runId), [1, 2, 3, 4, 5, 6, 7]);
|
||||
});
|
||||
|
||||
test('a tap reports the run and never navigates here', () async {
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(notify.dispose);
|
||||
await notify.load();
|
||||
|
||||
final opened = <int>[];
|
||||
notify.onOpenRun = opened.add;
|
||||
expect(notify.publishTransitions([transitionFor(3)]), 1);
|
||||
bridge.taps.single!.call();
|
||||
expect(opened, [3]);
|
||||
});
|
||||
});
|
||||
|
||||
group('an unavailable store cannot reach the UI', () {
|
||||
test('a failed read and write still notify and never throw', () async {
|
||||
final store = MemoryNotificationStore()
|
||||
..failReads = true
|
||||
..failWrites = true;
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final notify = NotificationController(store: store, bridge: bridge);
|
||||
addTearDown(notify.dispose);
|
||||
|
||||
await expectLater(notify.load(), completes);
|
||||
expect(notify.loaded, isTrue);
|
||||
expect(notify.notified, isEmpty);
|
||||
|
||||
expect(notify.publishTransitions([transitionFor(1)]), 1);
|
||||
await settle();
|
||||
expect(bridge.shown, hasLength(1));
|
||||
});
|
||||
|
||||
test('a throwing platform post is reported, not propagated', () async {
|
||||
final bridge = _ThrowingBridge();
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(notify.dispose);
|
||||
await notify.load();
|
||||
|
||||
expect(notify.publishTransitions([transitionFor(1)]), 0);
|
||||
expect(notify.notified, contains('1:Failed@${at(const Duration(minutes: 1))}'));
|
||||
});
|
||||
});
|
||||
|
||||
group('the platform seam', () {
|
||||
test('the non-web bridge reports unsupported honestly', () async {
|
||||
final bridge = createNotificationBridge();
|
||||
expect(bridge.environment().supported, isFalse);
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(notify.dispose);
|
||||
await notify.load();
|
||||
expect(notify.permission, NotificationPermissionState.unsupported);
|
||||
expect(
|
||||
bridge.show(
|
||||
const NotificationMessage(
|
||||
runId: 1,
|
||||
title: 'A run failed',
|
||||
body: 'release',
|
||||
tag: '1:Failed@x',
|
||||
),
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class _ThrowingBridge implements NotificationBridge {
|
||||
@override
|
||||
NotificationEnvironment environment() => const NotificationEnvironment(
|
||||
supported: true,
|
||||
secureContext: true,
|
||||
permission: 'granted',
|
||||
);
|
||||
|
||||
@override
|
||||
Future<String> requestPermission() async => 'granted';
|
||||
|
||||
@override
|
||||
bool show(NotificationMessage message, {void Function()? onTap}) =>
|
||||
throw StateError('the browser refused to construct a notification');
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/attention/notify/notification_ledger.dart';
|
||||
|
||||
import 'notification_test_support.dart';
|
||||
|
||||
void main() {
|
||||
group('the identity carries the finished marker', () {
|
||||
test('a recorded finish time distinguishes two failures of one run', () {
|
||||
final first = transitionFor(7, finishedAt: '2026-10-22T09:10:00Z');
|
||||
final second = transitionFor(7, finishedAt: '2026-10-22T11:30:00Z');
|
||||
expect(notificationIdentity(first), '7:Failed@2026-10-22T09:10:00Z');
|
||||
expect(notificationIdentity(second), '7:Failed@2026-10-22T11:30:00Z');
|
||||
expect(notificationIdentity(first), isNot(notificationIdentity(second)));
|
||||
});
|
||||
|
||||
test('a run without a finish time falls back to its creation time', () {
|
||||
final entry = transitionFor(7, status: 'waiting', finishedAt: '');
|
||||
expect(
|
||||
notificationIdentity(entry),
|
||||
'7:Decision@${attentionNow.subtract(const Duration(hours: 1)).toIso8601String()}',
|
||||
);
|
||||
});
|
||||
|
||||
test('the kind separates a decision from a failure of one run', () {
|
||||
expect(
|
||||
notificationIdentity(transitionFor(7)),
|
||||
isNot(notificationIdentity(transitionFor(7, status: 'waiting'))),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('the ledger records identities', () {
|
||||
test('recording is idempotent and never mutates the original', () {
|
||||
const base = NotificationLedger();
|
||||
final once = base.recording('7:Failed@a');
|
||||
final twice = once.recording('7:Failed@a');
|
||||
expect(base.notified, isEmpty);
|
||||
expect(once.hasNotified('7:Failed@a'), isTrue);
|
||||
expect(twice.notified, {'7:Failed@a'});
|
||||
});
|
||||
|
||||
test('asked is carried without touching the identities', () {
|
||||
final asked = const NotificationLedger(
|
||||
notified: {'a'},
|
||||
).withAsked(true);
|
||||
expect(asked.asked, isTrue);
|
||||
expect(asked.notified, {'a'});
|
||||
});
|
||||
});
|
||||
|
||||
group('persistence is total', () {
|
||||
test('a round trip keeps the asked flag and the identities', () {
|
||||
final ledger = const NotificationLedger(
|
||||
notified: {'b', 'a'},
|
||||
).withAsked(true);
|
||||
final decoded = decodeNotificationLedger(encodeNotificationLedger(ledger));
|
||||
expect(decoded.asked, isTrue);
|
||||
expect(decoded.notified, {'a', 'b'});
|
||||
});
|
||||
|
||||
test('an unchanged ledger always encodes to the same bytes', () {
|
||||
final first = encodeNotificationLedger(
|
||||
const NotificationLedger(notified: {'b', 'a'}),
|
||||
);
|
||||
final second = encodeNotificationLedger(
|
||||
const NotificationLedger(notified: {'a', 'b'}),
|
||||
);
|
||||
expect(first, second);
|
||||
});
|
||||
|
||||
test('blank, malformed and wrong-typed values never throw', () {
|
||||
expect(decodeNotificationLedger(null).notified, isEmpty);
|
||||
expect(decodeNotificationLedger('').asked, isFalse);
|
||||
expect(decodeNotificationLedger('not json').notified, isEmpty);
|
||||
expect(decodeNotificationLedger('[]').notified, isEmpty);
|
||||
expect(decodeNotificationLedger('"text"').notified, isEmpty);
|
||||
expect(
|
||||
decodeNotificationLedger('{"asked":"yes","notified":[1,null,"a",""]}')
|
||||
.notified,
|
||||
{'a'},
|
||||
);
|
||||
expect(
|
||||
decodeNotificationLedger('{"asked":"yes"}').asked,
|
||||
isFalse,
|
||||
reason: 'only a real true counts as asked',
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/attention/domain/attention_models.dart';
|
||||
import 'package:pipelines_app/features/attention/notify/notification_models.dart';
|
||||
|
||||
NotificationEnvironment _env({
|
||||
bool supported = true,
|
||||
bool secure = true,
|
||||
String permission = 'default',
|
||||
}) => NotificationEnvironment(
|
||||
supported: supported,
|
||||
secureContext: secure,
|
||||
permission: permission,
|
||||
);
|
||||
|
||||
void main() {
|
||||
group('permission state is the platform\'s own answer', () {
|
||||
test('an unsupported API and an insecure page win over the word', () {
|
||||
expect(
|
||||
notificationStateOf(_env(supported: false, permission: 'granted')),
|
||||
NotificationPermissionState.unsupported,
|
||||
);
|
||||
expect(
|
||||
notificationStateOf(_env(secure: false, permission: 'granted')),
|
||||
NotificationPermissionState.needsSecureContext,
|
||||
);
|
||||
});
|
||||
|
||||
test('granted is never assumed from an unrecognised word', () {
|
||||
expect(
|
||||
notificationStateOf(_env(permission: 'prompt')),
|
||||
NotificationPermissionState.notRequested,
|
||||
);
|
||||
expect(
|
||||
notificationStateOf(_env(permission: '')),
|
||||
NotificationPermissionState.notRequested,
|
||||
);
|
||||
expect(
|
||||
notificationStateOf(_env(permission: 'granted')),
|
||||
NotificationPermissionState.granted,
|
||||
);
|
||||
expect(
|
||||
notificationStateOf(_env(permission: 'denied')),
|
||||
NotificationPermissionState.denied,
|
||||
);
|
||||
});
|
||||
|
||||
test('each state carries the browser model\'s own wire word', () {
|
||||
expect(NotificationPermissionState.unsupported.wireName, 'unsupported');
|
||||
expect(
|
||||
NotificationPermissionState.needsSecureContext.wireName,
|
||||
'insecure',
|
||||
);
|
||||
expect(NotificationPermissionState.notRequested.wireName, 'default');
|
||||
expect(NotificationPermissionState.granted.wireName, 'granted');
|
||||
expect(NotificationPermissionState.denied.wireName, 'denied');
|
||||
});
|
||||
});
|
||||
|
||||
group('the permission view', () {
|
||||
test('only a still-undecided page can request', () {
|
||||
final view = notificationPermissionView(
|
||||
NotificationPermissionState.notRequested,
|
||||
);
|
||||
expect(view.canRequest, isTrue);
|
||||
expect(view.actionLabel, notificationRequestLabel);
|
||||
expect(view.body, notificationDefaultCopy);
|
||||
|
||||
for (final state in [
|
||||
NotificationPermissionState.unsupported,
|
||||
NotificationPermissionState.needsSecureContext,
|
||||
NotificationPermissionState.granted,
|
||||
NotificationPermissionState.denied,
|
||||
]) {
|
||||
final other = notificationPermissionView(state);
|
||||
expect(other.canRequest, isFalse, reason: '$state');
|
||||
expect(other.actionLabel, isNull, reason: '$state');
|
||||
}
|
||||
});
|
||||
|
||||
test('asked only changes the undecided wording', () {
|
||||
expect(
|
||||
notificationPermissionView(
|
||||
NotificationPermissionState.notRequested,
|
||||
asked: true,
|
||||
).body,
|
||||
notificationDefaultAskedCopy,
|
||||
);
|
||||
expect(
|
||||
notificationPermissionView(
|
||||
NotificationPermissionState.denied,
|
||||
asked: true,
|
||||
).body,
|
||||
notificationDeniedCopy,
|
||||
);
|
||||
});
|
||||
|
||||
test('every unavailable state names the in-app channel', () {
|
||||
for (final state in [
|
||||
NotificationPermissionState.unsupported,
|
||||
NotificationPermissionState.needsSecureContext,
|
||||
NotificationPermissionState.denied,
|
||||
]) {
|
||||
expect(
|
||||
notificationPermissionView(state).body,
|
||||
contains('in-app Needs you surface'),
|
||||
reason: '$state',
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group('notification content is the entry\'s own words', () {
|
||||
test('the headline is the kind, nothing invented', () {
|
||||
expect(
|
||||
notificationAlertTitle(AttentionKind.decision),
|
||||
'A run needs your decision',
|
||||
);
|
||||
expect(notificationAlertTitle(AttentionKind.failed), 'A run failed');
|
||||
});
|
||||
|
||||
test('the body is the run label then its pipeline', () {
|
||||
final entry = attentionEntryFor({
|
||||
'id': 7,
|
||||
'name': 'nightly-audit',
|
||||
'title': 'Nightly audit',
|
||||
'status': 'failed',
|
||||
'diagnostic': 'Stage 3 stopped',
|
||||
'created_at': '2026-10-22T09:00:00Z',
|
||||
'finished_at': '2026-10-22T09:10:00Z',
|
||||
})!;
|
||||
expect(notificationAlertBody(entry), 'Nightly audit \u00b7 nightly-audit');
|
||||
});
|
||||
|
||||
test('a blank or equal pipeline is never repeated', () {
|
||||
final untitled = attentionEntryFor({
|
||||
'id': 7,
|
||||
'name': 'release',
|
||||
'status': 'failed',
|
||||
'created_at': '2026-10-22T09:00:00Z',
|
||||
'finished_at': '2026-10-22T09:10:00Z',
|
||||
})!;
|
||||
expect(notificationAlertBody(untitled), 'release');
|
||||
|
||||
final nameless = attentionEntryFor({
|
||||
'id': 8,
|
||||
'name': '',
|
||||
'title': 'A title',
|
||||
'status': 'failed',
|
||||
'created_at': '2026-10-22T09:00:00Z',
|
||||
'finished_at': '2026-10-22T09:10:00Z',
|
||||
})!;
|
||||
expect(notificationAlertBody(nameless), 'A title');
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/attention/notify/notification_models.dart';
|
||||
import 'package:pipelines_app/features/attention/ui/notification_permission_tile.dart';
|
||||
|
||||
import 'needs_you_test_support.dart';
|
||||
|
||||
/// The tile inside a normal page, so both native hosts can draw it. The page
|
||||
/// scrolls like the Settings list does, so the tile is never given a fixed
|
||||
/// height to overflow.
|
||||
Widget _page({
|
||||
required NotificationPermissionState state,
|
||||
bool cupertino = false,
|
||||
bool asked = false,
|
||||
VoidCallback? onRequest,
|
||||
}) => attentionApp(
|
||||
Scaffold(
|
||||
body: SingleChildScrollView(
|
||||
child: NotificationPermissionTile(
|
||||
state: state,
|
||||
asked: asked,
|
||||
onRequest: onRequest,
|
||||
),
|
||||
),
|
||||
),
|
||||
cupertino: cupertino,
|
||||
);
|
||||
|
||||
void _noop() {}
|
||||
|
||||
void main() {
|
||||
for (final native in [false, true]) {
|
||||
final host = native ? 'Cupertino' : 'Material';
|
||||
|
||||
testWidgets('$host shows the state\'s own honest copy', (tester) async {
|
||||
final cases = <NotificationPermissionState, String>{
|
||||
NotificationPermissionState.unsupported: notificationUnsupportedCopy,
|
||||
NotificationPermissionState.needsSecureContext:
|
||||
notificationInsecureCopy,
|
||||
NotificationPermissionState.granted: notificationGrantedCopy,
|
||||
NotificationPermissionState.denied: notificationDeniedCopy,
|
||||
NotificationPermissionState.notRequested: notificationDefaultCopy,
|
||||
};
|
||||
for (final entry in cases.entries) {
|
||||
await tester.pumpWidget(
|
||||
_page(state: entry.key, cupertino: native, onRequest: _noop),
|
||||
);
|
||||
expect(find.text(notificationSectionTitle), findsOneWidget);
|
||||
expect(find.text(notificationRationale), findsOneWidget);
|
||||
expect(find.text(entry.value), findsOneWidget, reason: '${entry.key}');
|
||||
expect(
|
||||
find.byKey(notificationEnableKey),
|
||||
entry.key == NotificationPermissionState.notRequested
|
||||
? findsOneWidget
|
||||
: findsNothing,
|
||||
reason: '${entry.key}',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('$host shows the asked wording without an action', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
_page(
|
||||
state: NotificationPermissionState.notRequested,
|
||||
asked: true,
|
||||
cupertino: native,
|
||||
),
|
||||
);
|
||||
expect(find.text(notificationDefaultAskedCopy), findsOneWidget);
|
||||
expect(find.text(notificationDefaultCopy), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('$host Enable runs the explicit request', (tester) async {
|
||||
var requested = 0;
|
||||
await tester.pumpWidget(
|
||||
_page(
|
||||
state: NotificationPermissionState.notRequested,
|
||||
cupertino: native,
|
||||
onRequest: () => requested++,
|
||||
),
|
||||
);
|
||||
await tester.tap(find.byKey(notificationEnableKey));
|
||||
await tester.pump();
|
||||
expect(requested, 1);
|
||||
});
|
||||
|
||||
testWidgets('$host tile wraps at 320 logical px and 2x in dark', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.devicePixelRatio = 2;
|
||||
tester.view.physicalSize = const Size(640, 1280);
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
|
||||
// The undecided state is the hardest: it carries the Enable action as well
|
||||
// as the longest copy, so the section title must still have room to read.
|
||||
await tester.pumpWidget(
|
||||
attentionApp(
|
||||
Scaffold(
|
||||
body: SingleChildScrollView(
|
||||
child: NotificationPermissionTile(
|
||||
state: NotificationPermissionState.notRequested,
|
||||
onRequest: _noop,
|
||||
),
|
||||
),
|
||||
),
|
||||
cupertino: native,
|
||||
dark: true,
|
||||
textScale: 2,
|
||||
),
|
||||
);
|
||||
expect(tester.takeException(), isNull);
|
||||
|
||||
final title = tester.renderObject<RenderParagraph>(
|
||||
find.text(notificationSectionTitle),
|
||||
);
|
||||
// Cupertino's list tile deliberately caps its title at one line; the
|
||||
// defect this guards is the title being squeezed to a single glyph by the
|
||||
// action sharing the row, so its width is the meaningful check.
|
||||
expect(
|
||||
title.size.width,
|
||||
greaterThan(100),
|
||||
reason: 'the action must not squeeze the section title to one glyph',
|
||||
);
|
||||
final body = tester.renderObject<RenderParagraph>(
|
||||
find.text(notificationDefaultCopy),
|
||||
);
|
||||
expect(body.didExceedMaxLines, isFalse);
|
||||
expect(find.byKey(notificationEnableKey), findsOneWidget);
|
||||
|
||||
final rect = tester.getRect(find.byKey(notificationStateKey));
|
||||
expect(rect.left, greaterThanOrEqualTo(0));
|
||||
expect(rect.right, lessThanOrEqualTo(320));
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user