import 'package:crypton/crypton.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:marianum_mobile/push/push_keypair.dart';

void main() {
  group('generatePushKeypairPems', () {
    test('public PEM matches the Nextcloud SPKI format', () {
      final pems = generatePushKeypairPems();
      final pem = pems.publicKeyPem;

      expect(pem, startsWith('-----BEGIN PUBLIC KEY-----\n'));
      expect(pem, endsWith('\n-----END PUBLIC KEY-----'));

      // RSA-2048 SPKI base64 wrapped at 64 columns is byte-exactly 450 or 451
      // characters — the length Nextcloud validates against.
      expect(pem.length, anyOf(450, 451));

      // The END marker sits at a fixed offset (header + 392 base64 chars + 6
      // embedded newlines = 425, then '\n-----END...').
      expect(pem.indexOf('\n-----END PUBLIC KEY-----'), 425);

      // Body lines (between the header and footer) wrap at 64 columns.
      final body = pem
          .split('\n')
          .where((l) => !l.startsWith('-----'))
          .toList();
      for (final line in body.take(body.length - 1)) {
        expect(line.length, 64);
      }
    });

    test('private PEM round-trips back into a usable key', () {
      final pems = generatePushKeypairPems();
      final restored = RSAPrivateKey.fromPEM(pems.privateKeyPem);
      final restoredPublicPem = restored.publicKey.toFormattedPEM();
      expect(restoredPublicPem, pems.publicKeyPem);
    });
  });
}
