<commit_before>'use strict';

/* jasmine specs for controllers */

/* http://docs.angularjs.org/api/angular.mock.inject */

function make_project(key, subsecond) {
  return {
      'description': 'The project description',
      'key': key,
      'name': 'New project name',
      'orderby': '<EMAIL>-2013-01-03T01:05:32.' + subsecond,
      'run_url': 'http://localhost:9200/?_mimic_project=' + key,
  }
};

describe('HeaderController', function() {

  var scope, $httpBackend, location;

  beforeEach(inject(function($rootScope, $injector) {
    scope = $rootScope.$new();
    //$httpBackend = $injector.get('$httpBackend');
  }));

  beforeEach(inject(function($location) {
    doInit();
    location = $location
  }));

  function doInit() {
    inject(function($controller) {
      $controller(HeaderController, {$scope: scope});
      flushDoSerial();
      //$httpBackend.flush();
    });
  }


  it('alreadyhome function should only return true for /playground/', function() {
    expect(typeof scope.alreadyhome).toEqual('function');

    location.path('/');
    expect(scope.alreadyhome()).toBe(false);
    location.path('/playground');
    expect(scope.alreadyhome()).toBe(false);
    location.path('/playground/');
    expect(scope.alreadyhome()).toBe(true);
    location.path('/playground/p/42/');
    expect(scope.alreadyhome()).toBe(false);
  });

});



