<commit_before>require 'rails_helper'

describe FileTree do
  let(:file_tree) { described_class.new }

  describe '#file_icon' do
    let(:file_icon) { file_tree.send(:file_icon, file) }

    context 'for a media file' do
      context 'for an audio file' do
        let(:file) { FactoryGirl.build(:file, file_type: FactoryGirl.build(:dot_mp3)) }

        it 'is an audio file icon' do
          expect(file_icon).to include('fa-file-audio-o')
        end
      end

      context 'for an image file' do
        let(:file) { FactoryGirl.build(:file, file_type: FactoryGirl.build(:dot_jpg)) }

        it 'is an image file icon' do
          expect(file_icon).to include('fa-file-image-o')
        end
      end

      context 'for a video file' do
        let(:file) { FactoryGirl.build(:file, file_type: FactoryGirl.build(:dot_mp4)) }

        it 'is a video file icon' do
          expect(file_icon).to include('fa-file-video-o')
        end
      end
    end

    context 'for other files' do
      context 'for a read-only file' do
        let(:file) { FactoryGirl.build(:file, read_only: true) }

        it 'is a lock icon' do
          expect(file_icon).to include('fa-lock')
        end
      end

      context 'for an executable file' do
        let(:file) { FactoryGirl.build(:file, file_type: FactoryGirl.build(:dot_py)) }

        it 'is a code file icon' do
          expect(file_icon).to include('fa-file-code-o')
        end
      end

      context 'for a renderable file' do
        let(:file) { FactoryGirl.build(:file, file_type: FactoryGirl.build(:dot_svg)) }

        it 'is a text file icon' do
          expect(file_icon).to include('fa-file-text-o')
        end
      end

      context 'for all other files' do
        let(:file) { FactoryGirl.build(:file, file_type: FactoryGirl.build(:dot_md)) }

        it 'is a generic file icon' do
          expect(file_icon).to include('fa-file-o')
        end
      end
    end
  end

  describe '#folder_icon' do
    it 'is a folder icon' do
      expect(file_tree.send(:folder_icon)).to include('fa-folder-o')
    end
  end

  describe '#node_icon' do
    let(:node_icon) { file_tree.send(:node_icon, node) }
    let(:root) { Tree::TreeNode.new('') }

    context 'for the root node' do
      let(:node) { root }

      it 'is a folder icon' do
        expect(node_icon).to eq(file_tree.send(:folder_icon))
      end
    end

    context 'for leaf nodes' do
      let(:node) { root.add(Tree::TreeNode.new('')) }

      it 'is a file icon' do
        expect(file_tree).to receive(:file_icon)
        node_icon
      end
    end

    context 'for intermediary nodes' do
      let(:node) do
        root.add(Tree::TreeNode.new('').tap { |node| node.add(Tree::TreeNode.new('')) })
      end

      it 'is a folder icon' do<commit_msg>added tests<commit_after>require 'rails_helper'

describe FileTree do
  let(:file_tree) { described_class.new }

  describe '#file_icon' do
    let(:file_icon) { file_tree.send(:file_icon, file) }

    context 'for a media file' do
      context 'for an audio file' do
        let(:file) { FactoryGirl.build(:file, file_type: FactoryGirl.build(:dot_mp3)) }

        it 'is an audio file icon' do
          expect(file_icon).to include('fa-file-audio-o')
        end
      end

      context 'for an image file' do
        let(:file) { FactoryGirl.build(:file, file_type: FactoryGirl.build(:dot_jpg)) }

        it 'is an image file icon' do
          expect(file_icon).to include('fa-file-image-o')
        end
      end

      context 'for a video file' do
        let(:file) { FactoryGirl.build(:file, file_type: FactoryGirl.build(:dot_mp4)) }

        it 'is a video file icon' do
          expect(file_icon).to include('fa-file-video-o')
        end
      end
    end

    context 'for other files' do
      context 'for a read-only file' do
        let(:file) { FactoryGirl.build(:file, read_only: true) }

        it 'is a lock icon' do
          expect(file_icon).to include('fa-lock')
        end
      end

      context 'for an executable file' do
        let(:file) { FactoryGirl.build(:file, file_type: FactoryGirl.build(:dot_py)) }

        it 'is a code file icon' do
          expect(file_icon).to include('fa-file-code-o')
        end
      end

      context 'for a renderable file' do
        let(:file) { FactoryGirl.build(:file, file_type: FactoryGirl.build(:dot_svg)) }

        it 'is a text file icon' do
          expect(file_icon).to include('fa-file-text-o')
        end
      end

      context 'for all other files' do
        let(:file) { FactoryGirl.build(:file, file_type: FactoryGirl.build(:dot_md)) }

        it 'is a generic file icon' do
          expect(file_icon).to include('fa-file-o')
        end
      end
    end
  end

  describe '#folder_icon' do
    it 'is a folder icon' do
      expect(file_tree.send(:folder_icon)).to include('fa-folder-o')
    end
  end

  describe '#map_to_js_tree' do
    let(:file) { FactoryGirl.build(:file) }
    let(:js_tree) { file_tree.send(:map_to_js_tree, node) }
    let!(:leaf) { root.add(Tree::TreeNode.new('', file)) }
    let(:root) { Tree::TreeNode.new('', file) }

    context 'for a leaf node' do
      let(:node) { leaf }

      it 'produces the required attributes' do
        expect(js_tree).to include(:icon, :id, :text)
      end

      it 'is enabled' do
        expect(js_tree[:state][:disabled]).to be false
      end

      it 'is closed' do
        expect(js_tree[:state][:opened]).to be false
      end
    end

    context 'for a non-leaf node' do
      let(:node) { root }

      it "traverses the node's children" do
        node.children.each do |child|
          expect(file_tree).to receive(:map_to_js_tree).at_least(:once).with(child).and_call_original
        end
        js_tree
      end

      it 'produces the required attributes' do
        expect(js_tree).to include(:icon, :id, :text)
      end

      it 'is disabled' do
        expect(js_tree[:state][:disabled]).to be true
      end

      it 'is opened' do
        expect(js_tree[:state][:opened]).to be true
      end
    end
  end

  describe '#node_icon' do
    let(:node_icon) { file_tree.send(:node_icon, node) }
    let(:root) { Tree::TreeNode.new('') }

    context 'for the root node' do
      let(:node) { root }

      it 'is a folder icon' do
        expect(node_icon).to eq(file_tree.send(:folder_icon))
      end
    end

    context 'for leaf nodes' do
      let(:node) { root.add(Tree::TreeNode.new('')) }

      it 'is a file icon' do
        expect(file_tree).to receive(:file_icon)
        node_icon
      end
    end

    context 'for intermediary nodes' do
      let(:node) do
        root.add(Tree::TreeNode.new('').tap { |node| node.add(Tree::TreeNode.new('')) })
      end

      it 'is a folder icon' do
{'fraction_non_alphanumeric': 0.1239693757361602, 'fraction_numerical': 0.0005889281507656066, 'mean_word_length': 2.34465780403742, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3920069', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2148, 'n_words': 595}