require_relative '../boot'
require 'minitest/autorun'
require 'pry-rescue/minitest' unless ENV['CI']

describe "A webhook trigger" do
  it "can be used to execute konnectors" do
    Helpers.scenario "webhook_trigger"
    Helpers.start_mailhog

    # No debounce
    inst = Instance.create
    source_url = "file://" + File.expand_path("../konnector", __dir__)
    konnector_name = Faker::Creature::Cat.name.downcase.delete(" ")
    inst.install_konnector konnector_name, source_url
    account = Account.create inst, type: konnector_name, name: "1_#{Faker::TvShows::DrWho.character}"
    args = { "konnector" => konnector_name, "account" => account.couch_id, "foo" => "bar" }
    webhook_url = Trigger::Webhook.create inst, args

    opts = { :"content-type" => 'application/json' }
    body = { "quote" => Faker::TvShows::DrWho.quote }
    RestClient.post webhook_url, JSON.generate(body), opts

    done = false
    10.times do
      sleep 1
      done = File.exist? account.log
      break if done
    end
    assert done
    executed = JSON.parse File.read(account.log)
    assert_equal executed["account"]["_id"], account.couch_id
    assert_equal executed["fields"], args
    assert_equal executed["payload"], body

    # With a big payload
    account2 = Account.create inst, type: konnector_name, name: "2_#{Faker::TvShows::DrWho.character}"
    args = { "konnector" => konnector_name, "account" => account2.couch_id, "foo" => "bar" }
    webhook_url = Trigger::Webhook.create inst, args

    opts = { :"content-type" => 'application/json' }
    body = { "big" => "x" * 1_000_000 }
    RestClient.post webhook_url, JSON.generate(body), opts

    done = false
    10.times do
      sleep 1
      done = File.exist? account2.log
      break if done
    end
    assert done
    executed = JSON.parse File.read(account2.log)
    assert_equal executed["account"]["_id"], account2.couch_id
    assert_equal executed["fields"], args
    assert_equal executed["payload"], { "fromFile" => body }

    # With debounce
    account3 = Account.create inst, type: konnector_name, name: "3_#{Faker::TvShows::DrWho.character}"
    args = { "konnector" => konnector_name, "account" => account3.couch_id, "foo" => "bar" }
    webhook_url = Trigger::Webhook.create inst, args, "1s"

    opts = { :"content-type" => 'application/json' }
    RestClient.post webhook_url, JSON.generate(part: 1), opts
    RestClient.post webhook_url, JSON.generate(part: 2), opts
    RestClient.post webhook_url, JSON.generate(part: 3), opts
    RestClient.post webhook_url, JSON.generate(part: 4), opts

    done = false
    10.times do
      sleep 1
      done = File.exist? account3.log
      break if done
    end
    assert done
    executed = JSON.parse File.read(account3.log)
    payloads = [{ "part" => 1 }, { "part" => 2 }, { "part" => 3 }, { "part" => 4 }]
    assert_equal executed["account"]["_id"], account3.couch_id
    assert_equal executed["fields"], args
    assert_equal executed["payload"], { "payloads" => payloads }

    account.delete inst
    account2.delete inst
    inst.remove
  end
end
