Merge branch 'worktree-agent-ae6543d03d048d81f'

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 16:10:47 +00:00
28 changed files with 1391 additions and 70 deletions
@@ -0,0 +1,329 @@
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 shows a toast that opens the chat', () async {
await setUpWith();
gateway.controller.add(_push(_notice('a')));
await pump();
expect(bridge.shown, isEmpty);
expect(toasts.single.headline, 'Release chat: Build done');
expect(toasts.single.message, 'All tests pass');
expect(page.badges, isEmpty);
toasts.single.open();
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 and badges the title; '
'focus clears the badge', () 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, [1, 2]);
expect(notices.badge, 2);
expect(page.chimes, 0);
bridge.taps.first!();
expect(opened, ['ses_1']);
page.current = PagePresence.focused;
page.focus.add(null);
await pump();
expect(page.badges.last, 0);
expect(notices.badge, 0);
});
test('a low priority notice waits as a toast on a visible page and posts '
'only when the page is hidden', () async {
await setUpWith();
page.current = PagePresence.visible;
gateway.controller.add(_push(_notice('low', priority: 2)));
await pump();
expect(bridge.shown, isEmpty);
expect(toasts, hasLength(1));
expect(page.badges, [1]);
expect(page.chimes, 0);
page.current = PagePresence.hidden;
gateway.controller.add(_push(_notice('low2', priority: 2)));
await pump();
expect(bridge.shown, hasLength(1));
});
test(
'an insecure page falls back to a toast, a title count and a chime',
() 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.single.headline, 'Release chat: Build done');
expect(page.badges, [1]);
expect(page.chimes, 1);
},
);
test('a refused system notification falls back to the toast', () 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, hasLength(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(toasts, 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(toasts.map((t) => t.headline), ['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();
toasts.clear();
notices = build();
await notices.connected();
await pump();
expect(toasts, 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(toasts.single.headline, '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, hasLength(agentNoticeReplayLimit));
expect(page.chimes, 0);
expect(page.badges.last, agentNoticeReplayLimit);
});
}
@@ -140,4 +140,49 @@ void main() {
);
});
});
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);
@@ -0,0 +1,44 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/attention/notify/agent_notices.dart';
import 'package:pipelines_app/features/attention/notify/page_signals.dart';
import 'package:pipelines_app/features/attention/ui/agent_notice_toast.dart';
void main() {
testWidgets('the toast shows the headline and message; Open opens the chat', (
tester,
) async {
final messenger = GlobalKey<ScaffoldMessengerState>();
var opened = 0;
await tester.pumpWidget(
MaterialApp(
scaffoldMessengerKey: messenger,
home: const Scaffold(body: SizedBox.expand()),
),
);
showAgentNoticeToast(
messenger.currentState,
AgentToast(
headline: 'Release chat: Build done',
message: 'All tests pass',
open: () => opened++,
),
);
await tester.pumpAndSettle();
expect(find.byKey(agentNoticeToastKey), findsOneWidget);
expect(find.text('Release chat: Build done'), findsOneWidget);
expect(find.text('All tests pass'), findsOneWidget);
await tester.tap(find.text('Open'));
await tester.pumpAndSettle();
expect(opened, 1);
expect(find.byKey(agentNoticeToastKey), findsNothing);
});
test('the tab title count is added, replaced and removed', () {
expect(badgedTitle('Pipelines', 1), '(1) Pipelines');
expect(badgedTitle('(1) Pipelines', 3), '(3) Pipelines');
expect(badgedTitle('(3) Pipelines', 0), 'Pipelines');
expect(unbadgedTitle('(12) Runs (2)'), 'Runs (2)');
});
}