import path from 'path';
import {childProcess} from 'node-promise-es6';
import * as fse from 'fs-extra-promise-es6';

async function updateJson(filePath, overrides) {
  const absolutePath = path.resolve(filePath);
  const packageJson = await fse.readJson(absolutePath);
  await fse.writeJson(absolutePath, Object.assign(packageJson, overrides), {
    spaces: 2
  });
}

describe('jspm-node', () => {
  describe('for a new project', () => {
    beforeEach(async () => {
      await fse.mkdirs('project');
      await childProcess.exec('npm init -y', {cwd: 'project'});
      await updateJson('project/package.json', {
        scripts: {
          prepublish: 'babel-node ../src/bin/jspm-node.js'
        }
      });
    });

    it('does not break npm install', async () => {
      let successful;
      try {
        await childProcess.exec('npm install', {cwd: 'project'});
        successful = true;
      } catch (error) {
        successful = false;
      } finally {
        expect(successful).toBe(true);
      }
    }, 60000);

    describe('installing a package', () => {
      beforeEach(async () => {
        await updateJson('project/package.json', {
          dependencies: {
            lodash: '*'
          }
        });
        await childProcess.exec('npm install', {cwd: 'project'});
      }, 60000);

      it('installs both the npm and the jspm version of the package', async () => {
        expect(await fse.readJson(path.resolve('project/node_modules/lodash/package.json')))
          .toEqual(jasmine.objectContaining({name: 'lodash'}));

        const {stdout: version} = await childProcess.exec('npm show --json lodash version');
        expect(await fse.readJson(path.resolve(`project/jspm_packages/npm/lodash@${JSON.parse(version)}/package.json`)))
          .toEqual(jasmine.objectContaining({name: 'lodash'}));
      });

      it('does not add a jspm key to the package.json', async () => {
        const packageJson = await fse.readJson(path.resolve('project/package.json'));
        expect(packageJson.jspm).toBeUndefined();
      });

      it('can install again without the jspm key', async () => {
        await childProcess.exec('npm install', {cwd: 'project'});

        expect(await fse.readJson(path.resolve('project/node_modules/lodash/package.json')))
          .toEqual(jasmine.objectContaining({name: 'lodash'}));

        const {stdout: version} = await childProcess.exec('npm show --json lodash version');
        expect(await fse.readJson(path.resolve(`project/jspm_packages/npm/lodash@${JSON.parse(version)}/package.json`)))
          .toEqual(jasmine.objectContaining({name: 'lodash'}));
      }, 60000);
    });

    afterEach(async () => {
      await fse.remove('project');
    });
  });
});

@@ -65,16 +65,53 @@
 e-es6';%0A
+import %7Bfs%7D from 'node-promise-es6';%0A
 import *
@@ -2688,24 +2688,843 @@
 );%0A    %7D);%0A%0A
+    it('can install jspm packages from both GitHub and npm', async () =%3E %7B%0A      await updateJson('project/package.json', %7B%0A        dependencies: %7B%0A          lodash: '*',%0A          d3: '*'%0A        %7D%0A      %7D);%0A      await childProcess.exec('npm install', %7Bcwd: 'project'%7D);%0A%0A      const %7Bstdout: lodashVersion%7D = await childProcess.exec('npm show --json lodash version');%0A      expect(await fse.readJson(path.resolve(%60project/jspm_packages/npm/lodash@$%7BJSON.parse(lodashVersion)%7D/package.json%60)))%0A        .toEqual(jasmine.objectContaining(%7Bname: 'lodash'%7D));%0A%0A      const %7Bstdout: d3Version%7D = await childProcess.exec('npm show --json d3 version');%0A      expect(await fs.readFile(path.resolve(%60project/jspm_packages/github/mbostock/d3@$%7BJSON.parse(d3Version)%7D.js%60)))%0A        .toMatch(/module%5C.exports/);%0A    %7D, 60000);%0A%0A
     afterEac

Add test that ensures usage of the jspm registry