trie.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import { createTrie } from './trie';
  2. // eslint-disable-next-line import-x/no-unresolved -- fuck eslint-import
  3. import { describe, expect, it } from 'bun:test';
  4. describe('Trie', () => {
  5. it('should be possible to add items to a Trie.', () => {
  6. const trie = createTrie();
  7. trie.add('sukka');
  8. trie.add('ukka');
  9. trie.add('akku');
  10. expect(trie.size).toBe(3);
  11. expect(trie.has('sukka')).toBeTrue();
  12. expect(trie.has('ukka')).toBeTrue();
  13. expect(trie.has('akku')).toBeTrue();
  14. expect(trie.has('noc')).toBeFalse();
  15. expect(trie.has('suk')).toBeFalse();
  16. expect(trie.has('sukkaw')).toBeFalse();
  17. });
  18. it('adding the same item several times should not increase size.', () => {
  19. const trie = createTrie();
  20. trie.add('rat');
  21. trie.add('erat');
  22. trie.add('rat');
  23. expect(trie.size).toBe(2);
  24. expect(trie.has('rat')).toBeTrue();
  25. });
  26. it('should be possible to set the null sequence.', () => {
  27. const trie = createTrie();
  28. trie.add('');
  29. expect(trie.has('')).toBeTrue();
  30. });
  31. it('should be possible to delete items.', () => {
  32. const trie = createTrie();
  33. trie.add('rat');
  34. trie.add('rate');
  35. trie.add('tar');
  36. expect(trie.delete('')).toBeFalse();
  37. expect(trie.delete('')).toBeFalse();
  38. expect(trie.delete('hello')).toBeFalse();
  39. expect(trie.delete('rat')).toBeTrue();
  40. expect(trie.has('rat')).toBeFalse();
  41. expect(trie.has('rate')).toBeTrue();
  42. expect(trie.size).toBe(2);
  43. expect(trie.delete('rate')).toBeTrue();
  44. expect(trie.size).toBe(1);
  45. expect(trie.delete('tar')).toBeTrue();
  46. expect(trie.size).toBe(0);
  47. });
  48. it('should be possible to check the existence of a sequence in the Trie.', () => {
  49. const trie = createTrie();
  50. trie.add('romanesque');
  51. expect(trie.has('romanesque')).toBe(true);
  52. expect(trie.has('roman')).toBe(false);
  53. expect(trie.has('')).toBe(false);
  54. });
  55. it('should be possible to retrieve items matching the given prefix.', () => {
  56. const trie = createTrie();
  57. trie.add('roman');
  58. trie.add('esqueroman');
  59. trie.add('sesqueroman');
  60. trie.add('greek');
  61. console.log({ trie });
  62. expect(trie.find('roman')).toEqual(['roman', 'esqueroman', 'sesqueroman']);
  63. expect(trie.find('man')).toEqual(['roman', 'esqueroman', 'sesqueroman']);
  64. expect(trie.find('esqueroman')).toEqual(['esqueroman', 'sesqueroman']);
  65. expect(trie.find('eek')).toEqual(['greek']);
  66. expect(trie.find('hello')).toEqual([]);
  67. expect(trie.find('')).toEqual(['greek', 'roman', 'esqueroman', 'sesqueroman']);
  68. });
  69. it('should be possible to create a trie from an arbitrary iterable.', () => {
  70. const words = ['roman', 'esqueroman'];
  71. const trie = createTrie(words);
  72. expect(trie.size).toBe(2);
  73. expect(trie.has('roman')).toBe(true);
  74. });
  75. });
  76. describe.each([
  77. ['hostname mode off', false],
  78. ['hostname mode on', true]
  79. ])('surge domainset dedupe %s', (_, hostnameMode) => {
  80. it('should not remove same entry', () => {
  81. const trie = createTrie(['.skk.moe', 'noc.one'], hostnameMode);
  82. console.log(trie);
  83. expect(trie.find('.skk.moe')).toStrictEqual(['.skk.moe']);
  84. expect(trie.find('noc.one')).toStrictEqual(['noc.one']);
  85. });
  86. it('should match subdomain - 1', () => {
  87. const trie = createTrie(['www.noc.one', 'www.sukkaw.com', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], hostnameMode);
  88. console.log(trie);
  89. expect(trie.find('.skk.moe')).toStrictEqual(['image.cdn.skk.moe', 'blog.skk.moe']);
  90. expect(trie.find('.sukkaw.com')).toStrictEqual(['www.sukkaw.com']);
  91. });
  92. it('should match subdomain - 2', () => {
  93. const trie = createTrie(['www.noc.one', 'www.sukkaw.com', '.skk.moe', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], hostnameMode);
  94. console.log(trie);
  95. expect(trie.find('.skk.moe')).toStrictEqual(['.skk.moe', 'image.cdn.skk.moe', 'blog.skk.moe']);
  96. expect(trie.find('.sukkaw.com')).toStrictEqual(['www.sukkaw.com']);
  97. });
  98. it('should not remove non-subdomain', () => {
  99. const trie = createTrie(['skk.moe', 'sukkaskk.moe'], hostnameMode);
  100. expect(trie.find('.skk.moe')).toStrictEqual([]);
  101. });
  102. });
  103. describe('smol tree', () => {
  104. it('should create simple tree - 1', () => {
  105. const trie = createTrie([
  106. '.skk.moe', 'blog.skk.moe', '.cdn.skk.moe', 'skk.moe',
  107. 'www.noc.one', 'cdn.noc.one',
  108. '.blog.sub.example.com', 'sub.example.com', 'cdn.sub.example.com', '.sub.example.com'
  109. ], true, true);
  110. expect(trie.dump()).toStrictEqual([
  111. '.sub.example.com',
  112. 'cdn.noc.one', 'www.noc.one',
  113. '.skk.moe'
  114. ]);
  115. });
  116. it('should create simple tree - 2', () => {
  117. const trie = createTrie([
  118. '.skk.moe', 'blog.skk.moe', '.cdn.skk.moe', 'skk.moe'
  119. ], true, true);
  120. expect(trie.dump()).toStrictEqual([
  121. '.skk.moe'
  122. ]);
  123. });
  124. it('should create simple tree - 2', () => {
  125. const trie = createTrie([
  126. '.blog.sub.example.com', 'cdn.sub.example.com', '.sub.example.com'
  127. ], true, true);
  128. expect(trie.dump()).toStrictEqual([
  129. '.sub.example.com'
  130. ]);
  131. trie.add('.sub.example.com');
  132. expect(trie.dump()).toStrictEqual([
  133. '.sub.example.com'
  134. ]);
  135. });
  136. it('should create simple tree - 3', () => {
  137. const trie = createTrie([
  138. 'commercial.shouji.360.cn',
  139. 'act.commercial.shouji.360.cn',
  140. 'cdn.creative.medialytics.com',
  141. 'px.cdn.creative.medialytics.com'
  142. ], true, true);
  143. expect(trie.dump()).toStrictEqual([
  144. 'cdn.creative.medialytics.com',
  145. 'px.cdn.creative.medialytics.com',
  146. 'commercial.shouji.360.cn',
  147. 'act.commercial.shouji.360.cn'
  148. ]);
  149. });
  150. it('should dedupe subdomain properly', () => {
  151. const trie = createTrie([
  152. 'skk.moe',
  153. 'anotherskk.moe',
  154. 'blog.anotherskk.moe',
  155. 'blog.skk.moe'
  156. ], true, true);
  157. expect(trie.dump()).toStrictEqual([
  158. 'anotherskk.moe',
  159. 'blog.anotherskk.moe',
  160. 'skk.moe',
  161. 'blog.skk.moe'
  162. ]);
  163. });
  164. it('should efficiently whitelist domains', () => {
  165. const trie = createTrie([
  166. 'skk.moe',
  167. 'anotherskk.moe',
  168. 'blog.anotherskk.moe',
  169. 'blog.skk.moe'
  170. ], true, true);
  171. expect(trie.dump()).toStrictEqual([
  172. 'anotherskk.moe',
  173. 'blog.anotherskk.moe',
  174. 'skk.moe',
  175. 'blog.skk.moe'
  176. ]);
  177. trie.whitelist('.skk.moe');
  178. expect(trie.dump()).toStrictEqual([
  179. 'anotherskk.moe',
  180. 'blog.anotherskk.moe'
  181. ]);
  182. trie.whitelist('anotherskk.moe');
  183. expect(trie.dump()).toStrictEqual([
  184. 'blog.anotherskk.moe'
  185. ]);
  186. trie.add('anotherskk.moe');
  187. trie.whitelist('.anotherskk.moe');
  188. expect(trie.dump()).toStrictEqual([]);
  189. });
  190. });