LiteralNodesCacheStorage.spec.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import 'reflect-metadata';
  2. import { assert } from 'chai';
  3. import * as ESTree from 'estree';
  4. import { ServiceIdentifiers } from '../../../../../src/container/ServiceIdentifiers';
  5. import { TInputOptions } from '../../../../../src/types/options/TInputOptions';
  6. import { IInversifyContainerFacade } from '../../../../../src/interfaces/container/IInversifyContainerFacade';
  7. import { ILiteralNodesCacheStorage } from '../../../../../src/interfaces/storages/string-array-transformers/ILiteralNodesCacheStorage';
  8. import { StringArrayEncoding } from '../../../../../src/enums/StringArrayEncoding';
  9. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
  10. import { InversifyContainerFacade } from '../../../../../src/container/InversifyContainerFacade';
  11. import { NodeFactory } from '../../../../../src/node/NodeFactory';
  12. /**
  13. * @returns {IMapStorage<string, V>}
  14. */
  15. const getStorageInstance = (options: TInputOptions = {}): ILiteralNodesCacheStorage => {
  16. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  17. inversifyContainerFacade.load('', '', {
  18. ...NO_ADDITIONAL_NODES_PRESET,
  19. ...options
  20. });
  21. const storage: ILiteralNodesCacheStorage = inversifyContainerFacade.get(ServiceIdentifiers.ILiteralNodesCacheStorage);
  22. storage.initialize();
  23. return storage;
  24. };
  25. describe('LiteralNodesCacheStorage', () => {
  26. describe('buildKey', () => {
  27. const expectedCacheKey: string = 'foo-true';
  28. let cacheKey: string;
  29. before(() => {
  30. const literalNodesCacheStorage: ILiteralNodesCacheStorage = getStorageInstance();
  31. cacheKey = literalNodesCacheStorage.buildKey(
  32. 'foo',
  33. {
  34. index: 1,
  35. value: '_0x123abc',
  36. encoding: StringArrayEncoding.Rc4,
  37. encodedValue: 'encoded_value',
  38. decodeKey: 'key'
  39. }
  40. );
  41. });
  42. it('should build a key for the storage', () => {
  43. assert.equal(cacheKey, expectedCacheKey);
  44. });
  45. });
  46. describe('shouldUseCachedValue', () => {
  47. const literalNode: ESTree.Literal = NodeFactory.literalNode('foo');
  48. const key: string = 'key';
  49. describe('Encoding is not `rc4` and `stringArrayWrappersCount` option is disabled', () => {
  50. const expectedResult: boolean = true;
  51. let result: boolean;
  52. before(() => {
  53. const literalNodesCacheStorage: ILiteralNodesCacheStorage = getStorageInstance({
  54. stringArrayWrappersCount: 0
  55. });
  56. literalNodesCacheStorage.set(key, literalNode);
  57. result = literalNodesCacheStorage.shouldUseCachedValue(
  58. key,
  59. {
  60. index: 1,
  61. value: '_0x123abc',
  62. encoding: StringArrayEncoding.Base64,
  63. encodedValue: 'encoded_value',
  64. decodeKey: 'key'
  65. },
  66. );
  67. });
  68. it('should check if can use cached value', () => {
  69. assert.equal(result, expectedResult);
  70. });
  71. });
  72. describe('Encoding is `rc4` and `stringArrayWrappersCount` option is disabled', () => {
  73. const expectedResult: boolean = false
  74. let result: boolean;
  75. before(() => {
  76. const literalNodesCacheStorage: ILiteralNodesCacheStorage = getStorageInstance({
  77. stringArrayWrappersCount: 0
  78. });
  79. literalNodesCacheStorage.set(key, literalNode);
  80. result = literalNodesCacheStorage.shouldUseCachedValue(
  81. key,
  82. {
  83. index: 1,
  84. value: '_0x123abc',
  85. encoding: StringArrayEncoding.Rc4,
  86. encodedValue: 'encoded_value',
  87. decodeKey: 'key'
  88. },
  89. );
  90. });
  91. it('should check if can use cached value', () => {
  92. assert.equal(result, expectedResult);
  93. });
  94. });
  95. describe('Encoding is not `rc4` and `stringArrayWrappersCount` option is enabled', () => {
  96. const expectedResult: boolean = false;
  97. let result: boolean;
  98. before(() => {
  99. const literalNodesCacheStorage: ILiteralNodesCacheStorage = getStorageInstance({
  100. stringArray: true,
  101. stringArrayThreshold: 1,
  102. stringArrayWrappersCount: 5
  103. });
  104. literalNodesCacheStorage.set(key, literalNode);
  105. result = literalNodesCacheStorage.shouldUseCachedValue(
  106. key,
  107. {
  108. index: 1,
  109. value: '_0x123abc',
  110. encoding: StringArrayEncoding.Base64,
  111. encodedValue: 'encoded_value',
  112. decodeKey: 'key'
  113. },
  114. );
  115. });
  116. it('should check if can use cached value', () => {
  117. assert.equal(result, expectedResult);
  118. });
  119. });
  120. });
  121. });