{"size": 3048, "ext": "js", "max_stars_repo_path": "test/integration/prism-init-spec.js", "max_stars_repo_name": "ybybzj/connect-prism2", "max_stars_repo_stars_event_min_datetime": "2015-01-07T02:56:53.000Z", "max_stars_repo_stars_event_max_datetime": "2020-11-17T15:54:09.000Z", "max_issues_repo_path": "test/integration/prism-init-spec.js", "max_issues_repo_name": "ybybzj/connect-prism2", "max_issues_repo_issues_event_min_datetime": "2015-01-04T18:42:09.000Z", "max_issues_repo_issues_event_max_datetime": "2020-06-15T23:08:44.000Z", "max_forks_repo_path": "test/integration/prism-init-spec.js", "max_forks_repo_name": "ybybzj/connect-prism2", "max_forks_repo_forks_event_min_datetime": "2015-02-16T21:30:22.000Z", "max_forks_repo_forks_event_max_datetime": "2021-08-04T07:00:41.000Z"}
{"max_stars_count": 75, "max_issues_count": 52, "max_forks_count": 41, "avg_line_length": 27.2142857143, "max_line_length": 81, "alphanum_fraction": 0.5688976378}
'use strict';

var _ = require('lodash');
var assert = require('assert');
var fs = require('fs');
var path = require('path');

var prism = require('../../');

describe('prism', function() {
  var manager = prism.manager;

  afterEach(function() {
    manager.reset();
  });

  describe('task initialization', function() {
    it('should have initialized 2 proxies', function() {
      prism.create({
        name: 'proxy1',
        context: '/proxy1',
        host: 'localhost'
      });
      prism.create({
        name: 'proxy2',
        context: '/proxy2',
        host: 'localhost'
      });

      assert.equal(2, manager.prisms().length);
    });

    it('request options should be correctly mapped', function() {
      prism.create({
        name: 'proxyTest',
        mode: 'proxy',
        context: '/proxyRequest',
        host: 'localhost',
        port: 8090
      });

      var proxy = manager.get('/proxyRequest');

      assert.equal(_.isUndefined(proxy), false);
      assert.equal(proxy.config.mode, 'proxy');
      assert.equal(proxy.config.mocksPath, './mocks');
      assert.equal(proxy.config.context, '/proxyRequest');
      assert.equal(proxy.config.host, 'localhost');
      assert.equal(proxy.config.port, 8090);
      assert.equal(proxy.config.https, false);
    });

    describe('clear mocks on start', function() {
      var config = {
        mode: 'mock',
        name: 'clearOnStartTest',
        mocksPath: 'mocks',
        host: 'foo',
        context: '/context',
        clearOnStart: true
      };

      var mocksDir = path.join(config.mocksPath, config.name);
      var testMockFile = path.join(config.mocksPath, config.name, 'foo.json');
      var test404File = path.join(config.mocksPath, config.name, 'foo.json.404');

      beforeEach(function() {
        config.mode = 'proxy';
        if (!fs.existsSync(config.mocksPath)) {
          fs.mkdirSync(config.mocksPath);
        }
        if (!fs.existsSync(mocksDir)) {
          fs.mkdirSync(mocksDir);
        }
        // touch mock files
        fs.closeSync(fs.openSync(testMockFile, 'w'));
        fs.closeSync(fs.openSync(test404File, 'w'));
      });

      afterEach(function() {
        console.log('delete mock files');
        if (fs.existsSync(testMockFile)) {
          fs.unlinkSync(testMockFile);
        }
        if (fs.existsSync(test404File)) {
          fs.unlinkSync(test404File);
        }
      });

      it('in proxy mode don\'t clear mocks', function() {
        config.mode = 'proxy';
        prism.create(config);

        assert.equal(fs.existsSync(testMockFile), true);
      });
      it('in mock mode don\'t clear mocks', function() {
        config.mode = 'mock';
        prism.create(config);

        assert.equal(fs.existsSync(testMockFile), true);
      });
      it('in record mode clear mocks', function() {
        config.mode = 'record';
        prism.create(config);

        assert.equal(fs.existsSync(testMockFile), false);
        assert.equal(fs.existsSync(test404File), false);
      });
    });
  });
});
