From bb23df4dd689a9e7ebe041684f702b4e39f8c745 Mon Sep 17 00:00:00 2001 From: sjungwon03 Date: Wed, 28 Oct 2026 22:44:24 +0900 Subject: [PATCH 1/2] test(pipeline_schedules): cover stale page results across account and refresh Signed-off-by: sjungwon03 --- .../pipeline_schedules_controller_test.dart | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/apps/labfox/test/pipeline_schedules_controller_test.dart b/apps/labfox/test/pipeline_schedules_controller_test.dart index ccd8d91b..9de65f81 100644 --- a/apps/labfox/test/pipeline_schedules_controller_test.dart +++ b/apps/labfox/test/pipeline_schedules_controller_test.dart @@ -1,7 +1,10 @@ +import 'dart:async'; + import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:gitlab_api/gitlab_api.dart'; import 'package:gitlab_models/gitlab_models.dart'; +import 'package:labfox/core/auth/auth_controller.dart'; import 'package:labfox/features/pipeline_schedules/data/pipeline_schedules_repository.dart'; import 'package:labfox/features/pipeline_schedules/presentation/controllers/pipeline_schedules_controller.dart'; @@ -45,6 +48,46 @@ class _FakeRepository extends PipelineSchedulesRepository { } } +class _DelayedRepository extends PipelineSchedulesRepository { + _DelayedRepository(this.firstPage) + : super( + GitLabClient( + baseUrl: 'https://gitlab.example.com', + token: 'glpat-xxxxxxxxxxxx', + ), + ); + + Paginated firstPage; + final nextPage = Completer>(); + + @override + Future> list( + int projectId, { + bool? active, + int page = 1, + }) async { + expect(projectId, 7); + return page == 1 ? firstPage : nextPage.future; + } +} + +PipelineSchedule _schedule(int id) => PipelineSchedule( + id: id, + description: 'Schedule $id', + ref: 'main', + cron: '0 1 * * *', + active: true, +); + +const _firstAccount = Account( + instanceUrl: 'https://first.gitlab.example', + user: User(id: 23, username: 'developer', name: 'Developer'), +); +const _secondAccount = Account( + instanceUrl: 'https://second.gitlab.example', + user: User(id: 23, username: 'developer', name: 'Developer'), +); + void main() { test('loads schedule pages once and runs selected schedule', () async { final repository = _FakeRepository(); @@ -76,4 +119,93 @@ void main() { await container.read(action.notifier).play(); expect(repository.played, [1]); }); + + for (final reject in [false, true]) { + test( + 'old instance page cannot replace new instance schedules ($reject)', + () async { + final first = _DelayedRepository( + Paginated(items: [_schedule(1)], nextPage: 2), + ); + final second = _DelayedRepository(Paginated(items: [_schedule(9)])); + final activeAccount = StateProvider((ref) => _firstAccount); + final container = ProviderContainer( + overrides: [ + currentAccountProvider.overrideWith( + (ref) => ref.watch(activeAccount), + ), + pipelineSchedulesRepositoryProvider.overrideWith((ref) async { + final account = ref.watch(currentAccountProvider); + return account?.instanceUrl == _firstAccount.instanceUrl + ? first + : second; + }), + ], + ); + addTearDown(container.dispose); + final provider = pipelineScheduleListControllerProvider( + const PipelineScheduleListRef(projectId: 7), + ); + final subscription = container.listen(provider, (_, _) {}); + addTearDown(subscription.close); + + await container.read(provider.future); + final oldPage = container.read(provider.notifier).loadMore(); + await Future.delayed(Duration.zero); + container.read(activeAccount.notifier).state = _secondAccount; + await container.read(provider.future); + if (reject) { + first.nextPage.completeError(StateError('Old instance page failed')); + } else { + first.nextPage.complete(Paginated(items: [_schedule(2)])); + } + await oldPage; + + expect(container.read(provider).hasError, isFalse); + expect( + container.read(provider).requireValue.items.map((item) => item.id), + [9], + ); + }, + ); + } + + for (final reject in [false, true]) { + test('old page cannot replace refreshed schedule list ($reject)', () async { + final repository = _DelayedRepository( + Paginated(items: [_schedule(1)], nextPage: 2), + ); + final container = ProviderContainer( + overrides: [ + pipelineSchedulesRepositoryProvider.overrideWith( + (ref) async => repository, + ), + ], + ); + addTearDown(container.dispose); + final provider = pipelineScheduleListControllerProvider( + const PipelineScheduleListRef(projectId: 7), + ); + final subscription = container.listen(provider, (_, _) {}); + addTearDown(subscription.close); + + await container.read(provider.future); + final oldPage = container.read(provider.notifier).loadMore(); + await Future.delayed(Duration.zero); + repository.firstPage = Paginated(items: [_schedule(3)]); + await container.refresh(provider.future); + if (reject) { + repository.nextPage.completeError(StateError('Old page failed')); + } else { + repository.nextPage.complete(Paginated(items: [_schedule(2)])); + } + await oldPage; + + expect(container.read(provider).hasError, isFalse); + expect( + container.read(provider).requireValue.items.map((item) => item.id), + [3], + ); + }); + } } From e6d6213f94a34105035c4d6b2298b75b06f4a12d Mon Sep 17 00:00:00 2001 From: sjungwon03 Date: Wed, 28 Oct 2026 22:56:31 +0900 Subject: [PATCH 2/2] fix(pipeline_schedules): ignore stale page results after rebuild Signed-off-by: sjungwon03 --- .../controllers/pipeline_schedules_controller.dart | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/labfox/lib/features/pipeline_schedules/presentation/controllers/pipeline_schedules_controller.dart b/apps/labfox/lib/features/pipeline_schedules/presentation/controllers/pipeline_schedules_controller.dart index dcb84ecc..1ca9494b 100644 --- a/apps/labfox/lib/features/pipeline_schedules/presentation/controllers/pipeline_schedules_controller.dart +++ b/apps/labfox/lib/features/pipeline_schedules/presentation/controllers/pipeline_schedules_controller.dart @@ -52,9 +52,12 @@ class PipelineScheduleListController PipelineScheduleListRef > { bool _loadingMore = false; + int _generation = 0; @override Future> build(PipelineScheduleListRef arg) async { + _generation++; + _loadingMore = false; final repository = await ref.watch( pipelineSchedulesRepositoryProvider.future, ); @@ -63,21 +66,24 @@ class PipelineScheduleListController } Future loadMore() async { - if (_loadingMore) return; + if (_loadingMore || state.isLoading) return; final current = state.valueOrNull; final page = current?.nextPage; if (current == null || page == null) return; + final generation = _generation; _loadingMore = true; try { final repository = await ref.read( pipelineSchedulesRepositoryProvider.future, ); + if (generation != _generation) return; if (repository == null) throw StateError('No authenticated account'); final next = await repository.list( arg.projectId, active: arg.active, page: page, ); + if (generation != _generation) return; state = AsyncData( Paginated( items: [...current.items, ...next.items], @@ -87,9 +93,9 @@ class PipelineScheduleListController ), ); } catch (error, stackTrace) { - state = AsyncError(error, stackTrace); + if (generation == _generation) state = AsyncError(error, stackTrace); } finally { - _loadingMore = false; + if (generation == _generation) _loadingMore = false; } } }