@forge/cache 1.0.2 → 1.0.3-next.0-experimental-ab129b0

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 (61) hide show
  1. package/out/cache.d.ts +4 -1
  2. package/out/cache.d.ts.map +1 -1
  3. package/out/cache.js +11 -0
  4. package/out/kvs/conditions.d.ts +38 -0
  5. package/out/kvs/conditions.d.ts.map +1 -0
  6. package/out/kvs/conditions.js +98 -0
  7. package/out/kvs/entity-query.d.ts +26 -0
  8. package/out/kvs/entity-query.d.ts.map +1 -0
  9. package/out/kvs/entity-query.js +102 -0
  10. package/out/kvs/entity.d.ts +13 -0
  11. package/out/kvs/entity.d.ts.map +1 -0
  12. package/out/kvs/entity.js +35 -0
  13. package/out/kvs/errors.d.ts +21 -0
  14. package/out/kvs/errors.d.ts.map +1 -0
  15. package/out/kvs/errors.js +26 -0
  16. package/out/kvs/index.d.ts +10 -0
  17. package/out/kvs/index.d.ts.map +1 -0
  18. package/out/kvs/index.js +42 -0
  19. package/out/kvs/interfaces/entity-query.d.ts +41 -0
  20. package/out/kvs/interfaces/entity-query.d.ts.map +1 -0
  21. package/out/kvs/interfaces/entity-query.js +2 -0
  22. package/out/kvs/interfaces/kvs-api.d.ts +95 -0
  23. package/out/kvs/interfaces/kvs-api.d.ts.map +1 -0
  24. package/out/kvs/interfaces/kvs-api.js +2 -0
  25. package/out/kvs/interfaces/kvs.d.ts +21 -0
  26. package/out/kvs/interfaces/kvs.d.ts.map +1 -0
  27. package/out/kvs/interfaces/kvs.js +2 -0
  28. package/out/kvs/interfaces/query.d.ts +17 -0
  29. package/out/kvs/interfaces/query.d.ts.map +1 -0
  30. package/out/kvs/interfaces/query.js +2 -0
  31. package/out/kvs/interfaces/transaction.d.ts +29 -0
  32. package/out/kvs/interfaces/transaction.d.ts.map +1 -0
  33. package/out/kvs/interfaces/transaction.js +2 -0
  34. package/out/kvs/interfaces/types.d.ts +64 -0
  35. package/out/kvs/interfaces/types.d.ts.map +1 -0
  36. package/out/kvs/interfaces/types.js +8 -0
  37. package/out/kvs/kvs.d.ts +18 -0
  38. package/out/kvs/kvs.d.ts.map +1 -0
  39. package/out/kvs/kvs.js +40 -0
  40. package/out/kvs/query.d.ts +14 -0
  41. package/out/kvs/query.d.ts.map +1 -0
  42. package/out/kvs/query.js +38 -0
  43. package/out/kvs/storage-api.d.ts +22 -0
  44. package/out/kvs/storage-api.d.ts.map +1 -0
  45. package/out/kvs/storage-api.js +103 -0
  46. package/out/kvs/transaction-api.d.ts +14 -0
  47. package/out/kvs/transaction-api.d.ts.map +1 -0
  48. package/out/kvs/transaction-api.js +66 -0
  49. package/out/kvs/utils/error-handling.d.ts +7 -0
  50. package/out/kvs/utils/error-handling.d.ts.map +1 -0
  51. package/out/kvs/utils/error-handling.js +42 -0
  52. package/out/kvs/utils/transaction-request-builder.d.ts +6 -0
  53. package/out/kvs/utils/transaction-request-builder.d.ts.map +1 -0
  54. package/out/kvs/utils/transaction-request-builder.js +49 -0
  55. package/package.json +2 -2
  56. package/out/__test__/cache.test.d.ts +0 -2
  57. package/out/__test__/cache.test.d.ts.map +0 -1
  58. package/out/__test__/cache.test.js +0 -358
  59. package/out/__test__/tunnel.test.d.ts +0 -2
  60. package/out/__test__/tunnel.test.d.ts.map +0 -1
  61. package/out/__test__/tunnel.test.js +0 -176
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forge/cache",
3
- "version": "1.0.2",
3
+ "version": "1.0.3-next.0-experimental-ab129b0",
4
4
  "description": "Forge Cache methods",
