require "rails_helper"

RSpec.describe Store, type: :model do
  it { should validate_presence_of(:name) }
  it { should validate_presence_of(:catalog_url) }

  it "rejects a catalog url that is not http(s)" do
    store = build(:store, catalog_url: "git@example.org:thing.git")
    expect(store).not_to be_valid
    expect(store.errors[:catalog_url]).to include("must be an http(s) URL")
  end

  it "rejects a repository url that is not http(s)" do
    expect(build(:store, store_repository_url: "not a url")).not_to be_valid
  end

  # A store without a repository is a complete store: the client takes identity
  # from the record and everything else from the engine's defaults.
  it "accepts a store with no repository at all" do
    expect(build(:store, store_repository_url: nil)).to be_valid
    expect(build(:store, store_repository_url: "")).to be_valid
  end

  describe ".ordered" do
    it "lists stores by name" do
      later = create(:store, name: "Zed Games")
      first = create(:store, name: "Apex Games")

      expect(Store.ordered).to eq([first, later])
    end
  end

  it "hides soft-deleted stores" do
    store = create(:store)
    store.update!(deleted_at: Time.current)

    expect(Store.all).to be_empty
  end
end
