From 7daaacfefedc61e1e01d89bae35632ceab90cc98 Mon Sep 17 00:00:00 2001 From: Yusinto Ngadiman Date: Tue, 5 Mar 2024 13:01:28 -0800 Subject: [PATCH 1/3] fix: Append withReasons to stream uri. --- packages/shared/sdk-client/src/LDClientImpl.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/shared/sdk-client/src/LDClientImpl.ts b/packages/shared/sdk-client/src/LDClientImpl.ts index af8a873703..5a97255c2f 100644 --- a/packages/shared/sdk-client/src/LDClientImpl.ts +++ b/packages/shared/sdk-client/src/LDClientImpl.ts @@ -321,10 +321,14 @@ export default class LDClientImpl implements LDClient { } } else { this.streamer?.close(); + let streamUri = this.createStreamUriPath(context); + if (this.config.withReasons) { + streamUri = `${streamUri}?withReasons=true`; + } this.streamer = new internal.StreamingProcessor( this.sdkKey, this.clientContext, - this.createStreamUriPath(context), + streamUri, this.createStreamListeners(context, checkedContext.canonicalKey, identifyResolve), this.diagnosticsManager, (e) => { From 740d1d1fa3515d25bf4d6ec3c07359827c390816 Mon Sep 17 00:00:00 2001 From: Yusinto Ngadiman Date: Tue, 5 Mar 2024 13:25:58 -0800 Subject: [PATCH 2/3] chore: Added unit tests. --- .../sdk-client/src/LDClientImpl.test.ts | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/shared/sdk-client/src/LDClientImpl.test.ts b/packages/shared/sdk-client/src/LDClientImpl.test.ts index eb939fcd89..c386ae3fed 100644 --- a/packages/shared/sdk-client/src/LDClientImpl.test.ts +++ b/packages/shared/sdk-client/src/LDClientImpl.test.ts @@ -3,6 +3,7 @@ import { basicPlatform, hasher, logger, + MockStreamingProcessor, setupMockStreamingProcessor, } from '@launchdarkly/private-js-mocks'; @@ -12,13 +13,13 @@ import { Flags } from './types'; jest.mock('@launchdarkly/js-sdk-common', () => { const actual = jest.requireActual('@launchdarkly/js-sdk-common'); - const { MockStreamingProcessor } = jest.requireActual('@launchdarkly/private-js-mocks'); + const actualMock = jest.requireActual('@launchdarkly/private-js-mocks'); return { ...actual, ...{ internal: { ...actual.internal, - StreamingProcessor: MockStreamingProcessor, + StreamingProcessor: actualMock.MockStreamingProcessor, }, }, }; @@ -161,6 +162,34 @@ describe('sdk-client object', () => { expect(all).toMatchObject({ 'dev-test-flag': false, }); + expect(MockStreamingProcessor).toHaveBeenCalledWith( + expect.anything(), + expect.anything(), + '/stream/path', + expect.anything(), + undefined, + expect.anything(), + ); + }); + + test('identify success withReasons', async () => { + const carContext: LDContext = { kind: 'car', key: 'mazda-cx7' }; + ldc = new LDClientImpl(testSdkKey, AutoEnvAttributes.Enabled, basicPlatform, { + logger, + sendEvents: false, + withReasons: true, + }); + + await ldc.identify(carContext); + + expect(MockStreamingProcessor).toHaveBeenCalledWith( + expect.anything(), + expect.anything(), + '/stream/path?withReasons=true', + expect.anything(), + undefined, + expect.anything(), + ); }); test('identify success without auto env', async () => { From ca8b5a0ec99314db95dbdececaa6f8771ecb3c45 Mon Sep 17 00:00:00 2001 From: Yusinto Ngadiman Date: Tue, 5 Mar 2024 13:39:55 -0800 Subject: [PATCH 3/3] chore: Use fictional car make. --- .../shared/sdk-client/src/LDClientImpl.test.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/shared/sdk-client/src/LDClientImpl.test.ts b/packages/shared/sdk-client/src/LDClientImpl.test.ts index c386ae3fed..9c6416892c 100644 --- a/packages/shared/sdk-client/src/LDClientImpl.test.ts +++ b/packages/shared/sdk-client/src/LDClientImpl.test.ts @@ -148,7 +148,7 @@ describe('sdk-client object', () => { test('identify success', async () => { defaultPutResponse['dev-test-flag'].value = false; - const carContext: LDContext = { kind: 'car', key: 'mazda-cx7' }; + const carContext: LDContext = { kind: 'car', key: 'test-car' }; await ldc.identify(carContext); const c = ldc.getContext(); @@ -156,7 +156,7 @@ describe('sdk-client object', () => { expect(c).toEqual({ kind: 'multi', - car: { key: 'mazda-cx7' }, + car: { key: 'test-car' }, ...autoEnv, }); expect(all).toMatchObject({ @@ -173,7 +173,7 @@ describe('sdk-client object', () => { }); test('identify success withReasons', async () => { - const carContext: LDContext = { kind: 'car', key: 'mazda-cx7' }; + const carContext: LDContext = { kind: 'car', key: 'test-car' }; ldc = new LDClientImpl(testSdkKey, AutoEnvAttributes.Enabled, basicPlatform, { logger, sendEvents: false, @@ -194,7 +194,7 @@ describe('sdk-client object', () => { test('identify success without auto env', async () => { defaultPutResponse['dev-test-flag'].value = false; - const carContext: LDContext = { kind: 'car', key: 'mazda-cx7' }; + const carContext: LDContext = { kind: 'car', key: 'test-car' }; ldc = new LDClientImpl(testSdkKey, AutoEnvAttributes.Disabled, basicPlatform, { logger, sendEvents: false, @@ -238,7 +238,7 @@ describe('sdk-client object', () => { test('identify error stream error', async () => { setupMockStreamingProcessor(true); - const carContext: LDContext = { kind: 'car', key: 'mazda-3' }; + const carContext: LDContext = { kind: 'car', key: 'test-car' }; await expect(ldc.identify(carContext)).rejects.toMatchObject({ code: 401, @@ -254,10 +254,10 @@ describe('sdk-client object', () => { await ldc.identify(context); - const carContext1: LDContext = { kind: 'car', key: 'mazda-cx' }; + const carContext1: LDContext = { kind: 'car', key: 'test-car' }; await ldc.identify(carContext1); - const carContext2: LDContext = { kind: 'car', key: 'subaru-forrester' }; + const carContext2: LDContext = { kind: 'car', key: 'test-car-2' }; await ldc.identify(carContext2); expect(emitter.listenerCount('change')).toEqual(1);