|
| 1 | +/** |
| 2 | + * Copyright 2026, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +const fs = require('fs'); |
| 18 | +const path = require('path'); |
| 19 | + |
| 20 | +const { |
| 21 | + createInstance, |
| 22 | + createStaticProjectConfigManager, |
| 23 | + createForwardingEventProcessor, |
| 24 | + createLogger, |
| 25 | + createErrorNotifier, |
| 26 | + INFO, |
| 27 | +} = require('@optimizely/optimizely-sdk'); |
| 28 | + |
| 29 | +const datafilePath = path.join(__dirname, '..', 'shared', 'datafile.json'); |
| 30 | +const testDatafile = fs.readFileSync(datafilePath, 'utf8'); |
| 31 | + |
| 32 | +const TIMEOUT_MS = 10000; |
| 33 | + |
| 34 | +async function testCommonJsRequire() { |
| 35 | + let resolveUuid; |
| 36 | + const uuidPromise = new Promise((resolve, reject) => { |
| 37 | + resolveUuid = resolve; |
| 38 | + setTimeout(() => reject(new Error('Timed out waiting for event dispatch')), TIMEOUT_MS); |
| 39 | + }); |
| 40 | + |
| 41 | + const dispatcher = { |
| 42 | + dispatchEvent(logEvent) { |
| 43 | + const uuid = logEvent.params.visitors[0].snapshots[0].events[0].uuid; |
| 44 | + console.log(`Dispatched event with uuid: ${uuid}`); |
| 45 | + |
| 46 | + if (typeof uuid === 'string' && uuid.length > 0) { |
| 47 | + resolveUuid(uuid); |
| 48 | + } |
| 49 | + |
| 50 | + return Promise.resolve({}); |
| 51 | + }, |
| 52 | + }; |
| 53 | + |
| 54 | + const configManager = createStaticProjectConfigManager({ |
| 55 | + datafile: testDatafile, |
| 56 | + }); |
| 57 | + |
| 58 | + const eventProcessor = createForwardingEventProcessor(dispatcher); |
| 59 | + |
| 60 | + const client = createInstance({ |
| 61 | + projectConfigManager: configManager, |
| 62 | + eventProcessor: eventProcessor, |
| 63 | + }); |
| 64 | + |
| 65 | + await client.onReady(); |
| 66 | + |
| 67 | + const userContext = client.createUserContext('test_user', { age: 22 }); |
| 68 | + userContext.decide('flag_1'); |
| 69 | + |
| 70 | + const uuid = await uuidPromise; |
| 71 | + console.log(`Test passed: event contained valid uuid "${uuid}"`); |
| 72 | + |
| 73 | + client.close(); |
| 74 | +} |
| 75 | + |
| 76 | +testCommonJsRequire() |
| 77 | + .then(() => { |
| 78 | + console.log('\n=== CommonJS example completed successfully! ===\n'); |
| 79 | + process.exit(0); |
| 80 | + }) |
| 81 | + .catch((error) => { |
| 82 | + console.error('Test failed:', error); |
| 83 | + process.exit(1); |
| 84 | + }); |
0 commit comments