arblant
firefox-vpn-client / app / test / failure_text_test.dart
failure_text_test.dart268 lines · 8.0 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:firefox_vpn/core/models.dart';
import 'package:firefox_vpn/l10n/app_localizations.dart';
import 'package:firefox_vpn/ui/widgets.dart';

void main() {
  final l = lookupAppLocalizations(const Locale('en'));

  ProxyInfo proxy({String error = '', String code = '', String? at}) =>
      ProxyInfo.fromJson({
        'wake_error': error,
        'wake_error_code': code,
        'wake_failed_at': ?at,
      });

  ConnectionTest failedTest({
    String error = '',
    String code = '',
    String? at,
  }) => ConnectionTest.fromJson({
    'ok': false,
    'error': error,
    'error_code': code,
    'at': ?at,
  });

  test('the reason is named, not the internet', () {
    final quota = failureText(
      l,
      proxy(error: 'quota exceeded', code: Failure.quota),
      null,
    );
    expect(quota.text, l.failQuota);
    expect(quota.detail, 'quota exceeded');

    expect(
      failureText(l, null, failedTest(error: 'x', code: Failure.testSite)).text,
      l.failTestSite,
    );
    expect(
      failureText(
        l,
        null,
        failedTest(error: 'x', code: Failure.unreachable),
      ).text,
      l.failUnreachable,
    );
    expect(
      failureText(l, null, failedTest(error: 'odd', code: Failure.other)).text,
      l.failOther,
    );
  });

  test("a refused chosen location is named in the app's language", () {
    expect(
      failureText(
        l,
        null,
        failedTest(error: 'x', code: Failure.locationRefused),
      ).text,
      "This country isn't open to your account. Choose another country.",
    );
    final fa = lookupAppLocalizations(const Locale('fa'));
    expect(
      failureCodeText(fa, Failure.locationRefused),
      'این کشور برای حساب شما باز نیست. کشور دیگری انتخاب کنید.',
    );
    expect(fa.useRecommendedLocation, 'استفاده از مکان پیشنهادی');
  });

  test('every failure says what happened and what to do', () {
    expect(l.failNoInternet, 'No internet. Check Wi-Fi or mobile data.');
    expect(
      l.failSignIn,
      "This account's sign-in has expired. Sign in again on the Accounts "
      'page.',
    );
    expect(l.failOther, "Couldn't connect. Try again.");
    final fa = lookupAppLocalizations(const Locale('fa'));
    expect(
      failureCodeText(fa, Failure.noNetwork),
      'اینترنت وصل نیست. Wi-Fi یا اینترنت موبایل را بررسی کنید.',
    );
    // Mozilla answers and refuses the country (HTTP 451): not a block.
    expect(
      failureCodeText(fa, Failure.region),
      'سرویس Mozilla در کشور شما ارائه نمی‌شود.',
    );
    expect(fa.noNetwork, isNot(contains('داده')));
  });

  // In Persian and Arabic every failure and state text starts with a word
  // of the language: notifications and plain text take their direction
  // from the first strong character. Persian has no hamza above (U+0654)
  // and no «ی» ezafe after «ه».
  test('RTL failure texts start right to left', () {
    final rtlStart = RegExp('^[\u0600-\u06FF]');
    for (final language in ['fa', 'ar']) {
      final rtl = lookupAppLocalizations(Locale(language));
      final texts = [
        for (final code in [
          Failure.noNetwork,
          Failure.unreachable,
          Failure.refused,
          Failure.locationRefused,
          Failure.signIn,
          Failure.quota,
          Failure.notEntitled,
          Failure.region,
          Failure.server,
          Failure.noAccount,
          Failure.testSite,
          Failure.other,
        ])
          failureCodeText(rtl, code),
        rtl.noNetwork,
        rtl.waitingForNetwork,
        rtl.proxyStartFailed('10810'),
        rtl.systemProxyTakenBy('127.0.0.1:7890'),
        rtl.systemProxyTakenOff,
        rtl.setSystemProxyAgain,
        rtl.proxyPortInUse('10810'),
      ];
      for (final text in texts) {
        expect(text, matches(rtlStart), reason: '$language: $text');
        if (language == 'fa') {
          expect(text, isNot(contains('\u0654')), reason: text);
          expect(text, isNot(contains('\u0647\u200c\u06cc')), reason: text);
        }
      }
    }
  });

  test('the core states are worded by their code', () {
    EngineState state(
      String phase,
      String message, [
      String? code,
      String? address,
    ]) => EngineState.fromJson({
      'phase': phase,
      'message': message,
      'code': ?code,
      'address': ?address,
    });
    CoreStatus status(Map<String, Object?> state) => CoreStatus.fromJson({
      'state': state,
      'accounts': {'total': 3},
    });

    expect(
      stateText(
        l,
        state(
          'waiting',
          'Waiting for a network connection',
          StateCode.waitingForNetwork,
        ),
      ),
      l.waitingForNetwork,
    );
    expect(
      stateText(
        l,
        state(
          'waiting',
          'No usable account: 2 reserved, 1 reauth_required',
          StateCode.noUsableAccount,
        ),
      ),
      'No account is ready. Open the Accounts page.',
    );
    expect(
      stateText(l, state('waiting', 'Add an account', StateCode.noAccounts)),
      l.waitingForAccount,
    );
    // A port that cannot be used names it; the button changes it.
    final blocked = state(
      'error',
      'listening on 127.0.0.1:10810: bind: forbidden',
      StateCode.startFailed,
      '127.0.0.1:10810',
    );
    expect(
      stateText(l, blocked),
      "The proxy couldn't start on port 10810. Choose another port.",
    );
    expect(blocked.proxyPortBlocked, isTrue);
    // Without the address (an older core), the core's own words.
    final unnamed = state('error', 'listen failed', StateCode.startFailed);
    expect(stateText(l, unnamed), 'listen failed');
    expect(unnamed.proxyPortBlocked, isFalse);
    // The core's English for a code this app does not know, and without
    // one.
    expect(
      stateText(l, state('error', 'Something new', 'from_a_newer_core')),
      'Something new',
    );
    expect(stateText(l, state('error', 'Plain words')), 'Plain words');

    final fa = lookupAppLocalizations(const Locale('fa'));
    final look = PhaseLook.of(
      fa,
      status({
        'phase': 'waiting',
        'message': 'No usable account: 2 reserved, 1 reauth_required',
        'code': StateCode.noUsableAccount,
      }),
    );
    expect(look.hint, 'هیچ حسابی آماده نیست. صفحه حساب‌ها را باز کنید.');
    expect(
      PhaseLook.of(
        fa,
        status({
          'phase': 'waiting',
          'message': 'Waiting for a network connection',
          'code': StateCode.waitingForNetwork,
        }),
      ).hint,
      'منتظر اتصال اینترنت…',
    );
  });

  test('no internet says so; an unknown code asks to check it', () {
    expect(
      failureText(
        l,
        null,
        failedTest(error: 'x', code: Failure.noNetwork),
      ).text,
      l.failNoInternet,
    );
    // The core's own words stay out of the text.
    final other = failureText(
      l,
      null,
      failedTest(
        error: 'Get "https://www.google.com/generate_204": odd',
        code: Failure.other,
      ),
    );
    expect(other.text, l.failOther);
    expect(other.detail, contains('generate_204'));
    expect(
      failureText(l, null, failedTest(error: 'x', code: 'new_kind')).text,
      l.connectRetrying,
    );
  });

  test('the newer of the wake failure and the test speaks', () {
    const early = '2026-10-19T10:00:00Z';
    const late = '2026-10-19T10:05:00Z';
    final wakeLater = failureText(
      l,
      proxy(error: 'w', code: Failure.signIn, at: late),
      failedTest(error: 't', code: Failure.unreachable, at: early),
    );
    expect(wakeLater.text, l.failSignIn);
    final testLater = failureText(
      l,
      proxy(error: 'w', code: Failure.signIn, at: early),
      failedTest(error: 't', code: Failure.unreachable, at: late),
    );
    expect(testLater.text, l.failUnreachable);
    expect(testLater.detail, 't');
  });
}