|
6 | 6 | deserializePatch, |
7 | 7 | replacer, |
8 | 8 | reviver, |
| 9 | + serializeSegment, |
9 | 10 | } from '../../src/store/serialization'; |
10 | 11 |
|
11 | 12 | const flagWithAttributeNameInClause = { |
@@ -339,3 +340,109 @@ it('given bad json', () => { |
339 | 340 | expect(deserializePatch(data)).toBeUndefined(); |
340 | 341 | expect(deserializeDelete(data)).toBeUndefined(); |
341 | 342 | }); |
| 343 | + |
| 344 | +it('deserialization creates a set for a large number of includes/excludes', () => { |
| 345 | + const included = [...Array(500).keys()].map((i) => (i + 1).toString()); |
| 346 | + const excluded = [...Array(500).keys()].map((i) => (i + 10).toString()); |
| 347 | + |
| 348 | + const jsonString = makeSerializedPatchData(undefined, { |
| 349 | + key: 'test-segment-1', |
| 350 | + included, |
| 351 | + excluded, |
| 352 | + includedContexts: [], |
| 353 | + excludedContexts: [], |
| 354 | + salt: 'saltyA', |
| 355 | + rules: [], |
| 356 | + version: 0, |
| 357 | + deleted: false, |
| 358 | + }); |
| 359 | + |
| 360 | + const res = deserializePatch(jsonString); |
| 361 | + const segment = res?.data as Segment; |
| 362 | + expect(segment.included).toBeUndefined(); |
| 363 | + expect(segment.excluded).toBeUndefined(); |
| 364 | + |
| 365 | + expect([...segment.generated_includedSet!]).toEqual(included); |
| 366 | + expect([...segment.generated_excludedSet!]).toEqual(excluded); |
| 367 | +}); |
| 368 | + |
| 369 | +it('deserialization creates a set for a large number of included/excluded context values', () => { |
| 370 | + const included = [...Array(500).keys()].map((i) => (i + 10).toString()); |
| 371 | + const excluded = [...Array(500).keys()].map((i) => (i + 1).toString()); |
| 372 | + |
| 373 | + const jsonString = makeSerializedPatchData(undefined, { |
| 374 | + key: 'test-segment-1', |
| 375 | + included: [], |
| 376 | + excluded: [], |
| 377 | + includedContexts: [{ contextKind: 'org', values: included }], |
| 378 | + excludedContexts: [{ contextKind: 'user', values: excluded }], |
| 379 | + salt: 'saltyA', |
| 380 | + rules: [], |
| 381 | + version: 0, |
| 382 | + deleted: false, |
| 383 | + }); |
| 384 | + |
| 385 | + const res = deserializePatch(jsonString); |
| 386 | + const segment = res?.data as Segment; |
| 387 | + |
| 388 | + expect([...segment.includedContexts![0].generated_valuesSet!]).toEqual(included); |
| 389 | + expect([...segment.excludedContexts![0].generated_valuesSet!]).toEqual(excluded); |
| 390 | +}); |
| 391 | + |
| 392 | +it('serialization converts sets back to arrays for included/excluded', () => { |
| 393 | + const included = [...Array(500).keys()].map((i) => (i + 1).toString()); |
| 394 | + const excluded = [...Array(500).keys()].map((i) => (i + 10).toString()); |
| 395 | + |
| 396 | + const jsonString = makeSerializedPatchData(undefined, { |
| 397 | + key: 'test-segment-1', |
| 398 | + included, |
| 399 | + excluded, |
| 400 | + includedContexts: [], |
| 401 | + excludedContexts: [], |
| 402 | + salt: 'saltyA', |
| 403 | + rules: [], |
| 404 | + version: 0, |
| 405 | + deleted: false, |
| 406 | + }); |
| 407 | + |
| 408 | + const res = deserializePatch(jsonString); |
| 409 | + const segment = res?.data as Segment; |
| 410 | + |
| 411 | + const serializedSegment = serializeSegment(segment); |
| 412 | + // Just json parse. We don't want it to automatically re-populate the sets. |
| 413 | + const jsonDeserialized = JSON.parse(serializedSegment); |
| 414 | + |
| 415 | + expect(jsonDeserialized.included).toEqual(included); |
| 416 | + expect(jsonDeserialized.excluded).toEqual(excluded); |
| 417 | + expect(jsonDeserialized.generated_includedSet).toBeUndefined(); |
| 418 | + expect(jsonDeserialized.generated_excludedSet).toBeUndefined(); |
| 419 | +}); |
| 420 | + |
| 421 | +it('serialization converts sets back to arrays for includedContexts/excludedContexts', () => { |
| 422 | + const included = [...Array(500).keys()].map((i) => (i + 1).toString()); |
| 423 | + const excluded = [...Array(500).keys()].map((i) => (i + 10).toString()); |
| 424 | + |
| 425 | + const jsonString = makeSerializedPatchData(undefined, { |
| 426 | + key: 'test-segment-1', |
| 427 | + included: [], |
| 428 | + excluded: [], |
| 429 | + includedContexts: [{ contextKind: 'org', values: included }], |
| 430 | + excludedContexts: [{ contextKind: 'user', values: excluded }], |
| 431 | + salt: 'saltyA', |
| 432 | + rules: [], |
| 433 | + version: 0, |
| 434 | + deleted: false, |
| 435 | + }); |
| 436 | + |
| 437 | + const res = deserializePatch(jsonString); |
| 438 | + const segment = res?.data as Segment; |
| 439 | + |
| 440 | + const serializedSegment = serializeSegment(segment); |
| 441 | + // Just json parse. We don't want it to automatically re-populate the sets. |
| 442 | + const jsonDeserialized = JSON.parse(serializedSegment); |
| 443 | + |
| 444 | + expect(jsonDeserialized.includedContexts[0].values).toEqual(included); |
| 445 | + expect(jsonDeserialized.excludedContexts[0].values).toEqual(excluded); |
| 446 | + expect(jsonDeserialized.includedContexts[0].generated_valuesSet).toBeUndefined(); |
| 447 | + expect(jsonDeserialized.excludedContexts[0].generated_valuesSet).toBeUndefined(); |
| 448 | +}); |
0 commit comments