serverSideStorage.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import { cloneDeep } from 'lodash'
  2. import {
  3. VERSION,
  4. COMMAND_TRIM_FLAGS,
  5. COMMAND_TRIM_FLAGS_AND_RESET,
  6. _moveItemInArray,
  7. _getRecentData,
  8. _getAllFlags,
  9. _mergeFlags,
  10. _mergePrefs,
  11. _resetFlags,
  12. mutations,
  13. defaultState,
  14. newUserFlags
  15. } from 'src/modules/serverSideStorage.js'
  16. describe('The serverSideStorage module', () => {
  17. describe('mutations', () => {
  18. describe('setServerSideStorage', () => {
  19. const { setServerSideStorage } = mutations
  20. const user = {
  21. created_at: new Date('1999-02-09'),
  22. storage: {}
  23. }
  24. it('should initialize storage if none present', () => {
  25. const state = cloneDeep(defaultState)
  26. setServerSideStorage(state, user)
  27. expect(state.cache._version).to.eql(VERSION)
  28. expect(state.cache._timestamp).to.be.a('number')
  29. expect(state.cache.flagStorage).to.eql(defaultState.flagStorage)
  30. expect(state.cache.prefsStorage).to.eql(defaultState.prefsStorage)
  31. })
  32. it('should initialize storage with proper flags for new users if none present', () => {
  33. const state = cloneDeep(defaultState)
  34. setServerSideStorage(state, { ...user, created_at: new Date() })
  35. expect(state.cache._version).to.eql(VERSION)
  36. expect(state.cache._timestamp).to.be.a('number')
  37. expect(state.cache.flagStorage).to.eql(newUserFlags)
  38. expect(state.cache.prefsStorage).to.eql(defaultState.prefsStorage)
  39. })
  40. it('should merge flags even if remote timestamp is older', () => {
  41. const state = {
  42. ...cloneDeep(defaultState),
  43. cache: {
  44. _timestamp: Date.now(),
  45. _version: VERSION,
  46. ...cloneDeep(defaultState)
  47. }
  48. }
  49. setServerSideStorage(
  50. state,
  51. {
  52. ...user,
  53. storage: {
  54. _timestamp: 123,
  55. _version: VERSION,
  56. flagStorage: {
  57. ...defaultState.flagStorage,
  58. updateCounter: 1
  59. },
  60. prefsStorage: {
  61. ...defaultState.prefsStorage
  62. }
  63. }
  64. }
  65. )
  66. expect(state.cache.flagStorage).to.eql({
  67. ...defaultState.flagStorage,
  68. updateCounter: 1
  69. })
  70. })
  71. it('should reset local timestamp to remote if contents are the same', () => {
  72. const state = {
  73. ...cloneDeep(defaultState),
  74. cache: null
  75. }
  76. setServerSideStorage(
  77. state,
  78. {
  79. ...user,
  80. storage: {
  81. _timestamp: 123,
  82. _version: VERSION,
  83. flagStorage: {
  84. ...defaultState.flagStorage,
  85. updateCounter: 999
  86. }
  87. }
  88. }
  89. )
  90. expect(state.cache._timestamp).to.eql(123)
  91. expect(state.flagStorage.updateCounter).to.eql(999)
  92. expect(state.cache.flagStorage.updateCounter).to.eql(999)
  93. })
  94. it('should remote version if local missing', () => {
  95. const state = cloneDeep(defaultState)
  96. setServerSideStorage(state, user)
  97. expect(state.cache._version).to.eql(VERSION)
  98. expect(state.cache._timestamp).to.be.a('number')
  99. expect(state.cache.flagStorage).to.eql(defaultState.flagStorage)
  100. })
  101. })
  102. describe('setPreference', () => {
  103. const { setPreference, updateCache, addCollectionPreference, removeCollectionPreference } = mutations
  104. it('should set preference and update journal log accordingly', () => {
  105. const state = cloneDeep(defaultState)
  106. setPreference(state, { path: 'simple.testing', value: 1 })
  107. expect(state.prefsStorage.simple.testing).to.eql(1)
  108. expect(state.prefsStorage._journal.length).to.eql(1)
  109. expect(state.prefsStorage._journal[0]).to.eql({
  110. path: 'simple.testing',
  111. operation: 'set',
  112. args: [1],
  113. // should have A timestamp, we don't really care what it is
  114. timestamp: state.prefsStorage._journal[0].timestamp
  115. })
  116. })
  117. it('should keep journal to a minimum', () => {
  118. const state = cloneDeep(defaultState)
  119. setPreference(state, { path: 'simple.testing', value: 1 })
  120. setPreference(state, { path: 'simple.testing', value: 2 })
  121. addCollectionPreference(state, { path: 'collections.testing', value: 2 })
  122. removeCollectionPreference(state, { path: 'collections.testing', value: 2 })
  123. updateCache(state, { username: 'test' })
  124. expect(state.prefsStorage.simple.testing).to.eql(2)
  125. expect(state.prefsStorage.collections.testing).to.eql([])
  126. expect(state.prefsStorage._journal.length).to.eql(2)
  127. expect(state.prefsStorage._journal[0]).to.eql({
  128. path: 'simple.testing',
  129. operation: 'set',
  130. args: [2],
  131. // should have A timestamp, we don't really care what it is
  132. timestamp: state.prefsStorage._journal[0].timestamp
  133. })
  134. expect(state.prefsStorage._journal[1]).to.eql({
  135. path: 'collections.testing',
  136. operation: 'removeFromCollection',
  137. args: [2],
  138. // should have A timestamp, we don't really care what it is
  139. timestamp: state.prefsStorage._journal[1].timestamp
  140. })
  141. })
  142. })
  143. })
  144. describe('helper functions', () => {
  145. describe('_moveItemInArray', () => {
  146. it('should move item according to movement value', () => {
  147. expect(_moveItemInArray([1, 2, 3, 4], 4, -1)).to.eql([1, 2, 4, 3])
  148. expect(_moveItemInArray([1, 2, 3, 4], 1, 2)).to.eql([2, 3, 1, 4])
  149. })
  150. it('should clamp movement to within array', () => {
  151. expect(_moveItemInArray([1, 2, 3, 4], 4, -10)).to.eql([4, 1, 2, 3])
  152. expect(_moveItemInArray([1, 2, 3, 4], 3, 99)).to.eql([1, 2, 4, 3])
  153. })
  154. })
  155. describe('_getRecentData', () => {
  156. it('should handle nulls correctly', () => {
  157. expect(_getRecentData(null, null)).to.eql({ recent: null, stale: null, needUpload: true })
  158. })
  159. it('doesn\'t choke on invalid data', () => {
  160. expect(_getRecentData({ a: 1 }, { b: 2 })).to.eql({ recent: null, stale: null, needUpload: true })
  161. })
  162. it('should prefer the valid non-null correctly, needUpload works properly', () => {
  163. const nonNull = { _version: VERSION, _timestamp: 1 }
  164. expect(_getRecentData(nonNull, null)).to.eql({ recent: nonNull, stale: null, needUpload: true })
  165. expect(_getRecentData(null, nonNull)).to.eql({ recent: nonNull, stale: null, needUpload: false })
  166. })
  167. it('should prefer the one with higher timestamp', () => {
  168. const a = { _version: VERSION, _timestamp: 1 }
  169. const b = { _version: VERSION, _timestamp: 2 }
  170. expect(_getRecentData(a, b)).to.eql({ recent: b, stale: a, needUpload: false })
  171. expect(_getRecentData(b, a)).to.eql({ recent: b, stale: a, needUpload: false })
  172. })
  173. it('case where both are same', () => {
  174. const a = { _version: VERSION, _timestamp: 3 }
  175. const b = { _version: VERSION, _timestamp: 3 }
  176. expect(_getRecentData(a, b)).to.eql({ recent: b, stale: a, needUpload: false })
  177. expect(_getRecentData(b, a)).to.eql({ recent: b, stale: a, needUpload: false })
  178. })
  179. })
  180. describe('_getAllFlags', () => {
  181. it('should handle nulls properly', () => {
  182. expect(_getAllFlags(null, null)).to.eql([])
  183. })
  184. it('should output list of keys if passed single object', () => {
  185. expect(_getAllFlags({ flagStorage: { a: 1, b: 1, c: 1 } }, null)).to.eql(['a', 'b', 'c'])
  186. })
  187. it('should union keys of both objects', () => {
  188. expect(_getAllFlags({ flagStorage: { a: 1, b: 1, c: 1 } }, { flagStorage: { c: 1, d: 1 } })).to.eql(['a', 'b', 'c', 'd'])
  189. })
  190. })
  191. describe('_mergeFlags', () => {
  192. it('should handle merge two flag sets correctly picking higher numbers', () => {
  193. expect(
  194. _mergeFlags(
  195. { flagStorage: { a: 0, b: 3 } },
  196. { flagStorage: { b: 1, c: 4, d: 9 } },
  197. ['a', 'b', 'c', 'd'])
  198. ).to.eql({ a: 0, b: 3, c: 4, d: 9 })
  199. })
  200. })
  201. describe('_mergePrefs', () => {
  202. it('should prefer recent and apply journal to it', () => {
  203. expect(
  204. _mergePrefs(
  205. // RECENT
  206. {
  207. simple: { a: 1, b: 0, c: true },
  208. _journal: [
  209. { path: 'simple.b', operation: 'set', args: [0], timestamp: 2 },
  210. { path: 'simple.c', operation: 'set', args: [true], timestamp: 4 }
  211. ]
  212. },
  213. // STALE
  214. {
  215. simple: { a: 1, b: 1, c: false },
  216. _journal: [
  217. { path: 'simple.a', operation: 'set', args: [1], timestamp: 1 },
  218. { path: 'simple.b', operation: 'set', args: [1], timestamp: 3 }
  219. ]
  220. }
  221. )
  222. ).to.eql({
  223. simple: { a: 1, b: 1, c: true },
  224. _journal: [
  225. { path: 'simple.a', operation: 'set', args: [1], timestamp: 1 },
  226. { path: 'simple.b', operation: 'set', args: [1], timestamp: 3 },
  227. { path: 'simple.c', operation: 'set', args: [true], timestamp: 4 }
  228. ]
  229. })
  230. })
  231. it('should allow setting falsy values', () => {
  232. expect(
  233. _mergePrefs(
  234. // RECENT
  235. {
  236. simple: { a: 1, b: 0, c: false },
  237. _journal: [
  238. { path: 'simple.b', operation: 'set', args: [0], timestamp: 2 },
  239. { path: 'simple.c', operation: 'set', args: [false], timestamp: 4 }
  240. ]
  241. },
  242. // STALE
  243. {
  244. simple: { a: 0, b: 0, c: true },
  245. _journal: [
  246. { path: 'simple.a', operation: 'set', args: [0], timestamp: 1 },
  247. { path: 'simple.b', operation: 'set', args: [0], timestamp: 3 }
  248. ]
  249. }
  250. )
  251. ).to.eql({
  252. simple: { a: 0, b: 0, c: false },
  253. _journal: [
  254. { path: 'simple.a', operation: 'set', args: [0], timestamp: 1 },
  255. { path: 'simple.b', operation: 'set', args: [0], timestamp: 3 },
  256. { path: 'simple.c', operation: 'set', args: [false], timestamp: 4 }
  257. ]
  258. })
  259. })
  260. it('should work with strings', () => {
  261. expect(
  262. _mergePrefs(
  263. // RECENT
  264. {
  265. simple: { a: 'foo' },
  266. _journal: [
  267. { path: 'simple.a', operation: 'set', args: ['foo'], timestamp: 2 }
  268. ]
  269. },
  270. // STALE
  271. {
  272. simple: { a: 'bar' },
  273. _journal: [
  274. { path: 'simple.a', operation: 'set', args: ['bar'], timestamp: 4 }
  275. ]
  276. }
  277. )
  278. ).to.eql({
  279. simple: { a: 'bar' },
  280. _journal: [
  281. { path: 'simple.a', operation: 'set', args: ['bar'], timestamp: 4 }
  282. ]
  283. })
  284. })
  285. })
  286. describe('_resetFlags', () => {
  287. it('should reset all known flags to 0 when reset flag is set to > 0 and < 9000', () => {
  288. const totalFlags = { a: 0, b: 3, reset: 1 }
  289. expect(_resetFlags(totalFlags)).to.eql({ a: 0, b: 0, reset: 0 })
  290. })
  291. it('should trim all flags to known when reset is set to 1000', () => {
  292. const totalFlags = { a: 0, b: 3, c: 33, reset: COMMAND_TRIM_FLAGS }
  293. expect(_resetFlags(totalFlags, { a: 0, b: 0, reset: 0 })).to.eql({ a: 0, b: 3, reset: 0 })
  294. })
  295. it('should trim all flags to known and reset when reset is set to 1001', () => {
  296. const totalFlags = { a: 0, b: 3, c: 33, reset: COMMAND_TRIM_FLAGS_AND_RESET }
  297. expect(_resetFlags(totalFlags, { a: 0, b: 0, reset: 0 })).to.eql({ a: 0, b: 0, reset: 0 })
  298. })
  299. })
  300. })
  301. })