diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9d2dc4cb..126877a6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,6 +25,7 @@ jobs: - name: Build run: | rm package-lock.json + npm install -g npm@latest npm install npm run test @@ -42,6 +43,13 @@ jobs: uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} + + - name: Build + run: | + rm package-lock.json + npm install -g npm@latest + npm install + npm run test - name: API Test run: | diff --git a/src/puter-js/index.d.ts b/src/puter-js/index.d.ts index 5e81580c..d8c787f0 100644 --- a/src/puter-js/index.d.ts +++ b/src/puter-js/index.d.ts @@ -298,7 +298,9 @@ interface KV { get(key: string): Promise; del(key: string): Promise; incr(key: string, pathAndAmount: { [key: string]: number }): Promise; + incr(key: string, amount?: number): Promise; decr(key: string, pathAndAmount: { [key: string]: number }): Promise; + decr(key: string, amount?: number): Promise; list(pattern?: string, returnValues?: boolean): Promise; list(returnValues?: boolean): Promise; flush(): Promise; diff --git a/src/puter-js/src/modules/KV.js b/src/puter-js/src/modules/KV.js index 6337c5a9..5efeeb3f 100644 --- a/src/puter-js/src/modules/KV.js +++ b/src/puter-js/src/modules/KV.js @@ -173,7 +173,7 @@ class KV{ } options.key = args[0]; - options.pathAndAmountMap = args[1] ?? { '': 1 }; + options.pathAndAmountMap = !args[1] ? { '': 1 } : typeof args[1] === 'number' ? { '': args[1] } : args[1]; // key size cannot be larger than MAX_KEY_SIZE if ( options.key.length > this.MAX_KEY_SIZE ){ @@ -192,7 +192,7 @@ class KV{ } options.key = args[0]; - options.pathAndAmountMap = args[1] ?? { '': 1 }; + options.pathAndAmountMap = !args[1] ? { '': 1 } : typeof args[1] === 'number' ? { '': args[1] } : args[1]; // key size cannot be larger than MAX_KEY_SIZE if ( options.key.length > this.MAX_KEY_SIZE ){ diff --git a/tests/puterJsApiTests/kv.test.ts b/tests/puterJsApiTests/kv.test.ts index ecf822eb..b464e5d0 100644 --- a/tests/puterJsApiTests/kv.test.ts +++ b/tests/puterJsApiTests/kv.test.ts @@ -18,24 +18,83 @@ describe('Puter KV Module', () => { }); it('should increment a key success', async () => { - const getRes = await puter.kv.get(TEST_KEY); - expect(getRes).toBe(0); + await puter.kv.set(TEST_KEY, 0); const incrRes = await puter.kv.incr(TEST_KEY, { '': 5 }); - console.log('incrRes', incrRes); expect(incrRes).toBe(5); const finalGet = await puter.kv.get(TEST_KEY); expect(finalGet).toBe(5); }); it('should decrement a key success', async () => { - const getRes = await puter.kv.get(TEST_KEY); - expect(getRes).toBe(5); + const getRes = await puter.kv.set(TEST_KEY, 0); const decrRes = await puter.kv.decr(TEST_KEY, { '': 3 }); - console.log('decrRes', decrRes); - expect(decrRes).toBe(2); + expect(decrRes).toBe(-3); + const finalGet = await puter.kv.get(TEST_KEY); + expect(finalGet).toBe(-3); + }); + + it('should increment a key with second argument', async () => { + await puter.kv.set(TEST_KEY, 0); + const incrRes = await puter.kv.incr(TEST_KEY); + expect(incrRes).toBe(1); + const finalGet = await puter.kv.get(TEST_KEY); + expect(finalGet).toBe(1); + }) + + it('should decrement a key with second argument', async () => { + await puter.kv.set(TEST_KEY, 0); + const incrRes = await puter.kv.decr(TEST_KEY); + expect(incrRes).toBe(-1); + const finalGet = await puter.kv.get(TEST_KEY); + expect(finalGet).toBe(-1); + }) + + it('should increment a key with second argument', async () => { + await puter.kv.set(TEST_KEY, 0); + const incrRes = await puter.kv.incr(TEST_KEY, 2); + expect(incrRes).toBe(2); const finalGet = await puter.kv.get(TEST_KEY); expect(finalGet).toBe(2); - }); + }) + + it('should decrement a key with second argument', async () => { + await puter.kv.set(TEST_KEY, 0); + const incrRes = await puter.kv.decr(TEST_KEY, 3); + expect(incrRes).toBe(-3); + const finalGet = await puter.kv.get(TEST_KEY); + expect(finalGet).toBe(-3); + }) + + it('should increment a key with nested path', async () => { + await puter.kv.set(TEST_KEY, { a: { b: 0 } }); + const incrRes = await puter.kv.incr(TEST_KEY, { 'a.b': 1 }); + expect(incrRes).toEqual({ a: { b: 1 } }); + const finalGet = await puter.kv.get(TEST_KEY); + expect(finalGet).toEqual({ a: { b: 1 } }); + }) + + it('should decrement a key with nested path', async () => { + await puter.kv.set(TEST_KEY, { a: { b: 0 } }); + const incrRes = await puter.kv.decr(TEST_KEY, { 'a.b': 1 }); + expect(incrRes).toEqual({ a: { b: -1 } }); + const finalGet = await puter.kv.get(TEST_KEY); + expect(finalGet).toEqual({ a: { b: -1 } }); + }) + + it('should increment a nonexistent key with nested path', async () => { + const incrRes = await puter.kv.incr(TEST_KEY + 1, { 'a.b': 1 }); + expect(incrRes).toEqual({ a: { b: 1 } }); + const finalGet = await puter.kv.get(TEST_KEY + 1); + expect(finalGet).toEqual({ a: { b: 1 } }); + }) + + it('should decrement a nonexistent key with nested path', async () => { + const incrRes = await puter.kv.decr(TEST_KEY + 2, { 'a.b': 1 }); + expect(incrRes).toEqual({ a: { b: -1 } }); + const finalGet = await puter.kv.get(TEST_KEY + 2); + expect(finalGet).toEqual({ a: { b: -1 } }); + }) + it('should list keys', async () => { const listRes = await puter.kv.list(); expect(Array.isArray(listRes)).toBe(true);