import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/search/domain/search_order.dart'; import 'search_test_support.dart'; JsonMap _run(int id, String recency, {int score = 1}) => { 'kind': 'run', 'target': 'run:$id', 'score': score, 'recency': recency, 'run': {'id': id}, }; JsonMap _chat(String id, int updatedAt, {String? snippet}) { final hit = chatHit(id, snippet: snippet); return { ...hit, 'chat': {...hit['chat'] as Map, 'updated_at': updatedAt}, }; } List _targets(List hits) => [ for (final hit in hits) hit['target'], ]; void main() { test('hits sort newest first whatever their score; ties and undated hits ' 'keep the server order, undated last', () { final sorted = searchHitsNewestFirst([ _run(1, '2026-01-01T00:00:00Z', score: 3), { 'kind': 'run', 'target': 'run:9', 'run': {'id': 9}, }, _run(2, '2026-03-01T00:00:00Z'), _run(3, '2026-02-01T00:00:00Z'), _run(4, '2026-02-01T00:00:00Z'), ]); expect(_targets(sorted), ['run:2', 'run:3', 'run:4', 'run:1', 'run:9']); }); test('chat hits sort by last use, not by matched field', () { final sorted = searchHitsNewestFirst([ _chat('title_old', 1000), _chat('message_new', 3000, snippet: 'flaky'), _chat('dir_mid', 2000), ]); expect(_targets(sorted), [ 'chat:message_new', 'chat:dir_mid', 'chat:title_old', ]); }); test('the controller shows both groups newest first', () async { final gateway = QueuedGateway(holdChats: true); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('flaky'); await pumpEventQueue(); gateway.succeed( 0, envelope([ _run(1, '2026-01-01T00:00:00Z', score: 3), _run(2, '2026-05-01T00:00:00Z'), ]), ); gateway.chatPending.single.complete( envelope([_chat('a', 1000), _chat('b', 5000, snippet: 'flaky')]), ); await pumpEventQueue(); expect(_targets(controller.shownResults), ['run:2', 'run:1']); expect(_targets(controller.shownChats), ['chat:b', 'chat:a']); }); }