<commit_before>require 'spec_helper'

def metadata_values
  {
    started_at: DateTime.now,
    finished_at: DateTime.now,
    workflow_version: "1.1",
    user_language: 'en',
    user_agent: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0"
  }
end

def annotation_values
  [ { "task" => "question_key",
       "value" => "question_answer" },
    { "task" => "age",
      "value" => "adult"} ]
end

def setup_create_request(project_id, workflow_id, subject_id)
  request.session = { cellect_hosts: { workflow_id.to_s => "example.com" } }
  params =
    {
      classifications: {
        completed: true,
        metadata: metadata_values,
        annotations: annotation_values,
        links: {
          project: project_id,
          workflow: workflow_id,
          subjects: [subject_id]
        }
      }
    }
  unless gold_standard.nil?
    params[:classifications].merge!(gold_standard: gold_standard)
  end

  post :create, params
end

def create_classification_with_subject
  setup_create_request(project.id, workflow.id, subject.id)
end

describe Api::V1::ClassificationsController, type: :controller do
  let!(:user) { create(:user) }
  let(:gold_standard) { nil }
  let(:classification) { create(:classification, user: user) }
  let(:project) { create(:full_project) }
  let!(:workflow) { project.workflows.first }
  let!(:set_member_subject) { workflow.subject_sets.first.set_member_subjects.first }
  let!(:subject) { set_member_subject.subject }
  let(:created_classification_id) { created_instance_id("classifications") }

  let(:api_resource_name) { "classifications" }
  let(:api_resource_attributes) do
    [ "id", "annotations", "created_at" ]
  end
  let(:api_resource_links) do
    [ "classifications.project",
      "classifications.subjects",
      "classifications.user",
      "classifications.user_group" ]
  end

  let(:scopes) { %w(classification) }
  let(:authorized_user) { user }
  let(:resource_class) { Classification }

  context "logged in user" do
    before(:each) do
      default_request user_id: user.id, scopes: scopes
    end

    describe "#index" do
      let!(:classifications) { create_list(:classification, 2, user: user) }
      let!(:private_resource) { create(:classification) }
      let(:n_visible) { 2 }

      it_behaves_like "is indexable"
    end

    describe "#show" do
      let(:resource) { classification }
      it_behaves_like "is showable"
    end

    describe "#create" do
      context "with subject_ids" do
        let(:create_action) { create_classification_with_subject }

        it_behaves_like "a classification create"
        it_behaves_like "a classification lifecycle event"
        it_behaves_like "a gold standard classfication"

        context "when invalid link id strings are used", :focus do

          it "should fail via the schema validator" do
            req_params = [ project.id,
                           "MOCK_WORKFLOW_FOR_CLASSIFIER",
                           "MOCK_SUBJECT_FOR_CLASSIFIER" ]
            setup_create_request(*req_params)
            error = json_response["errors"].first["message"]
            expected = /MOCK_SUBJECT_FOR_CLASSIFIER.*did not match the regex/
            expect(error).to match(expected)
          end
        end
      end
    end
  end

  describe "#update" do
    context "an incomplete classification" do
      let(:resource) { create(:classification, user: authorized_user, completed: false) }
      let(:test_attr) { :completed }
      let(:test_attr_value) { true }
      let(:update_params) do
        {
          classifications: {
            completed: true,
            annotations: [{ "task" => "q-1",
                            "value" => "round" }]<commit_msg>test for specifically formatted link errors in the controller response<commit_after>require 'spec_helper'

def metadata_values
  {
    started_at: DateTime.now,
    finished_at: DateTime.now,
    workflow_version: "1.1",
    user_language: 'en',
    user_agent: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0"
  }
end

def annotation_values
  [ { "task" => "question_key",
       "value" => "question_answer" },
    { "task" => "age",
      "value" => "adult"} ]
end

def setup_create_request(project_id, workflow_id, subject_id)
  request.session = { cellect_hosts: { workflow_id.to_s => "example.com" } }
  params =
    {
      classifications: {
        completed: true,
        metadata: metadata_values,
        annotations: annotation_values,
        links: {
          project: project_id,
          workflow: workflow_id,
          subjects: [subject_id]
        }
      }
    }
  unless gold_standard.nil?
    params[:classifications].merge!(gold_standard: gold_standard)
  end

  post :create, params
end

def create_classification_with_subject
  setup_create_request(project.id, workflow.id, subject.id)
end

describe Api::V1::ClassificationsController, type: :controller do
  let!(:user) { create(:user) }
  let(:gold_standard) { nil }
  let(:classification) { create(:classification, user: user) }
  let(:project) { create(:full_project) }
  let!(:workflow) { project.workflows.first }
  let!(:set_member_subject) { workflow.subject_sets.first.set_member_subjects.first }
  let!(:subject) { set_member_subject.subject }
  let(:created_classification_id) { created_instance_id("classifications") }

  let(:api_resource_name) { "classifications" }
  let(:api_resource_attributes) do
    [ "id", "annotations", "created_at" ]
  end
  let(:api_resource_links) do
    [ "classifications.project",
      "classifications.subjects",
      "classifications.user",
      "classifications.user_group" ]
  end

  let(:scopes) { %w(classification) }
  let(:authorized_user) { user }
  let(:resource_class) { Classification }

  context "logged in user" do
    before(:each) do
      default_request user_id: user.id, scopes: scopes
    end

    describe "#index" do
      let!(:classifications) { create_list(:classification, 2, user: user) }
      let!(:private_resource) { create(:classification) }
      let(:n_visible) { 2 }

      it_behaves_like "is indexable"
    end

    describe "#show" do
      let(:resource) { classification }
      it_behaves_like "is showable"
    end

    describe "#create" do
      context "with subject_ids" do
        let(:create_action) { create_classification_with_subject }

        it_behaves_like "a classification create"
        it_behaves_like "a classification lifecycle event"
        it_behaves_like "a gold standard classfication"

        context "when invalid link id strings are used" do

          it "should fail via the schema validator" do
            req_params = [ project.id,
                           "MOCK_WORKFLOW_FOR_CLASSIFIER",
                           "MOCK_SUBJECT_FOR_CLASSIFIER" ]
            setup_create_request(*req_params)
            error = json_response["errors"].first["message"]
            expected_error = { "links/workflow"   => "did not match the regex '^[0-9]*$'",
                               "links/subjects/0" => "did not match the regex '^[0-9]*$'"}.to_s
            expect(error).to match(expected_error)
          end
        end
      end
    end
  end

  describe "#update" do
    context "an incomplete classification" do
      let(:resource) { create(:classification, user: authorized_user, completed: false) }
      let(:test_attr) { :completed }
      let(:test_attr_value) { true }
      let(:update_params) do
        {
          classifications: {
            completed: true,
            annotations: [{ "task" => "q-1",
                            "value" => "round" }]
{'fraction_non_alphanumeric': 0.11336297071129707, 'fraction_numerical': 0.007975941422594142, 'mean_word_length': 2.9005609382967874, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '2273362', 'n_tokens_mistral': 2416, 'n_tokens_neox': 2301, 'n_words': 501}