require "rails_helper"

describe AppHost do
  describe "#domain" do
    it "returns the domain set in the environment" do
      env = { "DOMAIN" => "test-domain.com" }

      app_domain = AppHost.new(env, "development")

      expect(app_domain.domain).to eq("test-domain.com")
    end

    context "when domain is not set in the environment" do
      it "returns the default domain for the development environment" do
        env = {}

        app_domain = AppHost.new(env, "development")

        expect(app_domain.domain).to eq("localhost")
      end

      it "returns the default domain for the test environment" do
        env = {}

        app_domain = AppHost.new(env, "test")

        expect(app_domain.domain).to eq("localhost")
      end

      it "returns the default domain for the production environment" do
        env = {}

        app_domain = AppHost.new(env, "production")

        expect(app_domain.domain).to eq("bigmedia.ai")
      end
    end
  end

  describe "#port" do
    it "returns the port set in the environment" do
      env = { "WEB_PORT" => 4000 }

      app_domain = AppHost.new(env, "development")

      expect(app_domain.port).to eq(4000)
    end

    context "when port is not set in the environment" do
      it "returns the port domain for the development environment" do
        env = {}

        app_domain = AppHost.new(env, "development")

        expect(app_domain.port).to eq(3000)
      end

      it "returns the default port for the test environment" do
        env = {}

        app_domain = AppHost.new(env, "test")

        expect(app_domain.port).to eq(31337)
      end

      it "returns the default port for the production environment" do
        env = {}

        app_domain = AppHost.new(env, "production")

        expect(app_domain.port).to eq(nil)
      end
    end
  end

  describe "#domain_with_port" do
    it "returns the combination of domain with the port" do
      env = { "DOMAIN" => "test-domain.com", "WEB_PORT" => 4000 }

      app_domain = AppHost.new(env, "development")

      expect(app_domain.domain_with_port).to eq("test-domain.com:4000")
    end

    context "when port is not specified at all" do
      it "omits the port portion of the URL" do
        env = { "DOMAIN" => "test-domain.com" }

        app_domain = AppHost.new(env, "production")

        expect(app_domain.domain_with_port).to eq("test-domain.com")
      end
    end
  end

  describe "#use_ssl" do
    it "returns true if the value set in the environment" do
      env = { "USE_SSL" => "true" }

      app_domain = AppHost.new(env, "development")

      expect(app_domain).to be_using_ssl
    end

    context "when the value is not set in the environment" do
      it "returns the default value for the development environment" do
        env = {}

        app_domain = AppHost.new(env, "development")

        expect(app_domain).not_to be_using_ssl
      end

      it "returns the default value for the test environment" do
        env = {}

        app_domain = AppHost.new(env, "test")

        expect(app_domain).not_to be_using_ssl
      end

      it "returns the default value for the production environment" do
        env = {}

        app_domain = AppHost.new(env, "production")

        expect(app_domain).to be_using_ssl
      end
    end

  end

  describe "#protocol" do
    it "returns 'https' when using SSL" do
      env = { "USE_SSL" => true }

      app_domain = AppHost.new(env)

      expect(app_domain.protocol).to eq :https
    end

    it "returns 'http's when not using SSL" do
      env = {}

      app_domain = AppHost.new(env)

      expect(app_domain.protocol).to eq :http
    end
  end
end
