<commit_before>require 'spec_helper'

describe DeploymentTarget do
  let(:target) { deployment_targets(:target1) }

  describe "#metadata" do
    let(:metadata) { deployment_target_metadata(:metadata1) }
    subject { target.metadata }
    before do
      target.metadata = metadata
      target.save!
      target.reload
    end

    it { should eq(metadata) }

    describe "destruction" do
      before { target.destroy }

      it "does not leave orphaned metadata" do
        expect(DeploymentTargetMetadata.count).to be_zero
      end
    end
  end

  describe '#refresh_metadata' do
    let(:deployment_target) { deployment_targets(:target1) }
    before do
      stub_agent_metadata_request(
        deployment_target,
        metadata: {
          agent: { version: "1" },
          adapter: { version: "2", type: "Test Type" }
        }
      )
    end
    subject(:refresh_metadata) { deployment_target.refresh_metadata }

    it "persists a single DeploymentTargetMetadata" do
      expect { refresh_metadata }.to change { DeploymentTargetMetadata.count }.by(1)
    end

    describe "the persisted metadata" do
      subject { DeploymentTargetMetadata.last }

      context "when metadata does not already exist for the target" do
        before { refresh_metadata }

        its(:agent_version) { should eq("1") }
        its(:adapter_version) { should eq("2") }
        its(:adapter_type) { should eq("Test Type") }
      end

      context "when metadata already exists for the target" do
        let(:deployment_target) { deployment_targets(:target_with_metadata) }

        it "overwrites the existing metadata" do
          expect { refresh_metadata }.to_not change { DeploymentTargetMetadata.count }<commit_msg>Use mocking-style test for DeploymentTarget#refresh_metadata.<commit_after>require 'spec_helper'

describe DeploymentTarget do
  let(:target) { deployment_targets(:target1) }

  describe "#metadata" do
    let(:metadata) { deployment_target_metadata(:metadata1) }
    subject { target.metadata }
    before do
      target.metadata = metadata
      target.save!
      target.reload
    end

    it { should eq(metadata) }

    describe "destruction" do
      before { target.destroy }

      it "does not leave orphaned metadata" do
        expect(DeploymentTargetMetadata.count).to be_zero
      end
    end
  end

  describe '#refresh_metadata' do
    let(:deployment_target) { deployment_targets(:target1) }
    let(:metadata_service) { double(find: metadata) }
    let(:metadata) do
      RemoteAgentMetadata.new(
        agent: { "version" => "1" },
        adapter: { "version" => "2", "type" => "Test Type" }
      )
    end
    before do
      deployment_target.
        stub(:new_agent_service).
        with(AgentMetadataService).
        and_return(metadata_service)
    end
    subject(:refresh_metadata) { deployment_target.refresh_metadata }

    it "persists a single DeploymentTargetMetadata" do
      expect { refresh_metadata }.to change { DeploymentTargetMetadata.count }.by(1)
    end

    describe "the persisted metadata" do
      subject { DeploymentTargetMetadata.last }

      context "when metadata does not already exist for the target" do
        before { refresh_metadata }

        its(:agent_version) { should eq("1") }
        its(:adapter_version) { should eq("2") }
        its(:adapter_type) { should eq("Test Type") }
      end

      context "when metadata already exists for the target" do
        let(:deployment_target) { deployment_targets(:target_with_metadata) }

        it "overwrites the existing metadata" do
          expect { refresh_metadata }.to_not change { DeploymentTargetMetadata.count }
{'fraction_non_alphanumeric': 0.09093384154641981, 'fraction_numerical': 0.004356112169888375, 'mean_word_length': 2.9676025917926565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1076842', 'n_tokens_mistral': 1020, 'n_tokens_neox': 988, 'n_words': 270}