 PackageConfigPanel = require 'package-config-panel'
 packageManager = require 'package-manager'
+_ = require 'underscore'
 
 describe "PackageConfigPanel", ->
   [panel, configObserver] = []
 
   beforeEach ->
-    packages = [
+    installedPackages = [
       {
         name: 'p1'
         version: '3.2.1'
-        homepage: 'http://p1.io'
       }
       {
         name: 'p2'
         version: '1.2.3'
-        repository: url: 'http://github.com/atom/p2.git'
-        bugs: url: 'http://github.com/atom/p2/issues'
       }
       {
         name: 'p3'

      panel.attachToDom()

      expect(panel.available.packagesArea.children('.panel').length).toBe 3
      p1View = panel.available.packagesArea.children('.panel:eq(0)').view()
      p2View = panel.available.packagesArea.children('.panel:eq(1)').view()
      p3View = panel.available.packagesArea.children('.panel:eq(2)').view()

      expect(p1View.name.text()).toBe 'p1'
      expect(p2View.name.text()).toBe 'p2'
      expect(p3View.name.text()).toBe 'p3'

      expect(p1View.version.text()).toBe '3.2.1'
      expect(p2View.version.text()).toBe '1.2.3'
      expect(p3View.version.text()).toBe '5.8.5'

      p1View.dropdownButton.click()
      expect(p1View.homepage).toBeVisible()
      expect(p1View.homepage.find('a').attr('href')).toBe 'http://p1.io'
      expect(p1View.issues).toBeHidden()

      p2View.dropdownButton.click()
      expect(p2View.homepage).toBeVisible()
      expect(p2View.homepage.find('a').attr('href')).toBe 'http://github.com/atom/p2'
      expect(p2View.issues).toBeVisible()
      expect(p2View.issues.find('a').attr('href')).toBe 'http://github.com/atom/p2/issues'

      p3View.dropdownButton.click()
      expect(p3View.homepage).toBeHidden()
      expect(p3View.issues).toBeHidden()

      }
    ]

    availablePackages = [
      {
        name: 'p4'
        version: '3.2.1'
        homepage: 'http://p4.io'
      }
      {
        name: 'p5'
        version: '1.2.3'
        repository: url: 'http://github.com/atom/p5.git'
        bugs: url: 'http://github.com/atom/p5/issues'
      }
      {
        name: 'p6'
        version: '5.8.5'
      }
    ]

    spyOn(packageManager, 'getAvailable').andCallFake (callback) ->
      callback(null, availablePackages)
    spyOn(packageManager, 'uninstall').andCallFake (pack, callback) ->
      _.remove(installedPackages, pack)
      callback()
    spyOn(packageManager, 'install').andCallFake (pack, callback) ->
      installedPackages.push(pack)
      callback()

    spyOn(atom, 'getAvailablePackageMetadata').andReturn(installedPackages)
    spyOn(atom, 'resolvePackagePath').andCallFake (name) ->
      if _.contains(_.pluck(installedPackages, 'name'), name)
        "/tmp/atom-packages/#{name}"

    configObserver = jasmine.createSpy("configObserver")
    observeSubscription = config.observe('core.disabledPackages', configObserver)
