import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/error_advice.dart'; import 'package:pipelines_app/core/transport/connection_state.dart'; import 'package:pipelines_app/ui/status/error_notice.dart'; import 'package:pipelines_app/ui/text/plural.dart'; /// Common failures read as what to do, with the raw detail kept behind /// Details; any other failure reads exactly as reported. void main() { test('common failures map to their kind', () { expect( errorAdviceKind(const NotConnectedError()), ErrorAdviceKind.notConnected, ); expect( errorAdviceKind(StateError('Connection closed')), ErrorAdviceKind.notConnected, ); expect( errorAdviceKind(const ProtocolError('unknown command: list_schedules')), ErrorAdviceKind.serverTooOld, ); expect( errorAdviceKind(const ProtocolError('x', code: 'elevation_required')), ErrorAdviceKind.permission, ); expect( errorAdviceKind('permission denied: /srv'), ErrorAdviceKind.permission, ); expect( errorAdviceKind(TimeoutException('request timed out after 30000ms')), ErrorAdviceKind.timeout, ); expect(errorAdviceKind('pipeline 4 has no stage review'), isNull); final advice = errorAdvice(StateError('Connection closed')); expect(advice?.detail, 'Connection closed'); expect(advice?.sentence, contains('Settings')); }); testWidgets('a common failure says what to do and keeps its detail', ( tester, ) async { await tester.pumpWidget( const MaterialApp( home: Scaffold( body: ErrorNotice(error: 'unknown command: list_schedules'), ), ), ); expect( find.text(errorAdviceSentence(ErrorAdviceKind.serverTooOld)), findsOneWidget, ); expect(find.text('unknown command: list_schedules'), findsNothing); await tester.tap(find.byKey(errorNoticeDetailsKey)); await tester.pump(); expect(find.text('unknown command: list_schedules'), findsOneWidget); expect(find.byKey(errorNoticeCopyKey), findsOneWidget); }); testWidgets('any other failure reads as reported', (tester) async { await tester.pumpWidget( const MaterialApp( home: Scaffold(body: ErrorNotice(error: 'Bad state: pipeline gone')), ), ); expect(find.text('Bad state: pipeline gone'), findsOneWidget); expect(find.byKey(errorNoticeDetailsKey), findsNothing); }); test('counts are pluralised', () { expect(countLabel(1, 'stage'), '1 stage'); expect(countLabel(3, 'stage'), '3 stages'); expect(countLabel(0, 'file'), '0 files'); }); }