describe('ProjectController', function() {

  var scope, $httpBackend;

  function make_file(filename, mime_type, contents) {
    var file = {
        'name': filename,
        'mime_type': mime_type,
    };
    if (contents) {
      file.contents = contents;
    }
    return file;
  }

  function make_files_response() {
    return [
        make_file('app.yaml',    'text/x-yaml'),
        make_file('favicon.ico', 'image/x-icon'),
        make_file('main.py',     'text/x-python'),
    ];
  }

  function make_files_data() {
    return {
        'app.yaml':    make_file('app.yaml',    'text/x-yaml'),
        'favicon.ico': make_file('favicon.ico', 'image/x-icon'),
        'main.py':     make_file('main.py',     'text/x-python'),
    };
  }

  beforeEach(module(function($provide) {
    $provide.factory('$window', function() {
      return {
        location: { replace: jasmine.createSpy(),
                    pathname: '/playground/p/76/' },
        navigator: {},
      };
    });
  }));

  beforeEach(module('playgroundApp.services'));

  beforeEach(inject(function($rootScope, $injector) {
    scope = $rootScope.$new();
    // TODO: remove if we instead instantiate a PageController
    scope.config = {
        'PLAYGROUND_USER_CONTENT_HOST': 'localhost:9100',
    };
    $httpBackend = $injector.get('$httpBackend');

    $httpBackend
    .whenGET('/playground/p/20/listfiles')
    .respond(make_files_response())
  }));

  beforeEach(inject(function($browser) {
    $browser.url('/playground/p/20/');
  }));


  afterEach(function() {
    flushDoSerial();
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  });


  function doInit() {
    inject(function($controller) {
      $controller(ProjectController, {$scope: scope});
      flushDoSerial();
      $httpBackend.flush();
    });
  }


  describe('initialization', function() {

    it('should set $scope.project to the project identified by $routeParams.project_id', inject(function($routeParams) {
      $routeParams.project_id = 42;
      var project = make_project(42);
      scope.projects = [make_project(17), project, make_project(13)];
      expect(scope.projects[1]).toBe(project);
      expect(scope.project).toBeUndefined();
      doInit();
      expect(scope.project).toBe(project);
    }));

    it('should call /playground/p/:project_id/listfiles', function() {
      $httpBackend.expectGET('/playground/p/20/listfiles');
      doInit();
    });

  });


  describe('runtime behavior', function() {

    beforeEach(function() {
      doInit();
    });

    describe('no_json_transform function', function() {

      it('should provide the identity transform', function() {
        var data = 'foo';
        expect(scope.no_json_transform(data)).toBe(data);
        expect(scope.no_json_transform(data)).toEqual('foo');
        data = '[1,2,3]';
        expect(scope.no_json_transform(data)).toBe(data);
        expect(scope.no_json_transform(data)).toEqual('[1,2,3]');
        data = undefined;
        expect(scope.no_json_transform(data)).toBe(undefined);
        data = null;
        expect(scope.no_json_transform(data)).toBe(null);
        data = Error('x');
        expect(scope.no_json_transform(data)).toEqual(Error('x'));
        expect(scope.no_json_transform(data)).toBe(data);
      });

    });


    describe('is_image_mime_type function', function() {

      it('should return true for "image/*" MIME types', function() {
        expect(scope.is_image_mime_type('image/png')).toBe(true);
        expect(scope.is_image_mime_type('image/gif')).toBe(true);
        expect(scope.is_image_mime_type('image')).toBe(false);
        expect(scope.is_image_mime_type('text/plain')).toBe(false);
        expect(scope.is_image_mime_type('text/png')).toBe(false);
        expect(scope.is_image_mime_type('application/octet-stream')).toBe(false);
      });

    });


    describe('url_of function', function() {

      it('should return //localhost:9100/p/:project_id/getfile/:filename', inject(function($window) {
        $window.location.pathname = '/playground/p/42/';
        var png = make_file('logo.png', 'image/png');
        expect(scope.url_of(png)).toEqual('//localhost:9100/playground/p/42/getfile/logo.png');
      }));

    });


    describe('image_url_of function', function() {

      it('should return emtpty string when no file is given', function() {
        expect(scope.image_url_of()).toEqual('');
        expect(scope.image_url_of(null)).toEqual('');
      });

      it('should return empty string for none "image/*" MIME types ', function() {
        expect(scope.image_url_of(make_file('filename', 'text/html'))).toEqual('');
      });

      it('should pass through to url_of() for "image/*" MIME types ', function() {
        var png = make_file('filename', 'image/png');
        var file_url = scope.url_of(png);
        expect(scope.image_url_of(png)).toBe(file_url);
      });

    });


    describe('_get function', function() {

      it('should call success_cb when file has contents', function() {
        var success_cb = jasmine.createSpy();
        var file = make_file('app.yaml', 'text/x-yaml', 'version: 1');
        scope._get(file, success_cb);
        expect(success_cb).toHaveBeenCalledWith();
      });

      it('should call success_cb when file has no contents', function() {
        var success_cb = jasmine.createSpy();
        var file = make_file('app.yaml', 'text/x-yaml');
        expect(file.contents).toBeUndefined();
        expect(file.hasOwnProperty('contents')).toBe(false);
        $httpBackend.expectGET(scope.url_of(file)).respond('foo: bar');
        scope._get(file, success_cb);
        $httpBackend.flush();
        expect(file.contents).toEqual('foo: bar');
        expect(success_cb).toHaveBeenCalledWith();
      });

      it('should not overwrite file contents regardless of dirty flag', function() {
        var noop = function() {};
        var file = make_file('app.yaml', 'text/x-yaml', 'version: 1');
        scope._get(file, noop);
        expect(file.dirty).not.toBeDefined();
        expect(file.hasOwnProperty('dirty')).toBe(false);
        expect(file.contents).toEqual('version: 1');
        file.dirty = true;
        scope._get(file, noop);
        expect(file.contents).toEqual('version: 1');
        file.dirty = false;
        scope._get(file, noop);
        expect(file.contents).toEqual('version: 1');
      });

      it('should set file.dirty=false after fetching contents', function() {
        var success_cb = jasmine.createSpy();
        var file = make_file('app.yaml', 'text/x-yaml');
        expect(file.contents).toBeUndefined();
        expect(file.hasOwnProperty('contents')).toBe(false);
        expect(file.dirty).toBe(undefined);
        $httpBackend.expectGET(scope.url_of(file)).respond('version: bar');
        scope._get(file, success_cb);
        $httpBackend.flush();
        expect(file.contents).toEqual('version: bar');
        expect(file.dirty).toBe(false);
      });

      it('should HTTP GET missing file contents', function() {
        var success_cb = jasmine.createSpy();
        var file = make_file('app.yaml', 'text/x-yaml');
        $httpBackend.expectGET(scope.url_of(file)).respond('x: y');
        expect(file.contents).toBeUndefined();
        scope._get(file, success_cb);
        $httpBackend.flush();
        expect(file.contents).toEqual('x: y');
      });

    });


    describe('_list_files function', function() {

      function make_plain_file() {
        return make_file('foo.txt', 'text/plain');
      }

      it('should call /playground/p/:project_id/listfiles', function() {
        $httpBackend.verifyNoOutstandingRequest();
        $httpBackend.verifyNoOutstandingExpectation();
        expect(scope.files).toEqual(make_files_data());
        $httpBackend
        .expectGET('/playground/p/20/listfiles')
        .respond([make_plain_file()]);
        scope.files = null;
        scope._list_files();
        $httpBackend.flush();
        expect(scope.files).toEqual({'foo.txt': make_plain_file()});
      });

    });


    describe('select_file function', function() {

      describe('with "image/*" MIME types', function() {

        function make_image_file() {
          return make_file('foo.png', 'image/png');
        }

        it('should set $scope.current_file', function() {
          expect(scope.current_file).toBeUndefined();
          scope.select_file(make_image_file());
          expect(scope.current_file).toEqual(make_image_file());
        });

        it('should return undefined', function() {
          var result = scope.select_file(make_image_file());<commit_msg>{20,76} -> 76 for consistency<commit_after>'use strict';

/* jasmine specs for controllers */

/* http://docs.angularjs.org/api/angular.mock.inject */

function make_project(key, subsecond) {
  return {
      'description': 'The project description',
      'key': key,
      'name': 'New project name',
      'orderby': '<EMAIL>-2013-01-03T01:05:32.' + subsecond,
      'run_url': 'http://localhost:9200/?_mimic_project=' + key,
  }
};

describe('HeaderController', function() {

  var scope, $httpBackend, location;

  beforeEach(inject(function($rootScope, $injector) {
    scope = $rootScope.$new();
    //$httpBackend = $injector.get('$httpBackend');
  }));

  beforeEach(inject(function($location) {
    doInit();
    location = $location
  }));

  function doInit() {
    inject(function($controller) {
      $controller(HeaderController, {$scope: scope});
      flushDoSerial();
      //$httpBackend.flush();
    });
  }


  it('alreadyhome function should only return true for /playground/', function() {
    expect(typeof scope.alreadyhome).toEqual('function');

    location.path('/');
    expect(scope.alreadyhome()).toBe(false);
    location.path('/playground');
    expect(scope.alreadyhome()).toBe(false);
    location.path('/playground/');
    expect(scope.alreadyhome()).toBe(true);
    location.path('/playground/p/42/');
    expect(scope.alreadyhome()).toBe(false);
  });

});



describe('ProjectController', function() {

  var scope, $httpBackend;

  function make_file(filename, mime_type, contents) {
    var file = {
        'name': filename,
        'mime_type': mime_type,
    };
    if (contents) {
      file.contents = contents;
    }
    return file;
  }

  function make_files_response() {
    return [
        make_file('app.yaml',    'text/x-yaml'),
        make_file('favicon.ico', 'image/x-icon'),
        make_file('main.py',     'text/x-python'),
    ];
  }

  function make_files_data() {
    return {
        'app.yaml':    make_file('app.yaml',    'text/x-yaml'),
        'favicon.ico': make_file('favicon.ico', 'image/x-icon'),
        'main.py':     make_file('main.py',     'text/x-python'),
    };
  }

  beforeEach(module(function($provide) {
    $provide.factory('$window', function() {
      return {
        location: { replace: jasmine.createSpy(),
                    pathname: '/playground/p/76/' },
        navigator: {},
      };
    });
  }));

  beforeEach(module('playgroundApp.services'));

  beforeEach(inject(function($rootScope, $injector) {
    scope = $rootScope.$new();
    // TODO: remove if we instead instantiate a PageController
    scope.config = {
        'PLAYGROUND_USER_CONTENT_HOST': 'localhost:9100',
    };
    $httpBackend = $injector.get('$httpBackend');

    $httpBackend
    .whenGET('/playground/p/76/listfiles')
    .respond(make_files_response())
  }));

  beforeEach(inject(function($browser) {
    $browser.url('/playground/p/76/');
  }));


  afterEach(function() {
    flushDoSerial();
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  });


  function doInit() {
    inject(function($controller) {
      $controller(ProjectController, {$scope: scope});
      flushDoSerial();
      $httpBackend.flush();
    });
  }


  describe('initialization', function() {

    it('should set $scope.project to the project identified by $routeParams.project_id', inject(function($routeParams) {
      $routeParams.project_id = 42;
      var project = make_project(42);
      scope.projects = [make_project(17), project, make_project(13)];
      expect(scope.projects[1]).toBe(project);
      expect(scope.project).toBeUndefined();
      doInit();
      expect(scope.project).toBe(project);
    }));

    it('should call /playground/p/:project_id/listfiles', function() {
      $httpBackend.expectGET('/playground/p/76/listfiles');
      doInit();
    });

  });


  describe('runtime behavior', function() {

    beforeEach(function() {
      doInit();
    });

    describe('no_json_transform function', function() {

      it('should provide the identity transform', function() {
        var data = 'foo';
        expect(scope.no_json_transform(data)).toBe(data);
        expect(scope.no_json_transform(data)).toEqual('foo');
        data = '[1,2,3]';
        expect(scope.no_json_transform(data)).toBe(data);
        expect(scope.no_json_transform(data)).toEqual('[1,2,3]');
        data = undefined;
        expect(scope.no_json_transform(data)).toBe(undefined);
        data = null;
        expect(scope.no_json_transform(data)).toBe(null);
        data = Error('x');
        expect(scope.no_json_transform(data)).toEqual(Error('x'));
        expect(scope.no_json_transform(data)).toBe(data);
      });

    });


    describe('is_image_mime_type function', function() {

      it('should return true for "image/*" MIME types', function() {
        expect(scope.is_image_mime_type('image/png')).toBe(true);
        expect(scope.is_image_mime_type('image/gif')).toBe(true);
        expect(scope.is_image_mime_type('image')).toBe(false);
        expect(scope.is_image_mime_type('text/plain')).toBe(false);
        expect(scope.is_image_mime_type('text/png')).toBe(false);
        expect(scope.is_image_mime_type('application/octet-stream')).toBe(false);
      });

    });


    describe('url_of function', function() {

      it('should return //localhost:9100/p/:project_id/getfile/:filename', inject(function($window) {
        $window.location.pathname = '/playground/p/42/';
        var png = make_file('logo.png', 'image/png');
        expect(scope.url_of(png)).toEqual('//localhost:9100/playground/p/42/getfile/logo.png');
      }));

    });


    describe('image_url_of function', function() {

      it('should return emtpty string when no file is given', function() {
        expect(scope.image_url_of()).toEqual('');
        expect(scope.image_url_of(null)).toEqual('');
      });

      it('should return empty string for none "image/*" MIME types ', function() {
        expect(scope.image_url_of(make_file('filename', 'text/html'))).toEqual('');
      });

      it('should pass through to url_of() for "image/*" MIME types ', function() {
        var png = make_file('filename', 'image/png');
        var file_url = scope.url_of(png);
        expect(scope.image_url_of(png)).toBe(file_url);
      });

    });


    describe('_get function', function() {

      it('should call success_cb when file has contents', function() {
        var success_cb = jasmine.createSpy();
        var file = make_file('app.yaml', 'text/x-yaml', 'version: 1');
        scope._get(file, success_cb);
        expect(success_cb).toHaveBeenCalledWith();
      });

      it('should call success_cb when file has no contents', function() {
        var success_cb = jasmine.createSpy();
        var file = make_file('app.yaml', 'text/x-yaml');
        expect(file.contents).toBeUndefined();
        expect(file.hasOwnProperty('contents')).toBe(false);
        $httpBackend.expectGET(scope.url_of(file)).respond('foo: bar');
        scope._get(file, success_cb);
        $httpBackend.flush();
        expect(file.contents).toEqual('foo: bar');
        expect(success_cb).toHaveBeenCalledWith();
      });

      it('should not overwrite file contents regardless of dirty flag', function() {
        var noop = function() {};
        var file = make_file('app.yaml', 'text/x-yaml', 'version: 1');
        scope._get(file, noop);
        expect(file.dirty).not.toBeDefined();
        expect(file.hasOwnProperty('dirty')).toBe(false);
        expect(file.contents).toEqual('version: 1');
        file.dirty = true;
        scope._get(file, noop);
        expect(file.contents).toEqual('version: 1');
        file.dirty = false;
        scope._get(file, noop);
        expect(file.contents).toEqual('version: 1');
      });

      it('should set file.dirty=false after fetching contents', function() {
        var success_cb = jasmine.createSpy();
        var file = make_file('app.yaml', 'text/x-yaml');
        expect(file.contents).toBeUndefined();
        expect(file.hasOwnProperty('contents')).toBe(false);
        expect(file.dirty).toBe(undefined);
        $httpBackend.expectGET(scope.url_of(file)).respond('version: bar');
        scope._get(file, success_cb);
        $httpBackend.flush();
        expect(file.contents).toEqual('version: bar');
        expect(file.dirty).toBe(false);
      });

      it('should HTTP GET missing file contents', function() {
        var success_cb = jasmine.createSpy();
        var file = make_file('app.yaml', 'text/x-yaml');
        $httpBackend.expectGET(scope.url_of(file)).respond('x: y');
        expect(file.contents).toBeUndefined();
        scope._get(file, success_cb);
        $httpBackend.flush();
        expect(file.contents).toEqual('x: y');
      });

    });


    describe('_list_files function', function() {

      function make_plain_file() {
        return make_file('foo.txt', 'text/plain');
      }

      it('should call /playground/p/:project_id/listfiles', function() {
        $httpBackend.verifyNoOutstandingRequest();
        $httpBackend.verifyNoOutstandingExpectation();
        expect(scope.files).toEqual(make_files_data());
        $httpBackend
        .expectGET('/playground/p/76/listfiles')
        .respond([make_plain_file()]);
        scope.files = null;
        scope._list_files();
        $httpBackend.flush();
        expect(scope.files).toEqual({'foo.txt': make_plain_file()});
      });

    });


    describe('select_file function', function() {

      describe('with "image/*" MIME types', function() {

        function make_image_file() {
          return make_file('foo.png', 'image/png');
        }

        it('should set $scope.current_file', function() {
          expect(scope.current_file).toBeUndefined();
          scope.select_file(make_image_file());
          expect(scope.current_file).toEqual(make_image_file());
        });

        it('should return undefined', function() {
          var result = scope.select_file(make_image_file());
{'fraction_non_alphanumeric': 0.16741249005568815, 'fraction_numerical': 0.006861575178997614, 'mean_word_length': 3.066518398706025, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '639203', 'n_tokens_mistral': 6238, 'n_tokens_neox': 6091, 'n_words': 964}