              :Fees "None currently"
              :Format "NetCDF-3"}]})
 
-(deftest reindex-suggestions-test
+(defn autocomplete-reindex-fixture
+  [f]
   (let [coll1 (d/ingest "PROV1"
                         (dc/collection
-                            {:DataCenters [(data-umm-spec/data-center {:Roles ["ARCHIVER"] :ShortName "DOI/USGS/CMG/WHSC"})]
-                             :ArchiveAndDistributionInformation gdf1
-                             :SpatialKeywords ["DC" "Miami"]
-                             :ProcessingLevelId (:ProcessingLevelId (fu/processing-level-id "PL1"))
-                             :Projects [(:Projects (fu/projects "proj1" "PROJ2"))]
-                             :Platforms [(:Platforms (fu/platforms fu/FROM_KMS 2 2 1))]
-                             :ScienceKeywords [(:ScienceKeywords (fu/science-keywords sk1 sk2))]}))
+                         {:DataCenters [(data-umm-spec/data-center {:Roles ["ARCHIVER"] :ShortName "DOI/USGS/CMG/WHSC"})]
+                          :ArchiveAndDistributionInformation gdf1
+                          :SpatialKeywords ["DC" "Miami"]
+                          :ProcessingLevelId (:ProcessingLevelId (fu/processing-level-id "PL1"))
+                          :Projects [(:Projects (fu/projects "proj1" "PROJ2"))]
+                          :Platforms [(:Platforms (fu/platforms fu/FROM_KMS 2 2 1))]
+                          :ScienceKeywords [(:ScienceKeywords (fu/science-keywords sk1 sk2))]}))
 
         coll2 (fu/make-coll 2 "PROV1"
                             (fu/science-keywords sk1 sk3)

                            {:DataCenters [(data-umm-spec/data-center {:Roles ["ARCHIVER"] :ShortName "DOI/USGS/CMG/WHSC"})]
                             :ScienceKeywords [(:ScienceKeywords (fu/science-keywords sk1 sk2))]})
        coll3 (d/ingest-concept-with-metadata-file "CMR-6287/C1000000029-EDF_OPS.xml"
                                                         {:provider-id "PROV1"
                                                          :concept-type :collection
                                                          :format-key :echo10})
        _ (index/wait-until-indexed)]

    (index/reindex-suggestions)
    (index/wait-until-indexed)
    (testing "Ensure that response is in proper format and results are correct"
      (compare-autocomplete-results
        (get-in (search/get-autocomplete-json "q=level2") [:feed :entry])
        [{:score 0.25604635 :type "organization" :value "Langley DAAC User Services" :fields "Langley DAAC User Services"}
         {:score 0.041718055 :type "instrument" :value "lVIs" :fields "lVIs"}]))
    (testing "Ensure science keywords are being indexed properly"
        (compare-autocomplete-results
         (get-in (search/get-autocomplete-json "q=solar") [:feed :entry])
         [{:score 3.4995544, :type "science_keywords", :value "Solar Irradiance", :fields "Sun-Earth Interactions:Solar Activity:Solar Irradiance"}
          {:score 1.9641972, :type "science_keywords", :value "Solar Irradiance", :fields "Atmosphere:Atmospheric Radiation:Solar Irradiance"}
          {:score 0.20964509, :type "organization", :value "ACRIM SCF", :fields "ACRIM SCF"}
          {:score 0.16771607, :type "organization", :value "Langley DAAC User Services", :fields "Langley DAAC User Services"}])
        (compare-autocomplete-results
         (get-in (search/get-autocomplete-json "q=solar irradiation") [:feed :entry])
         [{:score 5.0869484, :type "science_keywords", :value "Solar Irradiance", :fields "Sun-Earth Interactions:Solar Activity:Solar Irradiance"}
          {:score 2.7943783, :type "science_keywords", :value "Solar Irradiance", :fields "Atmosphere:Atmospheric Radiation:Solar Irradiance"}
          {:score 0.061936468, :type "organization", :value "ACRIM SCF", :fields "ACRIM SCF"}
          {:score 0.049549174, :type "organization", :value "Langley DAAC User Services", :fields "Langley DAAC User Services"}]))))

   [cmr.system-int-test.utils.ingest-util :as ingest]
   [cmr.system-int-test.utils.search-util :as search]))


(defn compare-autocomplete-results
  "Compare expected to actual response for the following:
  - Items are ordered by score in descending order
  - Ensure that the other fields match"
  [actual expected]
  (let [scores (map :score actual)
        actual-scores-descending? (->> actual
                                       (map :score)
                                       (apply >=))
                                        ; we don't need to actually compare scores
        expected (map #(dissoc % :score) expected)
        actual (map #(dissoc % :score) actual)]
    (is (true? actual-scores-descending?))
    (is (= expected actual))))

(def sk1 (umm-spec-common/science-keyword {:Category "Earth science"
                                           :Topic "Topic1"
