import 'dart:io';

import 'package:flutter_test/flutter_test.dart';

void main() {
  final loginSource = File('lib/pages/auth/login_page.dart').readAsStringSync();
  final appSource = File('lib/app.dart').readAsStringSync();

  test('login validates fields before asking for agreement', () {
    final submit = loginSource.substring(
      loginSource.indexOf('Future<void> _submit()'),
      loginSource.indexOf('Future<bool?> _showAgreementDialog()'),
    );
    expect(
      submit.indexOf("_error = '请输入手机号和验证码'"),
      lessThan(submit.indexOf('_showAgreementDialog()')),
    );
  });

  test('registration keeps the authenticated session', () {
    final submit = loginSource.substring(
      loginSource.indexOf('Future<void> _submit()'),
      loginSource.indexOf('Future<bool?> _showAgreementDialog()'),
    );
    expect(submit, isNot(contains('authProvider.notifier).logout()')));
  });

  test('boot gate stays visible until authenticated route changes', () {
    expect(appSource, contains("currentRoute == 'login'"));
  });

  test('keyboard moves the foreground without resizing the background', () {
    expect(loginSource, contains('resizeToAvoidBottomInset: false'));
    expect(loginSource, contains('MediaQuery.viewInsetsOf(context).bottom'));
  });

  test('development sms code is only filled in debug builds', () {
    expect(loginSource, contains('kDebugMode && result.devCode != null'));
  });
}
