{"size": 4231, "ext": "js", "max_stars_repo_path": "lib/clients/eventbridge.spec.js", "max_stars_repo_name": "pureskillgg/awsjs", "max_stars_repo_stars_event_min_datetime": "2021-12-21T07:00:38.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-21T07:00:38.000Z", "max_issues_repo_path": "lib/clients/eventbridge.spec.js", "max_issues_repo_name": "pureskillgg/awsjs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/clients/eventbridge.spec.js", "max_forks_repo_name": "pureskillgg/awsjs", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.7426900585, "max_line_length": 77, "alphanum_fraction": 0.6806901442}
import test from 'ava'
import * as td from 'testdouble'
import { v4 as uuidv4 } from 'uuid'
import { createLogger } from '@meltwater/mlabs-logger'
import { PutEventsCommand } from '@aws-sdk/client-eventbridge'

import { registerTestdoubleMatchers } from '../../testdouble-matchers.js'

import {
  EventbridgeClient,
  EventbridgeFailedEntriesError
} from './eventbridge.js'

test.before(() => {
  registerTestdoubleMatchers(td)
})

test.beforeEach((t) => {
  t.context.AwsEventBridgeClient = td.constructor(['send'])

  t.context.createClient = (t, options) => {
    const client = new EventbridgeClient({
      name: uuidv4(),
      AwsEventBridgeClient: t.context.AwsEventBridgeClient,
      eventBusName,
      reqId,
      log: createLogger({ t }),
      ...options
    })

    return client
  }
})

test('constructor: passes params to AWS EventBridgeClient', (t) => {
  const { AwsEventBridgeClient } = t.context
  const params = { foo: 'bar' }
  const client = new EventbridgeClient({
    name: uuidv4(),
    AwsEventBridgeClient,
    eventBusName,
    params,
    log: createLogger({ t })
  })
  td.verify(new AwsEventBridgeClient(params))
  t.truthy(client)
})

test('putEvents: returns response', async (t) => {
  const { AwsEventBridgeClient, createClient } = t.context
  const client = createClient(t)
  const input = eventInput
  const entry = eventEntry

  td.when(
    AwsEventBridgeClient.prototype.send(
      td.matchers.isAwsSdkCommand(new PutEventsCommand({ Entries: [entry] }))
    )
  ).thenResolve(eventResponse)

  const data = await client.putEvents(input)

  t.deepEqual(data, putEventsResponseFormatted)
})

test('putEvents: passes params', async (t) => {
  const { AwsEventBridgeClient, createClient } = t.context
  const client = createClient(t)
  const input = eventInput
  const entry = eventEntry

  td.when(
    AwsEventBridgeClient.prototype.send(
      td.matchers.isAwsSdkCommand(
        new PutEventsCommand({ Entries: [entry], Bar: 3 })
      )
    )
  ).thenResolve(eventResponse)

  const data = await client.putEvents(input, { bar: 3 })

  t.deepEqual(data, putEventsResponseFormatted)
})

test('putEvents: throws error from client', async (t) => {
  const { AwsEventBridgeClient, createClient } = t.context
  const client = createClient(t)
  const err = new Error('foo')

  td.when(
    AwsEventBridgeClient.prototype.send(td.matchers.anything())
  ).thenReject(err)

  await t.throwsAsync(() => client.putEvents(), { is: err })
})

test('putEvents: throws error on failed entries', async (t) => {
  const { AwsEventBridgeClient, createClient } = t.context
  const client = createClient(t)
  const input = eventInput
  const entry = eventEntry

  td.when(
    AwsEventBridgeClient.prototype.send(
      td.matchers.isAwsSdkCommand(new PutEventsCommand({ Entries: [entry] }))
    )
  ).thenResolve({
    ...eventResponse,
    FailedEntryCount: 1
  })

  const error = await t.throwsAsync(() => client.putEvents(input), {
    instanceOf: EventbridgeFailedEntriesError,
    message: /1/
  })
  t.is(error.failedEntryCount, 1)
  t.deepEqual(error.data, putEventsResponseFormatted)
})

test('EventbridgeFailedEntriesError', (t) => {
  const res = { ...eventResponse, FailedEntryCount: 4 }
  const err = new EventbridgeFailedEntriesError(res)
  t.like(err, {
    name: 'EventbridgeFailedEntriesError',
    code: 'err_eventbridge_failed_entries',
    failedEntryCount: 4,
    data: putEventsResponseFormatted
  })
})

const eventBusName = 'mock-event-bus'
const reqId = 'mock-req-id'

const eventInput = [
  {
    time: '2020',
    source: 'mock-source',
    resources: ['mock-resource'],
    detailType: 'mock-detail-type',
    detail: { foo: 2 }
  }
]

const eventEntry = {
  Time: '2020-01-01T00:00:00.000Z',
  Source: 'mock-source',
  Resources: ['mock-resource'],
  DetailType: 'mock-detail-type',
  Detail: '{"foo":2}',
  EventBusName: eventBusName
}

const eventResponse = {
  FailedEntryCount: 0,
  Entries: [
    {
      EventId: 'mock-event-id',
      ErrorCode: 'mock-error-code',
      ErrorMessage: 'mock-error-message'
    }
  ]
}

const putEventsResponseFormatted = [
  {
    eventId: 'mock-event-id',
    errorCode: 'mock-error-code',
    errorMessage: 'mock-error-message'
  }
]
