{"size": 2993, "ext": "js", "max_stars_repo_path": "src/scripting/logichook/asserter/ButtonsAsserter.js", "max_stars_repo_name": "andrewrfreed/botium-core", "max_stars_repo_stars_event_min_datetime": "2021-10-05T17:36:51.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-16T13:24:49.000Z", "max_issues_repo_path": "src/scripting/logichook/asserter/ButtonsAsserter.js", "max_issues_repo_name": "andrewrfreed/botium-core", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/scripting/logichook/asserter/ButtonsAsserter.js", "max_forks_repo_name": "andrewrfreed/botium-core", "max_forks_repo_forks_event_min_datetime": "2021-11-16T13:26:46.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-16T13:26:46.000Z"}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 27.9719626168, "max_line_length": 136, "alphanum_fraction": 0.5282325426}
const { BotiumError } = require('../../BotiumError')

module.exports = class ButtonsAsserter {
  constructor (context, caps = {}) {
    this.context = context
    this.caps = caps
    this.name = 'ButtonsAsserter'
  }

  _buttonTextsFromCardsRecursive (cards) {
    if (!cards) {
      return []
    }
    let result = []
    for (const card of cards) {
      result = result.concat(card.buttons ? card.buttons.map(b => b.text) : [])
      result = result.concat(this._buttonTextsFromCardsRecursive(card.cards))
    }

    return result
  }

  _evalButtons (args, botMsg) {
    const allButtons = (botMsg.buttons ? botMsg.buttons.map(b => b.text) : []).concat(this._buttonTextsFromCardsRecursive(botMsg.cards))
    if (!args || args.length === 0) {
      return { allButtons, buttonsNotFound: [], buttonsFound: allButtons }
    }
    const buttonsNotFound = []
    const buttonsFound = []
    for (let i = 0; i < (args || []).length; i++) {
      if (allButtons.findIndex(b => this.context.Match(b, args[i])) < 0) {
        buttonsNotFound.push(args[i])
      } else {
        buttonsFound.push(args[i])
      }
    }
    return { allButtons, buttonsNotFound, buttonsFound }
  }

  assertNotConvoStep ({ convo, convoStep, args = [], botMsg = [] }) {
    const { allButtons, buttonsFound } = this._evalButtons(args, botMsg)

    if (buttonsFound.length > 0) {
      return Promise.reject(new BotiumError(
        `${convoStep.stepTag}: Not expected button(s) with text "${buttonsFound}"`,
        {
          type: 'asserter',
          source: this.name,
          params: {
            args
          },
          cause: {
            not: true,
            expected: args,
            actual: allButtons,
            diff: buttonsFound
          }
        }
      ))
    }
    return Promise.resolve()
  }

  assertConvoStep ({ convo, convoStep, args = [], botMsg = [] }) {
    const { allButtons, buttonsNotFound, buttonsFound } = this._evalButtons(args, botMsg)

    if (!args || args.length === 0) {
      if (!buttonsFound.length) {
        return Promise.reject(new BotiumError(
          `${convoStep.stepTag}: Expected some button(s)`,
          {
            type: 'asserter',
            source: this.name,
            params: {
              args
            },
            cause: {
              not: false,
              expected: args,
              actual: allButtons,
              diff: buttonsNotFound
            }
          }
        ))
      }
    } else if (buttonsNotFound.length > 0) {
      return Promise.reject(new BotiumError(
          `${convoStep.stepTag}: Expected button(s) with text "${buttonsNotFound}"`,
          {
            type: 'asserter',
            source: this.name,
            params: {
              args
            },
            cause: {
              not: false,
              expected: args,
              actual: allButtons,
              diff: buttonsNotFound
            }
          }
      ))
    }
    return Promise.resolve()
  }
}
