File size: 14,339 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
import * as ExperimentAssignments from '../experiment-assignments';
import localStorage from '../local-storage';
import * as Requests from '../requests';
import { delayedValue, ONE_DELAY, validExperimentAssignment } from '../test-common';
import * as Timing from '../timing';
import type { Config, ExperimentAssignment } from '../../types';
const spiedMonotonicNow = jest.spyOn( Timing, 'monotonicNow' );
const mockedFetchExperimentAssignment = jest.fn();
const mockedGetAnonId = jest.fn();
const mockedConfig: Config = {
fetchExperimentAssignment: mockedFetchExperimentAssignment,
getAnonId: mockedGetAnonId,
logError: async () => {
throw new Error( 'The tested file should throw and not log.' );
},
isDevelopmentMode: false,
};
function mockFetchExperimentAssignmentToMatchExperimentAssignment(
experimentAssignment: ExperimentAssignment
) {
spiedMonotonicNow.mockImplementationOnce( () => experimentAssignment.retrievedTimestamp );
mockedFetchExperimentAssignment.mockImplementationOnce( () =>
delayedValue(
{
ttl: experimentAssignment.ttl,
variations: {
[ experimentAssignment.experimentName ]: experimentAssignment.variationName,
},
},
ONE_DELAY
)
);
}
beforeEach( () => {
localStorage.clear();
} );
describe( 'fetchExperimentAssignment', () => {
it( 'should successfully fetch and return a well formed response with an anonId', async () => {
mockedGetAnonId.mockImplementationOnce( () => delayedValue( 'asdf', ONE_DELAY ) );
mockFetchExperimentAssignmentToMatchExperimentAssignment( validExperimentAssignment );
const experimentAssignmentWithAnonId = await Requests.fetchExperimentAssignment(
mockedConfig,
validExperimentAssignment.experimentName
);
expect( experimentAssignmentWithAnonId ).toEqual( validExperimentAssignment );
expect( mockedFetchExperimentAssignment.mock.calls ).toEqual( [
[
{
anonId: 'asdf',
experimentName: 'experiment_name_a',
},
],
] );
} );
it( 'should successfully fetch and return a well formed response without an anonId', async () => {
mockedFetchExperimentAssignment.mockReset();
mockedGetAnonId.mockImplementationOnce( () => delayedValue( null, ONE_DELAY ) );
mockFetchExperimentAssignmentToMatchExperimentAssignment( validExperimentAssignment );
const experimentAssignmentWithoutAnonId = await Requests.fetchExperimentAssignment(
mockedConfig,
validExperimentAssignment.experimentName
);
expect( experimentAssignmentWithoutAnonId ).toEqual( validExperimentAssignment );
expect( mockedFetchExperimentAssignment.mock.calls ).toEqual( [
[
{
anonId: null,
experimentName: 'experiment_name_a',
},
],
] );
} );
it( 'should return an experiment assignment with a ttl as the maximum of the ttl provided from the server and the set minimum ttl', async () => {
const outputTtl = async ( inputTtl: number ) => {
mockedFetchExperimentAssignment.mockReset();
mockedGetAnonId.mockImplementationOnce( () => delayedValue( null, ONE_DELAY ) );
mockFetchExperimentAssignmentToMatchExperimentAssignment( {
...validExperimentAssignment,
ttl: inputTtl,
} );
const { ttl } = await Requests.fetchExperimentAssignment(
mockedConfig,
validExperimentAssignment.experimentName
);
return ttl;
};
await expect( outputTtl( ExperimentAssignments.minimumTtl - 1 ) ).resolves.toBe(
ExperimentAssignments.minimumTtl
);
await expect( outputTtl( ExperimentAssignments.minimumTtl ) ).resolves.toBe(
ExperimentAssignments.minimumTtl
);
await expect( outputTtl( ExperimentAssignments.minimumTtl + 1 ) ).resolves.toBe(
ExperimentAssignments.minimumTtl + 1
);
await expect( outputTtl( ExperimentAssignments.minimumTtl + 1000 ) ).resolves.toBe(
ExperimentAssignments.minimumTtl + 1000
);
} );
it( 'should throw for an invalid response', async () => {
mockedGetAnonId.mockImplementationOnce( () => delayedValue( null, ONE_DELAY ) );
mockedFetchExperimentAssignment.mockImplementationOnce( () =>
delayedValue(
{
ttl: 60,
},
ONE_DELAY
)
);
await expect(
Requests.fetchExperimentAssignment( mockedConfig, validExperimentAssignment.experimentName )
).rejects.toThrow( 'Invalid FetchExperimentAssignmentResponse' );
} );
it( 'should throw for multiple experiments in the response', async () => {
mockedGetAnonId.mockImplementationOnce( () => delayedValue( null, ONE_DELAY ) );
spiedMonotonicNow.mockImplementationOnce( () => validExperimentAssignment.retrievedTimestamp );
mockedFetchExperimentAssignment.mockImplementationOnce( () =>
delayedValue(
{
ttl: 60,
variations: {
[ validExperimentAssignment.experimentName ]: validExperimentAssignment.variationName,
[ validExperimentAssignment.experimentName + '_repeat' ]:
validExperimentAssignment.variationName,
},
},
ONE_DELAY
)
);
await expect(
Requests.fetchExperimentAssignment( mockedConfig, validExperimentAssignment.experimentName )
).rejects.toThrow(
'Received multiple experiment assignments while trying to fetch exactly one.'
);
} );
it( 'should return a fallbackExperimentAssignment for no experiments in the response', async () => {
mockedGetAnonId.mockImplementationOnce( () => delayedValue( null, ONE_DELAY ) );
spiedMonotonicNow.mockImplementationOnce( () => validExperimentAssignment.retrievedTimestamp );
spiedMonotonicNow.mockImplementationOnce(
() => validExperimentAssignment.retrievedTimestamp + 1000
);
mockedFetchExperimentAssignment.mockImplementationOnce( () =>
delayedValue(
{
ttl: 60,
variations: {},
},
ONE_DELAY
)
);
await expect(
Requests.fetchExperimentAssignment( mockedConfig, validExperimentAssignment.experimentName )
).resolves.toEqual( {
experimentName: 'experiment_name_a',
variationName: null,
retrievedTimestamp: validExperimentAssignment.retrievedTimestamp + 1000,
ttl: 60,
isFallbackExperimentAssignment: true,
} );
} );
it( 'should throw for response experiment not matching the requested name', async () => {
mockedGetAnonId.mockImplementationOnce( () => delayedValue( null, ONE_DELAY ) );
spiedMonotonicNow.mockImplementationOnce( () => validExperimentAssignment.retrievedTimestamp );
mockFetchExperimentAssignmentToMatchExperimentAssignment( validExperimentAssignment );
await expect(
Requests.fetchExperimentAssignment(
mockedConfig,
validExperimentAssignment.experimentName + '_different_name'
)
).rejects.toThrow(
"Newly fetched ExperimentAssignment's experiment name does not match request."
);
} );
it( 'should throw for returned experiment not being alive', async () => {
jest.spyOn( ExperimentAssignments, 'isAlive' ).mockImplementationOnce( () => false );
mockedGetAnonId.mockImplementationOnce( () => delayedValue( null, ONE_DELAY ) );
mockFetchExperimentAssignmentToMatchExperimentAssignment( validExperimentAssignment );
await expect(
Requests.fetchExperimentAssignment( mockedConfig, validExperimentAssignment.experimentName )
).rejects.toThrow( "Newly fetched experiment isn't alive." );
} );
} );
describe( 'isFetchExperimentAssignmentResponse', () => {
it( 'should return true for valid responses', () => {
expect(
Requests.isFetchExperimentAssignmentResponse( {
ttl: 1,
variations: {},
} )
).toBe( true );
expect(
Requests.isFetchExperimentAssignmentResponse( {
ttl: 60,
variations: {
experiment_a: 'variation_name_a',
experiment_b: null,
},
} )
).toBe( true );
} );
it( 'should return false for responses with 0 ttl', () => {
expect(
Requests.isFetchExperimentAssignmentResponse( {
ttl: 0,
variations: {},
} )
).toBe( false );
expect(
Requests.isFetchExperimentAssignmentResponse( {
ttl: 0,
variations: {
experiment_a: 'variation_name_a',
experiment_b: null,
},
} )
).toBe( false );
} );
it( 'should return false for invalid responses', () => {
expect(
Requests.isFetchExperimentAssignmentResponse( {
ttl: 0,
} )
).toBe( false );
expect(
Requests.isFetchExperimentAssignmentResponse( {
variations: {},
} )
).toBe( false );
expect(
Requests.isFetchExperimentAssignmentResponse( {
ttl: null,
variations: {},
} )
).toBe( false );
expect(
Requests.isFetchExperimentAssignmentResponse( {
ttl: undefined,
variations: {},
} )
).toBe( false );
expect(
Requests.isFetchExperimentAssignmentResponse( {
ttl: 'string',
variations: {},
} )
).toBe( false );
expect(
Requests.isFetchExperimentAssignmentResponse( {
ttl: {},
variations: {},
} )
).toBe( false );
expect(
Requests.isFetchExperimentAssignmentResponse( {
ttl: 0,
variations: null,
} )
).toBe( false );
expect(
Requests.isFetchExperimentAssignmentResponse( {
ttl: 0,
variations: undefined,
} )
).toBe( false );
expect(
Requests.isFetchExperimentAssignmentResponse( {
ttl: 0,
variations: 'string',
} )
).toBe( false );
} );
} );
describe( 'localStorageCachedGetAnonId', () => {
it( 'should return a fresh anonId for non-falsy values', async () => {
const mockGetAnonIdA = jest.fn( async () => 'asdf' );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdA ) ).toBe( 'asdf' );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdA ) ).toBe( 'asdf' );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdA ) ).toBe( 'asdf' );
expect( mockGetAnonIdA ).toHaveBeenCalledTimes( 3 );
const mockGetAnonIdB = jest.fn( async () => 'qwer' );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdB ) ).toBe( 'qwer' );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdB ) ).toBe( 'qwer' );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdB ) ).toBe( 'qwer' );
expect( mockGetAnonIdB ).toHaveBeenCalledTimes( 3 );
} );
it( 'should not cache an anonId for falsy values', async () => {
const mockGetAnonIdNull = jest.fn( async () => null );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( null );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( null );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( null );
expect( mockGetAnonIdNull ).toHaveBeenCalledTimes( 3 );
const mockGetAnonIdEmpty = jest.fn( async () => '' );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdEmpty ) ).toBe( null );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdEmpty ) ).toBe( null );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdEmpty ) ).toBe( null );
expect( mockGetAnonIdEmpty ).toHaveBeenCalledTimes( 3 );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( null );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( null );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( null );
expect( mockGetAnonIdNull ).toHaveBeenCalledTimes( 6 );
} );
it( 'should return a cached anonId for falsy-values within the expiry time', async () => {
const mockGetAnonIdA = jest.fn( async () => 'asdf' );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdA ) ).toBe( 'asdf' );
const mockGetAnonIdNull = jest.fn( async () => null );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( 'asdf' );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( 'asdf' );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( 'asdf' );
expect( mockGetAnonIdNull ).toHaveBeenCalledTimes( 3 );
const mockGetAnonIdEmpty = jest.fn( async () => '' );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdEmpty ) ).toBe( 'asdf' );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdEmpty ) ).toBe( 'asdf' );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdEmpty ) ).toBe( 'asdf' );
expect( mockGetAnonIdEmpty ).toHaveBeenCalledTimes( 3 );
} );
it( 'should not return the cached anonId for falsy-values outside of the expiry time', async () => {
const mockGetAnonIdA = jest.fn( async () => 'asdf' );
spiedMonotonicNow.mockImplementationOnce( () => 0 );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdA ) ).toBe( 'asdf' );
const mockGetAnonIdNull = jest.fn( async () => null );
spiedMonotonicNow.mockImplementationOnce( () => 1 );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( 'asdf' );
spiedMonotonicNow.mockImplementationOnce( () => 24 * 60 * 60 * 1000 - 1 );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( 'asdf' );
spiedMonotonicNow.mockImplementationOnce( () => 24 * 60 * 60 * 1000 );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( null );
spiedMonotonicNow.mockImplementationOnce( () => 24 * 60 * 60 * 1000 + 1 );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( null );
spiedMonotonicNow.mockImplementationOnce( () => Infinity );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( null );
// And let's make sure we can overwrite it:
spiedMonotonicNow.mockImplementationOnce( () => 0 );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdA ) ).toBe( 'asdf' );
spiedMonotonicNow.mockImplementationOnce( () => 1 );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( 'asdf' );
spiedMonotonicNow.mockImplementationOnce( () => 24 * 60 * 60 * 1000 - 1 );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( 'asdf' );
spiedMonotonicNow.mockImplementationOnce( () => 24 * 60 * 60 * 1000 );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( null );
spiedMonotonicNow.mockImplementationOnce( () => 24 * 60 * 60 * 1000 + 1 );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( null );
spiedMonotonicNow.mockImplementationOnce( () => Infinity );
expect( await Requests.localStorageCachedGetAnonId( mockGetAnonIdNull ) ).toBe( null );
} );
} );
|