Files
pipelines/app/test/features/attention/notify/notification_ledger_test.dart
T
alan 150481b57b App: agent notifications on the web (system notification, toast, tab title count)
The app subscribes to the notification stream on every connection and
delivers chat_notify notices: a toast when the page is focused (none if
that chat is in view), a browser notification titled '<chat>: <title>'
plus a '(n)' tab title count when it is not, and on insecure http pages a
toast, the title count and a short chime. Low priority posts only when
hidden. Replays within an hour show once; ids persist in the ledger.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 16:02:47 +00:00

189 lines
6.4 KiB
Dart

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',
);
});
});
group('the ledger keeps agent ids apart from run identities', () {
test('pruning runs keeps agent ids; age and count bound them', () {
var ledger = const NotificationLedger()
.recording('7:Failed')
.recordingAgent('chat:a:notify:1', _agentNow);
ledger = ledger.pruning({});
expect(ledger.notified, isEmpty);
expect(ledger.hasShownAgent('chat:a:notify:1'), isTrue);
ledger = ledger.recordingAgent(
'chat:a:notify:2',
_agentNow.add(NotificationLedger.agentShownRetention * 2),
);
expect(ledger.agentShown.keys, ['chat:a:notify:2']);
for (var i = 0; i < NotificationLedger.maxAgentShown + 10; i++) {
ledger = ledger.recordingAgent(
'x$i',
_agentNow.add(Duration(seconds: i)),
);
}
expect(ledger.agentShown, hasLength(NotificationLedger.maxAgentShown));
expect(ledger.hasShownAgent('x0'), isFalse);
});
test('the encoding round-trips and an old value decodes', () {
final ledger = const NotificationLedger(
asked: true,
).recordingAgent('chat:a:notify:1', _agentNow);
final decoded = decodeNotificationLedger(
encodeNotificationLedger(ledger),
);
expect(decoded.asked, isTrue);
expect(decoded.agentShown, ledger.agentShown);
expect(
decodeNotificationLedger(
'{"asked":false,"notified":["1:Failed"]}',
).agentShown,
isEmpty,
);
});
});
}
final _agentNow = DateTime.utc(2026, 12, 2, 12);