require "rails_helper"

RSpec.describe Testing::ConfigurationGenerator do

  let!(:page) { create(:page, name: "Test Page", url: "/") }
  let!(:partial) { create(:partial) }
  let!(:partial_field) { create(:partial_field, partial_id: partial.id, name: "Text", field_type: "text", char_limit: 144, order: 0) }
  let!(:page_partial) { create(:page_partial, page: page, order: 0, partial: partial) }

  describe ".call" do

    it "will write the page configuration to a yaml file in the config/testing/page_definitions" do
      file_path      = Rails.root.join("config", "testing", "page_definitions", "test-page.yml")
      test_file_path = Rails.root.join("spec", "fixtures", "test-page.yml")

      File.delete(file_path) if File.exist?(file_path) #clear out any old files from previous test runs

      subject.call(page.url)

      expect(File.read(file_path)).to eq File.read(test_file_path)
    end

  end

  describe ".get_page_partial_data" do

    it "will take a given page_partial and return a json hash of attributes related to it and its fields" do
      PagePartialField.create_page_partial_fields!(page_partial)
      expected_output = {
        name: "Best Partial",
        fields: [
          {
            name: "Text",
            type: "text",
            object_type: nil,
            mandatory: false,
            char_limit: 144,
            header_type: nil,
            options: [],
            value: "",
          },
        ],
      }
      expect(subject.get_page_partial_data(page_partial)).to eq expected_output
    end

  end

  describe ".get_configuration_value" do

    it "will take a given page_partial_field and return appropriate value for the page configuration file" do
      random_value = SecureRandom.hex
      PagePartialField.create_page_partial_fields!(page_partial)
      page_partial_field = page_partial.page_partial_fields.first
      page_partial_field.update!(value: random_value)

      expect(subject.get_configuration_value(page_partial_field)).to eq random_value
    end

  end

  describe ".get_object_fields" do

    it "will take a given page_partial_field, and return a json hash of the associated objekt's attributes" do
      random_value    = SecureRandom.hex
      template        = create(:template)
      field           = create(:field, field_type: "text", order: 0, object_type: "", template: template)
      objekt          = create(:objekt, template: template)
      objekt_field    = create(:objekt_field, field: field, objekt: objekt, value: random_value)

      PagePartialField.create_page_partial_fields!(page_partial)
      page_partial_field = page_partial.page_partial_fields.first
      page_partial_field.update!(value: objekt.id)

      expected_output = [
        {
          char_limit: 255,
          mandatory: true,
          name: "Name",
          object_type: "",
          type: "text",
          value: random_value,
        },
      ]
      expect(subject.get_object_fields(page_partial_field)).to eq expected_output
    end

  end

end
