const expect = require('chai').expect;
const denodeify = require('denodeify');
const request = denodeify(require('request'));
const AddonTestApp = require('ember-cli-addon-tests').AddonTestApp;
const path = require('path');
const fs = require('fs-extra');

const testEmberVersions = ['latest']; // ['beta', 'latest', '3.12', '3.8', '3.4', '2.18'];

testEmberVersions.forEach(version => {
  describe.skip(`basic registration in Ember version "${version}"`, function() {
    this.timeout(10000000);
    let app;

    before(function() {
      app = new AddonTestApp();

      return app.create('dummy', {
        emberVersion: version,
        fixturesPath: 'node-tests/fixtures',
        skipNpm: true
      })
      .then(() => {
        app.editPackageJSON(pkg => {
          pkg['ember-addon'] = pkg['ember-addon'] || {};
          pkg['ember-addon'].paths = pkg['ember-addon'].paths || [];
          pkg['ember-addon'].paths.push('lib/ember-service-worker-test');
        });
        return app.run('npm', 'install');
      }).then(() => {
        // https://github.com/tomdale/ember-cli-addon-tests/issues/176
        let addonPath = path.join(app.path, 'node_modules', 'ember-service-worker');
        fs.removeSync(addonPath);
        fs.ensureSymlinkSync(process.cwd(), addonPath);
      }).then(() => {
        return app.startServer({
          detectServerStart(output) {
            return output.indexOf('Serving on ') > -1;
          }
        });
      });
    });

    after(function() {
      return app.stopServer();
    });

    it('includes registration in script tag by default', () => {
      return request({
        url: 'http://localhost:49741',
        headers: {
          'Accept': 'text/html'
        }
      }).then(response => {
        expect(response.statusCode).to.equal(200);
        expect(response.body).to.contain('<script src="/sw-registration.js"');
      });
    });

    it('produces a sw-registration.js file', () => {
      return request({
        url: 'http://localhost:49741/sw-registration.js'
      }).then(response => {
        expect(response.statusCode).to.equal(200);
        expect(response.body).to.contain('self.hello');
      });
    });

    it('produces a sw.js file', () => {
      return request({
        url: 'http://localhost:49741/sw.js'
      }).then(response => {
        expect(response.statusCode).to.equal(200);
        expect(response.body).to.contain('self.hello');
      });
    });
  });
});

@@ -395,13 +395,8 @@
 ribe
-.skip
 (%60ba
@@ -951,24 +951,86 @@
 ker-test');%0A
+          delete pkg.devDependencies%5B'ember-service-worker'%5D;%0A
         %7D);%0A
@@ -1164,16 +1164,126 @@
 ues/176%0A
+        app.editPackageJSON(pkg =%3E %7B%0A          pkg.devDependencies%5B'ember-service-worker'%5D = '*';%0A        %7D);%0A
         

Update package handling