flagsmith-nodejs 6.1.0 → 6.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,7 +9,7 @@ import {
9
9
  badFetch
10
10
  } from './utils.js';
11
11
  import { DefaultFlag, Flags } from '../../sdk/models.js';
12
- import { delay } from '../../sdk/utils.js';
12
+ import { delay, getUserAgent } from '../../sdk/utils.js';
13
13
  import { EnvironmentModel } from '../../flagsmith-engine/environments/models.js';
14
14
  import { BaseOfflineHandler } from '../../sdk/offline_handlers.js';
15
15
  import { Agent } from 'undici';
@@ -40,6 +40,160 @@ test('test_update_environment_sets_environment', async () => {
40
40
  expect(await flg.getEnvironment()).toStrictEqual(model);
41
41
  });
42
42
 
43
+ test('test_update_environment_handles_paginated_document', async () => {
44
+ type EnvDocumentMockResponse = {
45
+ responseHeader: string | null;
46
+ page: any;
47
+ };
48
+
49
+ const createMockFetch = (pages: EnvDocumentMockResponse[]) => {
50
+ let callCount = 0;
51
+ return vi.fn((url: string, options?: RequestInit) => {
52
+ if (url.includes('/environment-document')) {
53
+ const document = envDocumentMockResponse[callCount];
54
+ if (document) {
55
+ callCount++;
56
+
57
+ const responseHeaders: Record<string, string> = {};
58
+
59
+ if (document.responseHeader) {
60
+ responseHeaders['Link'] = `<${document.responseHeader}>; rel="next"`;
61
+ }
62
+
63
+ return Promise.resolve(
64
+ new Response(JSON.stringify(document.page), {
65
+ status: 200,
66
+ headers: responseHeaders
67
+ })
68
+ );
69
+ }
70
+ }
71
+ return Promise.resolve(new Response('unknown url ' + url, { status: 404 }));
72
+ });
73
+ };
74
+
75
+ const envDocumentMockResponse: EnvDocumentMockResponse[] = [
76
+ {
77
+ responseHeader: '/api/v1/environment-document?page=2',
78
+ page: {
79
+ id: 1,
80
+ api_key: 'test-key',
81
+ project: {
82
+ id: 1,
83
+ name: 'test',
84
+ organisation: {
85
+ id: 1,
86
+ name: 'Test Org',
87
+ feature_analytics: false,
88
+ persist_trait_data: true,
89
+ stop_serving_flags: false
90
+ },
91
+ hide_disabled_flags: false,
92
+ segments: []
93
+ },
94
+ feature_states: [
95
+ {
96
+ feature_state_value: 'first_page_feature_state',
97
+ multivariate_feature_state_values: [],
98
+ django_id: 81027,
99
+ feature: {
100
+ id: 15058,
101
+ type: 'STANDARD',
102
+ name: 'string_feature'
103
+ },
104
+ enabled: false
105
+ },
106
+ {
107
+ feature_state_value: 'second_page_feature_state',
108
+ multivariate_feature_state_values: [],
109
+ django_id: 81027,
110
+ feature: {
111
+ id: 15058,
112
+ type: 'STANDARD',
113
+ name: 'string_feature'
114
+ },
115
+ enabled: false
116
+ },
117
+ {
118
+ feature_state_value: 'third_page_feature_state',
119
+ multivariate_feature_state_values: [],
120
+ django_id: 81027,
121
+ feature: {
122
+ id: 15058,
123
+ type: 'STANDARD',
124
+ name: 'string_feature'
125
+ },
126
+ enabled: false
127
+ }
128
+ ],
129
+ identity_overrides: [{ id: 1, identifier: 'user1' }]
130
+ }
131
+ },
132
+ {
133
+ responseHeader: '/api/v1/environment-document?page=3',
134
+ page: {
135
+ api_key: 'test-key',
136
+ project: {
137
+ id: 1,
138
+ name: 'test',
139
+ organisation: {
140
+ id: 1,
141
+ name: 'Test Org',
142
+ feature_analytics: false,
143
+ persist_trait_data: true,
144
+ stop_serving_flags: false
145
+ },
146
+ hide_disabled_flags: false,
147
+ segments: []
148
+ },
149
+ feature_states: [],
150
+ identity_overrides: [{ id: 2, identifier: 'user2' }]
151
+ }
152
+ },
153
+ {
154
+ responseHeader: null,
155
+ page: {
156
+ api_key: 'test-key',
157
+ project: {
158
+ id: 1,
159
+ name: 'test',
160
+ organisation: {
161
+ id: 1,
162
+ name: 'Test Org',
163
+ feature_analytics: false,
164
+ persist_trait_data: true,
165
+ stop_serving_flags: false
166
+ },
167
+ hide_disabled_flags: false,
168
+ segments: []
169
+ },
170
+ feature_states: [],
171
+ identity_overrides: [{ id: 2, identifier: 'user3' }]
172
+ }
173
+ }
174
+ ];
175
+
176
+ const flg = new Flagsmith({
177
+ environmentKey: 'ser.key',
178
+ enableLocalEvaluation: true,
179
+ fetch: createMockFetch(envDocumentMockResponse)
180
+ });
181
+
182
+ const environment = await flg.getEnvironment();
183
+
184
+ expect(environment.identityOverrides).toHaveLength(3);
185
+ expect(environment.identityOverrides[0].identifier).toBe('user1');
186
+ expect(environment.identityOverrides[1].identifier).toBe('user2');
187
+ expect(environment.identityOverrides[2].identifier).toBe('user3');
188
+ expect(environment.featureStates).toHaveLength(3);
189
+ expect(environment.featureStates[0].getValue()).toBe('first_page_feature_state');
190
+ expect(environment.featureStates[1].getValue()).toBe('second_page_feature_state');
191
+ expect(environment.featureStates[2].getValue()).toBe('third_page_feature_state');
192
+ expect(environment.project.name).toBe('test');
193
+ expect(environment.project.organisation.name).toBe('Test Org');
194
+ expect(environment.project.organisation.id).toBe(1);
195
+ });
196
+
43
197
  test('test_set_agent_options', async () => {
44
198
  const agent = new Agent({});
45
199
 
@@ -358,3 +512,10 @@ test('getIdentityFlags succeeds if initial fetch failed then succeeded', async (
358
512
  const flags2 = await flg.getIdentityFlags('test-user');
359
513
  expect(flags2.isFeatureEnabled('some_feature')).toBe(true);
360
514
  });
515
+
516
+ test('get_user_agent_extracts_version_from_package_json', async () => {
517
+ const userAgent = getUserAgent();
518
+ const packageJson = require('../../package.json');
519
+
520
+ expect(userAgent).toBe(`flagsmith-nodejs-sdk/${packageJson.version}`);
521
+ });