require 'rails_helper'

RSpec.describe Setting, type: :model do
  it 'accepts the default typed user settings' do
    setting = described_class.new({ user: create(:user) }.merge(described_class.defaults))

    expect(setting).to be_valid
  end

  it 'requires one settings row per user' do
    user = create(:user)
    described_class.create!({ user: }.merge(described_class.defaults))

    duplicate = described_class.new({ user: }.merge(described_class.defaults))

    expect(duplicate).not_to be_valid
    expect(duplicate.errors[:user_id]).to be_present
  end

  it 'validates enum-like settings columns' do
    setting = described_class.new(
        { user: create(:user),
          theme: 'neon',
          auto_fetch_title: 'sometimes',
          auto_fetch_thumbnail: 'sometimes',
          wiki_editor_mode: 'sideways' })

    expect(setting).not_to be_valid
    expect(setting.errors[:theme]).to be_present
    expect(setting.errors[:auto_fetch_title]).to be_present
    expect(setting.errors[:auto_fetch_thumbnail]).to be_present
    expect(setting.errors[:wiki_editor_mode]).to be_present
  end
end
