Files
IKAR/tool/native_message_peer.dart
T

62 lines
1.9 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'live_ikar_smoke.dart' as live;
Future<void> main(List<String> args) async {
final peer = await live.Device.create(
Uri.parse('https://ikar.kusoft.xyz'),
Platform.environment['IKAR_TEST_NODE'] ?? 'node',
'+999${DateTime.now().microsecondsSinceEpoch}',
'IKAR native test',
);
try {
final target = await peer.ok(46, {'phone': '+12025550101'});
final chatId = peer.id ^ (target['contact']['id'] as int);
if (args.contains('--notification')) {
await peer.ok(64, {
'chatId': chatId,
'message': {'text': 'IKAR background notification'},
});
stdout.writeln(
jsonEncode({'sent': 'IKAR background notification', 'chatId': chatId}),
);
await Future<void>.delayed(const Duration(seconds: 10));
} else {
final reply = peer.transport.events
.firstWhere(
(e) =>
e['opcode'] == 128 &&
e['payload']?['message']?['text'] == 'IKAR native reply',
)
.timeout(const Duration(seconds: 180));
await peer.ok(64, {
'chatId': chatId,
'message': {'text': 'IKAR native delivery'},
});
stdout.writeln(
jsonEncode({
'status': 'WAITING_NATIVE_REPLY',
'chatId': chatId,
'peer': peer.id,
}),
);
final received = await reply;
final report = {
'passed': true,
'nativeDevice': 'Android emulator API35',
'sent': 'IKAR native delivery',
'reply': received['payload']['message']['text'],
'timestamp': DateTime.now().toUtc().toIso8601String(),
};
await File(
'artifacts/native-message.json',
).writeAsString(const JsonEncoder.withIndent(' ').convert(report));
stdout.writeln(jsonEncode(report));
}
} finally {
await peer.ok(199, {});
await peer.close();
}
}