diff --git a/core/bus/test/.gitkeep b/core/bus/test/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/core/bus/test/dispatcher_test.dart b/core/bus/test/dispatcher_test.dart index c08de781..06e56268 100644 --- a/core/bus/test/dispatcher_test.dart +++ b/core/bus/test/dispatcher_test.dart @@ -7,6 +7,15 @@ import 'package:angel3_mq/mq.dart'; import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; +class IsMessage extends Matcher { + @override + bool matches(item, Map matchState) => item is Message; + + @override + Description describe(Description description) => + description.add('is a Message'); +} + class MockContainer extends Mock implements Container { final Map _instances = {}; @@ -31,7 +40,26 @@ class MockEventBus extends Mock implements EventBus { } } -class MockMQClient extends Mock implements MQClient {} +class MockMQClient extends Mock implements MQClient { + Message? capturedMessage; + String? capturedExchangeName; + String? capturedRoutingKey; + + @override + dynamic noSuchMethod(Invocation invocation, + {Object? returnValue, Object? returnValueForMissingStub}) { + if (invocation.memberName == #sendMessage) { + final namedArgs = invocation.namedArguments; + capturedMessage = namedArgs[#message] as Message?; + capturedExchangeName = namedArgs[#exchangeName] as String?; + capturedRoutingKey = namedArgs[#routingKey] as String?; + return null; + } + return super.noSuchMethod(invocation, + returnValue: returnValue, + returnValueForMissingStub: returnValueForMissingStub); + } +} class TestCommand implements Command { final String data; @@ -117,6 +145,45 @@ void main() { await commandEventController.close(); }); + test('dispatch should queue ShouldQueue commands', () async { + final command = TestQueuedCommand('queued data'); + + // Dispatch the command + await dispatcher.dispatch(command); + + // Verify that sendMessage was called and check the message properties + expect(mqClient.capturedMessage, isNotNull); + expect(mqClient.capturedMessage!.payload, equals(command)); + expect(mqClient.capturedMessage!.headers?['commandType'], + equals('TestQueuedCommand')); + + // Optionally, verify exchange name and routing key if needed + expect(mqClient.capturedExchangeName, isNull); + expect(mqClient.capturedRoutingKey, isNull); + }); + + test( + 'dispatchAfterResponse should send message to queue with specific header', + () { + final command = TestCommand('after response data'); + + // Call dispatchAfterResponse + dispatcher.dispatchAfterResponse(command); + + // Verify that sendMessage was called and check the message properties + expect(mqClient.capturedMessage, isNotNull); + expect(mqClient.capturedMessage!.payload, equals(command)); + expect(mqClient.capturedMessage!.headers?['commandType'], + equals('TestCommand')); + expect(mqClient.capturedMessage!.headers?['dispatchAfterResponse'], + equals('true')); + + // Verify routing key + expect(mqClient.capturedRoutingKey, equals('after_response_queue')); + + // Optionally, verify exchange name if needed + expect(mqClient.capturedExchangeName, isNull); + }); test('map should register command handlers', () { dispatcher.map({TestCommand: TestHandler});