# typed: false
# frozen_string_literal: true

RSpec.describe HasAuxTable::AuxTableConfig do
  # const :aux_table_name, Symbol
  # const :aux_association_name, Symbol
  # const :main_association_name, Symbol
  # const :main_class, T.class_of(ActiveRecord::Base)
  # const :aux_class, T.class_of(ActiveRecord::Base)
  # const :foreign_key, KeyType
  # const :primary_key, KeyType

  it "identifies columns on the aux table" do
    driver_aux_config = Driver.aux_table_config(:driver)
    expect(driver_aux_config.aux.column_names).to contain_exactly(
      "license_number",
      "car_id"
    )

    expect(driver_aux_config.main.column_names).to contain_exactly(
      "type",
      "id",
      "name",
      "created_at",
      "updated_at"
    )
  end

  describe "#remap_conditions" do
    it "works with simple conditions" do
      driver_aux_config = Driver.aux_table_config(:driver)
      conditions = { name: "John Doe", car_id: 1 }
      conditions = driver_aux_config.remap_conditions(conditions)
      expect(conditions).to eq({ name: "John Doe", driver_aux: { car_id: 1 } })
    end

    it "partitions columns referring to associations" do
      car = Car.create!(name: "Toyota Prius")
      driver_aux_config = Driver.aux_table_config(:driver)
      conditions = { car: car }
      conditions = driver_aux_config.remap_conditions(conditions)
      expect(conditions).to eq({ driver_aux: { "car_id" => car.id } })
    end
  end
end
