<commit_before>RootView = require 'root-view'
CommandPanelView = require 'command-panel/lib/command-panel-view'
_ = require 'underscore'

describe "CommandPanel", ->
  [editSession, buffer, commandPanel] = []

  beforeEach ->
    window.rootView = new RootView
    rootView.open('sample.js')
    rootView.enableKeymap()
    editSession = rootView.getActivePaneItem()
    buffer = editSession.buffer
    commandPanelMain = atom.activatePackage('command-panel', immediate: true).mainModule
    commandPanel = commandPanelMain.commandPanelView
    commandPanel.history = []
    commandPanel.historyIndex = 0

  describe "serialization", ->
    it "preserves the command panel's history across reloads", ->
      rootView.attachToDom()
      rootView.trigger 'command-panel:toggle'
      expect(commandPanel.miniEditor.isFocused).toBeTruthy()
      commandPanel.execute('/.')
      expect(commandPanel.history.length).toBe(1)
      expect(commandPanel.history[0]).toBe('/.')
      expect(commandPanel.historyIndex).toBe(1)
      rootView.trigger 'command-panel:toggle'
      expect(commandPanel.miniEditor.isFocused).toBeTruthy()

      atom.deactivatePackage('command-panel')
      atom.activatePackage('command-panel')

      expect(rootView.find('.command-panel')).not.toExist()
      rootView.trigger 'command-panel:toggle'
      expect(rootView.find('.command-panel')).toExist()
      commandPanel = rootView.find('.command-panel').view()
      expect(commandPanel.history.length).toBe(1)
      expect(commandPanel.history[0]).toBe('/.')
      expect(commandPanel.historyIndex).toBe(1)

    it "only retains the configured max serialized history size", ->
      rootView.attachToDom()

      commandPanel.maxSerializedHistorySize = 2
      commandPanel.execute('/test1')
      commandPanel.execute('/test2')
      commandPanel.execute('/test3')
      expect(commandPanel.history.length).toBe(3)
      expect(commandPanel.history[0]).toBe('/test1')
      expect(commandPanel.history[1]).toBe('/test2')
      expect(commandPanel.history[2]).toBe('/test3')
      expect(commandPanel.historyIndex).toBe(3)

      atom.deactivatePackage('command-panel')
      atom.activatePackage('command-panel')

      rootView.trigger 'command-panel:toggle'
      commandPanel = rootView.find('.command-panel').view()

      expect(commandPanel.history.length).toBe(2)
      expect(commandPanel.history[0]).toBe('/test2')
      expect(commandPanel.history[1]).toBe('/test3')
      expect(commandPanel.historyIndex).toBe(2)

  describe "when core:close is triggered on the command panel", ->
    it "detaches the command panel, focuses the RootView and does not bubble the core:close event", ->
      commandPanel.attach('command')
      expect(commandPanel.miniEditor.getText()).toBe 'command'
      rootViewCloseHandler = jasmine.createSpy('rootViewCloseHandler')
      rootView.on 'core:close', rootViewCloseHandler
      spyOn(rootView, 'focus')

      commandPanel.trigger('core:close')

      expect(rootView.focus).toHaveBeenCalled()
      expect(rootViewCloseHandler).not.toHaveBeenCalled()
      expect(commandPanel.hasParent()).toBeFalsy()
      expect(commandPanel.miniEditor.getText()).toBe ''

  describe "when core:cancel is triggered on the command panel's mini editor", ->
    it "detaches the command panel, focuses the RootView and does not bubble the core:cancel event", ->
      commandPanel.attach('command')
      expect(commandPanel.miniEditor.getText()).toBe 'command'
      rootViewCancelHandler = jasmine.createSpy('rootViewCancelHandler')
      rootView.on 'core:cancel', rootViewCancelHandler
      spyOn(rootView, 'focus')

      commandPanel.miniEditor.trigger('core:cancel')

      expect(rootView.focus).toHaveBeenCalled()
      expect(rootViewCancelHandler).not.toHaveBeenCalled()
      expect(commandPanel.hasParent()).toBeFalsy()
      expect(commandPanel.miniEditor.getText()).toBe ''

  describe "when command-panel:toggle is triggered on the root view", ->
    beforeEach ->
      rootView.attachToDom()

    describe "when the command panel is visible", ->
      beforeEach ->
        commandPanel.attach()

      describe "when the mini editor is focused", ->
        it "closes the command panel", ->
          expect(commandPanel.miniEditor.hiddenInput).toMatchSelector ':focus'
          rootView.trigger 'command-panel:toggle'
          expect(commandPanel.hasParent()).toBeFalsy()

      describe "when the mini editor is not focused", ->
        it "focuses the mini editor", ->
          rootView.focus()
          expect(commandPanel.miniEditor.hiddenInput).not.toMatchSelector ':focus'
          rootView.trigger 'command-panel:toggle'
          expect(commandPanel.hasParent()).toBeTruthy()
          expect(commandPanel.miniEditor.hiddenInput).toMatchSelector ':focus'

    describe "when the command panel is not visible", ->
      it "shows and focuses the command panel", ->
        expect(commandPanel.hasParent()).toBeFalsy()
        rootView.trigger 'command-panel:toggle'
        expect(commandPanel.hasParent()).toBeTruthy()

  describe "when command-panel:toggle-preview is triggered on the root view", ->
    beforeEach ->
      rootView.attachToDom()

    describe "when the preview list is/was previously visible", ->
      beforeEach ->
        rootView.trigger 'command-panel:toggle'
        waitsForPromise -> commandPanel.execute('X x/quicksort/')

      describe "when the command panel is visible", ->
        beforeEach ->
          expect(commandPanel.hasParent()).toBeTruthy()

        describe "when the preview list is visible", ->
          beforeEach ->
            expect(commandPanel.previewList).toBeVisible()

          it  "shows the expand and collapse all buttons", ->
            expect(commandPanel.find('.expand-collapse')).toBeVisible()

          describe "when the preview list is focused", ->
            it "hides the command panel", ->
              expect(commandPanel.previewList).toMatchSelector(':focus')
              rootView.trigger 'command-panel:toggle-preview'
              expect(commandPanel.hasParent()).toBeFalsy()

          describe "when the preview list is not focused", ->
            it "focuses the preview list", ->
              commandPanel.miniEditor.focus()
              rootView.trigger 'command-panel:toggle-preview'
              expect(commandPanel.previewList).toMatchSelector(':focus')

        describe "when the preview list is not visible", ->
          beforeEach ->
            commandPanel.miniEditor.focus()
            rootView.trigger 'command-panel:toggle'
            rootView.trigger 'command-panel:toggle'
            expect(commandPanel.hasParent()).toBeTruthy()
            expect(commandPanel.previewList).toBeHidden()

          it "shows and focuses the preview list", ->
            rootView.trigger 'command-panel:toggle-preview'
            expect(commandPanel.previewList).toBeVisible()
            expect(commandPanel.previewList).toMatchSelector(':focus')

      describe "when the command panel is not visible", ->
        it "shows the command panel and the preview list, and focuses the preview list", ->
          commandPanel.miniEditor.focus()<commit_msg>Verify the buttons individually in the spec<commit_after>RootView = require 'root-view'
