Skip to content

chore: Add hook support in browser contract tests #831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"packageManager": "yarn@3.4.1",
"//": "Pin jsonc-parser because v3.3.0 breaks rollup-plugin-esbuild",
"resolutions": {
"jsonc-parser": "3.2.0"
"jsonc-parser": "3.2.0",
"parse5": "7.2.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { initialize, LDClient, LDLogger, LDOptions } from '@launchdarkly/js-clie
import { CommandParams, CommandType, ValueType } from './CommandParams';
import { CreateInstanceParams, SDKConfigParams } from './ConfigParams';
import { makeLogger } from './makeLogger';
import TestHook from './TestHook';

export const badCommandError = new Error('unsupported command');
export const malformedCommand = new Error('command was malformed');
Expand Down Expand Up @@ -64,6 +65,12 @@ function makeSdkConfig(options: SDKConfigParams, tag: string) {
};
}

if (options.hooks) {
cf.hooks = options.hooks.hooks.map(
(hook) => new TestHook(hook.name, hook.callbackUri, hook.data, hook.errors),
);
}

cf.fetchGoals = false;

return cf;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default class TestHarnessWebSocket {
'anonymous-redaction',
'strongly-typed',
'client-prereq-events',
'track-hooks',
];

break;
Expand Down
94 changes: 94 additions & 0 deletions packages/sdk/browser/contract-tests/entity/src/TestHook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import {
EvaluationSeriesContext,
EvaluationSeriesData,
Hook,
HookMetadata,
LDEvaluationDetail,
TrackSeriesContext,
} from '@launchdarkly/js-client-sdk';

export interface HookData {
beforeEvaluation?: Record<string, unknown>;
afterEvaluation?: Record<string, unknown>;
}

export interface HookErrors {
beforeEvaluation?: string;
afterEvaluation?: string;
afterTrack?: string;
}

export default class TestHook implements Hook {
private _name: string;
private _endpoint: string;
private _data?: HookData;
private _errors?: HookErrors;

constructor(name: string, endpoint: string, data?: HookData, errors?: HookErrors) {
this._name = name;
this._endpoint = endpoint;
this._data = data;
this._errors = errors;
}

private async _safePost(body: unknown): Promise<void> {
try {
await fetch(this._endpoint, {
method: 'POST',
body: JSON.stringify(body),
});
} catch {
// The test could move on before the post, so we are ignoring
// failed posts.
}
}

getMetadata(): HookMetadata {
return {
name: this._name,
};
}

beforeEvaluation(
hookContext: EvaluationSeriesContext,
data: EvaluationSeriesData,
): EvaluationSeriesData {
if (this._errors?.beforeEvaluation) {
throw new Error(this._errors.beforeEvaluation);
}
this._safePost({
evaluationSeriesContext: hookContext,
evaluationSeriesData: data,
stage: 'beforeEvaluation',
});
return { ...data, ...(this._data?.beforeEvaluation || {}) };
}

afterEvaluation(
hookContext: EvaluationSeriesContext,
data: EvaluationSeriesData,
detail: LDEvaluationDetail,
): EvaluationSeriesData {
if (this._errors?.afterEvaluation) {
throw new Error(this._errors.afterEvaluation);
}
this._safePost({
evaluationSeriesContext: hookContext,
evaluationSeriesData: data,
stage: 'afterEvaluation',
evaluationDetail: detail,
});

return { ...data, ...(this._data?.afterEvaluation || {}) };
}

afterTrack(hookContext: TrackSeriesContext): void {
if (this._errors?.afterTrack) {
throw new Error(this._errors.afterTrack);
}
this._safePost({
trackSeriesContext: hookContext,
stage: 'afterTrack',
});
}
}
1 change: 1 addition & 0 deletions packages/sdk/browser/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type {
LDLogLevel,
LDMultiKindContext,
LDSingleKindContext,
TrackSeriesContext,
} from '@launchdarkly/js-client-sdk-common';

/**
Expand Down
1 change: 1 addition & 0 deletions packages/shared/sdk-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type {
IdentifySeriesData,
IdentifySeriesResult,
IdentifySeriesStatus,
TrackSeriesContext,
LDInspection,
} from './api';

Expand Down