mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	test: advanced playground (#3968)
This commit is contained in:
		
							parent
							
								
									b91b7276c5
								
							
						
					
					
						commit
						edd67f7046
					
				
							
								
								
									
										254
									
								
								src/lib/features/playground/advanced-playground.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										254
									
								
								src/lib/features/playground/advanced-playground.test.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,254 @@
 | 
				
			|||||||
 | 
					import {
 | 
				
			||||||
 | 
					    IUnleashTest,
 | 
				
			||||||
 | 
					    setupAppWithCustomConfig,
 | 
				
			||||||
 | 
					} from '../../../test/e2e/helpers/test-helper';
 | 
				
			||||||
 | 
					import dbInit, { ITestDb } from '../../../test/e2e/helpers/database-init';
 | 
				
			||||||
 | 
					import getLogger from '../../../test/fixtures/no-logger';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					let app: IUnleashTest;
 | 
				
			||||||
 | 
					let db: ITestDb;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					beforeAll(async () => {
 | 
				
			||||||
 | 
					    db = await dbInit('advanced_playground', getLogger);
 | 
				
			||||||
 | 
					    app = await setupAppWithCustomConfig(
 | 
				
			||||||
 | 
					        db.stores,
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            experimental: {
 | 
				
			||||||
 | 
					                flags: {
 | 
				
			||||||
 | 
					                    advancedPlayground: true,
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        db.rawDatabase,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const createFeatureToggle = async (featureName: string) => {
 | 
				
			||||||
 | 
					    await app.request
 | 
				
			||||||
 | 
					        .post('/api/admin/projects/default/features')
 | 
				
			||||||
 | 
					        .send({
 | 
				
			||||||
 | 
					            name: featureName,
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					        .set('Content-Type', 'application/json')
 | 
				
			||||||
 | 
					        .expect(201);
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const createFeatureToggleWithStrategy = async (featureName: string) => {
 | 
				
			||||||
 | 
					    await createFeatureToggle(featureName);
 | 
				
			||||||
 | 
					    return app.request
 | 
				
			||||||
 | 
					        .post(
 | 
				
			||||||
 | 
					            `/api/admin/projects/default/features/${featureName}/environments/default/strategies`,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        .send({
 | 
				
			||||||
 | 
					            name: 'default',
 | 
				
			||||||
 | 
					            parameters: {},
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					        .expect(200);
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const enableToggle = (featureName: string) =>
 | 
				
			||||||
 | 
					    app.request
 | 
				
			||||||
 | 
					        .post(
 | 
				
			||||||
 | 
					            `/api/admin/projects/default/features/${featureName}/environments/default/on`,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        .send({})
 | 
				
			||||||
 | 
					        .expect(200);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					afterAll(async () => {
 | 
				
			||||||
 | 
					    await app.destroy();
 | 
				
			||||||
 | 
					    await db.destroy();
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test('advanced playground evaluation with no toggles', async () => {
 | 
				
			||||||
 | 
					    const { body: result } = await app.request
 | 
				
			||||||
 | 
					        .post('/api/admin/playground/advanced')
 | 
				
			||||||
 | 
					        .send({
 | 
				
			||||||
 | 
					            environments: ['default'],
 | 
				
			||||||
 | 
					            projects: ['default'],
 | 
				
			||||||
 | 
					            context: { appName: 'test', userId: '1,2', channel: 'web,mobile' },
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					        .set('Content-Type', 'application/json')
 | 
				
			||||||
 | 
					        .expect(200);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    expect(result).toMatchObject({
 | 
				
			||||||
 | 
					        input: {
 | 
				
			||||||
 | 
					            environments: ['default'],
 | 
				
			||||||
 | 
					            projects: ['default'],
 | 
				
			||||||
 | 
					            context: {
 | 
				
			||||||
 | 
					                appName: 'test',
 | 
				
			||||||
 | 
					                userId: '1,2',
 | 
				
			||||||
 | 
					                channel: 'web,mobile',
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        features: [],
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test('advanced playground evaluation happy path', async () => {
 | 
				
			||||||
 | 
					    await createFeatureToggleWithStrategy('test-playground-feature');
 | 
				
			||||||
 | 
					    await enableToggle('test-playground-feature');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const { body: result } = await app.request
 | 
				
			||||||
 | 
					        .post('/api/admin/playground/advanced')
 | 
				
			||||||
 | 
					        .send({
 | 
				
			||||||
 | 
					            environments: ['default'],
 | 
				
			||||||
 | 
					            projects: ['default'],
 | 
				
			||||||
 | 
					            context: { appName: 'test', userId: '1,2', channel: 'web,mobile' },
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					        .set('Content-Type', 'application/json')
 | 
				
			||||||
 | 
					        .expect(200);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    expect(result).toMatchObject({
 | 
				
			||||||
 | 
					        input: {
 | 
				
			||||||
 | 
					            environments: ['default'],
 | 
				
			||||||
 | 
					            projects: ['default'],
 | 
				
			||||||
 | 
					            context: {
 | 
				
			||||||
 | 
					                appName: 'test',
 | 
				
			||||||
 | 
					                userId: '1,2',
 | 
				
			||||||
 | 
					                channel: 'web,mobile',
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        features: [
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                name: 'test-playground-feature',
 | 
				
			||||||
 | 
					                projectId: 'default',
 | 
				
			||||||
 | 
					                environments: {
 | 
				
			||||||
 | 
					                    default: [
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            isEnabled: true,
 | 
				
			||||||
 | 
					                            isEnabledInCurrentEnvironment: true,
 | 
				
			||||||
 | 
					                            strategies: {
 | 
				
			||||||
 | 
					                                result: true,
 | 
				
			||||||
 | 
					                                data: [
 | 
				
			||||||
 | 
					                                    {
 | 
				
			||||||
 | 
					                                        name: 'default',
 | 
				
			||||||
 | 
					                                        disabled: false,
 | 
				
			||||||
 | 
					                                        parameters: {},
 | 
				
			||||||
 | 
					                                        result: {
 | 
				
			||||||
 | 
					                                            enabled: true,
 | 
				
			||||||
 | 
					                                            evaluationStatus: 'complete',
 | 
				
			||||||
 | 
					                                        },
 | 
				
			||||||
 | 
					                                        constraints: [],
 | 
				
			||||||
 | 
					                                        segments: [],
 | 
				
			||||||
 | 
					                                    },
 | 
				
			||||||
 | 
					                                ],
 | 
				
			||||||
 | 
					                            },
 | 
				
			||||||
 | 
					                            projectId: 'default',
 | 
				
			||||||
 | 
					                            variant: {
 | 
				
			||||||
 | 
					                                name: 'disabled',
 | 
				
			||||||
 | 
					                                enabled: false,
 | 
				
			||||||
 | 
					                            },
 | 
				
			||||||
 | 
					                            name: 'test-playground-feature',
 | 
				
			||||||
 | 
					                            environment: 'default',
 | 
				
			||||||
 | 
					                            context: {
 | 
				
			||||||
 | 
					                                appName: 'test',
 | 
				
			||||||
 | 
					                                userId: '1',
 | 
				
			||||||
 | 
					                                channel: 'web',
 | 
				
			||||||
 | 
					                            },
 | 
				
			||||||
 | 
					                            variants: [],
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            isEnabled: true,
 | 
				
			||||||
 | 
					                            isEnabledInCurrentEnvironment: true,
 | 
				
			||||||
 | 
					                            strategies: {
 | 
				
			||||||
 | 
					                                result: true,
 | 
				
			||||||
 | 
					                                data: [
 | 
				
			||||||
 | 
					                                    {
 | 
				
			||||||
 | 
					                                        name: 'default',
 | 
				
			||||||
 | 
					                                        disabled: false,
 | 
				
			||||||
 | 
					                                        parameters: {},
 | 
				
			||||||
 | 
					                                        result: {
 | 
				
			||||||
 | 
					                                            enabled: true,
 | 
				
			||||||
 | 
					                                            evaluationStatus: 'complete',
 | 
				
			||||||
 | 
					                                        },
 | 
				
			||||||
 | 
					                                        constraints: [],
 | 
				
			||||||
 | 
					                                        segments: [],
 | 
				
			||||||
 | 
					                                    },
 | 
				
			||||||
 | 
					                                ],
 | 
				
			||||||
 | 
					                            },
 | 
				
			||||||
 | 
					                            projectId: 'default',
 | 
				
			||||||
 | 
					                            variant: {
 | 
				
			||||||
 | 
					                                name: 'disabled',
 | 
				
			||||||
 | 
					                                enabled: false,
 | 
				
			||||||
 | 
					                            },
 | 
				
			||||||
 | 
					                            name: 'test-playground-feature',
 | 
				
			||||||
 | 
					                            environment: 'default',
 | 
				
			||||||
 | 
					                            context: {
 | 
				
			||||||
 | 
					                                appName: 'test',
 | 
				
			||||||
 | 
					                                userId: '1',
 | 
				
			||||||
 | 
					                                channel: 'mobile',
 | 
				
			||||||
 | 
					                            },
 | 
				
			||||||
 | 
					                            variants: [],
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            isEnabled: true,
 | 
				
			||||||
 | 
					                            isEnabledInCurrentEnvironment: true,
 | 
				
			||||||
 | 
					                            strategies: {
 | 
				
			||||||
 | 
					                                result: true,
 | 
				
			||||||
 | 
					                                data: [
 | 
				
			||||||
 | 
					                                    {
 | 
				
			||||||
 | 
					                                        name: 'default',
 | 
				
			||||||
 | 
					                                        disabled: false,
 | 
				
			||||||
 | 
					                                        parameters: {},
 | 
				
			||||||
 | 
					                                        result: {
 | 
				
			||||||
 | 
					                                            enabled: true,
 | 
				
			||||||
 | 
					                                            evaluationStatus: 'complete',
 | 
				
			||||||
 | 
					                                        },
 | 
				
			||||||
 | 
					                                        constraints: [],
 | 
				
			||||||
 | 
					                                        segments: [],
 | 
				
			||||||
 | 
					                                    },
 | 
				
			||||||
 | 
					                                ],
 | 
				
			||||||
 | 
					                            },
 | 
				
			||||||
 | 
					                            projectId: 'default',
 | 
				
			||||||
 | 
					                            variant: {
 | 
				
			||||||
 | 
					                                name: 'disabled',
 | 
				
			||||||
 | 
					                                enabled: false,
 | 
				
			||||||
 | 
					                            },
 | 
				
			||||||
 | 
					                            name: 'test-playground-feature',
 | 
				
			||||||
 | 
					                            environment: 'default',
 | 
				
			||||||
 | 
					                            context: {
 | 
				
			||||||
 | 
					                                appName: 'test',
 | 
				
			||||||
 | 
					                                userId: '2',
 | 
				
			||||||
 | 
					                                channel: 'web',
 | 
				
			||||||
 | 
					                            },
 | 
				
			||||||
 | 
					                            variants: [],
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            isEnabled: true,
 | 
				
			||||||
 | 
					                            isEnabledInCurrentEnvironment: true,
 | 
				
			||||||
 | 
					                            strategies: {
 | 
				
			||||||
 | 
					                                result: true,
 | 
				
			||||||
 | 
					                                data: [
 | 
				
			||||||
 | 
					                                    {
 | 
				
			||||||
 | 
					                                        name: 'default',
 | 
				
			||||||
 | 
					                                        disabled: false,
 | 
				
			||||||
 | 
					                                        parameters: {},
 | 
				
			||||||
 | 
					                                        result: {
 | 
				
			||||||
 | 
					                                            enabled: true,
 | 
				
			||||||
 | 
					                                            evaluationStatus: 'complete',
 | 
				
			||||||
 | 
					                                        },
 | 
				
			||||||
 | 
					                                        constraints: [],
 | 
				
			||||||
 | 
					                                        segments: [],
 | 
				
			||||||
 | 
					                                    },
 | 
				
			||||||
 | 
					                                ],
 | 
				
			||||||
 | 
					                            },
 | 
				
			||||||
 | 
					                            projectId: 'default',
 | 
				
			||||||
 | 
					                            variant: {
 | 
				
			||||||
 | 
					                                name: 'disabled',
 | 
				
			||||||
 | 
					                                enabled: false,
 | 
				
			||||||
 | 
					                            },
 | 
				
			||||||
 | 
					                            name: 'test-playground-feature',
 | 
				
			||||||
 | 
					                            environment: 'default',
 | 
				
			||||||
 | 
					                            context: {
 | 
				
			||||||
 | 
					                                appName: 'test',
 | 
				
			||||||
 | 
					                                userId: '2',
 | 
				
			||||||
 | 
					                                channel: 'mobile',
 | 
				
			||||||
 | 
					                            },
 | 
				
			||||||
 | 
					                            variants: [],
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                    ],
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
@ -1,142 +0,0 @@
 | 
				
			|||||||
const sharedDetails = {
 | 
					 | 
				
			||||||
    isEnabled: true,
 | 
					 | 
				
			||||||
    isEnabledInCurrentEnvironment: true,
 | 
					 | 
				
			||||||
    strategies: {
 | 
					 | 
				
			||||||
        result: true,
 | 
					 | 
				
			||||||
        data: [
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                name: 'flexibleRollout',
 | 
					 | 
				
			||||||
                id: '9422b4bb-5f8a-4df5-863a-c2a4c4d83af7',
 | 
					 | 
				
			||||||
                disabled: false,
 | 
					 | 
				
			||||||
                parameters: {
 | 
					 | 
				
			||||||
                    groupId: 'test',
 | 
					 | 
				
			||||||
                    rollout: '100',
 | 
					 | 
				
			||||||
                    stickiness: 'default',
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                result: {
 | 
					 | 
				
			||||||
                    enabled: true,
 | 
					 | 
				
			||||||
                    evaluationStatus: 'complete',
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                constraints: [
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        inverted: false,
 | 
					 | 
				
			||||||
                        values: ['2', '4', '6', '8', '10'],
 | 
					 | 
				
			||||||
                        operator: 'IN',
 | 
					 | 
				
			||||||
                        contextName: 'userId',
 | 
					 | 
				
			||||||
                        caseInsensitive: false,
 | 
					 | 
				
			||||||
                        result: true,
 | 
					 | 
				
			||||||
                    },
 | 
					 | 
				
			||||||
                ],
 | 
					 | 
				
			||||||
                segments: [],
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
        ],
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    variant: {
 | 
					 | 
				
			||||||
        name: 'a',
 | 
					 | 
				
			||||||
        payload: {
 | 
					 | 
				
			||||||
            type: 'string',
 | 
					 | 
				
			||||||
            value: 'a',
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        enabled: true,
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    variants: [
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            name: 'a',
 | 
					 | 
				
			||||||
            weight: 334,
 | 
					 | 
				
			||||||
            payload: {
 | 
					 | 
				
			||||||
                type: 'string',
 | 
					 | 
				
			||||||
                value: 'a',
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            overrides: [],
 | 
					 | 
				
			||||||
            stickiness: 'default',
 | 
					 | 
				
			||||||
            weightType: 'variable',
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            name: 'b',
 | 
					 | 
				
			||||||
            weight: 333,
 | 
					 | 
				
			||||||
            payload: {
 | 
					 | 
				
			||||||
                type: 'string',
 | 
					 | 
				
			||||||
                value: 'b',
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            overrides: [],
 | 
					 | 
				
			||||||
            stickiness: 'default',
 | 
					 | 
				
			||||||
            weightType: 'variable',
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            name: 'c',
 | 
					 | 
				
			||||||
            weight: 333,
 | 
					 | 
				
			||||||
            payload: {
 | 
					 | 
				
			||||||
                type: 'string',
 | 
					 | 
				
			||||||
                value: 'c',
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            overrides: [],
 | 
					 | 
				
			||||||
            stickiness: 'default',
 | 
					 | 
				
			||||||
            weightType: 'variable',
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
    ],
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
const sharedEnv = [
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        ...sharedDetails,
 | 
					 | 
				
			||||||
        context: {
 | 
					 | 
				
			||||||
            appName: 'playground',
 | 
					 | 
				
			||||||
            userId: '1',
 | 
					 | 
				
			||||||
            channel: 'web',
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        ...sharedDetails,
 | 
					 | 
				
			||||||
        context: {
 | 
					 | 
				
			||||||
            appName: 'playground',
 | 
					 | 
				
			||||||
            userId: '2',
 | 
					 | 
				
			||||||
            channel: 'web',
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        ...sharedDetails,
 | 
					 | 
				
			||||||
        context: {
 | 
					 | 
				
			||||||
            appName: 'playground',
 | 
					 | 
				
			||||||
            userId: '1',
 | 
					 | 
				
			||||||
            channel: 'mobile',
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        ...sharedDetails,
 | 
					 | 
				
			||||||
        context: {
 | 
					 | 
				
			||||||
            appName: 'playground',
 | 
					 | 
				
			||||||
            userId: '2',
 | 
					 | 
				
			||||||
            channel: 'mobile',
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
];
 | 
					 | 
				
			||||||
export const fixedAdvancedPlaygroundResponse = {
 | 
					 | 
				
			||||||
    input: {
 | 
					 | 
				
			||||||
        environments: ['development', 'production'],
 | 
					 | 
				
			||||||
        projects: ['default'],
 | 
					 | 
				
			||||||
        context: {
 | 
					 | 
				
			||||||
            appName: 'playground',
 | 
					 | 
				
			||||||
            userId: '1,2',
 | 
					 | 
				
			||||||
            channel: 'web,mobile',
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    features: [
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            environments: {
 | 
					 | 
				
			||||||
                development: sharedEnv,
 | 
					 | 
				
			||||||
                production: sharedEnv,
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            projectId: 'default',
 | 
					 | 
				
			||||||
            name: 'featureA',
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            environments: {
 | 
					 | 
				
			||||||
                development: sharedEnv,
 | 
					 | 
				
			||||||
                production: sharedEnv,
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            projectId: 'default',
 | 
					 | 
				
			||||||
            name: 'featureB',
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
    ],
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user