OptionsNormalizer.spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { assert } from 'chai';
  2. import { TInputOptions } from '../../src/types/options/TInputOptions';
  3. import { IOptions } from '../../src/interfaces/options/IOptions';
  4. import { DEFAULT_PRESET } from '../../src/preset-options/DefaultPreset';
  5. import { Options } from '../../src/options/Options';
  6. import { OptionsNormalizer } from '../../src/options/OptionsNormalizer';
  7. /**
  8. * @param optionsPreset
  9. * @returns {IOptions}
  10. */
  11. function getNormalizedOptions (optionsPreset: TInputOptions): TInputOptions {
  12. const options: IOptions = new Options(optionsPreset);
  13. return OptionsNormalizer.normalizeOptions(options);
  14. }
  15. describe('OptionsNormalizer', () => {
  16. describe('normalizeOptions (options: IObfuscatorOptions): IObfuscatorOptions', () => {
  17. let optionsPreset: TInputOptions,
  18. expectedOptionsPreset: TInputOptions;
  19. it('should normalize options preset: domainLockRule', () => {
  20. optionsPreset = {
  21. ...DEFAULT_PRESET,
  22. domainLock: ['//localhost:9000', 'https://google.ru/abc?cde=fgh']
  23. };
  24. expectedOptionsPreset = {
  25. ...DEFAULT_PRESET,
  26. domainLock: ['localhost', 'google.ru']
  27. };
  28. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  29. });
  30. it('should normalize options preset: selfDefendingRule', () => {
  31. optionsPreset = {
  32. ...DEFAULT_PRESET,
  33. selfDefending: true,
  34. compact: false
  35. };
  36. expectedOptionsPreset = {
  37. ...DEFAULT_PRESET,
  38. selfDefending: true,
  39. compact: true
  40. };
  41. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  42. });
  43. it('should normalize options preset: sourceMapBaseUrlRule #1', () => {
  44. optionsPreset = {
  45. ...DEFAULT_PRESET,
  46. sourceMapBaseUrl: 'http://localhost:9000',
  47. };
  48. expectedOptionsPreset = {
  49. ...DEFAULT_PRESET,
  50. sourceMapBaseUrl: ''
  51. };
  52. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  53. });
  54. it('should normalize options preset: sourceMapBaseUrlRule #2', () => {
  55. optionsPreset = {
  56. ...DEFAULT_PRESET,
  57. sourceMapBaseUrl: 'http://localhost:9000',
  58. sourceMapFileName: '/outputSourceMapName.map'
  59. };
  60. expectedOptionsPreset = {
  61. ...DEFAULT_PRESET,
  62. sourceMapBaseUrl: 'http://localhost:9000/',
  63. sourceMapFileName: 'outputSourceMapName.js.map'
  64. };
  65. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  66. });
  67. it('should normalize options preset: sourceMapFileNameRule', () => {
  68. optionsPreset = {
  69. ...DEFAULT_PRESET,
  70. sourceMapBaseUrl: 'http://localhost:9000',
  71. sourceMapFileName: '//outputSourceMapName'
  72. };
  73. expectedOptionsPreset = {
  74. ...DEFAULT_PRESET,
  75. sourceMapBaseUrl: 'http://localhost:9000/',
  76. sourceMapFileName: 'outputSourceMapName.js.map'
  77. };
  78. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  79. });
  80. it('should normalize options preset: stringArrayRule', () => {
  81. optionsPreset = {
  82. ...DEFAULT_PRESET,
  83. stringArray: false,
  84. stringArrayEncoding: 'rc4',
  85. stringArrayThreshold: 0.5,
  86. rotateStringArray: true
  87. };
  88. expectedOptionsPreset = {
  89. ...DEFAULT_PRESET,
  90. stringArray: false,
  91. stringArrayEncoding: false,
  92. stringArrayThreshold: 0,
  93. rotateStringArray: false
  94. };
  95. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  96. });
  97. it('should normalize options preset: stringArrayEncodingRule', () => {
  98. optionsPreset = {
  99. ...DEFAULT_PRESET,
  100. stringArrayEncoding: true
  101. };
  102. expectedOptionsPreset = {
  103. ...DEFAULT_PRESET,
  104. stringArrayEncoding: 'base64'
  105. };
  106. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  107. });
  108. it('should normalize options preset: stringArrayThresholdRule', () => {
  109. optionsPreset = {
  110. ...DEFAULT_PRESET,
  111. rotateStringArray: true,
  112. stringArray: true,
  113. stringArrayThreshold: 0
  114. };
  115. expectedOptionsPreset = {
  116. ...DEFAULT_PRESET,
  117. rotateStringArray: false,
  118. stringArray: false,
  119. stringArrayThreshold: 0
  120. };
  121. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  122. });
  123. });
  124. });