11import { FallbackTreatmentsCalculator } from '../' ;
2- import type { FallbackTreatmentConfiguration } from '../../../../types/splitio' ; // adjust path if needed
2+ import type { FallbackTreatmentConfiguration } from '../../../../types/splitio' ;
3+ import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock' ;
4+ import { CONTROL } from '../../../utils/constants' ;
35
4- describe ( 'FallbackTreatmentsCalculator' , ( ) => {
6+ describe ( 'FallbackTreatmentsCalculator' , ( ) => {
7+ const longName = 'a' . repeat ( 101 ) ;
8+
9+ test ( 'logs an error if flag name is invalid - by Flag' , ( ) => {
10+ let config : FallbackTreatmentConfiguration = {
11+ byFlag : {
12+ 'feature A' : { treatment : 'TREATMENT_A' , config : '{ value: 1 }' } ,
13+ } ,
14+ } ;
15+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
16+ expect ( loggerMock . error . mock . calls [ 0 ] [ 0 ] ) . toBe (
17+ 'Fallback treatments - Discarded flag \'feature A\': Invalid flag name (max 100 chars, no spaces)'
18+ ) ;
19+ config = {
20+ byFlag : {
21+ [ longName ] : { treatment : 'TREATMENT_A' , config : '{ value: 1 }' } ,
22+ } ,
23+ } ;
24+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
25+ expect ( loggerMock . error . mock . calls [ 1 ] [ 0 ] ) . toBe (
26+ `Fallback treatments - Discarded flag '${ longName } ': Invalid flag name (max 100 chars, no spaces)`
27+ ) ;
28+
29+ config = {
30+ byFlag : {
31+ 'featureB' : { treatment : longName , config : '{ value: 1 }' } ,
32+ } ,
33+ } ;
34+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
35+ expect ( loggerMock . error . mock . calls [ 2 ] [ 0 ] ) . toBe (
36+ 'Fallback treatments - Discarded treatment for flag \'featureB\': Invalid treatment (max 100 chars and must match pattern)'
37+ ) ;
38+
39+ config = {
40+ byFlag : {
41+ // @ts -ignore
42+ 'featureC' : { config : '{ global: true }' } ,
43+ } ,
44+ } ;
45+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
46+ expect ( loggerMock . error . mock . calls [ 3 ] [ 0 ] ) . toBe (
47+ 'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)'
48+ ) ;
49+
50+ config = {
51+ byFlag : {
52+ // @ts -ignore
53+ 'featureC' : { treatment : 'invalid treatment!' , config : '{ global: true }' } ,
54+ } ,
55+ } ;
56+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
57+ expect ( loggerMock . error . mock . calls [ 4 ] [ 0 ] ) . toBe (
58+ 'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)'
59+ ) ;
60+ } ) ;
61+
62+ test ( 'logs an error if flag name is invalid - global' , ( ) => {
63+ let config : FallbackTreatmentConfiguration = {
64+ global : { treatment : longName , config : '{ value: 1 }' } ,
65+ } ;
66+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
67+ expect ( loggerMock . error . mock . calls [ 2 ] [ 0 ] ) . toBe (
68+ 'Fallback treatments - Discarded treatment for flag \'featureB\': Invalid treatment (max 100 chars and must match pattern)'
69+ ) ;
70+
71+ config = {
72+ // @ts -ignore
73+ global : { config : '{ global: true }' } ,
74+ } ;
75+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
76+ expect ( loggerMock . error . mock . calls [ 3 ] [ 0 ] ) . toBe (
77+ 'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)'
78+ ) ;
79+
80+ config = {
81+ // @ts -ignore
82+ global : { treatment : 'invalid treatment!' , config : '{ global: true }' } ,
83+ } ;
84+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
85+ expect ( loggerMock . error . mock . calls [ 4 ] [ 0 ] ) . toBe (
86+ 'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)'
87+ ) ;
88+ } ) ;
589
690 test ( 'returns specific fallback if flag exists' , ( ) => {
791 const config : FallbackTreatmentConfiguration = {
892 byFlag : {
993 'featureA' : { treatment : 'TREATMENT_A' , config : '{ value: 1 }' } ,
1094 } ,
1195 } ;
12- const calculator = new FallbackTreatmentsCalculator ( config ) ;
96+ const calculator = new FallbackTreatmentsCalculator ( loggerMock , config ) ;
1397 const result = calculator . resolve ( 'featureA' , 'label by flag' ) ;
1498
1599 expect ( result ) . toEqual ( {
@@ -24,7 +108,7 @@ describe('FallbackTreatmentsCalculator', () => {
24108 byFlag : { } ,
25109 global : { treatment : 'GLOBAL_TREATMENT' , config : '{ global: true }' } ,
26110 } ;
27- const calculator = new FallbackTreatmentsCalculator ( config ) ;
111+ const calculator = new FallbackTreatmentsCalculator ( loggerMock , config ) ;
28112 const result = calculator . resolve ( 'missingFlag' , 'label by global' ) ;
29113
30114 expect ( result ) . toEqual ( {
@@ -38,29 +122,29 @@ describe('FallbackTreatmentsCalculator', () => {
38122 const config : FallbackTreatmentConfiguration = {
39123 byFlag : { } ,
40124 } ;
41- const calculator = new FallbackTreatmentsCalculator ( config ) ;
125+ const calculator = new FallbackTreatmentsCalculator ( loggerMock , config ) ;
42126 const result = calculator . resolve ( 'missingFlag' , 'label by noFallback' ) ;
43127
44128 expect ( result ) . toEqual ( {
45- treatment : ' CONTROL' ,
129+ treatment : CONTROL ,
46130 config : null ,
47- label : 'fallback - label by noFallback' ,
131+ label : 'label by noFallback' ,
48132 } ) ;
49133 } ) ;
50134
51135 test ( 'returns undefined label if no label provided' , ( ) => {
52136 const config : FallbackTreatmentConfiguration = {
53137 byFlag : {
54- 'featureB' : { treatment : 'TREATMENT_B' } ,
138+ 'featureB' : { treatment : 'TREATMENT_B' , config : '{ value: 1 }' } ,
55139 } ,
56140 } ;
57- const calculator = new FallbackTreatmentsCalculator ( config ) ;
141+ const calculator = new FallbackTreatmentsCalculator ( loggerMock , config ) ;
58142 const result = calculator . resolve ( 'featureB' ) ;
59143
60144 expect ( result ) . toEqual ( {
61145 treatment : 'TREATMENT_B' ,
62- config : undefined ,
63- label : undefined ,
146+ config : '{ value: 1 }' ,
147+ label : '' ,
64148 } ) ;
65149 } ) ;
66150} ) ;
0 commit comments