-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathindex.spec.js
More file actions
181 lines (146 loc) · 5.25 KB
/
Copy pathindex.spec.js
File metadata and controls
181 lines (146 loc) · 5.25 KB
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
'use strict'
const assert = require('node:assert/strict')
const { describe, it, beforeEach, afterEach } = require('mocha')
const sinon = require('sinon')
const proxyquire = require('proxyquire')
const { channel } = require('dc-polyfill')
require('../setup/core')
describe('OpenFeature Module', () => {
let config
let openfeatureModule
let mockWriter
let ExposuresWriterStub
let setExposureDeliveryStrategyStub
beforeEach(() => {
config = {
ffeFlushInterval: 1000,
ffeTimeout: 5000,
}
mockWriter = {
append: sinon.spy(),
flush: sinon.spy(),
destroy: sinon.spy(),
setEnabled: sinon.spy(),
}
ExposuresWriterStub = sinon.stub().returns(mockWriter)
setExposureDeliveryStrategyStub = sinon.stub()
openfeatureModule = proxyquire('../../src/openfeature', {
'./writers/exposures': ExposuresWriterStub,
'./writers/util': { setExposureDeliveryStrategy: setExposureDeliveryStrategyStub },
})
})
afterEach(() => {
openfeatureModule.disable()
})
describe('enable/disable', () => {
it('should export enable and disable functions', () => {
assert.strictEqual(typeof openfeatureModule.enable, 'function')
assert.strictEqual(typeof openfeatureModule.disable, 'function')
})
it('should setup writer when enabled', () => {
openfeatureModule.enable(config)
sinon.assert.calledOnceWithExactly(ExposuresWriterStub, config)
sinon.assert.calledOnce(setExposureDeliveryStrategyStub)
})
it('configures the writer with the selected exposure route', () => {
openfeatureModule.enable(config)
const setWriterEnabled = setExposureDeliveryStrategyStub.firstCall.args[1]
const route = {
url: new URL('http://serverless-init:8126'),
basePath: '/evp_proxy/v4',
}
setWriterEnabled(true, route)
sinon.assert.calledOnceWithExactly(mockWriter.setEnabled, true, route)
})
it('ignores a discovery result for a replaced writer', () => {
const replacementWriter = {
append: sinon.spy(),
flush: sinon.spy(),
destroy: sinon.spy(),
setEnabled: sinon.spy(),
}
const staleRoute = {
url: new URL('http://stale-agent:8126'),
basePath: '/evp_proxy/v2',
}
const currentRoute = {
url: new URL('http://current-agent:8126'),
basePath: '/evp_proxy/v2',
}
ExposuresWriterStub.onSecondCall().returns(replacementWriter)
openfeatureModule.enable(config)
const staleCallback = setExposureDeliveryStrategyStub.firstCall.args[1]
openfeatureModule.disable()
openfeatureModule.enable(config)
const currentCallback = setExposureDeliveryStrategyStub.secondCall.args[1]
staleCallback(true, staleRoute)
sinon.assert.notCalled(replacementWriter.setEnabled)
currentCallback(true, currentRoute)
sinon.assert.calledOnceWithExactly(replacementWriter.setEnabled, true, currentRoute)
})
it('should handle multiple enable calls gracefully', () => {
openfeatureModule.enable(config)
openfeatureModule.enable(config)
sinon.assert.calledOnce(ExposuresWriterStub)
})
it('should destroy writer when disabled', () => {
openfeatureModule.enable(config)
openfeatureModule.disable()
sinon.assert.calledOnce(mockWriter.destroy)
})
})
describe('exposure event handling', () => {
beforeEach(() => {
openfeatureModule.enable(config)
})
it('appends to the exposures writer', () => {
const exposureSubmitCh = channel('ffe:exposure:submit')
const exposureEvent = {
timestamp: Date.now(),
allocation: { key: 'test-allocation' },
flag: { key: 'test-flag' },
variant: { key: 'test-variant' },
subject: {
id: 'user-123',
type: 'user',
attributes: {},
},
}
exposureSubmitCh.publish(exposureEvent)
sinon.assert.calledWith(mockWriter.append, exposureEvent)
})
it('handles array of exposure events', () => {
const exposureSubmitCh = channel('ffe:exposure:submit')
const exposureEvents = [
{
timestamp: Date.now(),
allocation: { key: 'test-allocation-1' },
flag: { key: 'test-flag-1' },
variant: { key: 'test-variant-1' },
subject: { id: 'user-123', type: 'user', attributes: {} },
},
{
timestamp: Date.now(),
allocation: { key: 'test-allocation-2' },
flag: { key: 'test-flag-2' },
variant: { key: 'test-variant-2' },
subject: { id: 'user-456', type: 'user', attributes: {} },
},
]
exposureSubmitCh.publish(exposureEvents)
sinon.assert.calledOnceWithExactly(mockWriter.append, exposureEvents)
})
it('flushes the exposures writer', () => {
const flushCh = channel('ffe:writers:flush')
flushCh.publish()
sinon.assert.calledOnce(mockWriter.flush)
})
it('removes all subscribers when disabling', () => {
const exposureSubmitCh = channel('ffe:exposure:submit')
const flushCh = channel('ffe:writers:flush')
openfeatureModule.disable()
assert.strictEqual(exposureSubmitCh.hasSubscribers, false)
assert.strictEqual(flushCh.hasSubscribers, false)
})
})
})