<commit_before>import 'rxjs/add/observable/of';

import { Observable } from 'rxjs/Rx';
import { setTimeout } from 'timers';
import { error } from 'util';

import { ManUpConfig } from './manup.config';
import { AlertType, ManUpService } from './manup.service';

class MockAppVersion {
  public static getVersionNumber() {
    return Promise.resolve('2.3.4');
  }
}

describe('Manup Spec', function() {
  describe('constructor', function() {
    it('Should configure translation files if it has to', function() {
      const mockTranslate = {
        setTranslation: function() {}
      };

      const config = {
        externalTranslations: false
      };

      spyOn(mockTranslate, 'setTranslation');
      let manup = new ManUpService(<any>config, null, null, null, null, null, mockTranslate, null);
      expect(mockTranslate.setTranslation).toHaveBeenCalled();
    });
    it('Should not configure translation files using external ones', function() {
      const mockTranslate = {
        setTranslation: function() {}
      };

      const config = {
        externalTranslations: true
      };

      spyOn(mockTranslate, 'setTranslation');
      let manup = new ManUpService(<any>config, null, null, null, null, null, mockTranslate, null);
      expect(mockTranslate.setTranslation).not.toHaveBeenCalled();
    });
  });

  describe('validate', function() {
    it('should call presentAlert with the platform data', done => {
      let json = {
        minimum: '2.3.5',
        latest: '2.3.5',
        url: 'http://example.com',
        enabled: false
      };
      const mockTranslate = {
        setTranslation: function() {}
      };
      const mockHttp = {
        get: function(url: string): Observable<Object> {
          return Observable.of({
            json: function(): Object {
              return {
                ios: {
                  minimum: '2.4.5',
                  latest: '2.4.5',
                  enabled: true,
                  url: 'http://http.example.com'
                }
              };
            }
          });
        }
      };

      const config = {
        externalTranslations: false,
        url: 'http://example.com'
      };

      const mockAlert = {
        create: function() {}
      };

      const mockPlatform = {
        ready: () => Promise.resolve(),
        is: (platform: string) => platform === 'ios'
      };

      let manup = new ManUpService(
        config,
        <any>mockHttp,
        <any>mockAlert,
        <any>mockPlatform,
        null,
        <any>MockAppVersion,
        null,
        null
      );
      spyOn(manup, 'presentAlert');

      manup.validate();
      setTimeout(() => {
        expect(manup.presentAlert).toHaveBeenCalledWith(AlertType.MANDATORY, {
          minimum: '2.4.5',
          latest: '2.4.5',
          enabled: true,
          url: 'http://http.example.com'
        });
        done();
      }, 1000);
    });
  });

  describe('evaluate', function() {
    it('Should return maintenance mode if json says disabled', function(done) {
      let json = {
        minimum: '2.3.4',
        latest: '2.3.4',
        url: 'http://example.com',
        enabled: false
      };

      let manup = new ManUpService(null, null, null, null, null, <any>MockAppVersion, null, null);

      manup.evaluate(json).then(function(alert) {
        expect(alert).toEqual(AlertType.MAINTENANCE);
        done();
      });
    });
    it('Should return mandatory update if app version less than minimum', function(done) {
      let json = {
        minimum: '4.3.4',<commit_msg>Tests for functionality on unknown platform<commit_after>import 'rxjs/add/observable/of';

import { Observable } from 'rxjs/Rx';
import { setTimeout } from 'timers';
import { error } from 'util';

import { ManUpConfig } from './manup.config';
import { AlertType, ManUpService } from './manup.service';

class MockAppVersion {
  public static getVersionNumber() {
    return Promise.resolve('2.3.4');
  }
}

describe('Manup Spec', function() {
  describe('constructor', function() {
    it('Should configure translation files if it has to', function() {
      const mockTranslate = {
        setTranslation: function() {}
      };

      const config = {
        externalTranslations: false
      };

      spyOn(mockTranslate, 'setTranslation');
      let manup = new ManUpService(<any>config, null, null, null, null, null, mockTranslate, null);
      expect(mockTranslate.setTranslation).toHaveBeenCalled();
    });
    it('Should not configure translation files using external ones', function() {
      const mockTranslate = {
        setTranslation: function() {}
      };

      const config = {
        externalTranslations: true
      };

      spyOn(mockTranslate, 'setTranslation');
      let manup = new ManUpService(<any>config, null, null, null, null, null, mockTranslate, null);
      expect(mockTranslate.setTranslation).not.toHaveBeenCalled();
    });
  });

  describe('validate', function() {
    let json = {
      minimum: '2.3.5',
      latest: '2.3.5',
      url: 'http://example.com',
      enabled: false
    };
    const mockTranslate = {
      setTranslation: function() {}
    };
    const mockHttp = {
      get: function(url: string): Observable<Object> {
        return Observable.of({
          json: function(): Object {
            return {
              ios: {
                minimum: '2.4.5',
                latest: '2.4.5',
                enabled: true,
                url: 'http://http.example.com'
              }
            };
          }
        });
      }
    };
    const config = {
      externalTranslations: false,
      url: 'http://example.com'
    };
    const mockAlert = {
      create: function() {}
    };

    it('should call presentAlert with the platform data', done => {
      const mockPlatform = {
        ready: () => Promise.resolve(),
        is: (platform: string) => platform === 'ios'
      };

      let manup = new ManUpService(
        config,
        <any>mockHttp,
        <any>mockAlert,
        <any>mockPlatform,
        null,
        <any>MockAppVersion,
        null,
        null
      );
      spyOn(manup, 'presentAlert');

      manup.validate();
      setTimeout(() => {
        expect(manup.presentAlert).toHaveBeenCalledWith(AlertType.MANDATORY, {
          minimum: '2.4.5',
          latest: '2.4.5',
          enabled: true,
          url: 'http://http.example.com'
        });
        done();
      }, 1000);
    });

    it('Should silently resolve if the platform was not found in manup config', async () => {
      const mockPlatform = {
        ready: () => Promise.resolve(),
        is: (platform: string) => platform === 'browser'
      };

      let manup = new ManUpService(
        config,
        <any>mockHttp,
        <any>mockAlert,
        <any>mockPlatform,
        null,
        <any>MockAppVersion,
        null,
        null
      );
      spyOn(manup, 'presentAlert');

      try {
        await manup.validate();
      } catch (e) {
        expect(e).toBeUndefined();
      }
    });
  });

  describe('evaluate', function() {
    it('Should return maintenance mode if json says disabled', function(done) {
      let json = {
        minimum: '2.3.4',
        latest: '2.3.4',
        url: 'http://example.com',
        enabled: false
      };

      let manup = new ManUpService(null, null, null, null, null, <any>MockAppVersion, null, null);

      manup.evaluate(json).then(function(alert) {
        expect(alert).toEqual(AlertType.MAINTENANCE);
        done();
      });
    });
    it('Should return mandatory update if app version less than minimum', function(done) {
      let json = {
        minimum: '4.3.4',
{'fraction_non_alphanumeric': 0.1379802699896158, 'fraction_numerical': 0.008826583592938734, 'mean_word_length': 2.058753473600635, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 34, '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': '4707473', 'n_tokens_mistral': 2244, 'n_tokens_neox': 2206, 'n_words': 522}