diff --git a/packages/shared/sdk-server/__tests__/MigrationOpTracker.test.ts b/packages/shared/sdk-server/__tests__/MigrationOpTracker.test.ts index 8346aa2b5..4c44f80bf 100644 --- a/packages/shared/sdk-server/__tests__/MigrationOpTracker.test.ts +++ b/packages/shared/sdk-server/__tests__/MigrationOpTracker.test.ts @@ -14,6 +14,8 @@ it('does not generate an event if an op is not set', () => { }, ); + tracker.invoked('old'); + expect(tracker.createEvent()).toBeUndefined(); }); @@ -22,8 +24,27 @@ it('does not generate an event with missing context keys', () => { kind: 'FALLTHROUGH', }); - // Set the op otherwise that would prevent an event as well. + // Set the op otherwise/invoked that would prevent an event as well. tracker.op('write'); + tracker.invoked('old'); + + expect(tracker.createEvent()).toBeUndefined(); +}); + +it('does not generate an event with empty flag key', () => { + const tracker = new MigrationOpTracker( + '', + { key: 'user-key' }, + LDMigrationStage.Off, + LDMigrationStage.Off, + { + kind: 'FALLTHROUGH', + }, + ); + + // Set the op/invoked otherwise that would prevent an event as well. + tracker.op('write'); + tracker.invoked('old'); expect(tracker.createEvent()).toBeUndefined(); }); diff --git a/packages/shared/sdk-server/src/MigrationOpTracker.ts b/packages/shared/sdk-server/src/MigrationOpTracker.ts index 9001e42fd..fae4884bf 100644 --- a/packages/shared/sdk-server/src/MigrationOpTracker.ts +++ b/packages/shared/sdk-server/src/MigrationOpTracker.ts @@ -1,4 +1,9 @@ -import { internal, LDEvaluationReason, LDLogger } from '@launchdarkly/js-sdk-common'; +import { + internal, + LDEvaluationReason, + LDLogger, + TypeValidators, +} from '@launchdarkly/js-sdk-common'; import { LDMigrationStage, LDMigrationTracker } from './api'; import { @@ -82,6 +87,11 @@ export default class MigrationOpTracker implements LDMigrationTracker { } createEvent(): LDMigrationOpEvent | undefined { + if (!TypeValidators.String.is(this.flagKey) || this.flagKey === '') { + this.logger?.error('The flag key for a migration operation must be a non-empty string.'); + return undefined; + } + if (!this.operation) { this.logger?.error('The operation must be set using "op" before an event can be created.'); return undefined;