@hyper-fetch/core 9.6.1 → 9.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/__tests__/features/adapter/adapter.bindings.progress.spec.ts +120 -0
  2. package/__tests__/features/adapter/adapter.bindings.spec.ts +4 -6
  3. package/__tests__/features/adapter/adapter.defaults.spec.ts +87 -0
  4. package/__tests__/features/adapter/adapter.mapping.spec.ts +257 -0
  5. package/__tests__/features/cache/cache.lazy-storage.clear.spec.ts +145 -0
  6. package/__tests__/features/cache/cache.lazy-storage.resilience.spec.ts +667 -0
  7. package/__tests__/features/cache/cache.scoped-events.spec.ts +183 -0
  8. package/__tests__/features/cache/cache.semantics.spec.ts +785 -0
  9. package/__tests__/features/cache/cache.structural-sharing.spec.ts +565 -0
  10. package/__tests__/features/cache/cache.timing.spec.ts +448 -0
  11. package/__tests__/features/client/client.clear.spec.ts +313 -0
  12. package/__tests__/features/client/client.configuration.spec.ts +416 -0
  13. package/__tests__/features/client/client.hydration.round-trip.spec.ts +324 -0
  14. package/__tests__/features/client/client.interceptor.spec.ts +54 -45
  15. package/__tests__/features/client/client.interceptors.pipeline.spec.ts +599 -0
  16. package/__tests__/features/client/client.user-errors.spec.ts +220 -0
  17. package/__tests__/features/dispatcher/dispatcher.cleanup.spec.ts +534 -0
  18. package/__tests__/features/dispatcher/dispatcher.control.spec.ts +381 -0
  19. package/__tests__/features/dispatcher/dispatcher.modes.spec.ts +527 -0
  20. package/__tests__/features/dispatcher/dispatcher.offline.spec.ts +278 -0
  21. package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +35 -12
  22. package/__tests__/features/dispatcher/dispatcher.reentrancy.spec.ts +309 -0
  23. package/__tests__/features/dispatcher/dispatcher.response-order.spec.ts +241 -0
  24. package/__tests__/features/dispatcher/dispatcher.retry-flow.spec.ts +554 -0
  25. package/__tests__/features/dispatcher/dispatcher.retry-removal.spec.ts +196 -0
  26. package/__tests__/features/dispatcher/dispatcher.retry.spec.ts +4 -3
  27. package/__tests__/features/http-adapter/http-adapter.e2e.spec.ts +917 -0
  28. package/__tests__/features/managers/app/app.manager.events.spec.ts +434 -0
  29. package/__tests__/features/managers/logger/logger.levels.spec.ts +199 -0
  30. package/__tests__/features/managers/request/request.manager.abort.spec.ts +449 -0
  31. package/__tests__/features/mocker/mocker.base.spec.ts +22 -3
  32. package/__tests__/features/mocker/mocker.errors.spec.ts +99 -0
  33. package/__tests__/features/mocker/mocker.outcomes.spec.ts +308 -0
  34. package/__tests__/features/optimistic/optimistic.concurrency.spec.ts +278 -0
  35. package/__tests__/features/optimistic/optimistic.helpers.spec.ts +307 -0
  36. package/__tests__/features/optimistic/optimistic.matrix.spec.ts +315 -0
  37. package/__tests__/features/optimistic/optimistic.races.spec.ts +270 -0
  38. package/__tests__/features/optimistic/optimistic.random.spec.ts +191 -0
  39. package/__tests__/features/optimistic/optimistic.rollback.spec.ts +1001 -0
  40. package/__tests__/features/optimistic/optimistic.typescript.spec.ts +196 -0
  41. package/__tests__/features/plugin/plugin.lifecycle.spec.ts +457 -0
  42. package/__tests__/features/request/request.async-mapper-removal.spec.ts +138 -0
  43. package/__tests__/features/request/request.immutability.spec.ts +259 -0
  44. package/__tests__/features/request/request.inference.spec.ts +269 -0
  45. package/__tests__/features/request/request.lifecycle.spec.ts +682 -0
  46. package/__tests__/features/request/request.optimistic-settlement.spec.ts +35 -17
  47. package/__tests__/features/request/request.optimistic.spec.ts +12 -1
  48. package/__tests__/features/request/request.values.spec.ts +408 -0
  49. package/__tests__/features/sdk/sdk.builder.spec.ts +304 -0
  50. package/__tests__/features/utils/emitter.listeners.spec.ts +202 -0
  51. package/__tests__/utils/controlled-adapter.utils.ts +119 -0
  52. package/__tests__/utils/fixtures.utils.ts +193 -0
  53. package/__tests__/utils/optimistic.utils.ts +124 -0
  54. package/dist/adapter/adapter.bindings.d.ts.map +1 -1
  55. package/dist/adapter/adapter.utils.d.ts.map +1 -1
  56. package/dist/cache/cache.d.ts +12 -1
  57. package/dist/cache/cache.d.ts.map +1 -1
  58. package/dist/cache/cache.events.d.ts +20 -6
  59. package/dist/cache/cache.events.d.ts.map +1 -1
  60. package/dist/cache/cache.utils.d.ts +1 -0
  61. package/dist/cache/cache.utils.d.ts.map +1 -1
  62. package/dist/client/client.d.ts +3 -3
  63. package/dist/client/client.d.ts.map +1 -1
  64. package/dist/client/client.utils.d.ts.map +1 -1
  65. package/dist/dispatcher/dispatcher.d.ts +26 -5
  66. package/dist/dispatcher/dispatcher.d.ts.map +1 -1
  67. package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
  68. package/dist/index.d.ts +1 -0
  69. package/dist/index.d.ts.map +1 -1
  70. package/dist/index.mjs +2787 -1893
  71. package/dist/index.mjs.map +1 -1
  72. package/dist/managers/app/app.manager.constants.d.ts.map +1 -1
  73. package/dist/managers/app/app.manager.d.ts +9 -0
  74. package/dist/managers/app/app.manager.d.ts.map +1 -1
  75. package/dist/managers/app/app.manager.utils.d.ts.map +1 -1
  76. package/dist/managers/logger/logger.manager.d.ts.map +1 -1
  77. package/dist/managers/request/request.manager.d.ts.map +1 -1
  78. package/dist/managers/request/request.manager.types.d.ts +2 -0
  79. package/dist/managers/request/request.manager.types.d.ts.map +1 -1
  80. package/dist/mocker/mocker.d.ts +1 -1
  81. package/dist/mocker/mocker.d.ts.map +1 -1
  82. package/dist/optimistic/index.d.ts +5 -0
  83. package/dist/optimistic/index.d.ts.map +1 -0
  84. package/dist/optimistic/optimistic.d.ts +67 -0
  85. package/dist/optimistic/optimistic.d.ts.map +1 -0
  86. package/dist/optimistic/optimistic.settlement.d.ts +60 -0
  87. package/dist/optimistic/optimistic.settlement.d.ts.map +1 -0
  88. package/dist/optimistic/optimistic.types.d.ts +109 -0
  89. package/dist/optimistic/optimistic.types.d.ts.map +1 -0
  90. package/dist/optimistic/optimistic.utils.d.ts +29 -0
  91. package/dist/optimistic/optimistic.utils.d.ts.map +1 -0
  92. package/dist/plugin/plugin.d.ts +2 -0
  93. package/dist/plugin/plugin.d.ts.map +1 -1
  94. package/dist/request/request.d.ts +14 -6
  95. package/dist/request/request.d.ts.map +1 -1
  96. package/dist/request/request.types.d.ts +40 -1
  97. package/dist/request/request.types.d.ts.map +1 -1
  98. package/dist/request/request.utils.d.ts.map +1 -1
  99. package/dist/utils/emitter.utils.d.ts.map +1 -1
  100. package/dist/utils/error.utils.d.ts +17 -0
  101. package/dist/utils/error.utils.d.ts.map +1 -0
  102. package/dist/utils/index.d.ts +1 -0
  103. package/dist/utils/index.d.ts.map +1 -1
  104. package/package.json +1 -1
  105. package/src/adapter/adapter.bindings.ts +46 -18
  106. package/src/adapter/adapter.utils.ts +47 -7
  107. package/src/cache/cache.events.ts +24 -8
  108. package/src/cache/cache.ts +110 -21
  109. package/src/cache/cache.utils.ts +33 -10
  110. package/src/client/client.ts +101 -7
  111. package/src/client/client.utils.ts +10 -4
  112. package/src/dispatcher/dispatcher.ts +285 -132
  113. package/src/http-adapter/http-adapter.fetch.ts +15 -3
  114. package/src/index.ts +4 -0
  115. package/src/managers/app/app.manager.constants.ts +12 -1
  116. package/src/managers/app/app.manager.ts +43 -23
  117. package/src/managers/app/app.manager.utils.ts +8 -4
  118. package/src/managers/logger/logger.manager.ts +21 -8
  119. package/src/managers/request/request.manager.ts +7 -1
  120. package/src/managers/request/request.manager.types.ts +2 -0
  121. package/src/mocker/mocker.ts +14 -11
  122. package/src/optimistic/index.ts +4 -0
  123. package/src/optimistic/optimistic.settlement.ts +406 -0
  124. package/src/optimistic/optimistic.ts +162 -0
  125. package/src/optimistic/optimistic.types.ts +175 -0
  126. package/src/optimistic/optimistic.utils.ts +112 -0
  127. package/src/plugin/plugin.ts +6 -0
  128. package/src/request/request.ts +50 -28
  129. package/src/request/request.types.ts +41 -1
  130. package/src/request/request.utils.ts +51 -66
  131. package/src/utils/emitter.utils.ts +9 -2
  132. package/src/utils/error.utils.ts +29 -0
  133. package/src/utils/index.ts +1 -0
  134. package/tsconfig.json +2 -0
