|
| 1 | +import * as fs from 'fs/promises'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | + |
| 5 | +import { createClient } from '../src'; |
| 6 | +import { resetNodeStorage } from '../src/platform/NodeStorage'; |
| 7 | +import { createMockLogger } from './testHelpers'; |
| 8 | + |
| 9 | +let tmpRoot: string; |
| 10 | +let logger: ReturnType<typeof createMockLogger>; |
| 11 | + |
| 12 | +beforeEach(async () => { |
| 13 | + tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'node-client-bootstrap-test-')); |
| 14 | + resetNodeStorage(); |
| 15 | + logger = createMockLogger(); |
| 16 | +}); |
| 17 | + |
| 18 | +afterEach(async () => { |
| 19 | + resetNodeStorage(); |
| 20 | + await fs.rm(tmpRoot, { recursive: true, force: true }); |
| 21 | +}); |
| 22 | + |
| 23 | +const goodBootstrapData = { |
| 24 | + 'string-flag': 'is bob', |
| 25 | + 'my-boolean-flag': false, |
| 26 | + $flagsState: { |
| 27 | + 'string-flag': { |
| 28 | + variation: 1, |
| 29 | + version: 3, |
| 30 | + }, |
| 31 | + 'my-boolean-flag': { |
| 32 | + variation: 1, |
| 33 | + version: 11, |
| 34 | + }, |
| 35 | + }, |
| 36 | + $valid: true, |
| 37 | +}; |
| 38 | + |
| 39 | +const bootstrapDataWithReasons = { |
| 40 | + json: ['a', 'b', 'c', 'd'], |
| 41 | + $flagsState: { |
| 42 | + json: { |
| 43 | + variation: 1, |
| 44 | + version: 3, |
| 45 | + reason: { kind: 'OFF' }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + $valid: true, |
| 49 | +}; |
| 50 | + |
| 51 | +it('start with bootstrap data resolves and exposes flags', async () => { |
| 52 | + const client = createClient('client-side-id', { kind: 'user', key: 'bob' }, { |
| 53 | + initialConnectionMode: 'offline', |
| 54 | + sendEvents: false, |
| 55 | + diagnosticOptOut: true, |
| 56 | + localStoragePath: tmpRoot, |
| 57 | + logger, |
| 58 | + }); |
| 59 | + |
| 60 | + const result = await client.start({ bootstrap: goodBootstrapData }); |
| 61 | + |
| 62 | + expect(result.status).toBe('complete'); |
| 63 | + expect(client.stringVariation('string-flag', 'default')).toBe('is bob'); |
| 64 | + expect(client.boolVariation('my-boolean-flag', true)).toBe(false); |
| 65 | +}); |
| 66 | + |
| 67 | +it('exposes evaluation reasons from bootstrap data', async () => { |
| 68 | + const client = createClient('client-side-id', { kind: 'user', key: 'bob' }, { |
| 69 | + initialConnectionMode: 'offline', |
| 70 | + sendEvents: false, |
| 71 | + diagnosticOptOut: true, |
| 72 | + localStoragePath: tmpRoot, |
| 73 | + logger, |
| 74 | + }); |
| 75 | + |
| 76 | + await client.start({ bootstrap: bootstrapDataWithReasons }); |
| 77 | + |
| 78 | + expect(client.jsonVariationDetail('json', undefined)).toEqual({ |
| 79 | + reason: { kind: 'OFF' }, |
| 80 | + value: ['a', 'b', 'c', 'd'], |
| 81 | + variationIndex: 1, |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +it('re-identifying with new bootstrap data replaces previous flags', async () => { |
| 86 | + const newBootstrapData = { |
| 87 | + 'string-flag': 'is alice', |
| 88 | + 'my-boolean-flag': true, |
| 89 | + $flagsState: { |
| 90 | + 'string-flag': { variation: 1, version: 4 }, |
| 91 | + 'my-boolean-flag': { variation: 0, version: 12 }, |
| 92 | + }, |
| 93 | + $valid: true, |
| 94 | + }; |
| 95 | + |
| 96 | + const client = createClient('client-side-id', { kind: 'user', key: 'bob' }, { |
| 97 | + initialConnectionMode: 'offline', |
| 98 | + sendEvents: false, |
| 99 | + diagnosticOptOut: true, |
| 100 | + localStoragePath: tmpRoot, |
| 101 | + logger, |
| 102 | + }); |
| 103 | + |
| 104 | + await client.start({ bootstrap: goodBootstrapData }); |
| 105 | + expect(client.stringVariation('string-flag', 'default')).toBe('is bob'); |
| 106 | + |
| 107 | + await client.identify({ kind: 'user', key: 'alice' }, { bootstrap: newBootstrapData }); |
| 108 | + expect(client.stringVariation('string-flag', 'default')).toBe('is alice'); |
| 109 | + expect(client.boolVariation('my-boolean-flag', false)).toBe(true); |
| 110 | +}); |
| 111 | + |
| 112 | +it('returns defaults when no bootstrap data is provided', async () => { |
| 113 | + const client = createClient('client-side-id', { kind: 'user', key: 'bob' }, { |
| 114 | + initialConnectionMode: 'offline', |
| 115 | + sendEvents: false, |
| 116 | + diagnosticOptOut: true, |
| 117 | + localStoragePath: tmpRoot, |
| 118 | + logger, |
| 119 | + }); |
| 120 | + |
| 121 | + await client.start(); |
| 122 | + |
| 123 | + expect(client.stringVariation('string-flag', 'default')).toBe('default'); |
| 124 | + expect(client.boolVariation('my-boolean-flag', true)).toBe(true); |
| 125 | +}); |
0 commit comments