Skip to content

Commit

Permalink
clean and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ABTastyAdel committed Oct 8, 2024
1 parent cdf5179 commit 68394e9
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 48 deletions.
2 changes: 1 addition & 1 deletion example/lib/Providers/fs_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class FSData extends ChangeNotifier {
}

class UserData extends ChangeNotifier {
String _visitorId = "flutter_user252";
String _visitorId = "flutter_user253";
Map<String, Object> context = {
"testing_tracking_manager": true,
"isQA": true,
Expand Down
31 changes: 10 additions & 21 deletions example/lib/widgets/Modifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'dart:convert';

import 'package:flagship/model/flag.dart';
import 'package:flagship/status.dart';
import 'package:flagship/visitor.dart';
import 'package:flagship/model/visitor_flag.dart';
import 'package:flagship_qa/widgets/FSinputField.dart';
import 'package:flagship_qa/widgets/modifications_json_screen.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -128,31 +126,23 @@ class _ModificationsState extends State<Modifications> {

// Get json view
_getJsonView(BuildContext ctx) {
// var flagCollection = Flagship.getCurrentVisitor()?.getFlags();

// if (flagCollection != null) {
// flagCollection.flags.forEach((itemkey, itemValue) {
// print(itemValue.metadata().toJson().toString());
// });
// }

Navigator.of(ctx)
.pushNamed(ModificationsJSONScreen.routeName, arguments: {});
}

_getCollection() {
Visitor? v1 = Flagship.getCurrentVisitor();
// _getCollection() {
// Visitor? v1 = Flagship.getCurrentVisitor();

if (v1 != null) {
FSFlagCollection flagCollection = v1.getFlags();
// if (v1 != null) {
// FSFlagCollection flagCollection = v1.getFlags();

var flag = flagCollection['btnColor'];
// var flag = flagCollection['btnColor'];

flag.value(12121);
// flag.value(12121);

flag.visitorExposed();
}
}
// flag.visitorExposed();
// }
// }

void _resetField() {
variationId = "None";
Expand Down Expand Up @@ -308,8 +298,7 @@ class _ModificationsState extends State<Modifications> {
child: ElevatedButton(
child: Text("JSON VIEW"),
onPressed: () {
_getCollection();
// _getJsonView(context);
_getJsonView(context);
},
),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/api/endpoints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Endpoints {
static const String DECISION_API = "https://decision.flagship.io/v2/";
static const String CAMPAIGNS =
"/campaigns/?exposeAllKeys=true&extras[]=accountSettings";
static const String ACTIVATION = "activate"; // TODO remove extra
static const String ACTIVATION = "activate";

// Bucketing
static const String BUCKETING_SCRIPT =
Expand Down
4 changes: 2 additions & 2 deletions lib/flagship_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ConfigBuilder {
// _pollingTime
int _pollingTime = 60;

// StatusListener
// _onSdkStatusChanged
SdkStatusChanged? _onSdkStatusChanged;

// Tracking Config
Expand Down Expand Up @@ -135,7 +135,7 @@ class ConfigBuilder {
return this;
}

// StatusListener
// onSdkStatusChanged
ConfigBuilder onSdkStatusChanged(SdkStatusChanged pSdkStatusChanged) {
_onSdkStatusChanged = pSdkStatusChanged;
return this;
Expand Down
3 changes: 0 additions & 3 deletions lib/hits/activate.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:flagship/flagship.dart';
import 'package:flagship/hits/hit.dart';
import 'package:flagship/model/exposed_flag.dart';
Expand Down Expand Up @@ -114,7 +112,6 @@ class Activate extends BaseHit {
this.modification != null);
}

/// TODO create a real one later
FSExposedInfo? getExposedInfo() {
if (this.exposure_flag != null) {
// Create map for exposeFlag
Expand Down
1 change: 0 additions & 1 deletion lib/visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ class Visitor {
// if the context still the same then no need to raise the warning
// Update flagSyncStatus to raise a warning when access to flag
this._flagSyncStatus = FlagSyncStatus.CONTEXT_UPDATED;
// TODO factorise with syncStaus
flagStatus = FlagStatus.FETCH_REQUIRED;
_fetchReasons = FetchFlagsRequiredStatusReason.VISITOR_CONTEXT_UPDATED;
}
Expand Down
35 changes: 19 additions & 16 deletions lib/visitor/strategy/default_strategy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ class DefaultStrategy implements IVisitor {
DataUsageTracking.sharedInstance()
.processTroubleShootingException(visitor, error);
return FetchResponse(FlagStatus.FETCH_REQUIRED, Error());
// TODO create better object error
// return Error(); // Return Error
}
}

Expand All @@ -248,6 +246,11 @@ class DefaultStrategy implements IVisitor {
@override
authenticateVisitor(String pVisitorId) {
if (visitor.config.decisionMode == Mode.DECISION_API) {
if (visitor.anonymousId == null) {
visitor.anonymousId = visitor.visitorId;
visitor.visitorId = pVisitorId;
}

DataUsageTracking.sharedInstance()
.processTSXpc(CriticalPoints.VISITOR_AUTHENTICATE.name, this.visitor);
} else {
Expand All @@ -259,12 +262,12 @@ class DefaultStrategy implements IVisitor {
@override
unAuthenticateVisitor() {
if (visitor.config.decisionMode == Mode.DECISION_API) {
DataUsageTracking.sharedInstance().processTSXpc(
CriticalPoints.VISITOR_UNAUTHENTICATE.name, this.visitor);
if (visitor.anonymousId != null) {
visitor.visitorId = visitor.anonymousId as String;
visitor.anonymousId = null;
}
DataUsageTracking.sharedInstance().processTSXpc(
CriticalPoints.VISITOR_UNAUTHENTICATE.name, this.visitor);
} else {
Flagship.logger(Level.ALL,
"unAuthenticateVisitor method will be ignored in Bucketing configuration");
Expand Down Expand Up @@ -363,19 +366,19 @@ class DefaultStrategy implements IVisitor {
FlagMetadata.withMap(pModification.toJsonInformation())));
}

void onExposureBis(List<FSExposedInfo> exposureInfos) {
print(" @@@@@@@@@ callback exposure is called with " +
exposureInfos.length.toString() +
" Activate @@@@@@@@@@@@@@@@@");
exposureInfos.forEach((item) {
print(" onExposure item " + item.visitorExposed.id);
// void onExposureBis(List<FSExposedInfo> exposureInfos) {
// print(" @@@@@@@@@ callback exposure is called with " +
// exposureInfos.length.toString() +
// " Activate @@@@@@@@@@@@@@@@@");
// exposureInfos.forEach((item) {
// print(" onExposure item " + item.visitorExposed.id);

Flagship.sharedInstance()
.getConfiguration()
?.onVisitorExposed
?.call(item.visitorExposed, item.exposedFlag);
});
}
// Flagship.sharedInstance()
// .getConfiguration()
// ?.onVisitorExposed
// ?.call(item.visitorExposed, item.exposedFlag);
// });
// }

@override
FlagStatus getFlagStatus(String key) {
Expand Down
8 changes: 6 additions & 2 deletions lib/visitor/strategy/no_consent_strategy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ class NoConsentStrategy extends DefaultStrategy {
}
}

// TODO, not consent have not lookup visitor
@override
Future<bool> lookupVisitor(String visitoId) async {
Flagship.logger(Level.INFO, "No lookup Visitor when not consent");
return false;
}

@override
void lookupHits() async {
Flagship.logger(Level.INFO, "No lookup Hits when not ready");
Flagship.logger(Level.INFO, "No lookup Hits when not consent");
}

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/visitor/visitor_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class VisitorDelegate implements IVisitor {
} else {
return DefaultStrategy(visitor);
}
case FSSdkStatus.SDK_INITIALIZING: // TODO check this part
case FSSdkStatus.SDK_INITIALIZING:
return NotReadyStrategy(visitor);
}
}
Expand Down
72 changes: 72 additions & 0 deletions test/activate_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import 'package:flagship/flagship.dart';
import 'package:flagship/flagship_config.dart';
import 'package:flagship/model/exposed_flag.dart';
import 'package:flagship/model/flag.dart';
import 'package:flagship/model/modification.dart';
import 'package:flagship/model/visitor_exposed.dart';
import 'package:flagship/visitor/strategy/default_strategy.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flagship/hits/activate.dart';

void main() {
WidgetsFlutterBinding.ensureInitialized();
test("Activate with Modification object ", () {
Modification fakeModif = Modification(
"key",
Expand All @@ -26,4 +34,68 @@ void main() {
expect(fakeJson["cid"], "envId");
expect(fakeJson["aid"], "anonym1");
});

test("OnExposureCallback", () {
var expoConfig = ConfigBuilder().withOnVisitorExposed((v, f) {
expect(f.metadata().campaignId, "campaignId");
expect(v.id, "expoVisitor");
}).build();
Flagship.start("bkk9glocmjcg0vtmdlrr", "apiKey", config: expoConfig);
var expoVisitor =
Flagship.newVisitor(visitorId: "expoVisitor", hasConsented: true)
.withContext({"expoKey": "expoVal"}).build();
// Create a default strategy
var dfltStrategy = DefaultStrategy(expoVisitor);

// Create Modification
var expoModif = Modification(
"key",
"campaignId",
"campaignName",
"variationGroupId",
"variationGroupName",
"variationId",
"variationName",
true,
"AB",
"slug",
"value");
// Trigger the callback
dfltStrategy.onExposure(expoModif);
});

test("OnExposureObject", () {
var expoConfig = ConfigBuilder().withOnVisitorExposed((v, f) {
expect(f.metadata().campaignId, "campaignId");
expect(v.id, "expoVisitorObj");
}).build();
Flagship.start("bkk9glocmjcg0vtmdlrr", "apiKey", config: expoConfig);
var expoVisitorObj =
Flagship.newVisitor(visitorId: "expoVisitorObj", hasConsented: true)
.withContext({"expoKey": "expoVal"}).build();
// Create a default strategy
var dfltStrategy = DefaultStrategy(expoVisitorObj);

// Create Modification
var expoModif = Modification(
"key",
"campaignId",
"campaignName",
"variationGroupId",
"variationGroupName",
"variationId",
"variationName",
true,
"AB",
null,
"value");
// Trigger the callback
dfltStrategy.onExposure(expoModif);

// Check brut objs
var vE = VisitorExposed("is", null, {});
expect(vE.anonymousId, null);
var eF = ExposedFlag("key", 12, 12, FlagMetadata.withMap({}));
expect(eF.metadata().campaignId, "");
});
}

0 comments on commit 68394e9

Please sign in to comment.