require "pact_broker/api/decorators/integration_decorator"
require "pact_broker/integrations/integration"

module PactBroker
  module Api
    module Decorators
      describe IntegrationDecorator do
        describe ".eager_load_associations" do
          subject { IntegrationDecorator }

          its(:eager_load_associations) { is_expected.to eq [:consumer, :provider] }
        end

        describe "#to_json" do
          before do
            allow(integration_decorator).to receive(:dashboard_url_for_integration).and_return("/dashboard")
            allow(integration_decorator).to receive(:matrix_url).and_return("/matrix")
            allow(integration_decorator).to receive(:group_url).and_return("/group")
            allow(integration_decorator).to receive(:integration_url).and_return("/integration")
          end

          let(:integration) do
            instance_double(PactBroker::Integrations::Integration,
              consumer: consumer,
              provider: provider,
              verification_status_for_latest_pact: pseudo_branch_verification_status
            )
          end
          let(:consumer) { double("consumer", name: "the consumer") }
          let(:provider) { double("provider", name: "the provider") }
          let(:pseudo_branch_verification_status) { double("pseudo_branch_verification_status", to_s: "some_status") }

          let(:options) { { user_options: { base_url: "http://example.org" } } }
          let(:expected_hash) do
            {
              "consumer" => {
                "name" => "the consumer"
              },
              "provider" => {
                "name" => "the provider"
              },
              "_links" => {
                "pb:dashboard" => {
                  "href" => "/dashboard"
                },
                "pb:matrix" => {
                  "title" => "Matrix of pacts/verification results for the consumer and the provider",
                  "href" => "/matrix"
                },
                "pb:group" => {
                  "href" => "/group"
                },
                "pb:delete_integration" => {
                  "title" => "Delete the integration between the consumer and the provider",
                  "href" => "/integration"
                }
              }
            }
          end

          let(:integration_decorator) { IntegrationDecorator.new(integration) }
          let(:json) { integration_decorator.to_json(options) }
          subject { JSON.parse(json) }

          it "generates a hash" do
            expect(subject).to match_pact expected_hash
          end

          it "generates the correct link for the dashboard" do
            expect(integration_decorator).to receive(:dashboard_url_for_integration).with(
              "the consumer",
              "the provider",
              "http://example.org"
            )
            subject
          end

          it "generates the correct link for the matrix" do
            expect(integration_decorator).to receive(:matrix_url).with(
              "the consumer",
              "the provider",
              "http://example.org"
            )
            subject
          end

          it "generates the correct group url for the matrix" do
            expect(integration_decorator).to receive(:group_url).with(
              "the consumer",
              "http://example.org"
            )
            subject
          end

          it "generates the correct delete url for the integration" do
            expect(integration_decorator).to receive(:integration_url).with(
              "the consumer",
              "the provider",
              "http://example.org"
            )
            subject
          end
        end
      end
    end
  end
end
