<commit_before># frozen_string_literal: true

require 'rails_helper'

describe Teachers::ClassroomManagerController, type: :controller do
  it { should use_before_action :teacher_or_public_activity_packs }
  it { should use_before_action :authorize_owner! }
  it { should use_before_action :authorize_teacher! }

  describe '#lesson_planner' do
    let!(:teacher) { create(:classrooms_teacher, user: user) }
    let(:user) { create(:teacher, first_name: "test") }

    before do
      allow(controller).to receive(:current_user) { user }
    end

    it 'should assign the tab, grade, students, last_classroom_name and last_lassroom_id' do
      get :lesson_planner, params: { id: teacher.id, tab: "test tab", grade: "test grade" }
      expect(assigns(:tab)).to eq "test tab"
      expect(assigns(:grade)).to eq "test grade"
      expect(assigns(:students)).to eq user.students.any?
      expect(assigns(:last_classroom_id)).to eq user.classrooms_i_teach.last.id
      expect(assigns(:last_classroom_name)).to eq user.classrooms_i_teach.last.name
    end
  end

  describe '#assign as a staff w/o classrooms' do
    let(:user) { create(:staff) }

    before do
      allow(controller).to receive(:current_user) { user }
    end

    it 'should assign the tab, grade, students, last_classroom_name and last_classroom_id' do
      get :assign, params: { tab: "test tab", grade: "test grade" }
      expect(response.status).to eq(200)
    end
  end

  describe '#assign' do
    let!(:teacher) { create(:classrooms_teacher, user: user, role: "owner") }
    let(:user) { create(:teacher, first_name: "test") }

    before do
      allow(controller).to receive(:current_user) { user }
    end

    context 'when current user is staff or has classrooms i teach' do
      context 'when user is staff' do
        before do
          user.role = "staff"
        end

        it 'should assign the tab, grade, students, last_classroom_name and last_classroom_id' do
          get :assign, params: { tab: "test tab", grade: "test grade" }
          expect(assigns(:tab)).to eq "test tab"
          expect(assigns(:grade)).to eq "test grade"
          expect(assigns(:students)).to eq user.students.any?
          expect(assigns(:last_classroom_id)).to eq user.classrooms_i_teach.last.id
          expect(assigns(:last_classroom_name)).to eq user.classrooms_i_teach.last.name
        end
      end

      context 'when user has classrooms i teach' do
        it 'should assign the tab, grade, students, last_classroom_name and last_classroom_id' do
          get :assign, params: { tab: "test tab", grade: "test grade" }
          expect(assigns(:tab)).to eq "test tab"
          expect(assigns(:grade)).to eq "test grade"
          expect(assigns(:students)).to eq user.students.any?
          expect(assigns(:last_classroom_id)).to eq user.classrooms_i_teach.last.id
          expect(assigns(:last_classroom_name)).to eq user.classrooms_i_teach.last.name
        end
      end

      describe 'show lessons banner' do
        it 'should be true if the user has not completed any lessons and does not have the milestone' do
          get :assign
          expect(assigns(:show_lessons_banner)).to eq true
        end

        it 'should be false if the user has already completed a lesson' do
          unit = create(:unit, user: user)
          classroom_unit = create(:classroom_unit, unit: unit, classroom: user.classrooms_i_teach.last)
          unit_activity = create(:unit_activity, :lesson_unit_activity, unit: unit)
          classroom_unit_activity_state = create(:classroom_unit_activity_state, classroom_unit: classroom_unit, unit_activity: unit_activity, completed: true)
          get :assign
          expect(assigns(:show_lessons_banner)).to eq false
        end

        it 'should be false if the user already has the acknowledge lessons banner milestone' do
          milestone = create(:acknowledge_lessons_banner)
          user_milestone = create(:user_milestone, milestone: milestone, user: user)
          get :assign
          expect(assigns(:show_lessons_banner)).to eq false
        end
      end

      describe 'show diagnostic banner' do
        it 'should be true if the user has not assigned any diagnostics and does not have the milestone' do
          get :assign
          expect(assigns(:show_diagnostic_banner)).to eq true
        end

        it 'should be false if the user has already assigned a diagnostic' do
          unit = create(:unit, user: user)
          unit_activity = create(:unit_activity, :diagnostic_unit_activity, unit: unit)
          get :assign
          expect(assigns(:show_diagnostic_banner)).to eq false
        end

        it 'should be false if the user already has the acknowledge diagnostic banner milestone' do
          milestone = create(:acknowledge_diagnostic_banner)
          user_milestone = create(:user_milestone, milestone: milestone, user: user)
          get :assign
          expect(assigns(:show_diagnostic_banner)).to eq false
        end
      end

      describe 'assigned_pre_tests' do
        let!(:starter_post_test) { create(:diagnostic_activity) }
        let!(:intermediate_post_test) { create(:diagnostic_activity) }
        let!(:advanced_post_test) { create(:diagnostic_activity) }
        let!(:starter_pre_test) { create(:diagnostic_activity, id: Activity::STARTER_DIAGNOSTIC_ACTIVITY_ID, follow_up_activity_id: starter_post_test.id) }
        let!(:intermediate_pre_test) { create(:diagnostic_activity, id: Activity::INTERMEDIATE_DIAGNOSTIC_ACTIVITY_ID, follow_up_activity_id: intermediate_post_test.id) }
        let!(:advanced_pre_test) { create(:diagnostic_activity, id: Activity::ADVANCED_DIAGNOSTIC_ACTIVITY_ID, follow_up_activity_id: advanced_post_test.id) }
        let!(:unit) { create(:unit, user: user) }
        let!(:unit_activity) { create(:unit_activity, unit: unit, activity: starter_pre_test) }
        let!(:students_classrooms) { create(:students_classrooms, classroom: user.classrooms_i_teach.last) }
        let!(:classroom_unit) { create(:classroom_unit, unit: unit, classroom: user.classrooms_i_teach.last, assigned_student_ids: [students_classrooms.student.id]) }
        let!(:activity_session) { create(:activity_session, user: students_classrooms.student, activity: starter_pre_test, classroom_unit: classroom_unit) }


        it 'should be a an array with objects containing the activity id, post test id, and assigned classroom ids' do
          get :assign
          expect(assigns(:assigned_pre_tests)).to eq ([
            {
              id: starter_pre_test.id,
              post_test_id: starter_post_test.id,
              assigned_classroom_ids: [user.classrooms_i_teach.last.id],
              all_classrooms: [
                {
                  id: user.classrooms_i_teach.last.id,
                  completed_pre_test_student_ids: [students_classrooms.student.id],
                  completed_post_test_student_ids: []
                }
              ]
            },
            {
              id: intermediate_pre_test.id,
              post_test_id: intermediate_post_test.id,
              assigned_classroom_ids: [],
              all_classrooms: [
                {
                  id: user.classrooms_i_teach.last.id,
                  completed_pre_test_student_ids: [],
                  completed_post_test_student_ids: []
                }
              ]
            },
            {
              id: advanced_pre_test.id,
              post_test_id: advanced_post_test.id,
              assigned_classroom_ids: [],
              all_classrooms: [
                {
                  id: user.classrooms_i_teach.last.id,
                  completed_pre_test_student_ids: [],
                  completed_post_test_student_ids: []
                }
              ]
            }
          ])<commit_msg>Update spec to not expect unsorted data to be in a specific order (#9189)<commit_after># frozen_string_literal: true

require 'rails_helper'

describe Teachers::ClassroomManagerController, type: :controller do
  it { should use_before_action :teacher_or_public_activity_packs }
  it { should use_before_action :authorize_owner! }
  it { should use_before_action :authorize_teacher! }

  describe '#lesson_planner' do
    let!(:teacher) { create(:classrooms_teacher, user: user) }
    let(:user) { create(:teacher, first_name: "test") }

    before do
      allow(controller).to receive(:current_user) { user }
    end

    it 'should assign the tab, grade, students, last_classroom_name and last_lassroom_id' do
      get :lesson_planner, params: { id: teacher.id, tab: "test tab", grade: "test grade" }
      expect(assigns(:tab)).to eq "test tab"
      expect(assigns(:grade)).to eq "test grade"
      expect(assigns(:students)).to eq user.students.any?
      expect(assigns(:last_classroom_id)).to eq user.classrooms_i_teach.last.id
      expect(assigns(:last_classroom_name)).to eq user.classrooms_i_teach.last.name
    end
  end

  describe '#assign as a staff w/o classrooms' do
    let(:user) { create(:staff) }

    before do
      allow(controller).to receive(:current_user) { user }
    end

    it 'should assign the tab, grade, students, last_classroom_name and last_classroom_id' do
      get :assign, params: { tab: "test tab", grade: "test grade" }
      expect(response.status).to eq(200)
    end
  end

  describe '#assign' do
    let!(:teacher) { create(:classrooms_teacher, user: user, role: "owner") }
    let(:user) { create(:teacher, first_name: "test") }

    before do
      allow(controller).to receive(:current_user) { user }
    end

    context 'when current user is staff or has classrooms i teach' do
      context 'when user is staff' do
        before do
          user.role = "staff"
        end

        it 'should assign the tab, grade, students, last_classroom_name and last_classroom_id' do
          get :assign, params: { tab: "test tab", grade: "test grade" }
          expect(assigns(:tab)).to eq "test tab"
          expect(assigns(:grade)).to eq "test grade"
          expect(assigns(:students)).to eq user.students.any?
          expect(assigns(:last_classroom_id)).to eq user.classrooms_i_teach.last.id
          expect(assigns(:last_classroom_name)).to eq user.classrooms_i_teach.last.name
        end
      end

      context 'when user has classrooms i teach' do
        it 'should assign the tab, grade, students, last_classroom_name and last_classroom_id' do
          get :assign, params: { tab: "test tab", grade: "test grade" }
          expect(assigns(:tab)).to eq "test tab"
          expect(assigns(:grade)).to eq "test grade"
          expect(assigns(:students)).to eq user.students.any?
          expect(assigns(:last_classroom_id)).to eq user.classrooms_i_teach.last.id
          expect(assigns(:last_classroom_name)).to eq user.classrooms_i_teach.last.name
        end
      end

      describe 'show lessons banner' do
        it 'should be true if the user has not completed any lessons and does not have the milestone' do
          get :assign
          expect(assigns(:show_lessons_banner)).to eq true
        end

        it 'should be false if the user has already completed a lesson' do
          unit = create(:unit, user: user)
          classroom_unit = create(:classroom_unit, unit: unit, classroom: user.classrooms_i_teach.last)
          unit_activity = create(:unit_activity, :lesson_unit_activity, unit: unit)
          classroom_unit_activity_state = create(:classroom_unit_activity_state, classroom_unit: classroom_unit, unit_activity: unit_activity, completed: true)
          get :assign
          expect(assigns(:show_lessons_banner)).to eq false
        end

        it 'should be false if the user already has the acknowledge lessons banner milestone' do
          milestone = create(:acknowledge_lessons_banner)
          user_milestone = create(:user_milestone, milestone: milestone, user: user)
          get :assign
          expect(assigns(:show_lessons_banner)).to eq false
        end
      end

      describe 'show diagnostic banner' do
        it 'should be true if the user has not assigned any diagnostics and does not have the milestone' do
          get :assign
          expect(assigns(:show_diagnostic_banner)).to eq true
        end

        it 'should be false if the user has already assigned a diagnostic' do
          unit = create(:unit, user: user)
          unit_activity = create(:unit_activity, :diagnostic_unit_activity, unit: unit)
          get :assign
          expect(assigns(:show_diagnostic_banner)).to eq false
        end

        it 'should be false if the user already has the acknowledge diagnostic banner milestone' do
          milestone = create(:acknowledge_diagnostic_banner)
          user_milestone = create(:user_milestone, milestone: milestone, user: user)
          get :assign
          expect(assigns(:show_diagnostic_banner)).to eq false
        end
      end

      describe 'assigned_pre_tests' do
        let!(:starter_post_test) { create(:diagnostic_activity) }
        let!(:intermediate_post_test) { create(:diagnostic_activity) }
        let!(:advanced_post_test) { create(:diagnostic_activity) }
        let!(:starter_pre_test) { create(:diagnostic_activity, id: Activity::STARTER_DIAGNOSTIC_ACTIVITY_ID, follow_up_activity_id: starter_post_test.id) }
        let!(:intermediate_pre_test) { create(:diagnostic_activity, id: Activity::INTERMEDIATE_DIAGNOSTIC_ACTIVITY_ID, follow_up_activity_id: intermediate_post_test.id) }
        let!(:advanced_pre_test) { create(:diagnostic_activity, id: Activity::ADVANCED_DIAGNOSTIC_ACTIVITY_ID, follow_up_activity_id: advanced_post_test.id) }
        let!(:unit) { create(:unit, user: user) }
        let!(:unit_activity) { create(:unit_activity, unit: unit, activity: starter_pre_test) }
        let!(:students_classrooms) { create(:students_classrooms, classroom: user.classrooms_i_teach.last) }
        let!(:classroom_unit) { create(:classroom_unit, unit: unit, classroom: user.classrooms_i_teach.last, assigned_student_ids: [students_classrooms.student.id]) }
        let!(:activity_session) { create(:activity_session, user: students_classrooms.student, activity: starter_pre_test, classroom_unit: classroom_unit) }


        it 'should be a an array with objects containing the activity id, post test id, and assigned classroom ids' do
          get :assign
          expect(assigns(:assigned_pre_tests)).to include({
            id: starter_pre_test.id,
            post_test_id: starter_post_test.id,
            assigned_classroom_ids: [user.classrooms_i_teach.last.id],
            all_classrooms: [
              {
                id: user.classrooms_i_teach.last.id,
                completed_pre_test_student_ids: [students_classrooms.student.id],
                completed_post_test_student_ids: []
              }
            ]
          },
          {
            id: intermediate_pre_test.id,
            post_test_id: intermediate_post_test.id,
            assigned_classroom_ids: [],
            all_classrooms: [
              {
                id: user.classrooms_i_teach.last.id,
                completed_pre_test_student_ids: [],
                completed_post_test_student_ids: []
              }
            ]
          },
          {
            id: advanced_pre_test.id,
            post_test_id: advanced_post_test.id,
            assigned_classroom_ids: [],
            all_classrooms: [
              {
                id: user.classrooms_i_teach.last.id,
                completed_pre_test_student_ids: [],
                completed_post_test_student_ids: []
              }
            ]
          })
{'fraction_non_alphanumeric': 0.0921455938697318, 'fraction_numerical': 0.0006385696040868455, 'mean_word_length': 2.787424425634825, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4267391', 'n_tokens_mistral': 4643, 'n_tokens_neox': 4518, 'n_words': 1115}