CommandPanelView = require 'command-panel/lib/command-panel-view'
_ = require 'underscore'

describe "CommandPanel", ->
  [editSession, buffer, commandPanel] = []

  beforeEach ->
    window.rootView = new RootView
    rootView.open('sample.js')
    rootView.enableKeymap()
    editSession = rootView.getActivePaneItem()
    buffer = editSession.buffer
    commandPanelMain = atom.activatePackage('command-panel', immediate: true).mainModule
    commandPanel = commandPanelMain.commandPanelView
    commandPanel.history = []
    commandPanel.historyIndex = 0

  describe "serialization", ->
    it "preserves the command panel's history across reloads", ->
      rootView.attachToDom()
      rootView.trigger 'command-panel:toggle'
      expect(commandPanel.miniEditor.isFocused).toBeTruthy()
      commandPanel.execute('/.')
      expect(commandPanel.history.length).toBe(1)
      expect(commandPanel.history[0]).toBe('/.')
      expect(commandPanel.historyIndex).toBe(1)
      rootView.trigger 'command-panel:toggle'
      expect(commandPanel.miniEditor.isFocused).toBeTruthy()

      atom.deactivatePackage('command-panel')
      atom.activatePackage('command-panel')

      expect(rootView.find('.command-panel')).not.toExist()
      rootView.trigger 'command-panel:toggle'
      expect(rootView.find('.command-panel')).toExist()
      commandPanel = rootView.find('.command-panel').view()
      expect(commandPanel.history.length).toBe(1)
      expect(commandPanel.history[0]).toBe('/.')
      expect(commandPanel.historyIndex).toBe(1)

    it "only retains the configured max serialized history size", ->
      rootView.attachToDom()

      commandPanel.maxSerializedHistorySize = 2
      commandPanel.execute('/test1')
      commandPanel.execute('/test2')
      commandPanel.execute('/test3')
      expect(commandPanel.history.length).toBe(3)
      expect(commandPanel.history[0]).toBe('/test1')
      expect(commandPanel.history[1]).toBe('/test2')
      expect(commandPanel.history[2]).toBe('/test3')
      expect(commandPanel.historyIndex).toBe(3)

      atom.deactivatePackage('command-panel')
      atom.activatePackage('command-panel')

      rootView.trigger 'command-panel:toggle'
      commandPanel = rootView.find('.command-panel').view()

      expect(commandPanel.history.length).toBe(2)
      expect(commandPanel.history[0]).toBe('/test2')
      expect(commandPanel.history[1]).toBe('/test3')
      expect(commandPanel.historyIndex).toBe(2)

  describe "when core:close is triggered on the command panel", ->
    it "detaches the command panel, focuses the RootView and does not bubble the core:close event", ->
      commandPanel.attach('command')
      expect(commandPanel.miniEditor.getText()).toBe 'command'
      rootViewCloseHandler = jasmine.createSpy('rootViewCloseHandler')
      rootView.on 'core:close', rootViewCloseHandler
      spyOn(rootView, 'focus')

      commandPanel.trigger('core:close')

      expect(rootView.focus).toHaveBeenCalled()
      expect(rootViewCloseHandler).not.toHaveBeenCalled()
      expect(commandPanel.hasParent()).toBeFalsy()
      expect(commandPanel.miniEditor.getText()).toBe ''

  describe "when core:cancel is triggered on the command panel's mini editor", ->
    it "detaches the command panel, focuses the RootView and does not bubble the core:cancel event", ->
      commandPanel.attach('command')
      expect(commandPanel.miniEditor.getText()).toBe 'command'
      rootViewCancelHandler = jasmine.createSpy('rootViewCancelHandler')
      rootView.on 'core:cancel', rootViewCancelHandler
      spyOn(rootView, 'focus')

      commandPanel.miniEditor.trigger('core:cancel')

      expect(rootView.focus).toHaveBeenCalled()
      expect(rootViewCancelHandler).not.toHaveBeenCalled()
      expect(commandPanel.hasParent()).toBeFalsy()
      expect(commandPanel.miniEditor.getText()).toBe ''

  describe "when command-panel:toggle is triggered on the root view", ->
    beforeEach ->
      rootView.attachToDom()

    describe "when the command panel is visible", ->
      beforeEach ->
        commandPanel.attach()

      describe "when the mini editor is focused", ->
        it "closes the command panel", ->
          expect(commandPanel.miniEditor.hiddenInput).toMatchSelector ':focus'
          rootView.trigger 'command-panel:toggle'
          expect(commandPanel.hasParent()).toBeFalsy()

      describe "when the mini editor is not focused", ->
        it "focuses the mini editor", ->
          rootView.focus()
          expect(commandPanel.miniEditor.hiddenInput).not.toMatchSelector ':focus'
          rootView.trigger 'command-panel:toggle'
          expect(commandPanel.hasParent()).toBeTruthy()
          expect(commandPanel.miniEditor.hiddenInput).toMatchSelector ':focus'

    describe "when the command panel is not visible", ->
      it "shows and focuses the command panel", ->
        expect(commandPanel.hasParent()).toBeFalsy()
        rootView.trigger 'command-panel:toggle'
        expect(commandPanel.hasParent()).toBeTruthy()

  describe "when command-panel:toggle-preview is triggered on the root view", ->
    beforeEach ->
      rootView.attachToDom()

    describe "when the preview list is/was previously visible", ->
      beforeEach ->
        rootView.trigger 'command-panel:toggle'
        waitsForPromise -> commandPanel.execute('X x/quicksort/')

      describe "when the command panel is visible", ->
        beforeEach ->
          expect(commandPanel.hasParent()).toBeTruthy()

        describe "when the preview list is visible", ->
          beforeEach ->
            expect(commandPanel.previewList).toBeVisible()

          it  "shows the expand and collapse all buttons", ->
            expect(commandPanel.collapseAll).toBeVisible()
            expect(commandPanel.expandAll).toBeVisible()

          describe "when the preview list is focused", ->
            it "hides the command panel", ->
              expect(commandPanel.previewList).toMatchSelector(':focus')
              rootView.trigger 'command-panel:toggle-preview'
              expect(commandPanel.hasParent()).toBeFalsy()

          describe "when the preview list is not focused", ->
            it "focuses the preview list", ->
              commandPanel.miniEditor.focus()
              rootView.trigger 'command-panel:toggle-preview'
              expect(commandPanel.previewList).toMatchSelector(':focus')

        describe "when the preview list is not visible", ->
          beforeEach ->
            commandPanel.miniEditor.focus()
            rootView.trigger 'command-panel:toggle'
            rootView.trigger 'command-panel:toggle'
            expect(commandPanel.hasParent()).toBeTruthy()
            expect(commandPanel.previewList).toBeHidden()

          it "shows and focuses the preview list", ->
            rootView.trigger 'command-panel:toggle-preview'
            expect(commandPanel.previewList).toBeVisible()
            expect(commandPanel.previewList).toMatchSelector(':focus')

      describe "when the command panel is not visible", ->
        it "shows the command panel and the preview list, and focuses the preview list", ->
          commandPanel.miniEditor.focus()
{'fraction_non_alphanumeric': 0.11825726141078838, 'fraction_numerical': 0.003457814661134163, 'mean_word_length': 3.235793790275337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 79, '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': '3519459', 'n_tokens_mistral': 3869, 'n_tokens_neox': 3776, 'n_words': 778}