@forge/kvs

1.3.2-next.01.3.2-next.0-experimental-bf21d1e
out/__test__/index.test.js
~out/__test__/index.test.jsModified
+291
Index: package/out/__test__/index.test.js
===================================================================
--- package/out/__test__/index.test.js
+++ package/out/__test__/index.test.js
@@ -1180,5 +1180,296 @@
         const { sut } = prepare(response);
         const items = [{ key: 'foo', value: 'bar' }];
         await expect(sut.batchSet(items)).rejects.toMatchError(new errors_1.ForgeKvsAPIError({ status: 500, statusText: 'Internal Server Error', traceId }, { code: 'INTERNAL_SERVER_ERROR', message: 'An internal server error has occurred' }));
     });
+    it('should handle batchDelete correctly with mixed entity and non-entity items', async () => {
+        const response = new Response(JSON.stringify({
+            successfulKeys: [{ key: 'foo', entityName: 'employees' }, { key: 'bar' }],
+            failedKeys: [
+                {
+                    key: 'baz',
+                    entityName: 'departments',
+                    error: { code: 'KEY_NOT_FOUND', message: 'Key does not exist' }
+                }
+            ]
+        }), {
+            status: 200,
+            headers: { 'x-trace-id': traceId }
+        });
+        const { sut, apiClient } = prepare(response);
+        const items = [{ key: 'foo', entityName: 'employees' }, { key: 'bar' }, { key: 'baz', entityName: 'departments' }];
+        const rs = await sut.batchDelete(items);
+        expect(rs).toEqual({
+            successfulKeys: [{ key: 'foo', entityName: 'employees' }, { key: 'bar' }],
+            failedKeys: [
+                {
+                    key: 'baz',
+                    entityName: 'departments',
+                    error: { code: 'KEY_NOT_FOUND', message: 'Key does not exist' }
+                }
+            ]
+        });
+        expect(apiClient).toHaveBeenCalledWith('/api/v1/batch/delete', expect.objectContaining({
+            body: JSON.stringify(items)
+        }));
+    });
+    it('should handle batchDelete with all successful keys', async () => {
+        const response = new Response(JSON.stringify({
+            successfulKeys: [{ key: 'foo' }, { key: 'bar' }],
+            failedKeys: []
+        }), {
+            status: 200,
+            headers: { 'x-trace-id': traceId }
+        });
+        const { sut, apiClient } = prepare(response);
+        const items = [{ key: 'foo' }, { key: 'bar' }];
+        const rs = await sut.batchDelete(items);
+        expect(rs).toEqual({
+            successfulKeys: [{ key: 'foo' }, { key: 'bar' }],
+            failedKeys: []
+        });
+        expect(apiClient).toHaveBeenCalledWith('/api/v1/batch/delete', expect.objectContaining({
+            body: JSON.stringify(items)
+        }));
+    });
+    it('should handle batchDelete with all failed keys', async () => {
+        const response = new Response(JSON.stringify({
+            successfulKeys: [],
+            failedKeys: [
+                {
+                    key: 'foo',
+                    error: { code: 'PERMISSION_DENIED', message: 'Access denied' }
+                },
+                {
+                    key: 'bar',
+                    entityName: 'employees',
+                    error: { code: 'KEY_NOT_FOUND', message: 'Key does not exist' }
+                }
+            ]
+        }), {
+            status: 200,
+            headers: { 'x-trace-id': traceId }
+        });
+        const { sut, apiClient } = prepare(response);
+        const items = [{ key: 'foo' }, { key: 'bar', entityName: 'employees' }];
+        const rs = await sut.batchDelete(items);
+        expect(rs).toEqual({
+            successfulKeys: [],
+            failedKeys: [
+                {
+                    key: 'foo',
+                    error: { code: 'PERMISSION_DENIED', message: 'Access denied' }
+                },
+                {
+                    key: 'bar',
+                    entityName: 'employees',
+                    error: { code: 'KEY_NOT_FOUND', message: 'Key does not exist' }
+                }
+            ]
+        });
+        expect(apiClient).toHaveBeenCalledWith('/api/v1/batch/delete', expect.objectContaining({
+            body: JSON.stringify(items)
+        }));
+    });
+    it('should handle batchDelete API error response', async () => {
+        const response = new Response(JSON.stringify({ code: 'INTERNAL_SERVER_ERROR', message: 'An internal server error has occurred' }), {
+            status: 500,
+            statusText: 'Internal Server Error',
+            headers: { 'x-trace-id': traceId }
+        });
+        const { sut } = prepare(response);
+        const items = [{ key: 'foo' }];
+        await expect(sut.batchDelete(items)).rejects.toMatchError(new errors_1.ForgeKvsAPIError({ status: 500, statusText: 'Internal Server Error', traceId }, { code: 'INTERNAL_SERVER_ERROR', message: 'An internal server error has occurred' }));
+    });
+    it('should handle batchGet with mixed entity and non-entity items', async () => {
+        const response = new Response(JSON.stringify({
+            successfulKeys: [
+                { key: 'foo', value: 'John Doe', entityName: 'employees' },
+                { key: 'bar', value: 'simple value' }
+            ],
+            failedKeys: [
+                {
+                    key: 'baz',
+                    entityName: 'departments',
+                    error: { code: 'KEY_NOT_FOUND', message: 'Key does not exist' }
+                }
+            ]
+        }), {
+            status: 200,
+            headers: { 'x-trace-id': traceId }
+        });
+        const { sut, apiClient } = prepare(response);
+        const items = [{ key: 'foo', entityName: 'employees' }, { key: 'bar' }, { key: 'baz', entityName: 'departments' }];
+        const rs = await sut.batchGet(items);
+        expect(rs).toEqual({
+            successfulKeys: [
+                { key: 'foo', value: 'John Doe', entityName: 'employees' },
+                { key: 'bar', value: 'simple value' }
+            ],
+            failedKeys: [
+                {
+                    key: 'baz',
+                    entityName: 'departments',
+                    error: { code: 'KEY_NOT_FOUND', message: 'Key does not exist' }
+                }
+            ]
+        });
+        expect(apiClient).toHaveBeenCalledWith('/api/v1/batch/get', expect.objectContaining({
+            body: JSON.stringify(items)
+        }));
+    });
+    it('should handle batchGet with metadata fields', async () => {
+        const response = new Response(JSON.stringify({
+            successfulKeys: [
+                {
+                    key: 'foo',
+                    value: 'John Doe',
+                    entityName: 'employees',
+                    createdAt: 1718236800,
+                    updatedAt: 1718236800,
+                    expireTime: '2025-01-12T13:00:00Z'
+                },
+                {
+                    key: 'bar',
+                    value: 'simple value',
+                    createdAt: 1718236800,
+                    updatedAt: 1718236800
+                }
+            ],
+            failedKeys: []
+        }), {
+            status: 200,
+            headers: { 'x-trace-id': traceId }
+        });
+        const { sut, apiClient } = prepare(response);
+        const items = [
+            {
+                key: 'foo',
+                entityName: 'employees',
+                options: {
+                    metadataFields: [types_1.MetadataField.CREATED_AT, types_1.MetadataField.UPDATED_AT, types_1.MetadataField.EXPIRE_TIME]
+                }
+            },
+            {
+                key: 'bar',
+                options: {
+                    metadataFields: [types_1.MetadataField.CREATED_AT, types_1.MetadataField.UPDATED_AT]
+                }
+            }
+        ];
+        const rs = await sut.batchGet(items);
+        expect(rs).toEqual({
+            successfulKeys: [
+                {
+                    key: 'foo',
+                    value: 'John Doe',
+                    entityName: 'employees',
+                    createdAt: 1718236800,
+                    updatedAt: 1718236800,
+                    expireTime: '2025-01-12T13:00:00Z'
+                },
+                {
+                    key: 'bar',
+                    value: 'simple value',
+                    createdAt: 1718236800,
+                    updatedAt: 1718236800
+                }
+            ],
+            failedKeys: []
+        });
+        expect(apiClient).toHaveBeenCalledWith('/api/v1/batch/get', expect.objectContaining({
+            body: JSON.stringify(items)
+        }));
+    });
+    it('should return all successful keys from batchGet', async () => {
+        const response = new Response(JSON.stringify({
+            successfulKeys: [
+                { key: 'foo', value: 'value1' },
+                { key: 'bar', value: 'value2' }
+            ],
+            failedKeys: []
+        }), {
+            status: 200,
+            headers: { 'x-trace-id': traceId }
+        });
+        const { sut, apiClient } = prepare(response);
+        const items = [{ key: 'foo' }, { key: 'bar' }];
+        const rs = await sut.batchGet(items);
+        expect(rs).toEqual({
+            successfulKeys: [
+                { key: 'foo', value: 'value1' },
+                { key: 'bar', value: 'value2' }
+            ],
+            failedKeys: []
+        });
+        expect(apiClient).toHaveBeenCalledWith('/api/v1/batch/get', expect.objectContaining({
+            body: JSON.stringify(items)
+        }));
+    });
+    it('should return all failed keys from batchGet', async () => {
+        const response = new Response(JSON.stringify({
+            successfulKeys: [],
+            failedKeys: [
+                {
+                    key: 'foo',
+                    error: { code: 'PERMISSION_DENIED', message: 'Access denied' }
+                },
+                {
+                    key: 'bar',
+                    entityName: 'employees',
+                    error: { code: 'KEY_NOT_FOUND', message: 'Key does not exist' }
+                }
+            ]
+        }), {
+            status: 200,
+            headers: { 'x-trace-id': traceId }
+        });
+        const { sut, apiClient } = prepare(response);
+        const items = [{ key: 'foo' }, { key: 'bar', entityName: 'employees' }];
+        const rs = await sut.batchGet(items);
+        expect(rs).toEqual({
+            successfulKeys: [],
+            failedKeys: [
+                {
+                    key: 'foo',
+                    error: { code: 'PERMISSION_DENIED', message: 'Access denied' }
+                },
+                {
+                    key: 'bar',
+                    entityName: 'employees',
+                    error: { code: 'KEY_NOT_FOUND', message: 'Key does not exist' }
+                }
+            ]
+        });
+        expect(apiClient).toHaveBeenCalledWith('/api/v1/batch/get', expect.objectContaining({
+            body: JSON.stringify(items)
+        }));
+    });
+    it('should handle batchGet API error response', async () => {
+        const response = new Response(JSON.stringify({ code: 'INTERNAL_SERVER_ERROR', message: 'An internal server error has occurred' }), {
+            status: 500,
+            statusText: 'Internal Server Error',
+            headers: { 'x-trace-id': traceId }
+        });
+        const { sut } = prepare(response);
+        const items = [{ key: 'foo' }];
+        await expect(sut.batchGet(items)).rejects.toMatchError(new errors_1.ForgeKvsAPIError({ status: 500, statusText: 'Internal Server Error', traceId }, { code: 'INTERNAL_SERVER_ERROR', message: 'An internal server error has occurred' }));
+    });
+    it('should handle batchGet with invalid metadata fields', async () => {
+        const response = new Response(JSON.stringify({
+            code: 'BAD_REQUEST',
+            message: 'Provided request body is invalid'
+        }), {
+            status: 400,
+            statusText: 'Bad Request',
+            headers: { 'x-trace-id': traceId }
+        });
+        const { sut } = prepare(response);
+        const items = [
+            {
+                key: 'foo',
+                options: { metadataFields: ['INVALID_METADATA_FIELD'] }
+            }
+        ];
+        await expect(sut.batchGet(items)).rejects.toMatchError(new errors_1.ForgeKvsAPIError({ status: 400, statusText: 'Bad Request', traceId }, { code: 'BAD_REQUEST', message: 'Provided request body is invalid' }));
+    });
 });