import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/data/chat_recency.dart'; import 'package:shared_preferences/shared_preferences.dart'; void main() { test('touch moves a chat to the front and persists the order', () async { final store = MemoryChatRecencyStore(['b', 'a']); final recency = ChatRecency(store: store); await recency.load(); expect(recency.ids, ['b', 'a']); recency.touch('a'); recency.touch('c'); expect(recency.ids, ['c', 'a', 'b']); await Future.delayed(Duration.zero); expect(store.ids, ['c', 'a', 'b']); }); test('a chat touched before the read stays in front', () async { final recency = ChatRecency(store: MemoryChatRecencyStore(['b', 'a'])); recency.touch('a'); await recency.load(); expect(recency.ids, ['a', 'b']); }); test('keeps at most the limit', () async { final recency = ChatRecency(limit: 2); for (final id in ['a', 'b', 'c']) { recency.touch(id); } expect(recency.ids, ['c', 'b']); }); test('shared preferences keep the order per server', () async { SharedPreferences.setMockInitialValues({}); const one = SharedPreferencesChatRecencyStore(serverScope: 'one'); const two = SharedPreferencesChatRecencyStore(serverScope: 'two'); await one.write(['x', 'y']); expect(await one.read(), ['x', 'y']); expect(await two.read(), isEmpty); final reloaded = ChatRecency(store: one); await reloaded.load(); expect(reloaded.ids, ['x', 'y']); }); }