npm package diff
Package: @forge/cache
Versions: 1.0.3-next.0 - 1.0.3-next.0-experimental-ab129b0
File: package/out/__test__/tunnel.test.js
Index: package/out/__test__/tunnel.test.js
===================================================================
--- package/out/__test__/tunnel.test.js
+++ package/out/__test__/tunnel.test.js
@@ -1,176 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-const tslib_1 = require("tslib");
-const tunnel_1 = require("../tunnel");
-const validator_1 = require("../utils/validator");
-const fs_1 = tslib_1.__importDefault(require("fs"));
-const tmp_1 = require("tmp");
-describe('Forge Cache Tunnel', () => {
- let cache;
- beforeEach(() => {
- const fileName = (0, tmp_1.tmpNameSync)({ postfix: '.json' });
- cache = new tunnel_1.TunnelCache(fileName);
- });
- test('set method', async () => {
- const response = await cache.set('key', 'value');
- expect(response).toStrictEqual(undefined);
- });
- test('get method', async () => {
- await cache.set('key', 'value');
- const response = await cache.get('key');
- expect(response).toStrictEqual('value');
- });
- test('get method throws error for list value', async () => {
- await cache.leftPush('key', 'value1');
- try {
- await cache.get('key');
- }
- catch (error) {
- expect(error).toEqual(new validator_1.WrongTypeOperationError());
- }
- });
- test('setIfNotExists method', async () => {
- const response = await cache.setIfNotExists('key', 'value');
- expect(response).toStrictEqual('OK');
- const newResponse = await cache.setIfNotExists('key', 'new value');
- expect(newResponse).toStrictEqual(null);
- });
- test('getAndSet method', async () => {
- await cache.set('key', 'value');
- const response = await cache.getAndSet('key', 'new value');
- expect(response).toStrictEqual('value');
- const newResponse = await cache.get('key');
- expect(newResponse).toStrictEqual('new value');
- });
- test('delete method', async () => {
- await cache.set('key', 'value');
- const response = await cache.delete('key');
- expect(response).toStrictEqual(1);
- const newResponse = await cache.get('key');
- expect(newResponse).toStrictEqual(null);
- });
- test('incrementAndGet method', async () => {
- await cache.set('key', '5');
- const response = await cache.incrementAndGet('key');
- expect(response).toStrictEqual(6);
- });
- test('leftPush method', async () => {
- await cache.leftPush('key', 'value1');
- const response = await cache.leftPush('key', 'value2');
- const result = ['value2', 'value1'].length;
- expect(response).toStrictEqual(result);
- });
- test('rightPop method', async () => {
- await cache.leftPush('key', 'value1');
- await cache.leftPush('key', 'value2');
- const response = await cache.rightPop('key');
- expect(response).toStrictEqual('value1');
- });
- test('listLength method', async () => {
- await cache.leftPush('key', 'value1');
- await cache.leftPush('key', 'value2');
- const response = await cache.listLength('key');
- expect(response).toStrictEqual(2);
- });
- test('decrementAndGet method', async () => {
- await cache.set('key', '5');
- const response = await cache.decrementAndGet('key');
- expect(response).toStrictEqual(4);
- });
- test('incrementAndGet method throws error when key holds a list value', async () => {
- await cache.leftPush('key', 'value1');
- try {
- await cache.incrementAndGet('key');
- }
- catch (error) {
- expect(error).toEqual(new validator_1.WrongTypeOperationError());
- }
- });
- test('leftPush method throws error when key holds a non-list value', async () => {
- await cache.set('key', 'value');
- try {
- await cache.leftPush('key', 'newValue');
- }
- catch (error) {
- expect(error).toEqual(new validator_1.WrongTypeOperationError());
- }
- });
- test('rightPop method throws error when key holds a non-list value', async () => {
- await cache.set('key', 'value');
- try {
- await cache.rightPop('key');
- }
- catch (error) {
- expect(error).toEqual(new validator_1.WrongTypeOperationError());
- }
- });
- test('listLength method throws error when key holds a non-list value', async () => {
- await cache.set('key', 'value');
- try {
- await cache.listLength('key');
- }
- catch (error) {
- expect(error).toEqual(new validator_1.WrongTypeOperationError());
- }
- });
- test('decrementAndGet method throws error when key holds a list value', async () => {
- await cache.leftPush('key', 'value');
- try {
- await cache.decrementAndGet('key');
- }
- catch (error) {
- expect(error).toEqual(new validator_1.WrongTypeOperationError());
- }
- });
- test('scan method returns empty array when no matching keys', async () => {
- await cache.set('key1', 'value1');
- await cache.set('key2', 'value2');
- await cache.set('key3', 'value3');
- await cache.set('key4', 'value4');
- await cache.set('key5', 'value5');
- const scanResult = await cache.scan('nonexistent*');
- expect(scanResult.keys).toEqual([]);
- });
- test('scan method returns keys starting with "key" prefix', async () => {
- await cache.set('key1', 'value1');
- await cache.set('key2', 'value2');
- await cache.set('otherKey1', 'value3');
- await cache.set('otherKey2', 'value4');
- await cache.set('key3', 'value5');
- const scanResult = await cache.scan('key*');
- expect(scanResult.keys).toEqual(['key1', 'key2', 'key3']);
- });
- test('scan method returns keys with "2" in the middle', async () => {
- await cache.set('key1', 'value1');
- await cache.set('key2', 'value2');
- await cache.set('otherKey1', 'value3');
- await cache.set('otherKey2', 'value4');
- await cache.set('key3', 'value5');
- const scanResult = await cache.scan('*2*');
- expect(scanResult.keys).toEqual(['key2', 'otherKey2']);
- });
- test('scan method returns keys with "l?st*" redis pattern', async () => {
- await cache.set('list1', 'value1');
- await cache.set('list2', 'value2');
- await cache.set('list3', 'value3');
- await cache.set('list4', 'value4');
- await cache.set('lest5', 'value5');
- await cache.set('key1', 'value1');
- const scanResult = await cache.scan('l?st*');
- expect(scanResult.keys).toEqual(['list1', 'list2', 'list3', 'list4', 'lest5']);
- });
-});
-describe('Forge Cache Tunnel with json file', () => {
- it('should save and load data from file', async () => {
- const fileName = (0, tmp_1.tmpNameSync)({ postfix: '.json' });
- const cache1 = new tunnel_1.TunnelCache(fileName);
- await cache1.set('key', 'value');
- await cache1.set('key2', 'value2');
- await new Promise((resolve) => setTimeout(resolve, 500));
- const savedFileContent = fs_1.default.readFileSync(fileName, `utf-8`);
- expect(JSON.parse(savedFileContent)).toStrictEqual({ key: 'value', key2: 'value2' });
- const cache2 = new tunnel_1.TunnelCache(fileName);
- const res = await cache2.get('key');
- expect(res).toBe('value');
- });
-});