import 'package:moxlib/moxlib.dart'; import 'package:moxxmpp/moxxmpp.dart'; import 'package:test/test.dart'; import '../helpers/logging.dart'; import '../helpers/manager.dart'; import '../helpers/xmpp.dart'; class StubbedDiscoManager extends DiscoManager { StubbedDiscoManager(this._itemError) : super([]); final bool _itemError; @override Future> discoInfoQuery( JID entity, { String? node, bool shouldEncrypt = true, bool shouldCache = true, }) async { final result = DiscoInfo.fromQuery( XMLNode.fromString(''' '''), JID.fromString('pubsub.server.example.org'), ); return Result(result); } @override Future>> discoItemsQuery( JID entity, { String? node, bool shouldEncrypt = true, }) async { if (_itemError) { return Result( UnknownDiscoError(), ); } return const Result>( [], ); } } void main() { initLogger(); test( 'Test pre-processing with pubsub#max_items when the server does not support it (1/2)', () async { final manager = PubSubManager(); final tm = TestingManagerHolder(); await tm.register([ StubbedDiscoManager(false), manager, ]); final result = await manager.preprocessPublishOptions( JID.fromString('pubsub.server.example.org'), 'urn:xmpp:omemo:2:bundles', const PubSubPublishOptions(maxItems: 'max'), ); expect(result.maxItems, '1'); }); test( 'Test pre-processing with pubsub#max_items when the server does not support it (2/2)', () async { final manager = PubSubManager(); final tm = TestingManagerHolder(); await tm.register([ StubbedDiscoManager(true), manager, ]); final result = await manager.preprocessPublishOptions( JID.fromString('pubsub.server.example.org'), 'urn:xmpp:omemo:2:bundles', const PubSubPublishOptions(maxItems: 'max'), ); expect(result.maxItems, '1'); }); test( 'Test publishing with pubsub#max_items when the server does not support it', () async { final socket = StubTCPSocket.authenticated( TestingManagerHolder.settings, [ StanzaExpectation( ''' ''', ''' ''', ignoreId: true, adjustId: true, ), StanzaExpectation( ''' ''', ''' ''', ignoreId: true, adjustId: true, ), StanzaExpectation( ''' http://jabber.org/protocol/pubsub#publish-options 1 ''', ''' ''', ignoreId: true, adjustId: true, ), ], ); final connection = XmppConnection( TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), ClientToServerNegotiator(), socket, )..connectionSettings = TestingManagerHolder.settings; await connection.registerManagers([ PubSubManager(), DiscoManager([]), PresenceManager(), RosterManager(TestingRosterStateManager(null, [])), ]); await connection.registerFeatureNegotiators([ SaslPlainNegotiator(), ResourceBindingNegotiator(), ]); await connection.connect( waitUntilLogin: true, ); final item = XMLNode(tag: 'test-item'); final result = await connection.getManagerById(pubsubManager)!.publish( JID.fromString('pubsub.server.example.org'), 'princely_musings', item, id: 'current', options: const PubSubPublishOptions(maxItems: 'max'), ); expect(result.isType(), true); }); }