import 'package:flutter_test/flutter_test.dart';

import 'package:appsok/src/features/settings/jenkins_web_login_page.dart';

void main() {
  group('jenkinsUri', () {
    test('root URL produces login path', () {
      expect(
        jenkinsUri('https://jenkins.example.com', 'login').toString(),
        'https://jenkins.example.com/login',
      );
    });

    test('root URL with trailing slash produces login path', () {
      expect(
        jenkinsUri('https://jenkins.example.com/', 'login').toString(),
        'https://jenkins.example.com/login',
      );
    });

    test('context path URL preserves base path for login', () {
      expect(
        jenkinsUri('https://jenkins.example.com/jenkins', 'login').toString(),
        'https://jenkins.example.com/jenkins/login',
      );
    });

    test('context path URL with trailing slash preserves base path', () {
      expect(
        jenkinsUri('https://jenkins.example.com/jenkins/', 'login').toString(),
        'https://jenkins.example.com/jenkins/login',
      );
    });

    test('root URL produces whoAmI probe path', () {
      expect(
        jenkinsUri('https://jenkins.example.com', 'whoAmI/api/json').path,
        '/whoAmI/api/json',
      );
    });

    test('context path URL produces whoAmI probe path with base', () {
      expect(
        jenkinsUri(
          'https://jenkins.example.com/jenkins',
          'whoAmI/api/json',
        ).path,
        '/jenkins/whoAmI/api/json',
      );
    });

    test('strips query and fragment from base URL', () {
      final uri = jenkinsUri('https://host/jenkins?foo=1#bar', 'login');
      expect(uri.query, '');
      expect(uri.fragment, '');
      expect(uri.path, '/jenkins/login');
    });
  });

  group('parseWhoAmIResult', () {
    test('returns null for anonymous user', () {
      const json = '{"anonymous":true,"id":"anonymous","fullName":"unknown"}';
      expect(parseWhoAmIResult(json), isNull);
    });

    test('returns null when anonymous field is missing (defaults to true)', () {
      const json = '{"id":"","fullName":""}';
      expect(parseWhoAmIResult(json), isNull);
    });

    test('returns null when id is empty string', () {
      const json = '{"anonymous":false,"id":"","fullName":"Toki"}';
      expect(parseWhoAmIResult(json), isNull);
    });

    test('returns result with username and displayName for logged-in user', () {
      const json = '{"anonymous":false,"id":"toki","fullName":"Toki Lab"}';
      final result = parseWhoAmIResult(json);
      expect(result, isNotNull);
      expect(result!.username, 'toki');
      expect(result.displayName, 'Toki Lab');
    });

    test('returns result with null displayName when fullName is absent', () {
      const json = '{"anonymous":false,"id":"toki"}';
      final result = parseWhoAmIResult(json);
      expect(result, isNotNull);
      expect(result!.username, 'toki');
      expect(result.displayName, isNull);
    });

    test('returns null for invalid JSON', () {
      expect(parseWhoAmIResult('not-json'), isNull);
    });

    test('returns null for empty string', () {
      expect(parseWhoAmIResult(''), isNull);
    });

    test('returns null for null literal string', () {
      expect(parseWhoAmIResult('null'), isNull);
    });
  });
}
