Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions src/__tests__/node.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import tape from 'tape-catch';

import clientSuite from './nodeSuites/client.spec.js';
import providerSuite from './nodeSuites/provider.spec.js';

tape('## OpenFeature JavaScript Split Provider - tests', async function (assert) {
tape('## OpenFeature JavaScript Split Client - tests', async function (assert) {
assert.test('Client Tests', clientSuite);
});
});


tape('## OpenFeature JavaScript Split Provider - tests', async function (assert) {
assert.test('Provider Tests', providerSuite);
});
4 changes: 2 additions & 2 deletions src/__tests__/nodeSuites/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ export default async function(assert) {
const getBooleanSplitWithKeyTest = async (client) => {
let result = await client.getBooleanDetails('my_feature', false);
assert.equals(result.value, true);
assert.looseEquals(result.flagMetadata, { desc: 'this applies only to ON treatment' });
assert.looseEquals(result.flagMetadata, { config: '{"desc" : "this applies only to ON treatment"}' });

result = await client.getBooleanDetails('my_feature', true, { targetingKey: 'randomKey' });
assert.equals(result.value, false);
assert.looseEquals(result.flagMetadata, {});
assert.looseEquals(result.flagMetadata, { config: ''});
};

const getStringSplitTest = async (client) => {
Expand Down
60 changes: 60 additions & 0 deletions src/__tests__/nodeSuites/provider.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { ParseError } from "@openfeature/server-sdk";
import { makeProviderWithSpy } from "../testUtils";

export default async function(assert) {

const shouldFailWithBadApiKeyTest = () => {
Expand Down Expand Up @@ -80,6 +83,63 @@ export default async function(assert) {
assert.equal(1, 1);
};

const trackingSuite = (t) => {

t.test("track: throws when missing eventName", async (t) => {
const { provider } = makeProviderWithSpy();
try {
await provider.track("", { targetingKey: "u1", trafficType: "user" }, {});
t.fail("expected ParseError for eventName");
} catch (e) {
t.ok(e instanceof ParseError, "got ParseError");
}
t.end();
});

t.test("track: throws when missing trafficType", async (t) => {
const { provider } = makeProviderWithSpy();
try {
await provider.track("evt", { targetingKey: "u1" }, {});
t.fail("expected ParseError for trafficType");
} catch (e) {
t.ok(e instanceof ParseError, "got ParseError");
}
t.end();
});

t.test("track: ok without details", async (t) => {
const { provider, calls } = makeProviderWithSpy();
await provider.track("view", { targetingKey: "u1", trafficType: "user" }, null);

t.equal(calls.count, 1, "Split track called once");
t.deepEqual(
calls.args,
["u1", "user", "view", 0, {}],
"called with key, trafficType, eventName, 0, {}"
);
t.end();
});

t.test("track: ok with details", async (t) => {
const { provider, calls } = makeProviderWithSpy();
await provider.track(
"purchase",
{ targetingKey: "u1", trafficType: "user" },
{ value: 9.99, properties: { plan: "pro", beta: true } }
);

t.equal(calls.count, 1, "Split track called once");
t.equal(calls.args[0], "u1");
t.equal(calls.args[1], "user");
t.equal(calls.args[2], "purchase");
t.equal(calls.args[3], 9.99);
t.deepEqual(calls.args[4], { plan: "pro", beta: true });
t.end();
});
}

trackingSuite(assert);

shouldFailWithBadApiKeyTest();

evalBooleanNullEmptyTest();
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/testUtils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { OpenFeatureSplitProvider } from "../..";

const DEFAULT_ERROR_MARGIN = 50; // 0.05 secs

/**
Expand Down Expand Up @@ -67,3 +69,23 @@ export function url(settings, target) {
}
return `${settings.urls.sdk}${target}`;
}


/**
* Create a spy for the OpenFeatureSplitProvider.
* @returns {provider: OpenFeatureSplitProvider, calls: {count: number, args: any[]}}
*/
export function makeProviderWithSpy() {
const calls = { count: 0, args: null };
const track = (...args) => { calls.count++; calls.args = args; return true; };

const splitClient = {
__getStatus: () => ({ isReady: true }),
on: () => {},
Event: { SDK_READY: "SDK_READY" },
track,
getTreatmentWithConfig: () => ({ treatment: "on", config: "" }),
};

return { provider: new OpenFeatureSplitProvider({ splitClient }), calls };
}
48 changes: 44 additions & 4 deletions src/lib/js-split-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
JsonValue,
TargetingKeyMissingError,
StandardResolutionReasons,
TrackingEventDetails,
} from "@openfeature/server-sdk";
import type SplitIO from "@splitsoftware/splitio/types/splitio";

Expand Down Expand Up @@ -53,16 +54,17 @@ export class OpenFeatureSplitProvider implements Provider {
flagKey,
this.transformContext(context)
);
const flagName = details.value.toLowerCase();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flagName sounds like the feature flag name, but here the variable refers to the treatment value, right? So flagValue or flagTreatment are more appropriate names.


if ( details.value === "on" || details.value === "true" ) {
if ( flagName === "on" || flagName === "true" ) {
return { ...details, value: true };
}

if ( details.value === "off" || details.value === "false" ) {
if ( flagName === "off" || flagName === "false" ) {
return { ...details, value: false };
}

throw new ParseError(`Invalid boolean value for ${details.value}`);
throw new ParseError(`Invalid boolean value for ${flagName}`);
}

async resolveStringEvaluation(
Expand Down Expand Up @@ -119,7 +121,7 @@ export class OpenFeatureSplitProvider implements Provider {
if (value === CONTROL_TREATMENT) {
throw new FlagNotFoundError(CONTROL_VALUE_ERROR_MESSAGE);
}
const flagMetadata = config ? JSON.parse(config) : undefined;
const flagMetadata = { config: config ? config : '' };
const details: ResolutionDetails<string> = {
value: value,
variant: value,
Expand All @@ -130,6 +132,44 @@ export class OpenFeatureSplitProvider implements Provider {
}
}

async track(
trackingEventName: string,
context: EvaluationContext,
details: TrackingEventDetails
): Promise<void> {

// targetingKey is always required
const { targetingKey } = context;
if (targetingKey == null || targetingKey === "")
throw new TargetingKeyMissingError();

// eventName is always required
if (trackingEventName == null || trackingEventName === "")
throw new ParseError("Missing eventName, required to track");

// trafficType is always required
const ttVal = context["trafficType"];
const trafficType =
ttVal != null && typeof ttVal === "string" && ttVal.trim() !== ""
? ttVal
: null;
if (trafficType == null || trafficType === "")
throw new ParseError("Missing trafficType variable, required to track");

let value = 0;
Copy link
Contributor

@EmilianoSanchez EmilianoSanchez Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value should not be 0 by default, but undefined. We should support the default case where the user doesn't want to send a value with the event. For example when calling track without value: client.track(targettingKey, trafficType, trackingEventName);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, but internally the sdk sends 0 if value is undefined, so it will not have major difference

let properties: SplitIO.Properties = {};
if (details != null) {
if (details.value != null) {
value = details.value;
}
if (details.properties != null) {
properties = details.properties as SplitIO.Properties;
}
}

this.client.track(targetingKey, trafficType, trackingEventName, value, properties);
}

//Transform the context into an object useful for the Split API, an key string with arbitrary Split "Attributes".
private transformContext(context: EvaluationContext): Consumer {
const { targetingKey, ...attributes } = context;
Expand Down