@forge/kvs

2.0.5-next.12.0.5-next.1-experimental-e9e08bb
out/__test__/local-storage-routing.test.js
+out/__test__/local-storage-routing.test.jsNew file
+50
Index: package/out/__test__/local-storage-routing.test.js
===================================================================
--- package/out/__test__/local-storage-routing.test.js
+++ package/out/__test__/local-storage-routing.test.js
@@ -0,0 +1,50 @@
+"use strict";
+const LOCAL_ENVIRONMENT = {
+    FORGE_LOCAL_STORAGE: '1',
+    FORGE_LOCAL_KVS_URL: 'http://127.0.0.1:18090',
+    FORGE_LOCAL_STORAGE_APP_ID: 'local-app',
+    FORGE_LOCAL_STORAGE_ENVIRONMENT_ID: 'local-environment',
+    FORGE_LOCAL_STORAGE_INSTALLATION_ID: 'local-installation'
+};
+describe('@forge/kvs local-storage routing', () => {
+    beforeEach(() => {
+        jest.resetModules();
+        Object.assign(process.env, LOCAL_ENVIRONMENT);
+        global.__forge_fetch__ = jest.fn().mockRejectedValue(new Error('hosted storage was called'));
+        global.__forge_local_fetch__ = jest.fn().mockResolvedValue(new Response('', { status: 200 }));
+    });
+    afterEach(() => {
+        for (const key of Object.keys(LOCAL_ENVIRONMENT))
+            delete process.env[key];
+        delete global.__forge_fetch__;
+        delete global.__forge_local_fetch__;
+    });
+    it('routes ordinary SDK operations to the loopback emulator with deterministic namespace headers', async () => {
+        const { kvs } = jest.requireActual('../index');
+        await kvs.set('key', { value: true });
+        expect(global.__forge_local_fetch__).toHaveBeenCalledWith(new URL('http://127.0.0.1:18090/api/v1/set'), expect.objectContaining({
+            method: 'POST',
+            headers: expect.objectContaining({
+                'x-forge-app-id': 'local-app',
+                'x-forge-environment-id': 'local-environment',
+                'x-forge-installation-id': 'local-installation'
+            })
+        }));
+        expect(global.__forge_fetch__).not.toHaveBeenCalled();
+    });
+    it('fails closed when the tunnel local-fetch bridge is missing', () => {
+        delete global.__forge_local_fetch__;
+        expect(() => jest.requireActual('../index')).toThrow('requires the tunnel local-fetch bridge');
+        expect(global.__forge_fetch__).not.toHaveBeenCalled();
+    });
+    it('fails closed for incomplete or non-loopback routing instead of falling through to hosted storage', () => {
+        delete process.env.FORGE_LOCAL_STORAGE_INSTALLATION_ID;
+        expect(() => jest.requireActual('../index')).toThrow('local namespace is incomplete');
+        expect(global.__forge_fetch__).not.toHaveBeenCalled();
+        jest.resetModules();
+        process.env.FORGE_LOCAL_STORAGE_INSTALLATION_ID = 'local-installation';
+        process.env.FORGE_LOCAL_KVS_URL = 'http://example.com:18090';
+        expect(() => jest.requireActual('../index')).toThrow('must be http://127.0.0.1:18090');
+        expect(global.__forge_fetch__).not.toHaveBeenCalled();
+    });
+});