|
| 1 | +import { Source } from '../src/config'; |
1 | 2 | import { ExperimentClient } from '../src/experimentClient'; |
2 | | -import { ExperimentUser } from '../src/types/user'; |
| 3 | +import { ExperimentUser, ExperimentUserProvider } from '../src/types/user'; |
| 4 | +import { Variant, Variants } from '../src/types/variant'; |
| 5 | + |
| 6 | +const delay = (ms: number) => new Promise((res) => setTimeout(res, ms)); |
| 7 | + |
| 8 | +class TestUserProvider implements ExperimentUserProvider { |
| 9 | + getUser(): ExperimentUser { |
| 10 | + return testUser; |
| 11 | + } |
| 12 | +} |
3 | 13 |
|
4 | 14 | const API_KEY = 'client-DvWljIjiiuqLbyjqdvBaLFfEBrAvGuA3'; |
5 | 15 |
|
6 | 16 | const testUser: ExperimentUser = { user_id: 'test_user' }; |
7 | | -const testClient: ExperimentClient = new ExperimentClient(API_KEY, {}); |
8 | 17 |
|
9 | | -const testTimeoutNoRetriesClient = new ExperimentClient(API_KEY, { |
10 | | - retryFetchOnFailure: false, |
11 | | - fetchTimeoutMillis: 1, |
12 | | -}); |
| 18 | +const serverKey = 'sdk-ci-test'; |
| 19 | +const serverVariant: Variant = { value: 'on', payload: 'payload' }; |
| 20 | +const serverOffVariant: Variant = { value: 'off' }; |
13 | 21 |
|
14 | | -const testTimeoutRetrySuccessClient = new ExperimentClient(API_KEY, { |
15 | | - fetchTimeoutMillis: 1, |
16 | | -}); |
| 22 | +const initialKey = 'initial-key'; |
| 23 | +const initialVariant: Variant = { value: 'initial' }; |
| 24 | + |
| 25 | +const initialVariants: Variants = { |
| 26 | + 'sdk-ci-test': serverOffVariant, |
| 27 | + 'initial-key': initialVariant, |
| 28 | +}; |
| 29 | + |
| 30 | +const fallbackVariant: Variant = { value: 'fallback', payload: 'payload' }; |
| 31 | +const explicitFallbackString = 'first'; |
| 32 | +const explicitFallbackVariant: Variant = { value: explicitFallbackString }; |
| 33 | +const unknownKey = 'not-a-valid-key'; |
17 | 34 |
|
18 | 35 | beforeEach(() => { |
19 | 36 | localStorage.clear(); |
20 | 37 | }); |
21 | 38 |
|
| 39 | +/** |
| 40 | + * Basic test that fetching variants for a user succeeds. |
| 41 | + */ |
22 | 42 | test('ExperimentClient.fetch, success', async () => { |
23 | | - await testClient.fetch(testUser); |
24 | | - const variant = testClient.variant('sdk-ci-test'); |
25 | | - expect(variant).toEqual({ value: 'on', payload: 'payload' }); |
| 43 | + const client = new ExperimentClient(API_KEY, {}); |
| 44 | + await client.fetch(testUser); |
| 45 | + const variant = client.variant(serverKey); |
| 46 | + expect(variant).toEqual(serverVariant); |
26 | 47 | }); |
27 | 48 |
|
| 49 | +/** |
| 50 | + * Test that a timed out fetch request with retries disabled does not fetch any |
| 51 | + * variants. |
| 52 | + */ |
28 | 53 | test('ExperimentClient.fetch, no retries, timeout failure', async () => { |
29 | | - await testTimeoutNoRetriesClient.fetch(testUser); |
30 | | - const variants = testTimeoutNoRetriesClient.all(); |
| 54 | + const client = new ExperimentClient(API_KEY, { |
| 55 | + retryFetchOnFailure: false, |
| 56 | + fetchTimeoutMillis: 1, |
| 57 | + }); |
| 58 | + await client.fetch(testUser); |
| 59 | + const variants = client.all(); |
31 | 60 | expect(variants).toEqual({}); |
32 | 61 | }); |
33 | 62 |
|
34 | | -test('ExperimentClient.fetch, no retries, timeout failure', async () => { |
35 | | - await testTimeoutRetrySuccessClient.fetch(testUser); |
36 | | - const variant = testClient.variant('sdk-ci-test'); |
| 63 | +/** |
| 64 | + * Test that a timed out fetch request with (background) retries enabled will |
| 65 | + * complete successfully within a reasonable amount of time. |
| 66 | + */ |
| 67 | +test('ExperimentClient.fetch, with retries, retry success', async () => { |
| 68 | + const client = new ExperimentClient(API_KEY, { |
| 69 | + fallbackVariant: fallbackVariant, |
| 70 | + fetchTimeoutMillis: 1, |
| 71 | + }); |
| 72 | + await client.fetch(testUser); |
| 73 | + let variant = client.variant(serverKey); |
| 74 | + expect(variant).toEqual(fallbackVariant); |
| 75 | + await delay(2000); |
| 76 | + variant = client.variant(serverKey); |
| 77 | + expect(variant).toEqual(serverVariant); |
| 78 | +}); |
| 79 | + |
| 80 | +/** |
| 81 | + * Test that the client always prefers the explicit fallback over any |
| 82 | + * configured fallbacks when there are no stored variants--even if the |
| 83 | + * provided key is present in the initialVariants config. |
| 84 | + */ |
| 85 | +test('ExperimentClient.variant, no stored variants, explicit fallback returned', () => { |
| 86 | + let variant: Variant; |
| 87 | + const client = new ExperimentClient(API_KEY, { |
| 88 | + fallbackVariant: fallbackVariant, |
| 89 | + initialVariants: initialVariants, |
| 90 | + }); |
| 91 | + |
| 92 | + variant = client.variant(unknownKey, explicitFallbackVariant); |
| 93 | + expect(variant).toEqual(explicitFallbackVariant); |
| 94 | + |
| 95 | + variant = client.variant(unknownKey, explicitFallbackString); |
| 96 | + expect(variant).toEqual(explicitFallbackVariant); |
| 97 | + |
| 98 | + variant = client.variant(initialKey, explicitFallbackVariant); |
| 99 | + expect(variant).toEqual(explicitFallbackVariant); |
| 100 | + |
| 101 | + variant = client.variant(initialKey, explicitFallbackString); |
| 102 | + expect(variant).toEqual(explicitFallbackVariant); |
| 103 | +}); |
| 104 | + |
| 105 | +/** |
| 106 | + * Test that the client falls back to the configured `fallbackVariant` for an |
| 107 | + * unknown key when no explicit fallback is provided. |
| 108 | + */ |
| 109 | +test('ExperimentClient.variant, unknown key returns default fallback', () => { |
| 110 | + const client = new ExperimentClient(API_KEY, { |
| 111 | + fallbackVariant: fallbackVariant, |
| 112 | + initialVariants: initialVariants, |
| 113 | + }); |
| 114 | + const variant: Variant = client.variant(unknownKey); |
| 115 | + expect(variant).toEqual(fallbackVariant); |
| 116 | +}); |
| 117 | + |
| 118 | +/** |
| 119 | + * Test that the client falls back to the configured `initialVariants` for |
| 120 | + * flag keys included in the initial set. After a fetch, the client should |
| 121 | + * take flags from local storage instead. |
| 122 | + */ |
| 123 | +test('ExperimentClient.variant, initial variants fallback before fetch, no fallback after fetch', async () => { |
| 124 | + let variant: Variant; |
| 125 | + const client = new ExperimentClient(API_KEY, { |
| 126 | + fallbackVariant: fallbackVariant, |
| 127 | + initialVariants: initialVariants, |
| 128 | + }); |
| 129 | + |
| 130 | + variant = client.variant(initialKey); |
| 131 | + expect(variant).toEqual(initialVariant); |
| 132 | + |
| 133 | + variant = client.variant(serverKey); |
| 134 | + expect(variant).toEqual(serverOffVariant); |
| 135 | + |
| 136 | + await client.fetch(); |
| 137 | + |
| 138 | + variant = client.variant(initialKey); |
| 139 | + expect(variant).toEqual(initialVariant); |
| 140 | + |
| 141 | + variant = client.variant(serverKey); |
| 142 | + expect(variant).toEqual(serverVariant); |
| 143 | +}); |
| 144 | + |
| 145 | +/** |
| 146 | + * Calling `all()` prior to fetch with an empty storage returns configured |
| 147 | + * initial variants. |
| 148 | + */ |
| 149 | +test('ExperimentClient.all, initial variants returned', async () => { |
| 150 | + const client = new ExperimentClient(API_KEY, { |
| 151 | + initialVariants: initialVariants, |
| 152 | + }); |
| 153 | + const variants = client.all(); |
| 154 | + expect(variants).toEqual(initialVariants); |
| 155 | +}); |
| 156 | + |
| 157 | +/** |
| 158 | + * Setting source to initial variants will prioritize variants in initial |
| 159 | + * variants over those stored in local storage. |
| 160 | + */ |
| 161 | +test('ExperimentClient.fetch, initial variants source, prefer initial', async () => { |
| 162 | + const client = new ExperimentClient(API_KEY, { |
| 163 | + source: Source.InitialVariants, |
| 164 | + initialVariants: initialVariants, |
| 165 | + }); |
| 166 | + let variant = client.variant(serverKey); |
| 167 | + expect(variant).toEqual(serverOffVariant); |
| 168 | + await client.fetch(testUser); |
| 169 | + variant = client.variant(serverKey); |
| 170 | + expect(variant).toEqual(serverOffVariant); |
| 171 | +}); |
| 172 | + |
| 173 | +/** |
| 174 | + * Test that fetch with an explicit user arguement will set the user within the |
| 175 | + * client, and calling setUser() after will overwrite the user. |
| 176 | + */ |
| 177 | +test('ExperimentClient.fetch, sets user, setUser overrides', async () => { |
| 178 | + const client = new ExperimentClient(API_KEY, {}); |
| 179 | + await client.fetch(testUser); |
| 180 | + expect(client.getUser()).toEqual(testUser); |
| 181 | + const newUser = { user_id: 'new_test_user' }; |
| 182 | + client.setUser(newUser); |
| 183 | + expect(client.getUser()).toEqual(newUser); |
| 184 | +}); |
| 185 | + |
| 186 | +/** |
| 187 | + * Test that fetch with a user provided by a user provider rather than an |
| 188 | + * explicit user argument is successful. |
| 189 | + */ |
| 190 | +test('ExperimentClient.fetch, with user provider, success', async () => { |
| 191 | + const client = new ExperimentClient(API_KEY, {}).setUserProvider( |
| 192 | + new TestUserProvider(), |
| 193 | + ); |
| 194 | + await client.fetch(); |
| 195 | + const variant = client.variant('sdk-ci-test'); |
37 | 196 | expect(variant).toEqual({ value: 'on', payload: 'payload' }); |
38 | 197 | }); |
0 commit comments