import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/settings/settings_server_load.dart'; void main() { test('concurrent loads share one request', () async { var calls = 0; final reply = Completer(); final load = ServerLoad(() { calls++; return reply.future; }); addTearDown(load.dispose); final first = load.load(); final second = load.refresh(); expect(calls, 1); expect(load.loading, isTrue); reply.complete(7); await first; await second; expect(load.value, 7); expect(load.hasValue, isTrue); expect(load.loading, isFalse); expect(load.error, isNull); }); test( 'a failed refresh keeps the last value; a retry clears the error', () async { var calls = 0; final load = ServerLoad(() async { calls++; if (calls == 2) throw StateError('server unreachable'); return 'value $calls'; }); addTearDown(load.dispose); await load.load(); expect(load.value, 'value 1'); await load.refresh(); expect(load.value, 'value 1', reason: 'the cached value survives'); expect(load.error, isA()); await load.refresh(); expect(load.error, isNull); expect(load.value, 'value 3'); expect(calls, 3); }, ); test('a reply that lands after dispose is dropped quietly', () async { final reply = Completer(); final load = ServerLoad(() => reply.future); final pending = load.load(); load.dispose(); reply.complete(1); await pending; expect(load.hasValue, isFalse); }); }