import { SNSClient, PublishCommand } from '@aws-sdk/client-sns' import { expect } from 'chai' import Publisher from '../../src/Publisher/Publisher.js' import { mockAwsClient, mockAwsCommand, mockAwsResult } from '../AwsSdkTest.utils.js' const SNSMock = mockAwsClient(SNSClient) describe('SNS Pub-Sub', () => { // reset mock beforeEach(() => { SNSMock.reset() }) const provider = new Publisher({ region: 'ca-central-1', }) test('Publishes with success', async () => { const msg = { text: '123' } const topic = '123' const messageId = 'id-123' SNSMock.on(mockAwsCommand(PublishCommand), { Message: JSON.stringify(msg), TopicArn: topic, }).resolves( mockAwsResult({ MessageId: messageId, }) ) const res = await provider.publishOnTopic(msg, topic) expect(res).is.not.null expect(res?.MessageId).to.be.equals(messageId) }) test('Publishes with success (additional props)', async () => { const msg = { text: '123' } const topic = '123' const messageId = 'id-123' SNSMock.on(mockAwsCommand(PublishCommand), { Message: JSON.stringify(msg), TopicArn: topic, }).resolves( mockAwsResult({ MessageId: messageId, }) ) const res = await provider.publishOnTopic(msg, topic, { FifoGroup: 123, }) expect(res).is.not.null expect(res?.MessageId).to.be.equals(messageId) }) test('Publish failure', async () => { const msg = { text: '123' } const topic = '123' SNSMock.on(mockAwsCommand(PublishCommand)).rejects(new Error('failed!')) const res = await provider.publishOnTopic(msg, topic) expect(res).is.undefined }) }) describe('SNS SMS', () => { // reset mock beforeEach(() => { SNSMock.reset() }) const provider = new Publisher({ region: 'ca-central-1', }) test('Publishes with success', async () => { const msg = 'hello world' const phoneNumber = '+16042001234' const messageId = 'id-123' SNSMock.on(mockAwsCommand(PublishCommand), { Message: msg, PhoneNumber: phoneNumber, }).resolves( mockAwsResult({ MessageId: messageId, }) ) const res = await provider.publishSMS(msg, phoneNumber) expect(res).is.not.null expect(res?.MessageId).to.be.equals(messageId) }) test('Publish failure', async () => { const msg = 'hello world' const phoneNumber = '+16042001234' SNSMock.on(mockAwsCommand(PublishCommand)).rejects(new Error('failed!')) const res = await provider.publishSMS(msg, phoneNumber) expect(res).is.undefined }) }) export {}