require "rails_helper"

RSpec.describe Api::StoresController, type: :request do
  describe "GET /api/stores" do
    it "lists stores without a login, in name order" do
      create(:store, name: "Zed Games", catalog_url: "https://zed.example")
      create(:store, name: "Apex Games", catalog_url: "https://apex.example")

      get "/api/stores"

      expect(response).to have_http_status(:ok)
      json = JSON.parse(response.body)
      expect(json.map { |s| s["name"] }).to eq(["Apex Games", "Zed Games"])
    end

    it "answers with the fields a client needs, camelCased" do
      create(:store)

      get "/api/stores"

      store = JSON.parse(response.body).first
      expect(store).to include("name", "catalogUrl", "storeRepositoryUrl")
      expect(store["catalogUrl"]).to eq("https://teletypegames.org")
      expect(store["storeRepositoryUrl"]).to end_with("/stores/ttg-desktop-store")
    end

    it "answers null for a store with no repository" do
      create(:store, store_repository_url: nil)

      get "/api/stores"

      expect(JSON.parse(response.body).first["storeRepositoryUrl"]).to be_nil
    end

    it "leaves out soft-deleted stores" do
      create(:store, name: "Gone").update!(deleted_at: Time.current)

      get "/api/stores"

      expect(JSON.parse(response.body)).to be_empty
    end
  end
end
