355a5767fb
An agent's notify call now always posts an OS notification when permitted, whatever app is in front (only skipped while that chat is open in the focused page); no toast or chime. Without permission the tab title counts it. The browser's permission prompt is shown on the operator's first click. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
320 lines
9.3 KiB
Dart
320 lines
9.3 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/core/api_gateway.dart';
|
|
import 'package:pipelines_app/features/attention/notify/agent_notice.dart';
|
|
import 'package:pipelines_app/features/attention/notify/agent_notices.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_store.dart';
|
|
import 'package:pipelines_app/features/attention/notify/page_signals.dart';
|
|
|
|
import 'notification_test_support.dart';
|
|
|
|
final _now = DateTime.utc(2026, 12, 2, 12);
|
|
|
|
class _Gateway implements ApiGateway {
|
|
final controller = StreamController<JsonMap>.broadcast();
|
|
List<JsonMap> backlog = [];
|
|
final calls = <String>[];
|
|
|
|
@override
|
|
Stream<JsonMap> get frames => controller.stream;
|
|
|
|
@override
|
|
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
|
calls.add(type);
|
|
return {
|
|
'type': 'notifications_subscribed',
|
|
'notifications': backlog,
|
|
'ids': <String>[],
|
|
};
|
|
}
|
|
}
|
|
|
|
class _Page implements PageSignals {
|
|
PagePresence current = PagePresence.focused;
|
|
final badges = <int>[];
|
|
int chimes = 0;
|
|
final focus = StreamController<void>.broadcast();
|
|
|
|
@override
|
|
PagePresence get presence => current;
|
|
@override
|
|
Stream<void> get focused => focus.stream;
|
|
@override
|
|
void setBadge(int count) => badges.add(count);
|
|
@override
|
|
void chime() => chimes++;
|
|
@override
|
|
void dispose() {}
|
|
}
|
|
|
|
JsonMap _notice(
|
|
String id, {
|
|
String session = 'ses_1',
|
|
String kind = 'chat_notify',
|
|
int priority = 3,
|
|
Duration age = Duration.zero,
|
|
}) => {
|
|
'id': 'chat:$session:notify:$id',
|
|
'kind': kind,
|
|
'title': 'Build done',
|
|
'message': 'All tests pass',
|
|
'click': 'pipelines://chat/$session',
|
|
'priority': priority,
|
|
'tags': <String>[],
|
|
'created_at': _now.subtract(age).toIso8601String(),
|
|
'group': 'chat:$session',
|
|
};
|
|
|
|
JsonMap _push(JsonMap notice) => {
|
|
'type': 'notification',
|
|
'notification': notice,
|
|
};
|
|
|
|
void main() {
|
|
late _Gateway gateway;
|
|
late _Page page;
|
|
late FakeNotificationBridge bridge;
|
|
late MemoryNotificationStore store;
|
|
late NotificationController notifications;
|
|
late AgentNotices notices;
|
|
late List<AgentToast> toasts;
|
|
late List<String> opened;
|
|
late String? active;
|
|
|
|
AgentNotices build() => AgentNotices(
|
|
gateway: gateway,
|
|
notifications: notifications,
|
|
page: page,
|
|
activeChatSessionId: () => active,
|
|
sessionTitle: (id) async => id == 'ses_1' ? 'Release chat' : null,
|
|
openChat: opened.add,
|
|
showToast: toasts.add,
|
|
now: () => _now,
|
|
);
|
|
|
|
Future<void> setUpWith({
|
|
String permission = 'granted',
|
|
bool secure = true,
|
|
}) async {
|
|
gateway = _Gateway();
|
|
page = _Page();
|
|
bridge = FakeNotificationBridge(
|
|
permission: permission,
|
|
secureContext: secure,
|
|
);
|
|
store = MemoryNotificationStore();
|
|
notifications = NotificationController(store: store, bridge: bridge);
|
|
await notifications.load();
|
|
toasts = [];
|
|
opened = [];
|
|
active = null;
|
|
notices = build();
|
|
}
|
|
|
|
Future<void> pump() async {
|
|
for (var i = 0; i < 10; i++) {
|
|
await Future<void>.delayed(Duration.zero);
|
|
}
|
|
}
|
|
|
|
tearDown(() async {
|
|
notices.dispose();
|
|
notifications.dispose();
|
|
await gateway.controller.close();
|
|
});
|
|
|
|
test('a focused page on another chat still posts a system notification '
|
|
'that opens the chat; nothing shows in the app', () async {
|
|
await setUpWith();
|
|
gateway.controller.add(_push(_notice('a')));
|
|
await pump();
|
|
|
|
expect(bridge.shown.single.title, 'Release chat: Build done');
|
|
expect(bridge.shown.single.body, 'All tests pass');
|
|
expect(toasts, isEmpty);
|
|
expect(page.badges, isEmpty);
|
|
bridge.taps.first!();
|
|
expect(opened, ['ses_1']);
|
|
});
|
|
|
|
test('nothing shows when the chat is already in view', () async {
|
|
await setUpWith();
|
|
active = 'ses_1';
|
|
gateway.controller.add(_push(_notice('a')));
|
|
await pump();
|
|
expect(toasts, isEmpty);
|
|
expect(bridge.shown, isEmpty);
|
|
expect(
|
|
notifications.claimAgentNotice('chat:ses_1:notify:a', _now),
|
|
isFalse,
|
|
);
|
|
});
|
|
|
|
test('an unfocused page posts a system notification, even for the chat '
|
|
'in view', () async {
|
|
await setUpWith();
|
|
page.current = PagePresence.hidden;
|
|
active = 'ses_1'; // In view but not looked at: still notified.
|
|
gateway.controller.add(_push(_notice('a')));
|
|
gateway.controller.add(_push(_notice('b', session: 'ses_2')));
|
|
await pump();
|
|
|
|
expect(bridge.shown.map((m) => m.title), [
|
|
'Release chat: Build done',
|
|
'Chat: Build done',
|
|
]);
|
|
expect(bridge.shown.first.tag, 'chat:ses_1:notify:a');
|
|
expect(bridge.shown.first.body, 'All tests pass');
|
|
expect(toasts, isEmpty);
|
|
expect(page.badges, isEmpty);
|
|
expect(page.chimes, 0);
|
|
bridge.taps.first!();
|
|
expect(opened, ['ses_1']);
|
|
});
|
|
|
|
test('a low priority notice is a system notification too', () async {
|
|
await setUpWith();
|
|
page.current = PagePresence.visible;
|
|
gateway.controller.add(_push(_notice('low', priority: 2)));
|
|
await pump();
|
|
expect(bridge.shown, hasLength(1));
|
|
expect(toasts, isEmpty);
|
|
});
|
|
|
|
test('without notification permission only the tab title counts it, and '
|
|
'focus clears the count', () async {
|
|
await setUpWith(permission: 'default', secure: false);
|
|
page.current = PagePresence.hidden;
|
|
gateway.controller.add(_push(_notice('a', priority: 4)));
|
|
await pump();
|
|
|
|
expect(notifications.canNotify, isFalse);
|
|
expect(bridge.shown, isEmpty);
|
|
expect(toasts, isEmpty);
|
|
expect(page.badges, [1]);
|
|
expect(page.chimes, 0);
|
|
|
|
page.current = PagePresence.focused;
|
|
page.focus.add(null);
|
|
await pump();
|
|
expect(page.badges.last, 0);
|
|
});
|
|
|
|
test('a refused system notification is counted in the tab title', () async {
|
|
await setUpWith();
|
|
bridge.accepts = false;
|
|
page.current = PagePresence.hidden;
|
|
gateway.controller.add(_push(_notice('a')));
|
|
await pump();
|
|
expect(bridge.shown, hasLength(1));
|
|
expect(toasts, isEmpty);
|
|
expect(page.badges, [1]);
|
|
});
|
|
|
|
test('other notification kinds are left to their own surfaces', () async {
|
|
await setUpWith();
|
|
gateway.controller.add(_push(_notice('r', kind: 'chat_reply')));
|
|
gateway.controller.add({'type': 'chat_event', 'chat_event': {}});
|
|
await pump();
|
|
expect(toasts, isEmpty);
|
|
expect(bridge.shown, isEmpty);
|
|
});
|
|
|
|
test(
|
|
'a repeated push and a replay of a shown notice are deduplicated',
|
|
() async {
|
|
await setUpWith();
|
|
gateway.controller.add(_push(_notice('a')));
|
|
gateway.controller.add(_push(_notice('a')));
|
|
await pump();
|
|
gateway.backlog = [_notice('a')];
|
|
await notices.connected();
|
|
await pump();
|
|
expect(bridge.shown, hasLength(1));
|
|
},
|
|
);
|
|
|
|
test(
|
|
'a reconnect replays recent missed notices once, across a reload',
|
|
() async {
|
|
await setUpWith();
|
|
gateway.backlog = [
|
|
_notice('old', age: const Duration(hours: 2)),
|
|
_notice('recent', age: const Duration(minutes: 20)),
|
|
_notice('reply', kind: 'chat_reply'),
|
|
];
|
|
await notices.connected();
|
|
await pump();
|
|
expect(gateway.calls, ['subscribe_notifications']);
|
|
expect(bridge.shown.map((m) => m.title), ['Release chat: Build done']);
|
|
|
|
// The ledger persisted both chat_notify ids, so a reload shows neither.
|
|
final stored = decodeNotificationLedger(store.stored);
|
|
expect(stored.agentShown.keys, {
|
|
'chat:ses_1:notify:old',
|
|
'chat:ses_1:notify:recent',
|
|
});
|
|
notices.dispose();
|
|
notifications.dispose();
|
|
notifications = NotificationController(
|
|
store: MemoryNotificationStore(stored: store.stored),
|
|
bridge: bridge,
|
|
);
|
|
await notifications.load();
|
|
bridge.shown.clear();
|
|
notices = build();
|
|
await notices.connected();
|
|
await pump();
|
|
expect(bridge.shown, isEmpty);
|
|
},
|
|
);
|
|
|
|
test('with a send-only path the subscribe reply is read from the frames, '
|
|
'so it never holds the request lane', () async {
|
|
await setUpWith();
|
|
notices.dispose();
|
|
final sent = <String>[];
|
|
notices = AgentNotices(
|
|
gateway: gateway,
|
|
notifications: notifications,
|
|
page: page,
|
|
activeChatSessionId: () => active,
|
|
sessionTitle: (id) async => 'Release chat',
|
|
openChat: opened.add,
|
|
showToast: toasts.add,
|
|
sendOnly: (type, fields) {
|
|
sent.add(type);
|
|
return 'flutter-1';
|
|
},
|
|
now: () => _now,
|
|
);
|
|
await notices.connected();
|
|
expect(sent, ['subscribe_notifications']);
|
|
expect(gateway.calls, isEmpty);
|
|
gateway.controller.add({
|
|
'type': 'notifications_subscribed',
|
|
'id': 'flutter-1',
|
|
'notifications': [_notice('missed', age: const Duration(minutes: 5))],
|
|
});
|
|
await pump();
|
|
expect(bridge.shown.single.title, 'Release chat: Build done');
|
|
});
|
|
|
|
test('a replay shows at most the newest few and never chimes', () async {
|
|
await setUpWith(permission: 'default', secure: false);
|
|
page.current = PagePresence.hidden;
|
|
gateway.backlog = [
|
|
for (var i = 0; i < 8; i++)
|
|
_notice('n$i', age: Duration(minutes: 30 - i)),
|
|
];
|
|
await notices.connected();
|
|
await pump();
|
|
expect(toasts, isEmpty);
|
|
expect(page.chimes, 0);
|
|
expect(page.badges.last, agentNoticeReplayLimit);
|
|
});
|
|
}
|