5
5
  "author": "Atlassian",
6
6
  "license": "SEE LICENSE IN LICENSE.txt",
@@ -21,7 +21,7 @@
21
21
  "tmp": "^0.2.3"
22
22
  },
23
23
  "dependencies": {
24
- "@forge/api": "^6.0.2",
24
+ "@forge/api": "^6.0.3-next.0-experimental-ab129b0",
25
25
  "agentkeepalive": "^4.5.0",
26
26
  "node-fetch": "2.7.0",
27
27
  "object-sizeof": "^2.6.5"
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=cache.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"cache.test.d.ts","sourceRoot":"","sources":["../../src/__test__/cache.test.ts"],"names":[],"mappings":""}
@@ -1,358 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
5
- const cache_1 = require("../cache");
6
- jest.mock('node-fetch');
7
- const fetchMock = node_fetch_1.default;
8
- describe('Forge Cache', () => {
9
- beforeEach(() => {
10
- global['__forge_runtime__'] = {
11
- proxy: {
12
- token: 'token',
13
- url: 'https://proxy.atlassian.com'
14
- },
15
- rms: {
16
- url: 'https://dev.services.atlassian.com',
17
- host: 'rockmelon-storage.dev.atl-paas.net'
18
- },
19
- tracing: {
20
- traceId: 'trace-id-test',
21
- spanId: 'span-id-test'
22
- }
23
- };
24
- });
25
- it('chooses the v2 fetch based on runtime', async () => {
26
- const cacheClient = (0, cache_1.connect)();
27
- await cacheClient['client']('asdf');
28
- expect(fetchMock).toHaveBeenCalled();
29
- });
30
- });
31
- describe('createFetch', () => {
32
- it('fetch fails when rms config is not available', async () => {
33
- global['__forge_runtime__'] = {
34
- proxy: {
35
- token: 'token',
36
- url: 'https://proxy.atlassian.com'
37
- }
38
- };
39
- await expect((0, cache_1.createFetchRmsRuntimeV2)()).rejects.toThrowError(new Error('RMS config not available.'));
40
- });
41
- it('creates a fetch that adds the right headers and url', async () => {
42
- global['__forge_runtime__'] = {
43
- proxy: {
44
- token: 'token',
45
- url: 'https://proxy.atlassian.com'
46
- },
47
- rms: {
48
- url: 'https://dev.services.atlassian.com',
49
- host: 'rockmelon-storage.dev.atl-paas.net'
50
- },
51
- tracing: {
52
- traceId: 'trace-id-test',
53
- spanId: 'span-id-test'
54
- }
55
- };
56
- const fetch = (0, cache_1.createFetchRmsRuntimeV2)();
57
- await fetch('path');
58
- const [absoluteUrl, options] = fetchMock.mock.lastCall ?? ['', {}];
59
- expect(absoluteUrl.toString()).toEqual('https://rms/path');
60
- expect((options?.agent)['passthrough']['keepAlive']).toBeTruthy();
61
- expect(options?.headers).toMatchObject({
62
- Authorization: 'Bearer token',
63
- Host: 'rockmelon-storage.dev.atl-paas.net',
64
- 'x-b3-traceid': 'trace-id-test',
65
- 'x-b3-spanid': 'span-id-test'
66
- });
67
- });
68
- });
69
- describe('Cache', () => {
70
- function buildCache() {
71
- const fetch = jest.fn();
72
- const cache = new cache_1.Cache(fetch);
73
- return { cache, fetch };
74
- }
75
- describe('set', () => {
76
- it('handles success', async () => {
77
- const { cache, fetch } = buildCache();
78
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({}), status: 200 });
79
- await cache.set('key', 'value');
80
- expect(fetch.mock.lastCall).toMatchSnapshot();
81
- });
82
- it('handles success with ttl', async () => {
83
- const { cache, fetch } = buildCache();
84
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({}), status: 200 });
85
- await cache.set('key', 'value', { ttlSeconds: 100 });
86
- expect(fetch.mock.lastCall).toMatchSnapshot();
87
- });
88
- it('handles failure', async () => {
89
- const { cache, fetch } = buildCache();
90
- fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
91
- await expect(cache.set('key', 'value', { ttlSeconds: 100 })).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
92
- expect(fetch.mock.lastCall).toMatchSnapshot();
93
- });
94
- it('handles failure with error code', async () => {
95
- const { cache, fetch } = buildCache();
96
- fetch.mockResolvedValueOnce({
97
- ok: false,
98
- text: async () => JSON.stringify({ error: { code: 'NOT_ALLOWED', title: 'Invalid operation' } }),
99
- status: 400
100
- });
101
- await expect(cache.set('key', 'value', { ttlSeconds: 100 })).rejects.toMatchError(new cache_1.ApiError(400, 'NOT_ALLOWED', '"title":"Invalid operation"'));
102
- });
103
- });
104
- describe('setIfNotExists', () => {
105
- it('handles success', async () => {
106
- const { cache, fetch } = buildCache();
107
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 'OK' }), status: 200 });
108
- const result = await cache.setIfNotExists('key', 'value');
109
- expect(result).toEqual('OK');
110
- expect(fetch.mock.lastCall).toMatchSnapshot();
111
- });
112
- it('handles success when key exists', async () => {
113
- const { cache, fetch } = buildCache();
114
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: null }), status: 200 });
115
- const result = await cache.setIfNotExists('key', 'value');
116
- expect(result).toBeNull();
117
- expect(fetch.mock.lastCall).toMatchSnapshot();
118
- });
119
- it('handles success with ttl', async () => {
120
- const { cache, fetch } = buildCache();
121
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 'OK' }), status: 200 });
122
- await cache.setIfNotExists('key', 'value', { ttlSeconds: 100 });
123
- expect(fetch.mock.lastCall).toMatchSnapshot();
124
- });
125
- it('handles failure', async () => {
126
- const { cache, fetch } = buildCache();
127
- fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
128
- await expect(cache.setIfNotExists('key', 'value', { ttlSeconds: 100 })).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
129
- expect(fetch.mock.lastCall).toMatchSnapshot();
130
- });
131
- });
132
- describe('get', () => {
133
- it('handles success', async () => {
134
- const { cache, fetch } = buildCache();
135
- fetch.mockResolvedValueOnce({
136
- ok: true,
137
- text: async () => JSON.stringify({ response: 'asdfasdf' }),
138
- status: 200
139
- });
140
- const result = await cache.get('key');
141
- expect(result).toEqual('asdfasdf');
142
- expect(fetch.mock.lastCall).toMatchSnapshot();
143
- });
144
- it('handles success when key does not exist', async () => {
145
- const { cache, fetch } = buildCache();
146
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: null }), status: 200 });
147
- const result = await cache.get('key');
148
- expect(result).toBeNull();
149
- expect(fetch.mock.lastCall).toMatchSnapshot();
150
- });
151
- it('handles failure', async () => {
152
- const { cache, fetch } = buildCache();
153
- fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
154
- await expect(cache.get('key')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
155
- expect(fetch.mock.lastCall).toMatchSnapshot();
156
- });
157
- });
158
- describe('getAndSet', () => {
159
- it('handles success', async () => {
160
- const { cache, fetch } = buildCache();
161
- fetch.mockResolvedValueOnce({
162
- ok: true,
163
- text: async () => JSON.stringify({ response: 'oldValue' }),
164
- status: 200
165
- });
166
- const result = await cache.getAndSet('key', 'newValue');
167
- expect(result).toEqual('oldValue');
168
- expect(fetch.mock.lastCall).toMatchSnapshot();
169
- });
170
- it('handles success when key does not exist', async () => {
171
- const { cache, fetch } = buildCache();
172
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: null }), status: 200 });
173
- const result = await cache.getAndSet('key', 'value');
174
- expect(result).toBeNull();
175
- expect(fetch.mock.lastCall).toMatchSnapshot();
176
- });
177
- it('handles success with ttl', async () => {
178
- const { cache, fetch } = buildCache();
179
- fetch.mockResolvedValueOnce({
180
- ok: true,
181
- text: async () => JSON.stringify({ response: 'oldValue' }),
182
- status: 200
183
- });
184
- await cache.getAndSet('key', 'newValue', { ttlSeconds: 100 });
185
- expect(fetch.mock.lastCall).toMatchSnapshot();
186
- });
187
- it('handles failure', async () => {
188
- const { cache, fetch } = buildCache();
189
- fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
190
- await expect(cache.getAndSet('key', 'value', { ttlSeconds: 100 })).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
191
- expect(fetch.mock.lastCall).toMatchSnapshot();
192
- });
193
- });
194
- describe('incrementAndGet', () => {
195
- it('handles success', async () => {
196
- const { cache, fetch } = buildCache();
197
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 0 }), status: 200 });
198
- const result = await cache.incrementAndGet('key');
199
- expect(result).toEqual(0);
200
- expect(fetch.mock.lastCall).toMatchSnapshot();
201
- });
202
- it('handles success with ttl', async () => {
203
- const { cache, fetch } = buildCache();
204
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 0 }), status: 200 });
205
- const result = await cache.incrementAndGet('key', { ttlSeconds: 100 });
206
- expect(result).toEqual(0);
207
- expect(fetch.mock.lastCall).toMatchSnapshot();
208
- });
209
- it('handles success when key exists', async () => {
210
- const { cache, fetch } = buildCache();
211
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 11 }), status: 200 });
212
- const result = await cache.incrementAndGet('key');
213
- expect(result).toEqual(11);
214
- expect(fetch.mock.lastCall).toMatchSnapshot();
215
- });
216
- it('handles failure', async () => {
217
- const { cache, fetch } = buildCache();
218
- fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
219
- await expect(cache.incrementAndGet('key')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
220
- expect(fetch.mock.lastCall).toMatchSnapshot();
221
- });
222
- });
223
- describe('decrementAndGet', () => {
224
- it('handles success', async () => {
225
- const { cache, fetch } = buildCache();
226
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 0 }), status: 200 });
227
- const result = await cache.decrementAndGet('key');
228
- expect(result).toEqual(0);
229
- expect(fetch.mock.lastCall).toMatchSnapshot();
230
- });
231
- it('handles success with ttl', async () => {
232
- const { cache, fetch } = buildCache();
233
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 0 }), status: 200 });
234
- const result = await cache.decrementAndGet('key', { ttlSeconds: 100 });
235
- expect(result).toEqual(0);
236
- expect(fetch.mock.lastCall).toMatchSnapshot();
237
- });
238
- it('handles success when key exists', async () => {
239
- const { cache, fetch } = buildCache();
240
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 11 }), status: 200 });
241
- const result = await cache.decrementAndGet('key');
242
- expect(result).toEqual(11);
243
- expect(fetch.mock.lastCall).toMatchSnapshot();
244
- });
245
- it('handles failure', async () => {
246
- const { cache, fetch } = buildCache();
247
- fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
248
- await expect(cache.decrementAndGet('key')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
249
- expect(fetch.mock.lastCall).toMatchSnapshot();
250
- });
251
- });
252
- describe('delete', () => {
253
- it('handles success', async () => {
254
- const { cache, fetch } = buildCache();
255
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 1 }), status: 200 });
256
- const result = await cache.delete('key');
257
- expect(result).toEqual(1);
258
- expect(fetch.mock.lastCall).toMatchSnapshot();
259
- });
260
- it('handles failure', async () => {
261
- const { cache, fetch } = buildCache();
262
- fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
263
- await expect(cache.delete('key')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
264
- expect(fetch.mock.lastCall).toMatchSnapshot();
265
- });
266
- });
267
- describe('scan', () => {
268
- it('handles success', async () => {
269
- const { cache, fetch } = buildCache();
270
- const expectedResponse = { cursor: '0', keys: ['key1', 'key2'] };
271
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify(expectedResponse), status: 200 });
272
- const actualResponse = await cache.scan('key*');
273
- expect(actualResponse).toEqual(expectedResponse);
274
- expect(fetch.mock.lastCall).toMatchSnapshot();
275
- });
276
- it('handles success with cursor', async () => {
277
- const { cache, fetch } = buildCache();
278
- const expectedResponse = { cursor: '0', keys: ['key2'] };
279
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify(expectedResponse), status: 200 });
280
- const actualResponse = await cache.scan('key*', { cursor: '1' });
281
- expect(actualResponse).toEqual(expectedResponse);
282
- expect(fetch.mock.lastCall).toMatchSnapshot();
283
- });
284
- it('handles success with count', async () => {
285
- const { cache, fetch } = buildCache();
286
- const expectedResponse = { cursor: '0', keys: ['key1'] };
287
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify(expectedResponse), status: 200 });
288
- const actualResponse = await cache.scan('key*', { count: 1 });
289
- expect(actualResponse).toEqual(expectedResponse);
290
- expect(fetch.mock.lastCall).toMatchSnapshot();
291
- });
292
- it('handles failure', async () => {
293
- const { cache, fetch } = buildCache();
294
- fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
295
- await expect(cache.scan('key*', { cursor: '0', count: 2 })).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
296
- expect(fetch.mock.lastCall).toMatchSnapshot();
297
- });
298
- });
299
- describe('leftPush', () => {
300
- it('handles success', async () => {
301
- const { cache, fetch } = buildCache();
302
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 1 }), status: 200 });
303
- const result = await cache.leftPush('list', 'value');
304
- expect(result).toEqual(1);
305
- expect(fetch.mock.lastCall).toMatchSnapshot();
306
- });
307
- it('handles failure', async () => {
308
- const { cache, fetch } = buildCache();
309
- fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
310
- await expect(cache.leftPush('list', 'value')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
311
- expect(fetch.mock.lastCall).toMatchSnapshot();
312
- });
313
- });
314
- describe('rightPop', () => {
315
- it('handles success', async () => {
316
- const { cache, fetch } = buildCache();
317
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 'value' }), status: 200 });
318
- const result = await cache.rightPop('list');
319
- expect(result).toEqual('value');
320
- expect(fetch.mock.lastCall).toMatchSnapshot();
321
- });
322
- it('handles success when key does not exist', async () => {
323
- const { cache, fetch } = buildCache();
324
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: null }), status: 200 });
325
- const result = await cache.rightPop('list');
326
- expect(result).toBeNull();
327
- expect(fetch.mock.lastCall).toMatchSnapshot();
328
- });
329
- it('handles failure', async () => {
330
- const { cache, fetch } = buildCache();
331
- fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
332
- await expect(cache.rightPop('list')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
333
- expect(fetch.mock.lastCall).toMatchSnapshot();
334
- });
335
- });
336
- describe('listLength', () => {
337
- it('handles success', async () => {
338
- const { cache, fetch } = buildCache();
339
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 2 }), status: 200 });
340
- const result = await cache.listLength('list');
341
- expect(result).toBe(2);
342
- expect(fetch.mock.lastCall).toMatchSnapshot();
343
- });
344
- it('handles success when key does not exist', async () => {
345
- const { cache, fetch } = buildCache();
346
- fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 0 }), status: 200 });
347
- const result = await cache.listLength('list');
348
- expect(result).toBe(0);
349
- expect(fetch.mock.lastCall).toMatchSnapshot();
350
- });
351
- it('handles failure', async () => {
352
- const { cache, fetch } = buildCache();
353
- fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
354
- await expect(cache.listLength('list')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
355
- expect(fetch.mock.lastCall).toMatchSnapshot();
356
- });
357
- });
358
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=tunnel.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tunnel.test.d.ts","sourceRoot":"","sources":["../../src/__test__/tunnel.test.ts"],"names":[],"mappings":""}
@@ -1,176 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- const tunnel_1 = require("../tunnel");
5
- const validator_1 = require("../utils/validator");
6
- const fs_1 = tslib_1.__importDefault(require("fs"));
7
- const tmp_1 = require("tmp");
8
- describe('Forge Cache Tunnel', () => {
9
- let cache;
10
- beforeEach(() => {
11
- const fileName = (0, tmp_1.tmpNameSync)({ postfix: '.json' });
12
- cache = new tunnel_1.TunnelCache(fileName);
13
- });
14
- test('set method', async () => {
15
- const response = await cache.set('key', 'value');
16
- expect(response).toStrictEqual(undefined);
17
- });
18
- test('get method', async () => {
19
- await cache.set('key', 'value');
20
- const response = await cache.get('key');
21
- expect(response).toStrictEqual('value');
22
- });
23
- test('get method throws error for list value', async () => {
24
- await cache.leftPush('key', 'value1');
25
- try {
26
- await cache.get('key');
27
- }
28
- catch (error) {
29
- expect(error).toEqual(new validator_1.WrongTypeOperationError());
30
- }
31
- });
32
- test('setIfNotExists method', async () => {
33
- const response = await cache.setIfNotExists('key', 'value');
34
- expect(response).toStrictEqual('OK');
35
- const newResponse = await cache.setIfNotExists('key', 'new value');
36
- expect(newResponse).toStrictEqual(null);
37
- });
38
- test('getAndSet method', async () => {
39
- await cache.set('key', 'value');
40
- const response = await cache.getAndSet('key', 'new value');
41
- expect(response).toStrictEqual('value');
42
- const newResponse = await cache.get('key');
43
- expect(newResponse).toStrictEqual('new value');
44
- });
45
- test('delete method', async () => {
46
- await cache.set('key', 'value');
47
- const response = await cache.delete('key');
48
- expect(response).toStrictEqual(1);
49
- const newResponse = await cache.get('key');
50
- expect(newResponse).toStrictEqual(null);
51
- });
52
- test('incrementAndGet method', async () => {
53
- await cache.set('key', '5');
54
- const response = await cache.incrementAndGet('key');
55
- expect(response).toStrictEqual(6);
56
- });
57
- test('leftPush method', async () => {
58
- await cache.leftPush('key', 'value1');
59
- const response = await cache.leftPush('key', 'value2');
60
- const result = ['value2', 'value1'].length;
61
- expect(response).toStrictEqual(result);
62
- });
63
- test('rightPop method', async () => {
64
- await cache.leftPush('key', 'value1');
65
- await cache.leftPush('key', 'value2');
66
- const response = await cache.rightPop('key');
67
- expect(response).toStrictEqual('value1');
68
- });
69
- test('listLength method', async () => {
70
- await cache.leftPush('key', 'value1');
71
- await cache.leftPush('key', 'value2');
72
- const response = await cache.listLength('key');
73
- expect(response).toStrictEqual(2);
74
- });
75
- test('decrementAndGet method', async () => {
76
- await cache.set('key', '5');
77
- const response = await cache.decrementAndGet('key');
78
- expect(response).toStrictEqual(4);
79
- });
80
- test('incrementAndGet method throws error when key holds a list value', async () => {
81
- await cache.leftPush('key', 'value1');
82
- try {
83
- await cache.incrementAndGet('key');
84
- }
85
- catch (error) {
86
- expect(error).toEqual(new validator_1.WrongTypeOperationError());
87
- }
88
- });
89
- test('leftPush method throws error when key holds a non-list value', async () => {
90
- await cache.set('key', 'value');
91
- try {
92
- await cache.leftPush('key', 'newValue');
93
- }
94
- catch (error) {
95
- expect(error).toEqual(new validator_1.WrongTypeOperationError());
96
- }
97
- });
98
- test('rightPop method throws error when key holds a non-list value', async () => {
99
- await cache.set('key', 'value');
100
- try {
101
- await cache.rightPop('key');
102
- }
103
- catch (error) {
104
- expect(error).toEqual(new validator_1.WrongTypeOperationError());
105
- }
106
- });
107
- test('listLength method throws error when key holds a non-list value', async () => {
108
- await cache.set('key', 'value');
109
- try {
110
- await cache.listLength('key');
111
- }
112
- catch (error) {
113
- expect(error).toEqual(new validator_1.WrongTypeOperationError());
114
- }
115
- });
116
- test('decrementAndGet method throws error when key holds a list value', async () => {
117
- await cache.leftPush('key', 'value');
118
- try {
119
- await cache.decrementAndGet('key');
120
- }
121
- catch (error) {
122
- expect(error).toEqual(new validator_1.WrongTypeOperationError());
123
- }
124
- });
125
- test('scan method returns empty array when no matching keys', async () => {
126
- await cache.set('key1', 'value1');
127
- await cache.set('key2', 'value2');
128
- await cache.set('key3', 'value3');
129
- await cache.set('key4', 'value4');
130
- await cache.set('key5', 'value5');
131
- const scanResult = await cache.scan('nonexistent*');
132
- expect(scanResult.keys).toEqual([]);
133
- });
134
- test('scan method returns keys starting with "key" prefix', async () => {
135
- await cache.set('key1', 'value1');
136
- await cache.set('key2', 'value2');
137
- await cache.set('otherKey1', 'value3');
138
- await cache.set('otherKey2', 'value4');
139
- await cache.set('key3', 'value5');
140
- const scanResult = await cache.scan('key*');
141
- expect(scanResult.keys).toEqual(['key1', 'key2', 'key3']);
142
- });
143
- test('scan method returns keys with "2" in the middle', async () => {
144
- await cache.set('key1', 'value1');
145
- await cache.set('key2', 'value2');
146
- await cache.set('otherKey1', 'value3');
147
- await cache.set('otherKey2', 'value4');
148
- await cache.set('key3', 'value5');
149
- const scanResult = await cache.scan('*2*');
150
- expect(scanResult.keys).toEqual(['key2', 'otherKey2']);
151
- });
152
- test('scan method returns keys with "l?st*" redis pattern', async () => {
153
- await cache.set('list1', 'value1');
154
- await cache.set('list2', 'value2');
155
- await cache.set('list3', 'value3');
156
- await cache.set('list4', 'value4');
157
- await cache.set('lest5', 'value5');
158
- await cache.set('key1', 'value1');
159
- const scanResult = await cache.scan('l?st*');
160
- expect(scanResult.keys).toEqual(['list1', 'list2', 'list3', 'list4', 'lest5']);
161
- });
162
- });
163
- describe('Forge Cache Tunnel with json file', () => {
164
- it('should save and load data from file', async () => {
165
- const fileName = (0, tmp_1.tmpNameSync)({ postfix: '.json' });
166
- const cache1 = new tunnel_1.TunnelCache(fileName);
167
- await cache1.set('key', 'value');
168
- await cache1.set('key2', 'value2');
169
- await new Promise((resolve) => setTimeout(resolve, 500));
170
- const savedFileContent = fs_1.default.readFileSync(fileName, `utf-8`);
171
- expect(JSON.parse(savedFileContent)).toStrictEqual({ key: 'value', key2: 'value2' });
172
- const cache2 = new tunnel_1.TunnelCache(fileName);
173
- const res = await cache2.get('key');
174
- expect(res).toBe('value');
175
- });
176
- });