<commit_before>RSpec.describe AdminSet, type: :model do
  let(:gf1) { create(:generic_work, user: user) }
  let(:gf2) { create(:generic_work, user: user) }
  let(:gf3) { create(:generic_work, user: user) }

  let(:user) { create(:user) }

  subject { described_class.new(title: ['Some title']) }

  describe '#active_workflow' do
    it 'leverages Sipity::Workflow.find_active_workflow_for' do
      admin_set = build(:admin_set, id: 1234)
      expect(Sipity::Workflow).to receive(:find_active_workflow_for).with(admin_set_id: admin_set.id).and_return(:workflow)
      expect(admin_set.active_workflow).to eq(:workflow)
    end
  end

  describe '#permission_template' do
    it 'queries for a Hyrax::PermissionTemplate with a matching admin_set_id' do
      admin_set = build(:admin_set, id: '123')
      permission_template = build(:permission_template)
      expect(Hyrax::PermissionTemplate).to receive(:find_by!).with(admin_set_id: '123').and_return(permission_template)
      expect(admin_set.permission_template).to eq(permission_template)
    end
  end

  describe 'factories' do
    it 'will create a permission_template when one is requested' do
      expect { create(:admin_set, with_permission_template: true) }.to change { Hyrax::PermissionTemplate.count }.by(1)
    end

    it 'will not create a permission_template by default' do
      expect { create(:admin_set) }.not_to change { Hyrax::PermissionTemplate.count }
    end

    it 'will create a permission_template with attributes' do
      permission_template = create(:admin_set,
                                   with_permission_template: {
                                     visibility: Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC,
                                     release_date: Hyrax::PermissionTemplate::RELEASE_TEXT_VALUE_FIXED,
                                     release_period: Hyrax::PermissionTemplate::RELEASE_TEXT_VALUE_6_MONTHS
                                   }).permission_template
      expect(permission_template.visibility).to eq(Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC)
      expect(permission_template.release_period).to eq(Hyrax::PermissionTemplate::RELEASE_TEXT_VALUE_6_MONTHS)
      expect(permission_template.release_date).to eq(6.months.from_now.to_date)
    end
  end

  describe '.after_destroy' do
    it 'will destroy the associated permission template' do
      admin_set = create(:admin_set, with_permission_template: true)
      expect { admin_set.destroy }.to change { Hyrax::PermissionTemplate.count }.by(-1)
    end
  end

  describe "#to_solr" do
    let(:admin_set) do
      build(:admin_set, title: ['A good title'],
                        creator: ['<EMAIL>'])
    end
    let(:solr_document) { admin_set.to_solr }

    it "has title and creator information" do
      expect(solr_document).to include 'title_tesim' => ['A good title'],
                                       'title_sim' => ['A good title'],
                                       'creator_ssim' => ['<EMAIL>']
    end
  end

  describe "#members" do
    it "is empty by default" do
      skip 'This test is plagued by this bug https://github.com/samvera/active_fedora/issues/1238'
      expect(subject.members).to be_empty
    end

    context "adding members" do
      context "using assignment" do
        subject { described_class.create!(title: ['Some title'], members: [gf1, gf2]) }

        it "has many files" do
          expect(subject.reload.members).to match_array [gf1, gf2]
        end
      end

      context "using append" do
        before do
          subject.members = [gf1]
          subject.save
        end
        it "allows new files to be added" do
          subject.reload
          subject.members << gf2
          subject.save
          expect(subject.reload.members).to match_array [gf1, gf2]
        end
      end
    end
  end

  it "has a title" do
    subject.title = ["title"]
    subject.save
    expect(subject.reload.title).to eq ["title"]
  end

  it "has a description" do
    subject.title = ["title"]
    subject.description = ["description"]
    subject.save
    expect(subject.reload.description).to eq ["description"]
  end

  describe ".default_set?" do
    context "with default AdminSet ID" do
      it "returns true" do
        expect(AdminSet.default_set?(described_class::DEFAULT_ID)).to be true
      end
    end

    context "with a non-default  ID" do
      it "returns false" do<commit_msg>Aligning spec description with spec body<commit_after>RSpec.describe AdminSet, type: :model do
  let(:gf1) { create(:generic_work, user: user) }
  let(:gf2) { create(:generic_work, user: user) }
  let(:gf3) { create(:generic_work, user: user) }

  let(:user) { create(:user) }

  subject { described_class.new(title: ['Some title']) }

  describe '#active_workflow' do
    it 'leverages Sipity::Workflow.find_active_workflow_for' do
      admin_set = build(:admin_set, id: 1234)
      expect(Sipity::Workflow).to receive(:find_active_workflow_for).with(admin_set_id: admin_set.id).and_return(:workflow)
      expect(admin_set.active_workflow).to eq(:workflow)
    end
  end

  describe '#permission_template' do
    it 'queries for a Hyrax::PermissionTemplate with a matching admin_set_id' do
      admin_set = build(:admin_set, id: '123')
      permission_template = build(:permission_template)
      expect(Hyrax::PermissionTemplate).to receive(:find_by!).with(admin_set_id: '123').and_return(permission_template)
      expect(admin_set.permission_template).to eq(permission_template)
    end
  end

  describe 'factories' do
    it 'will create a permission_template when one is requested' do
      expect { create(:admin_set, with_permission_template: true) }.to change { Hyrax::PermissionTemplate.count }.by(1)
    end

    it 'will not create a permission_template by default' do
      expect { create(:admin_set) }.not_to change { Hyrax::PermissionTemplate.count }
    end

    it 'will create a permission_template with attributes' do
      permission_template = create(:admin_set,
                                   with_permission_template: {
                                     visibility: Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC,
                                     release_date: Hyrax::PermissionTemplate::RELEASE_TEXT_VALUE_FIXED,
                                     release_period: Hyrax::PermissionTemplate::RELEASE_TEXT_VALUE_6_MONTHS
                                   }).permission_template
      expect(permission_template.visibility).to eq(Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC)
      expect(permission_template.release_period).to eq(Hyrax::PermissionTemplate::RELEASE_TEXT_VALUE_6_MONTHS)
      expect(permission_template.release_date).to eq(6.months.from_now.to_date)
    end
  end

  describe '.after_destroy' do
    it 'will destroy the associated permission template' do
      admin_set = create(:admin_set, with_permission_template: true)
      expect { admin_set.destroy }.to change { Hyrax::PermissionTemplate.count }.by(-1)
    end
  end

  describe "#to_solr" do
    let(:admin_set) do
      build(:admin_set, title: ['A good title'],
                        creator: ['<EMAIL>'])
    end
    let(:solr_document) { admin_set.to_solr }

    it "has title and creator information" do
      expect(solr_document).to include 'title_tesim' => ['A good title'],
                                       'title_sim' => ['A good title'],
                                       'creator_ssim' => ['<EMAIL>']
    end
  end

  describe "#members" do
    it "is empty by default" do
      skip 'This test is plagued by this bug https://github.com/samvera/active_fedora/issues/1238'
      expect(subject.members).to be_empty
    end

    context "adding members" do
      context "using assignment" do
        subject { described_class.create!(title: ['Some title'], members: [gf1, gf2]) }

        it "has many files" do
          expect(subject.reload.members).to match_array [gf1, gf2]
        end
      end

      context "using append" do
        before do
          subject.members = [gf1]
          subject.save
        end
        it "allows new files to be added" do
          subject.reload
          subject.members << gf2
          subject.save
          expect(subject.reload.members).to match_array [gf1, gf2]
        end
      end
    end
  end

  it "has a title that is an ActiveTriples::Relation" do
    subject.title = ['Hello World']
    expect(subject.title).to eq(['Hello World'])
    expect(subject.title).to be_a(ActiveTriples::Relation)
  end

  it "has a description that is an ActiveTriples::Relation" do
    subject.description = ['My description']
    expect(subject.description).to eq(['My description'])
    expect(subject.description).to be_a(ActiveTriples::Relation)
  end

  describe '#title=' do
    it 'raises an error if set with a String' do
      expect { subject.title = 'New Title' }.to raise_error(ArgumentError)
    end
  end

  describe '#description=' do
    it 'raises an error if set with a String' do
      expect { subject.description = 'New Description' }.to raise_error(ArgumentError)
    end
  end

  describe ".default_set?" do
    context "with default AdminSet ID" do
      it "returns true" do
        expect(AdminSet.default_set?(described_class::DEFAULT_ID)).to be true
      end
    end

    context "with a non-default  ID" do
      it "returns false" do
{'fraction_non_alphanumeric': 0.0999682975800486, 'fraction_numerical': 0.006340483990277925, 'mean_word_length': 2.8284789644012944, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4096345', 'n_tokens_mistral': 2846, 'n_tokens_neox': 2713, 'n_words': 696}