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); }); test('a scroll toward older rows releases the end however small', () { // Toward the end, landing within the threshold follows again. expect( chatFollowsAfterScroll(20, itemCount: 3, towardOlder: false), isTrue, ); expect( chatFollowsAfterScroll(33, itemCount: 3, towardOlder: false), isFalse, ); // A wheel or trackpad tick up is the operator's: even 2 px releases. expect( chatFollowsAfterScroll(2, itemCount: 3, towardOlder: true), isFalse, ); expect( chatFollowsAfterScroll(0, itemCount: 3, towardOlder: true), isTrue, ); expect( chatFollowsAfterScroll(0, itemCount: 0, towardOlder: true), 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); }); test('a bottom-anchored position above the centre stays negative', () { double restore(double saved) => chatRestoreOffset( savedOffset: saved, minScrollExtent: -2000, maxScrollExtent: 300, ); expect(restore(-750), -750); expect(restore(-5000), -2000); expect(restore(900), 300); }); }); group('store', () { test('remembers one conversation without touching another', () { final store = MemoryChatScrollStore(); store.write('a', (offset: 120, anchor: null)); store.write('b', (offset: -30, anchor: 'm4')); expect(store.read('a'), (offset: 120.0, anchor: null)); expect(store.read('b'), (offset: -30.0, anchor: 'm4')); expect(store.read('c'), isNull); store.clear('a'); expect(store.read('a'), isNull); expect(store.entries, {'b': (offset: -30.0, anchor: 'm4')}); }); test('ignores an unusable offset instead of inventing a position', () { final store = MemoryChatScrollStore(); store.write('a', (offset: double.nan, anchor: 'm1')); store.write('a', (offset: double.infinity, anchor: null)); // Only a bottom-anchored offset counts from a centre and can be negative. store.write('a', (offset: -5, anchor: null)); expect(store.read('a'), isNull); }); }); }