import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/conversation/chat_scroll.dart'; void main() { group('follow geometry', () { test('a transcript with no content has no distance or follow state', () { expect( chatBottomDistance(maxScrollExtent: 0, pixels: 0, itemCount: 0), isNull, ); expect(chatFollowsAt(null, itemCount: 0), isFalse); }); test('the distance never goes negative past the end', () { expect( chatBottomDistance(maxScrollExtent: 800, pixels: 300, itemCount: 3), 500, ); expect( chatBottomDistance(maxScrollExtent: 800, pixels: 900, itemCount: 3), 0, ); }); test('the follow threshold includes its boundary and needs an item', () { expect(chatFollowsAt(0, itemCount: 3), isTrue); expect(chatFollowsAt(32, itemCount: 3), isTrue); expect(chatFollowsAt(33, itemCount: 3), isFalse); expect(chatFollowsAt(null, itemCount: 3), isFalse); expect(chatFollowsAt(0, itemCount: 0), isFalse); }); }); group('jump control', () { test('is offered only when not following with content below the fold', () { expect( chatShowsJumpToLatest( following: false, distanceFromBottom: 400, itemCount: 5, ), isTrue, ); expect( chatShowsJumpToLatest( following: true, distanceFromBottom: 400, itemCount: 5, ), isFalse, ); // The end is in view, so there is nothing to return to. expect( chatShowsJumpToLatest( following: false, distanceFromBottom: 0, itemCount: 5, ), isFalse, ); expect( chatShowsJumpToLatest( following: false, distanceFromBottom: 400, itemCount: 0, ), isFalse, ); }); }); group('arrival count', () { test('counts only messages after the release point', () { expect( chatNewMessageCount(['m1', 'm2', 'm3'], 'm2'), 1, ); expect( chatNewMessageCount(['older', 'm1', 'm2', 'live', 'newer'], 'm2'), 2, ); // A growing delta keeps the same id and is not a new message. expect(chatNewMessageCount(['m1', 'm2'], 'm2'), 0); }); test('reports nothing without a release point or a surviving message', () { expect(chatNewMessageCount(['m1'], null), 0); expect(chatNewMessageCount(['m1'], 'replaced'), 0); expect(chatNewMessageCount(const [], 'm1'), 0); }); test('the label keeps the action and states the singular/plural count', () { expect(chatJumpToLatestLabel(0), 'Jump to latest'); expect(chatJumpToLatestLabel(1), 'Jump to latest · 1 new message'); expect(chatJumpToLatestLabel(2), 'Jump to latest · 2 new messages'); }); }); group('restore', () { test('no saved position lands on the newest end', () { expect( chatRestoreOffset(savedOffset: null, maxScrollExtent: 640), 640, ); }); test('a saved position is kept within the current extent', () { expect( chatRestoreOffset(savedOffset: 120, maxScrollExtent: 640), 120, ); expect(chatRestoreOffset(savedOffset: 0, maxScrollExtent: 640), 0); expect( chatRestoreOffset(savedOffset: 900, maxScrollExtent: 640), 640, ); }); }); group('store', () { test('remembers one conversation without touching another', () { final store = MemoryChatScrollStore(); store.write('a', 120); store.write('b', 30); expect(store.read('a'), 120); expect(store.read('b'), 30); expect(store.read('c'), isNull); store.clear('a'); expect(store.read('a'), isNull); expect(store.entries, {'b': 30.0}); }); test('ignores an unusable offset instead of inventing a position', () { final store = MemoryChatScrollStore(); store.write('a', double.nan); store.write('a', double.infinity); store.write('a', -5); expect(store.read('a'), isNull); }); }); }