fix(app): isolate runs list and detail request generations

This commit is contained in:
2026-10-21 13:54:52 +01:00
parent 6b8d5ea32e
commit 4bc95f47be
3 changed files with 365 additions and 62 deletions
@@ -129,4 +129,216 @@ void main() {
expect(controller.detail?['status'], 'succeeded');
},
);
test('load with initial id clears list and detail loading', () async {
final load = controller.load(7);
expect(controller.loading, isTrue);
gateway.listReply(0, [
{'id': 7},
{'id': 8},
]);
await Future<void>.delayed(Duration.zero);
expect(gateway.details[7], hasLength(1));
expect(controller.loading, isFalse);
expect(controller.detailLoading, isTrue);
gateway.detailReply(7, 0, {'id': 7});
await load;
expect(controller.loading, isFalse);
expect(controller.detailLoading, isFalse);
expect(controller.detail?['id'], 7);
expect(controller.selectedRunId, 7);
});
test('newer list load wins when replies complete out of order', () async {
final first = controller.load();
final second = controller.load();
gateway.listReply(1, [
{'id': 2},
]);
await second;
gateway.listReply(0, [
{'id': 1},
]);
await first;
expect(controller.runs.single['id'], 2);
expect(controller.loading, isFalse);
});
test('list resync leaves the selected detail request valid', () async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({'type': 'run_event', 'status': 'running'});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
gateway.listReply(0, [
{'id': 1, 'status': 'running'},
]);
await Future<void>.delayed(Duration.zero);
expect(controller.detail?['id'], 1);
expect(controller.selectedRunId, 1);
expect(gateway.details[1], hasLength(1));
});
test('manual list load keeps an in-flight detail refresh valid', () async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'succeeded',
});
await Future<void>.delayed(const Duration(milliseconds: 180));
expect(gateway.details[1], hasLength(2));
final load = controller.load();
gateway.listReply(1, [
{'id': 1},
]);
await load;
gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'});
await Future<void>.delayed(Duration.zero);
expect(controller.detail?['status'], 'succeeded');
expect(controller.detailLoading, isFalse);
gateway.listReply(0, [
{'id': 1},
]);
});
test('event during a superseded resync still runs one followup', () async {
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 9,
'status': 'running',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
final load = controller.load();
expect(gateway.listCalls, 2);
gateway.listReply(1, [
{'id': 1},
]);
await load;
gateway.framesController.add({
'type': 'run_event',
'run_id': 10,
'status': 'running',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 2);
gateway.listReply(0, [
{'id': 1},
]);
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 3);
gateway.listReply(2, [
{'id': 1},
{'id': 2},
]);
await Future<void>.delayed(Duration.zero);
expect(controller.runs, hasLength(2));
expect(controller.loading, isFalse);
});
test('unknown run events coalesce into one followup resync', () async {
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 9,
'status': 'running',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
expect(controller.loading, isTrue);
gateway.framesController.add({
'type': 'run_event',
'run_id': 10,
'status': 'running',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
gateway.listReply(0, [
{'id': 3},
]);
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 2);
expect(controller.loading, isTrue);
gateway.listReply(1, [
{'id': 3},
{'id': 4},
]);
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 2);
expect(controller.loading, isFalse);
expect(controller.runs, hasLength(2));
});
test(
'selected refresh coalesces and Back ignores the pending reply',
() async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'succeeded',
});
await Future<void>.delayed(const Duration(milliseconds: 180));
expect(gateway.details[1], hasLength(2));
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'failed',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.details[1], hasLength(2));
gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'});
await Future<void>.delayed(Duration.zero);
expect(gateway.details[1], hasLength(3));
controller.back();
gateway.detailReply(1, 2, {'id': 1, 'status': 'failed'});
await Future<void>.delayed(Duration.zero);
expect(controller.detail, isNull);
expect(controller.detailLoading, isFalse);
expect(controller.error, isNull);
},
);
test('choosing another run does not display the previous detail', () async {
final first = controller.select(1);
gateway.detailReply(1, 0, {'id': 1});
await first;
final second = controller.select(2);
expect(controller.detail, isNull);
expect(controller.selectedRunId, 2);
expect(controller.detailLoading, isTrue);
gateway.detailReply(2, 0, {'id': 2});
await second;
expect(controller.detail?['id'], 2);
expect(controller.detailLoading, isFalse);
});
test('pending detail refresh is safe after dispose', () async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'succeeded',
});
await Future<void>.delayed(const Duration(milliseconds: 180));
expect(gateway.details[1], hasLength(2));
controller.dispose();
disposed = true;
gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'});
await Future<void>.delayed(Duration.zero);
expect(controller.detail?['id'], 1);
});
}