import 'dart:convert';
import 'package:consumer_app/core/networking/api_client.dart';
import 'package:consumer_app/models/messaging_models.dart';
import 'package:consumer_app/repositories/http_messaging_repository.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
void main() {
test(
'lists encrypted messages without introducing plaintext fields',
() async {
Uri? requestedUri;
final client = MockClient((request) async {
requestedUri = request.url;
return http.Response(
jsonEncode({
'messages': [
{
'id': 4,
'conversation_id': 9,
'sender_user_id': 2,
'sender_device_id': 'device-a',
'envelope_version': 'mw-e2ee-envelope-v1',
'crypto_suite': 'signal-protocol',
'ciphertext_b64': 'Y2lwaGVydGV4dA==',
'header_b64': 'aGVhZGVy',
'client_message_id': 'client-4',
'created_at': '2026-09-21 10:00:00',
},
],
'encrypted_only': true,
}),
200,
);
});
final api = ApiClient(
baseUrl: Uri.parse('https://example.test/wp-json/meinweimar/v1'),
locale: () => 'de-DE',
client: client,
);
final result = await HttpMessagingRepository(
api,
).listMessages(9, before: 20, limit: 10);
expect(result.encryptedOnly, isTrue);
expect(result.items.single.ciphertextB64, 'Y2lwaGVydGV4dA==');
expect(requestedUri?.queryParameters, {'limit': '10', 'before': '20'});
},
);
test('does not send private key material in a device bundle', () async {
final api = ApiClient(
baseUrl: Uri.parse('https://example.test/wp-json/meinweimar/v1'),
locale: () => 'de-DE',
client: MockClient((request) async => http.Response('{}', 201)),
);
final repository = HttpMessagingRepository(api);
const device = MessagingDeviceData(
deviceId: 'device-a',
identityKeyB64: 'identity',
signedPrekeyB64: 'prekey',
signedPrekeySignatureB64: 'signature',
prekeyBundle: {'identity_key': 'public', 'private_key': 'must-not-send'},
createdAt: '',
lastSeenAt: '',
);
expect(
() => repository.registerDevice(device),
throwsA(isA<ArgumentError>()),
);
});
test('revokes a device through the DELETE transport route', () async {
String? method;
String? path;
final api = ApiClient(
baseUrl: Uri.parse('https://example.test/wp-json/meinweimar/v1'),
locale: () => 'de-DE',
client: MockClient((request) async {
method = request.method;
path = request.url.path;
return http.Response('{"device_id":"device-a","revoked":true}', 200);
}),
);
final revoked = await HttpMessagingRepository(api).revokeDevice('device-a');
expect(revoked, isTrue);
expect(method, 'DELETE');
expect(path, endsWith('/messaging/devices/device-a'));
});
}
