import 'package:flutter_test/flutter_test.dart';
import 'package:minstrel/update/client_update_provider.dart';

void main() {
  group('isVersionNewer (semver path)', () {
    test('strictly newer', () {
      expect(isVersionNewer('0.1.1', '0.1.0'), isTrue);
      expect(isVersionNewer('0.2.0', '0.1.99'), isTrue);
      expect(isVersionNewer('1.0.0', '0.99.99'), isTrue);
    });

    test('equal returns false', () {
      expect(isVersionNewer('0.1.0', '0.1.0'), isFalse);
    });

    test('older returns false', () {
      expect(isVersionNewer('0.1.0', '0.1.1'), isFalse);
      expect(isVersionNewer('0.1.0', '0.2.0'), isFalse);
    });

    test('strips leading v on either side', () {
      expect(isVersionNewer('v0.1.1', '0.1.0'), isTrue);
      expect(isVersionNewer('0.1.1', 'v0.1.0'), isTrue);
      expect(isVersionNewer('v0.1.0', 'v0.1.0'), isFalse);
    });

    test('zero-pads shorter version when comparing', () {
      // "1.2" treated as "1.2.0" — shorter side gets zero-padded so
      // comparison is component-wise.
      expect(isVersionNewer('1.2.1', '1.2'), isTrue);
      expect(isVersionNewer('1.2', '1.2.1'), isFalse);
      expect(isVersionNewer('1.2', '1.2.0'), isFalse);
    });
  });

  group('isVersionNewer (date-style versions)', () {
    // Date tags are 3- or 4-part: 2026.05.10 or 2026.05.10.1. The
    // numeric comparator parses each component as int (so "05" == 5)
    // and zero-pads the shorter side.
    test('newer date → newer', () {
      expect(isVersionNewer('2026.05.10', '2026.05.09'), isTrue);
    });
    test('older date → not newer', () {
      expect(isVersionNewer('2026.05.09', '2026.05.10'), isFalse);
    });
    test('equal date → not newer', () {
      expect(isVersionNewer('2026.05.10', '2026.05.10'), isFalse);
    });
    test('leading zeros do not affect ordering', () {
      // "2026.5.11" and "2026.05.11" parse to the same components.
      // 12 > 5 numerically so month rollover stays correct.
      expect(isVersionNewer('2026.5.11', '2026.05.11'), isFalse);
      expect(isVersionNewer('2026.12.1', '2026.5.31'), isTrue);
    });
    test('4-part build suffix breaks the tie', () {
      // 2026.05.10 vs 2026.05.10.1 — first three equal, server has a
      // 4th component (1), client zero-pads → server is newer.
      expect(isVersionNewer('2026.05.10.1', '2026.05.10'), isTrue);
      expect(isVersionNewer('2026.05.10', '2026.05.10.0'), isFalse);
    });
  });

  group('isVersionNewer (truly non-semver fallback)', () {
    // Branch-name-style strings (no version structure) hit the
    // try/catch fallback. Any string difference reads as "newer" so
    // operators see _something_ rather than silently miss updates.
    test('different unparseable strings → newer', () {
      expect(isVersionNewer('main', 'dev'), isTrue);
      expect(isVersionNewer('dev', 'main'), isTrue);
    });
    test('equal unparseable strings → not newer', () {
      expect(isVersionNewer('main', 'main'), isFalse);
    });
  });
}
