# typed: false
require "rails_helper"

RSpec.describe Domain::User::E621User, type: :model do
  describe "validations" do
    let(:user) { build(:domain_user_e621_user) }

    it "is valid with valid attributes" do
      expect(user).to be_valid
    end

    it "requires e621_id" do
      user.e621_id = nil
      expect(user).not_to be_valid
      expect(user.errors[:e621_id]).to include("can't be blank")
    end

    it "requires name" do
      user.name = nil
      expect(user).not_to be_valid
      expect(user.errors[:name][0]).to include("is too short")
    end

    it "allows a whitespace name" do
      user.name = " "
      expect(user).to be_valid
    end

    it "validates scanned_favs_status inclusion" do
      user.scanned_favs_status = nil
      expect(user).to be_valid # optional field

      user.scanned_favs_status = "ok"
      expect(user).to be_valid

      user.scanned_favs_status = "error"
      expect(user).to be_valid

      expect do user.scanned_favs_status = "invalid" end.to raise_error(
        ArgumentError,
      )
    end
  end

  describe "initialization" do
    let(:user) { described_class.new }

    it "has default values" do
      expect(user.favs_are_hidden).to be_nil
      expect(user.num_other_favs_cached).to be_nil
      expect(user.scanned_favs_status).to be_nil
      expect(user.scanned_favs_at).to be_nil
    end
  end

  describe "attributes" do
    let(:user) { create(:domain_user_e621_user) }

    it "can update favs_are_hidden" do
      user.favs_are_hidden = true
      expect(user.save).to be true
      user.reload
      expect(user.favs_are_hidden).to be true
    end

    it "can update num_other_favs_cached" do
      user.num_other_favs_cached = 100
      expect(user.save).to be true
      user.reload
      expect(user.num_other_favs_cached).to eq(100)
    end

    it "can update scanned_favs_status" do
      user.scanned_favs_status = "error"
      expect(user.save).to be true
      user.reload
      expect(user.scanned_favs_status).to eq("error")
    end

    it "can update scanned_favs_at" do
      time = Time.now
      user.scanned_favs_at = time
      expect(user.save).to be true
      user.reload
      expect(user.scanned_favs_at).to be_within(1.second).of(time)
    end
  end
end