@@ -0,0 +1,448 @@
1
+ import { Cache } from "cache";
2
+ import { Client } from "client";
3
+ import { Time } from "constants/time.constants";
4
+ import { Plugin } from "plugin";
5
+ import { scopeKey } from "request";
6
+
7
+ import { createCacheResponse, createCacheValue } from "../../utils/fixtures.utils";
8
+
9
+ describe("Cache [ Timing ]", () => {
10
+ const cacheTime = 1000;
11
+
12
+ let client = new Client({ url: "shared-base-url" });
13
+ let request = client.createRequest<{ response: any }>()({ endpoint: "/users", cacheTime });
14
+
15
+ beforeEach(() => {
16
+ vi.useFakeTimers();
17
+ vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
18
+ client = new Client({ url: "shared-base-url" });
19
+ request = client.createRequest<{ response: any }>()({ endpoint: "/users", cacheTime });
20
+ });
21
+
22
+ afterEach(() => {
23
+ client.cache.clear();
24
+ vi.useRealTimers();
25
+ });
26
+
27
+ describe("When cache time passes", () => {
28
+ it("should keep the element just before the cache time passes", () => {
29
+ client.cache.set(request, createCacheResponse({ id: 1 }));
30
+
31
+ vi.advanceTimersByTime(cacheTime - 1);
32
+
33
+ expect(client.cache.get(request.cacheKey)?.data).toStrictEqual({ id: 1 });
34
+ });
35
+
36
+ it("should remove the element exactly when the cache time passes", () => {
37
+ client.cache.set(request, createCacheResponse({ id: 1 }));
38
+
39
+ vi.advanceTimersByTime(cacheTime);
40
+
41
+ expect(client.cache.get(request.cacheKey)).toBeUndefined();
42
+ expect(client.cache.keys()).toHaveLength(0);
43
+ });
44
+
45
+ it("should notify the listeners and plugins about the collected element", () => {
46
+ const deleteSpy = vi.fn();
47
+ const deleteByKeySpy = vi.fn();
48
+ const pluginSpy = vi.fn();
49
+ client.addPlugin(new Plugin({ name: "delete" }).onCacheItemDelete(pluginSpy));
50
+ client.cache.events.onDelete(deleteSpy);
51
+ client.cache.events.onDeleteByKey(request.cacheKey, deleteByKeySpy);
52
+ client.cache.set(request, createCacheResponse({ id: 1 }));
53
+
54
+ vi.advanceTimersByTime(cacheTime);
55
+
56
+ expect(deleteSpy).toHaveBeenCalledTimes(1);
57
+ expect(deleteSpy).toHaveBeenCalledWith(request.cacheKey);
58
+ expect(deleteByKeySpy).toHaveBeenCalledTimes(1);
59
+ expect(pluginSpy).toHaveBeenCalledTimes(1);
60
+ expect(pluginSpy).toHaveBeenCalledWith({ cache: client.cache, cacheKey: request.cacheKey });
61
+ });
62
+
63
+ it("should collect the element even when it has active listeners", () => {
64
+ const dataSpy = vi.fn();
65
+ client.cache.events.onDataByKey(request.cacheKey, dataSpy);
66
+ client.cache.set(request, createCacheResponse({ id: 1 }));
67
+
68
+ vi.advanceTimersByTime(cacheTime);
69
+
70
+ expect(client.cache.get(request.cacheKey)).toBeUndefined();
71
+ expect(dataSpy).toHaveBeenCalledTimes(1);
72
+ });
73
+
74
+ it("should count the cache time from the response timestamp", () => {
75
+ client.cache.set(request, createCacheResponse({ id: 1 }, { responseTimestamp: Date.now() - 400 }));
76
+
77
+ vi.advanceTimersByTime(cacheTime - 401);
78
+ expect(client.cache.get(request.cacheKey)).toBeDefined();
79
+
80
+ vi.advanceTimersByTime(1);
81
+ expect(client.cache.get(request.cacheKey)).toBeUndefined();
82
+ });
83
+
84
+ it("should remove the element right away when the response is already outdated", () => {
85
+ const spy = vi.fn();
86
+ client.cache.events.onDataByKey(request.cacheKey, spy);
87
+
88
+ client.cache.set(request, createCacheResponse({ id: 1 }, { responseTimestamp: Date.now() - cacheTime - 1 }));
89
+
90
+ expect(client.cache.get(request.cacheKey)).toBeUndefined();
91
+ expect(client.cache.garbageCollectors.size).toBe(0);
92
+ expect(spy).toHaveBeenCalledTimes(1);
93
+ });
94
+
95
+ it("should keep the element with the response as old as the cache time until the timers run", () => {
96
+ client.cache.set(request, createCacheResponse({ id: 1 }, { responseTimestamp: Date.now() - cacheTime }));
97
+
98
+ expect(client.cache.get(request.cacheKey)).toBeDefined();
99
+
100
+ vi.advanceTimersByTime(0);
101
+ expect(client.cache.get(request.cacheKey)).toBeUndefined();
102
+ });
103
+
104
+ it("should emit the data and remove the element with the next timers run when the cache time is zero", () => {
105
+ const spy = vi.fn();
106
+ const zeroRequest = request.setCacheTime(0);
107
+ client.cache.events.onDataByKey(zeroRequest.cacheKey, spy);
108
+
109
+ client.cache.set(zeroRequest, createCacheResponse({ id: 1 }));
110
+
111
+ expect(spy).toHaveBeenCalledTimes(1);
112
+ expect(spy.mock.calls[0][0].data).toStrictEqual({ id: 1 });
113
+
114
+ vi.advanceTimersByTime(0);
115
+ expect(client.cache.get(zeroRequest.cacheKey)).toBeUndefined();
116
+ });
117
+
118
+ it("should collect the elements independently", () => {
119
+ const short = request.setCacheKey("short").setCacheTime(100);
120
+ const long = request.setCacheKey("long").setCacheTime(200);
121
+ client.cache.set(short, createCacheResponse("short"));
122
+ client.cache.set(long, createCacheResponse("long"));
123
+
124
+ vi.advanceTimersByTime(100);
125
+ expect(short.read()).toBeUndefined();
126
+ expect(long.read()?.data).toBe("long");
127
+
128
+ vi.advanceTimersByTime(100);
129
+ expect(long.read()).toBeUndefined();
130
+ });
131
+
132
+ it("should collect the scoped element", () => {
133
+ const scoped = request.setScope("user-1");
134
+ const storageKey = scopeKey(scoped.cacheKey, "user-1");
135
+ const spy = vi.fn();
136
+ client.cache.events.onDelete(spy);
137
+ client.cache.set(scoped, createCacheResponse({ id: 1 }));
138
+
139
+ vi.advanceTimersByTime(cacheTime);
140
+
141
+ expect(client.cache.get(storageKey)).toBeUndefined();
142
+ expect(spy).toHaveBeenCalledWith(storageKey);
143
+ });
144
+
145
+ it("should forget the collector once the element is collected", () => {
146
+ client.cache.set(request, createCacheResponse({ id: 1 }));
147
+ expect(client.cache.garbageCollectors.size).toBe(1);
148
+
149
+ vi.advanceTimersByTime(cacheTime);
150
+
151
+ expect(client.cache.garbageCollectors.size).toBe(0);
152
+ expect(vi.getTimerCount()).toBe(0);
153
+ });
154
+ });
155
+
156
+ describe("When the element changes before it is collected", () => {
157
+ it("should count the cache time from the latest response", () => {
158
+ client.cache.set(request, createCacheResponse({ id: 1 }));
159
+ vi.advanceTimersByTime(600);
160
+ client.cache.set(request, createCacheResponse({ id: 2 }));
161
+
162
+ vi.advanceTimersByTime(cacheTime - 1);
163
+ expect(client.cache.get(request.cacheKey)?.data).toStrictEqual({ id: 2 });
164
+
165
+ vi.advanceTimersByTime(1);
166
+ expect(client.cache.get(request.cacheKey)).toBeUndefined();
167
+ });
168
+
169
+ it("should keep a single collector for the element", () => {
170
+ client.cache.set(request, createCacheResponse({ id: 1 }));
171
+ client.cache.set(request, createCacheResponse({ id: 2 }));
172
+ client.cache.set(request, createCacheResponse({ id: 3 }));
173
+
174
+ expect(client.cache.garbageCollectors.size).toBe(1);
175
+ expect(vi.getTimerCount()).toBe(1);
176
+ });
177
+
178
+ it("should use the cache time of the latest request", () => {
179
+ client.cache.set(request, createCacheResponse({ id: 1 }));
180
+ client.cache.set(request.setCacheTime(5000), createCacheResponse({ id: 2 }));
181
+
182
+ vi.advanceTimersByTime(4999);
183
+ expect(client.cache.get(request.cacheKey)).toBeDefined();
184
+
185
+ vi.advanceTimersByTime(1);
186
+ expect(client.cache.get(request.cacheKey)).toBeUndefined();
187
+ });
188
+
189
+ it("should not extend the cache time with the update which keeps the response timestamp", () => {
190
+ client.cache.set(request, createCacheResponse({ id: 1 }));
191
+ vi.advanceTimersByTime(600);
192
+ client.cache.update(request, { data: { id: 2 } });
193
+
194
+ vi.advanceTimersByTime(399);
195
+ expect(client.cache.get(request.cacheKey)?.data).toStrictEqual({ id: 2 });
196
+
197
+ vi.advanceTimersByTime(1);
198
+ expect(client.cache.get(request.cacheKey)).toBeUndefined();
199
+ });
200
+
201
+ it("should not extend the cache time with the invalidation", () => {
202
+ client.cache.set(request, createCacheResponse({ id: 1 }));
203
+ vi.advanceTimersByTime(600);
204
+ client.cache.invalidate(request.cacheKey);
205
+
206
+ vi.advanceTimersByTime(400);
207
+
208
+ expect(client.cache.get(request.cacheKey)).toBeUndefined();
209
+ });
210
+
211
+ it("should not emit the deletion again when the element was deleted manually", () => {
212
+ const spy = vi.fn();
213
+ client.cache.events.onDelete(spy);
214
+ client.cache.set(request, createCacheResponse({ id: 1 }));
215
+
216
+ client.cache.delete(request.cacheKey);
217
+ vi.advanceTimersByTime(cacheTime * 2);
218
+
219
+ expect(spy).toHaveBeenCalledTimes(1);
220
+ });
221
+
222
+ it("should stop the collector of the element deleted manually", () => {
223
+ client.cache.set(request, createCacheResponse({ id: 1 }));
224
+
225
+ client.cache.delete(request.cacheKey);
226
+
227
+ expect(client.cache.garbageCollectors.size).toBe(0);
228
+ expect(vi.getTimerCount()).toBe(0);
229
+ });
230
+
231
+ it("should collect the element set again after the manual deletion with its own time", () => {
232
+ client.cache.set(request, createCacheResponse({ id: 1 }));
233
+ vi.advanceTimersByTime(600);
234
+ client.cache.delete(request.cacheKey);
235
+ client.cache.set(request, createCacheResponse({ id: 2 }));
236
+
237
+ vi.advanceTimersByTime(cacheTime - 1);
238
+ expect(client.cache.get(request.cacheKey)?.data).toStrictEqual({ id: 2 });
239
+
240
+ vi.advanceTimersByTime(1);
241
+ expect(client.cache.get(request.cacheKey)).toBeUndefined();
242
+ });
243
+ });
244
+
245
+ describe("When cache time is infinite or very long", () => {
246
+ it("should never collect the element with the infinite cache time", () => {
247
+ const infinite = request.setCacheTime(Infinity);
248
+ client.cache.set(infinite, createCacheResponse({ id: 1 }));
249
+
250
+ vi.advanceTimersByTime(Time.YEAR);
251
+
252
+ expect(client.cache.get(infinite.cacheKey)?.data).toStrictEqual({ id: 1 });
253
+ expect(vi.getTimerCount()).toBe(0);
254
+ });
255
+
256
+ it("should never collect the element with the cache time serialized to null", () => {
257
+ const storage = new Map<any, any>([
258
+ ["serialized", createCacheValue({ cacheKey: "serialized", cacheTime: null as any })],
259
+ ]);
260
+ const cache = new Cache({ storage }).initialize(client);
261
+
262
+ vi.advanceTimersByTime(Time.YEAR);
263
+
264
+ expect(cache.get("serialized")).toBeDefined();
265
+ expect(cache.garbageCollectors.size).toBe(0);
266
+ });
267
+
268
+ it.each([
269
+ { name: "30 days", time: Time.MONTH_30 },
270
+ { name: "a year", time: Time.YEAR },
271
+ ])("should not collect the element with the cache time of $name right away", ({ time }) => {
272
+ const longRequest = request.setCacheTime(time);
273
+ client.cache.set(longRequest, createCacheResponse({ id: 1 }));
274
+
275
+ vi.advanceTimersByTime(Time.DAY);
276
+
277
+ expect(client.cache.get(longRequest.cacheKey)?.data).toStrictEqual({ id: 1 });
278
+ });
279
+
280
+ it("should collect the element with the cache time longer than the timers limit exactly on time", () => {
281
+ const longRequest = request.setCacheTime(Time.MONTH_30);
282
+ client.cache.set(longRequest, createCacheResponse({ id: 1 }));
283
+
284
+ vi.advanceTimersByTime(Time.MONTH_30 - 1);
285
+ expect(client.cache.get(longRequest.cacheKey)).toBeDefined();
286
+
287
+ vi.advanceTimersByTime(1);
288
+ expect(client.cache.get(longRequest.cacheKey)).toBeUndefined();
289
+ });
290
+
291
+ it("should keep a single collector while it waits for the very long cache time", () => {
292
+ const longRequest = request.setCacheTime(Time.YEAR);
293
+ client.cache.set(longRequest, createCacheResponse({ id: 1 }));
294
+
295
+ vi.advanceTimersByTime(Time.MONTH_31 * 3);
296
+
297
+ expect(client.cache.garbageCollectors.size).toBe(1);
298
+ expect(vi.getTimerCount()).toBe(1);
299
+ });
300
+
301
+ it("should collect the element with the cache time equal to the timers limit", () => {
302
+ const limit = 2 ** 31 - 1;
303
+ const limitRequest = request.setCacheTime(limit);
304
+ client.cache.set(limitRequest, createCacheResponse({ id: 1 }));
305
+
306
+ vi.advanceTimersByTime(limit - 1);
307
+ expect(client.cache.get(limitRequest.cacheKey)).toBeDefined();
308
+
309
+ vi.advanceTimersByTime(1);
310
+ expect(client.cache.get(limitRequest.cacheKey)).toBeUndefined();
311
+ });
312
+ });
313
+
314
+ describe("When application is offline", () => {
315
+ it("should keep the outdated element while offline", () => {
316
+ client.cache.set(request, createCacheResponse({ id: 1 }));
317
+ client.appManager.setOnline(false);
318
+
319
+ vi.advanceTimersByTime(cacheTime * 10);
320
+
321
+ expect(client.cache.get(request.cacheKey)?.data).toStrictEqual({ id: 1 });
322
+ });
323
+
324
+ it("should collect the outdated element once the application is back online", () => {
325
+ const spy = vi.fn();
326
+ client.cache.events.onDelete(spy);
327
+ client.cache.set(request, createCacheResponse({ id: 1 }));
328
+ client.appManager.setOnline(false);
329
+ vi.advanceTimersByTime(cacheTime * 10);
330
+
331
+ client.appManager.setOnline(true);
332
+
333
+ expect(client.cache.get(request.cacheKey)).toBeUndefined();
334
+ expect(spy).toHaveBeenCalledTimes(1);
335
+ });
336
+
337
+ it("should keep the element set while offline even if it is already outdated", () => {
338
+ client.appManager.setOnline(false);
339
+
340
+ client.cache.set(request, createCacheResponse({ id: 1 }, { responseTimestamp: Date.now() - cacheTime * 2 }));
341
+
342
+ expect(client.cache.get(request.cacheKey)).toBeDefined();
343
+ });
344
+
345
+ it("should keep counting the cache time of the elements which are still valid after going back online", () => {
346
+ client.cache.set(request, createCacheResponse({ id: 1 }));
347
+ client.appManager.setOnline(false);
348
+ vi.advanceTimersByTime(400);
349
+
350
+ client.appManager.setOnline(true);
351
+ expect(client.cache.get(request.cacheKey)).toBeDefined();
352
+ expect(vi.getTimerCount()).toBe(1);
353
+
354
+ vi.advanceTimersByTime(599);
355
+ expect(client.cache.get(request.cacheKey)).toBeDefined();
356
+
357
+ vi.advanceTimersByTime(1);
358
+ expect(client.cache.get(request.cacheKey)).toBeUndefined();
359
+ });
360
+
361
+ it("should not schedule the collectors twice when the online event repeats", () => {
362
+ client.cache.set(request, createCacheResponse({ id: 1 }));
363
+
364
+ client.appManager.setOnline(true);
365
+ client.appManager.setOnline(true);
366
+ client.appManager.setOnline(true);
367
+
368
+ expect(vi.getTimerCount()).toBe(1);
369
+ });
370
+ });
371
+
372
+ describe("When cache is initialized with the stored elements", () => {
373
+ it("should schedule the collection of the stored elements", () => {
374
+ const storage = new Map<any, any>([
375
+ ["stored", createCacheValue({ cacheKey: "stored", cacheTime, responseTimestamp: Date.now() - 300 })],
376
+ ]);
377
+ const cache = new Cache({ storage }).initialize(client);
378
+
379
+ vi.advanceTimersByTime(699);
380
+ expect(cache.get("stored")).toBeDefined();
381
+
382
+ vi.advanceTimersByTime(1);
383
+ expect(cache.get("stored")).toBeUndefined();
384
+ });
385
+
386
+ it("should remove the outdated stored elements right away", () => {
387
+ const storage = new Map<any, any>([
388
+ [
389
+ "outdated",
390
+ createCacheValue({ cacheKey: "outdated", cacheTime, responseTimestamp: Date.now() - cacheTime - 1 }),
391
+ ],
392
+ ["valid", createCacheValue({ cacheKey: "valid", cacheTime, responseTimestamp: Date.now() })],
393
+ ]);
394
+
395
+ const cache = new Cache({ storage }).initialize(client);
396
+
397
+ expect(cache.keys()).toStrictEqual(["valid"]);
398
+ });
399
+
400
+ it("should remove the stored elements without the cache time", () => {
401
+ const storage = new Map<any, any>([
402
+ ["broken", createCacheValue({ cacheKey: "broken", cacheTime: undefined as any })],
403
+ ]);
404
+
405
+ const cache = new Cache({ storage }).initialize(client);
406
+
407
+ expect(cache.keys()).toHaveLength(0);
408
+ });
409
+ });
410
+
411
+ describe("When cache gets cleared", () => {
412
+ it("should stop every collector", async () => {
413
+ client.cache.set(request.setCacheKey("first"), createCacheResponse(1));
414
+ client.cache.set(request.setCacheKey("second"), createCacheResponse(2));
415
+ expect(vi.getTimerCount()).toBe(2);
416
+
417
+ await client.cache.clear();
418
+
419
+ expect(vi.getTimerCount()).toBe(0);
420
+ expect(client.cache.garbageCollectors.size).toBe(0);
421
+ });
422
+
423
+ it("should not emit the deletions of the cleared elements later", async () => {
424
+ const spy = vi.fn();
425
+ client.cache.events.onDelete(spy);
426
+ client.cache.set(request, createCacheResponse(1));
427
+
428
+ await client.cache.clear();
429
+ vi.advanceTimersByTime(cacheTime * 2);
430
+
431
+ expect(spy).not.toHaveBeenCalled();
432
+ });
433
+
434
+ it("should stop the collectors when the client is cleared", () => {
435
+ const previousCache = client.cache;
436
+ const spy = vi.fn();
437
+ previousCache.events.onDelete(spy);
438
+ client.cache.set(request, createCacheResponse(1));
439
+
440
+ client.clear();
441
+ vi.advanceTimersByTime(cacheTime * 2);
442
+
443
+ expect(previousCache.garbageCollectors.size).toBe(0);
444
+ expect(vi.getTimerCount()).toBe(0);
445
+ expect(spy).not.toHaveBeenCalled();
446
+ });
447
+ });
448
+ });