merge(app): land notification capability after a retained deadline

This commit is contained in:
2026-10-22 02:05:36 +01:00
19 changed files with 2882 additions and 0 deletions
@@ -0,0 +1,181 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/attention/notify/notification_controller.dart';
import 'package:pipelines_app/features/attention/notify/notification_store.dart';
import 'notification_test_support.dart';
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(publish(notify, attention), 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(publish(notify, attention), 1);
final message = bridge.shown.single;
expect(message.title, 'A run failed');
expect(message.body, 'Nightly audit \u00b7 nightly');
expect(message.tag, '1:Failed');
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(publish(notify, attention), 1);
attention.observeFrame(runFrame(1, 'failed'));
expect(attention.transitions, hasLength(1));
expect(publish(notify, attention), 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(publish(notify, attention), 1);
attention.resetForReconnect();
attention.seedRows([runSummary(1, 'failed')], reconnect: true);
expect(publish(notify, attention), 0);
attention.observeFrame(runFrame(1, 'failed'));
expect(publish(notify, attention), 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(publish(notify, attention), 1);
await settle();
final restarted = NotificationController(store: store, bridge: bridge);
addTearDown(restarted.dispose);
await restarted.load();
expect(restarted.notified, contains('1:Failed'));
expect(publish(restarted, attention), 0);
expect(bridge.shown, hasLength(1));
});
test('a later failure of another run notifies independently', () 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'), runSummary(2, 'running')]);
attention.observeFrame(runFrame(1, 'failed'));
expect(publish(notify, attention), 1);
attention.observeFrame(runFrame(2, 'failed'));
expect(publish(notify, attention), 1);
expect(bridge.shown, hasLength(2));
expect(bridge.shown.map((m) => m.tag), ['1:Failed', '2:Failed']);
});
test('a transition for a run that left is never re-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')]);
attention.observeFrame(runFrame(1, 'failed'));
expect(publish(notify, attention), 1);
// Publishing the leaving frame forgets the identity; republishing the
// accumulated list afterwards must not resurrect the old alert.
attention.observeFrame(runFrame(1, 'running'));
expect(publish(notify, attention), 0);
expect(publish(notify, attention), 0);
expect(bridge.shown, hasLength(1));
});
});
}
@@ -0,0 +1,238 @@
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_models.dart';
import 'package:pipelines_app/features/attention/notify/notification_store.dart';
import 'notification_test_support.dart';
void main() {
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(publish(notify, attention), 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],
qualifying: {transition.key},
dismissed: {transition.key},
),
0,
);
expect(notify.notified, contains(transition.key));
expect(bridge.shown, isEmpty);
},
);
});
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)];
final qualifying = {for (final t in transitions) t.key};
expect(notify.publishTransitions(transitions, qualifying: qualifying), 5);
expect(bridge.shown.map((m) => m.runId), [1, 2, 3, 4, 5]);
expect(notify.publishTransitions(transitions, qualifying: qualifying), 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)], qualifying: {'3:Failed'}),
1,
);
bridge.taps.single!.call();
expect(opened, [3]);
});
});
group('the ledger cannot be read into by a caller', () {
test('notified is an unmodifiable copy', () async {
final bridge = FakeNotificationBridge(permission: 'granted');
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(notify.dispose);
await notify.load();
expect(
notify.publishTransitions([transitionFor(1)], qualifying: {'1:Failed'}),
1,
);
expect(notify.notified, {'1:Failed'});
expect(() => notify.notified.add('9:Failed'), throwsUnsupportedError);
expect(notify.notified, {'1:Failed'});
});
});
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)], qualifying: {'1:Failed'}),
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)], qualifying: {'1:Failed'}),
0,
);
expect(notify.notified, contains('1:Failed'));
});
test('ledger writes land in call order, one at a time', () async {
final store = _SlowStore();
final bridge = FakeNotificationBridge(permission: 'granted');
final notify = NotificationController(store: store, bridge: bridge);
addTearDown(notify.dispose);
await notify.load();
notify.publishTransitions([transitionFor(1)], qualifying: {'1:Failed'});
notify.publishTransitions(
[transitionFor(2)],
qualifying: {'1:Failed', '2:Failed'},
);
await Future<void>.delayed(const Duration(milliseconds: 40));
expect(store.maxActive, 1, reason: 'the writes must be serialized');
expect(store.stored, contains('"1:Failed"'));
expect(store.stored, contains('"2:Failed"'));
expect(store.writes.last, store.stored);
});
});
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',
),
),
isFalse,
);
});
});
}
/// A store whose writes take a tick, so a racing implementation would overlap
/// them and the last value to land would be the older snapshot.
class _SlowStore implements NotificationStore {
String? stored;
int active = 0;
int maxActive = 0;
final List<String> writes = [];
@override
Future<String?> read() async => stored;
@override
Future<bool> write(String value) async {
active++;
if (active > maxActive) maxActive = active;
await Future<void>.delayed(const Duration(milliseconds: 2));
stored = value;
writes.add(value);
active--;
return true;
}
}
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,143 @@
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 is the run and kind', () {
test('two failures of one run are the same announced state', () {
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');
expect(notificationIdentity(second), '7:Failed');
});
test('a waiting run needs no finish time to have an identity', () {
final entry = transitionFor(7, status: 'waiting', finishedAt: '');
expect(notificationIdentity(entry), '7:Decision');
});
test('the kind separates a decision from a failure of one run', () {
expect(
notificationIdentity(transitionFor(7)),
isNot(notificationIdentity(transitionFor(7, status: 'waiting'))),
);
});
});
group('the qualifying set is the attention surface\'s own keys', () {
test('only failed and human-waiting runs qualify', () {
expect(
notificationQualifyingKeys([
runSummary(1, 'failed'),
runSummary(2, 'waiting'),
runSummary(3, 'running'),
runSummary(4, 'succeeded'),
runSummary(5, 'queued'),
runSummary(6, 'waiting', exportPending: 2),
]),
{'1:Failed', '2:Decision'},
);
});
test('a duplicate row and a malformed id add nothing', () {
expect(
notificationQualifyingKeys([
runSummary(1, 'failed'),
runSummary(1, 'failed'),
{'id': 0, 'status': 'failed'},
{'id': 'not a run', 'status': 'failed'},
]),
{'1:Failed'},
);
expect(notificationQualifyingKeys(null), isEmpty);
});
test('the frame key form is read too', () {
expect(notificationQualifyingKeys([runFrame(9, 'failed')]), {'9:Failed'});
});
});
group('the ledger records identities', () {
test('recording is idempotent and never mutates the original', () {
const base = NotificationLedger();
final once = base.recording('7:Failed');
final twice = once.recording('7:Failed');
expect(base.notified, isEmpty);
expect(once.hasNotified('7:Failed'), isTrue);
expect(twice.notified, {'7:Failed'});
});
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('pruning keeps only the current qualifying run', () {
test('a run that left the state is forgotten; asked survives', () {
final ledger = const NotificationLedger(
notified: {'7:Decision', '8:Failed'},
).withAsked(true);
final pruned = ledger.pruning({'8:Failed'});
expect(pruned.notified, {'8:Failed'});
expect(pruned.asked, isTrue);
});
test('nothing to drop returns the same ledger', () {
const ledger = NotificationLedger(notified: {'7:Failed'});
expect(identical(ledger.pruning({'7:Failed'}), ledger), isTrue);
});
test('a timestamped identity from an older build is dropped', () {
final old = decodeNotificationLedger(
'{"asked":false,"notified":["7:Decision@2026-10-22T11:00:00.000"]}',
);
expect(old.notified, hasLength(1));
expect(old.pruning({'7:Decision'}).notified, isEmpty);
});
});
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,158 @@
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,103 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/attention/notify/notification_controller.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';
void main() {
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)], qualifying: {'1:Failed'}),
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)], qualifying: {'1:Failed'}),
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);
},
);
});
}
@@ -0,0 +1,93 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/attention/notify/notification_controller.dart';
import 'package:pipelines_app/features/attention/notify/notification_store.dart';
import 'notification_test_support.dart';
/// A run that leaves a qualifying state and enters it again is a new event.
///
/// A waiting run has no `finished_at`, and its creation time never changes, so
/// both gates of one run carry exactly the same timestamps. These tests pin the
/// behaviour that was silently dropped when the identity included that marker.
void main() {
test('a second decision gate 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(7, 'running')]);
attention.observeFrame(runFrame(7, 'waiting', finishedAt: ''));
expect(publish(notify, attention), 1);
expect(bridge.shown.single.tag, '7:Decision');
attention.observeFrame(runFrame(7, 'running'));
expect(publish(notify, attention), 0);
attention.observeFrame(runFrame(7, 'waiting', finishedAt: ''));
expect(attention.transitions, hasLength(2));
expect(publish(notify, attention), 1);
expect(bridge.shown, hasLength(2));
// The browser coalesces by run+kind, so both alerts share one tag.
expect(bridge.shown.map((m) => m.tag).toSet(), {'7:Decision'});
});
test(
'a second decision gate notifies even without the leaving publish',
() 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(7, 'running')]);
attention.observeFrame(runFrame(7, 'waiting', finishedAt: ''));
expect(publish(notify, attention), 1);
// The caller misses the frame that leaves the state; the detector still
// creates a new instance for the next gate, so it must notify again.
attention.observeFrame(runFrame(7, 'running'));
attention.observeFrame(runFrame(7, 'waiting', finishedAt: ''));
expect(publish(notify, attention), 1);
expect(bridge.shown, hasLength(2));
expect(bridge.shown.map((m) => m.tag).toSet(), {'7:Decision'});
},
);
test('a coalesced backlog of two gates notifies once', () 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(7, 'running')]);
attention.observeFrame(runFrame(7, 'waiting', finishedAt: ''));
attention.observeFrame(runFrame(7, 'running'));
attention.observeFrame(runFrame(7, 'waiting', finishedAt: ''));
expect(attention.transitions, hasLength(2));
// Both gates were folded before the caller published; the current state is
// what the operator should be told about, once.
expect(publish(notify, attention), 1);
expect(bridge.shown, hasLength(1));
expect(bridge.shown.single.tag, '7:Decision');
});
}
@@ -0,0 +1,136 @@
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;
}
}
@@ -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);
});
}
}